]> git.openstreetmap.org Git - rails.git/blob - app/controllers/search_controller.rb
The to_xml_node method for a way wants a list of node IDs, not a list
[rails.git] / app / controllers / search_controller.rb
1 class SearchController < ApplicationController
2   # Support searching for nodes, ways, or all
3   # Can search by tag k, v, or both (type->k,value->v)
4   # Can search by name (k=name,v=....)
5
6   after_filter :compress_output
7
8   def search_all
9     do_search(true,true,true)
10   end
11
12   def search_ways
13     do_search(true,false,false)
14   end
15   def search_nodes
16     do_search(false,true,false)
17   end
18   def search_relations
19     do_search(false,false,true)
20   end
21
22   def do_search(do_ways,do_nodes,do_relations)
23     type = params['type']
24     value = params['value']
25     unless type or value
26       name = params['name']
27       if name
28         type = 'name'
29         value = name
30       end
31     end
32
33     way_ids = Array.new
34     ways = Array.new
35     nodes = Array.new
36     relations = Array.new
37
38     # Matching for tags table
39     cond_way = Array.new
40     sql = '1=1'
41     if type
42       sql += ' AND current_way_tags.k=?'
43       cond_way += [type]
44     end
45     if value
46       sql += ' AND current_way_tags.v=? AND MATCH (current_way_tags.v) AGAINST (? IN BOOLEAN MODE)'
47       cond_way += [value,'"' + value.sub(/[-+*<>"~()]/, ' ') + '"']
48     end
49     cond_way = [sql] + cond_way
50
51     # Matching for tags table
52     cond_rel = Array.new
53     sql = '1=1'
54     if type
55       sql += ' AND current_relation_tags.k=?'
56       cond_rel += [type]
57     end
58     if value
59       sql += ' AND current_relation_tags.v=? AND MATCH (current_relation_tags.v) AGAINST (? IN BOOLEAN MODE)'
60       cond_rel += [value,'"' + value.sub(/[-+*<>"~()]/, ' ') + '"']
61     end
62     cond_rel = [sql] + cond_rel
63
64     # Matching for tags column
65     if type and value
66       cond_tags = ['tags LIKE ? OR tags LIKE ? OR tags LIKE ? OR tags LIKE ?', 
67       ''+type+'='+value+'',
68       ''+type+'='+value+';%',
69       '%;'+type+'='+value+';%',
70       '%;'+type+'='+value+'' ]
71     elsif type
72       cond_tags = ['tags LIKE ? OR tags LIKE ?',
73       ''+type+'=%',
74       '%;'+type+'=%' ]
75     elsif value
76       cond_tags = ['tags LIKE ? OR tags LIKE ?',
77       '%='+value+';%',
78       '%='+value+'' ]
79     else
80       cond_tags = ['1=1']
81     end
82
83     # First up, look for the relations we want
84     if do_relations
85       relations = Relation.find(:all,
86                                 :joins => "INNER JOIN current_relation_tags ON current_relation_tags.id = current_relations.id",
87                                 :conditions => cond_rel, :limit => 100)
88     end
89
90     # then ways
91     if do_ways
92       ways = Way.find(:all,
93                       :joins => "INNER JOIN current_way_tags ON current_way_tags.id = current_ways.id",
94                       :conditions => cond_way, :limit => 100)
95     end
96
97     # Now, nodes
98     if do_nodes
99       nodes = Node.find(:all, :conditions => cond_tags, :limit => 2000)
100     end
101
102     # Fetch any node needed for our ways (only have matching nodes so far)
103     nodes += Node.find(ways.collect { |w| w.nds }.uniq)
104
105     # Print
106     user_display_name_cache = {}
107     doc = OSM::API.new.get_xml_doc
108     nodes.each do |node|
109       doc.root << node.to_xml_node(user_display_name_cache)
110     end
111
112     ways.each do |way|
113       doc.root << way.to_xml_node(nodes.collect { |n| n.id }, user_display_name_cache)
114     end 
115
116     relations.each do |rel|
117       doc.root << rel.to_xml_node(user_display_name_cache)
118     end 
119     render :text => doc.to_s, :content_type => "text/xml"
120   end
121 end