- # ----- putpoi
- # save POI to the database
-
- # in: [0] user token (string),
- # [1] original node id (may be negative),
- # [2] projected longitude, [3] projected latitude,
- # [4] hash of tags, [5] visible (0 to delete, 1 otherwise),
- # [6] baselong, [7] basey, [8] masterscale
- # does: saves POI node to the database
- # refuses save if the node has since become part of a way
- # out: [0] 0 (success), [1] original node id (unchanged),
- # [2] new node id
-
- def putpoi(args)
- usertoken,id,x,y,tags,visible,baselong,basey,masterscale=args
- uid=getuserid(usertoken)
- if !uid then return -1,"You are not logged in, so the point could not be saved." end
-
- db_now='@now'+(rand*100).to_i.to_s+uid.to_s+id.to_i.abs.to_s+Time.new.to_i.to_s # 'now' variable name, typically 51 chars
- ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
-
- id=id.to_i
- visible=visible.to_i
- if visible==0 then
- # if deleting, check node hasn't become part of a way
- inway=ActiveRecord::Base.connection.select_one("SELECT cw.id FROM current_ways cw,current_way_nodes cwn WHERE cw.id=cwn.id AND cw.visible=1 AND cwn.node_id=#{id} LIMIT 1")
- unless inway.nil? then return -1,"The point has since become part of a way, so you cannot save it as a POI." end
- deleteitemrelations(id,'node',uid,db_now)
- end
+ ##
+ # Find all the ways, POI nodes (i.e. not part of ways), and relations
+ # in a given bounding box. Nodes are returned in full; ways and relations
+ # are IDs only.
+ #
+ # return is of the form:
+ # [success_code, success_message,
+ # [[way_id, way_version], ...],
+ # [[node_id, lat, lon, [tags, ...], node_version], ...],
+ # [[rel_id, rel_version], ...]]
+ # where the ways are any visible ways which refer to any visible
+ # nodes in the bbox, nodes are any visible nodes in the bbox but not
+ # used in any way, rel is any relation which refers to either a way
+ # or node that we're returning.
+ def whichways(xmin, ymin, xmax, ymax) #:doc:
+ amf_handle_error_with_timeout("'whichways'") do
+ enlarge = [(xmax-xmin)/8,0.01].min
+ xmin -= enlarge; ymin -= enlarge
+ xmax += enlarge; ymax += enlarge
+
+ # check boundary is sane and area within defined
+ # see /config/application.yml
+ check_boundaries(xmin, ymin, xmax, ymax)
+
+ if POTLATCH_USE_SQL then
+ ways = sql_find_ways_in_area(xmin, ymin, xmax, ymax)
+ points = sql_find_pois_in_area(xmin, ymin, xmax, ymax)
+ relations = sql_find_relations_in_area_and_ways(xmin, ymin, xmax, ymax, ways.collect {|x| x[0]})
+ else
+ # find the way ids in an area
+ nodes_in_area = Node.find_by_area(ymin, xmin, ymax, xmax, :conditions => ["current_nodes.visible = ?", true], :include => :ways)
+ ways = nodes_in_area.inject([]) { |sum, node|
+ visible_ways = node.ways.select { |w| w.visible? }
+ sum + visible_ways.collect { |w| [w.id,w.version] }
+ }.uniq
+ ways.delete([])
+
+ # find the node ids in an area that aren't part of ways
+ nodes_not_used_in_area = nodes_in_area.select { |node| node.ways.empty? }
+ points = nodes_not_used_in_area.collect { |n| [n.id, n.lon, n.lat, n.tags, n.version] }.uniq
+
+ # find the relations used by those nodes and ways
+ relations = Relation.find_for_nodes(nodes_in_area.collect { |n| n.id }, :conditions => {:visible => true}) +
+ Relation.find_for_ways(ways.collect { |w| w[0] }, :conditions => {:visible => true})
+ relations = relations.collect { |relation| [relation.id,relation.version] }.uniq
+ end