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 after_filter :compress_output
8 do_search(true,true,true)
12 do_search(true,false,false)
15 do_search(false,true,false)
18 do_search(false,false,true)
21 def do_search(do_ways,do_nodes,do_relations)
23 value = params['value']
33 response.headers['Error'] = "Searching of nodes is currently unavailable"
34 render :nothing => true, :status => :service_unavailable
39 response.headers['Error'] = "Searching for a key without value is currently unavailable"
40 render :nothing => true, :status => :service_unavailable
49 # Matching for node tags table
53 sql += ' AND current_node_tags.k=?'
57 sql += ' AND current_node_tags.v=?'
60 cond_node = [sql] + cond_node
62 # Matching for way tags table
66 sql += ' AND current_way_tags.k=?'
70 sql += ' AND current_way_tags.v=?'
73 cond_way = [sql] + cond_way
75 # Matching for relation tags table
79 sql += ' AND current_relation_tags.k=?'
83 sql += ' AND current_relation_tags.v=?'
86 cond_rel = [sql] + cond_rel
88 # First up, look for the relations we want
90 relations = Relation.find(:all,
91 :joins => "INNER JOIN current_relation_tags ON current_relation_tags.id = current_relations.id",
92 :conditions => cond_rel, :limit => 100)
98 :joins => "INNER JOIN current_way_tags ON current_way_tags.id = current_ways.id",
99 :conditions => cond_way, :limit => 100)
104 nodes = Node.find(:all,
105 :joins => "INNER JOIN current_node_tags ON current_node_tags.id = current_nodes.id",
106 :conditions => cond_node, :limit => 2000)
109 # Fetch any node needed for our ways (only have matching nodes so far)
110 nodes += Node.find(ways.collect { |w| w.nds }.uniq)
115 user_display_name_cache = {}
116 doc = OSM::API.new.get_xml_doc
118 doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
119 visible_nodes[node.id] = node
123 doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
126 relations.each do |rel|
127 doc.root << rel.to_xml_node(changeset_cache, user_display_name_cache)
129 render :text => doc.to_s, :content_type => "text/xml"