Merge remote-tracking branch 'upstream/pull/2223'

This commit is contained in:
Tom Hughes 2019-07-07 16:14:32 +01:00
commit d021bdf5cf
31 changed files with 306 additions and 96 deletions

View file

@ -19,15 +19,15 @@ module Api
# check boundary is sane and area within defined
# see /config/application.yml
begin
bbox = BoundingBox.from_bbox_params(params)
bbox.check_boundaries
bbox.check_size
@bounds = BoundingBox.from_bbox_params(params)
@bounds.check_boundaries
@bounds.check_size
rescue StandardError => e
report_error(e.message)
return
end
nodes = Node.bbox(bbox).where(:visible => true).includes(:node_tags).limit(Settings.max_number_of_nodes + 1)
nodes = Node.bbox(@bounds).where(:visible => true).includes(:node_tags).limit(Settings.max_number_of_nodes + 1)
node_ids = nodes.collect(&:id)
if node_ids.length > Settings.max_number_of_nodes
@ -35,11 +35,6 @@ module Api
return
end
doc = OSM::API.new.get_xml_doc
# add bounds
doc.root << bbox.add_bounds_to(XML::Node.new("bounds"))
# get ways
# find which ways are needed
ways = []
@ -62,43 +57,40 @@ module Api
nodes += Node.includes(:node_tags).find(nodes_to_fetch) unless nodes_to_fetch.empty?
visible_nodes = {}
changeset_cache = {}
user_display_name_cache = {}
@nodes = []
nodes.each do |node|
if node.visible?
doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
visible_nodes[node.id] = node
@nodes << node
end
end
@ways = []
way_ids = []
ways.each do |way|
if way.visible?
doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
way_ids << way.id
@ways << way
end
end
relations = Relation.nodes(visible_nodes.keys).visible +
Relation.ways(way_ids).visible
@relations = Relation.nodes(visible_nodes.keys).visible +
Relation.ways(way_ids).visible
# we do not normally return the "other" partners referenced by an relation,
# e.g. if we return a way A that is referenced by relation X, and there's
# another way B also referenced, that is not returned. But we do make
# an exception for cases where an relation references another *relation*;
# in that case we return that as well (but we don't go recursive here)
relations += Relation.relations(relations.collect(&:id)).visible
@relations += Relation.relations(@relations.collect(&:id)).visible
# this "uniq" may be slightly inefficient; it may be better to first collect and output
# all node-related relations, then find the *not yet covered* way-related ones etc.
relations.uniq.each do |relation|
doc.root << relation.to_xml_node(changeset_cache, user_display_name_cache)
end
@relations.uniq!
response.headers["Content-Disposition"] = "attachment; filename=\"map.osm\""
render :xml => doc.to_s
# Render the result
render :formats => [:xml]
end
end
end

View file

@ -26,12 +26,13 @@ module Api
# Dump the details on a node given in params[:id]
def show
node = Node.find(params[:id])
@node = Node.find(params[:id])
response.last_modified = node.timestamp
response.last_modified = @node.timestamp
if node.visible
render :xml => node.to_xml.to_s
if @node.visible
# Render the result
render :formats => [:xml]
else
head :gone
end
@ -69,13 +70,10 @@ module Api
raise OSM::APIBadUserInput, "No nodes were given to search for" if ids.empty?
doc = OSM::API.new.get_xml_doc
@nodes = Node.find(ids)
Node.find(ids).each do |node|
doc.root << node.to_xml_node
end
render :xml => doc.to_s
# Render the result
render :formats => [:xml]
end
end
end

View file

@ -22,19 +22,15 @@ module Api
# to do that ourselves.
raise OSM::APINotFoundError if @elements.empty?
doc = OSM::API.new.get_xml_doc
# determine visible elements
@elems = if show_redactions?
@elements
else
@elements.unredacted
end
visible_elements = if show_redactions?
@elements
else
@elements.unredacted
end
visible_elements.each do |element|
doc.root << element.to_xml_node
end
render :xml => doc.to_s
# Render the result
render :formats => [:xml]
end
def version
@ -44,10 +40,8 @@ module Api
else
response.last_modified = @old_element.timestamp
doc = OSM::API.new.get_xml_doc
doc.root << @old_element.to_xml_node
render :xml => doc.to_s
# Render the result
render :formats => [:xml]
end
end

View file

@ -22,10 +22,11 @@ module Api
end
def show
relation = Relation.find(params[:id])
response.last_modified = relation.timestamp
if relation.visible
render :xml => relation.to_xml.to_s
@relation = Relation.find(params[:id])
response.last_modified = @relation.timestamp
if @relation.visible
# Render the result
render :formats => [:xml]
else
head :gone
end
@ -88,35 +89,35 @@ module Api
node_ids += way_node_ids.flatten
nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
# create XML.
doc = OSM::API.new.get_xml_doc
visible_nodes = {}
changeset_cache = {}
user_display_name_cache = {}
@nodes = []
nodes.each do |node|
next unless node.visible? # should be unnecessary if data is consistent.
doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
@nodes << node
visible_nodes[node.id] = node
end
@ways = []
ways.each do |way|
next unless way.visible? # should be unnecessary if data is consistent.
doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
@ways << way
end
@relations = []
relations.each do |rel|
next unless rel.visible? # should be unnecessary if data is consistent.
doc.root << rel.to_xml_node(changeset_cache, user_display_name_cache)
@relations << rel
end
# finally add self and output
doc.root << relation.to_xml_node(changeset_cache, user_display_name_cache)
render :xml => doc.to_s
# finally add self
@relations << relation
# Render the result
render :formats => [:xml]
else
head :gone
end
@ -129,13 +130,10 @@ module Api
raise OSM::APIBadUserInput, "No relations were given to search for" if ids.empty?
doc = OSM::API.new.get_xml_doc
@relations = Relation.find(ids)
Relation.find(ids).each do |relation|
doc.root << relation.to_xml_node
end
render :xml => doc.to_s
# Render the result
render :formats => [:xml]
end
def relations_for_way
@ -155,13 +153,14 @@ module Api
def relations_for_object(objtype)
relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect(&:relation_id).uniq
doc = OSM::API.new.get_xml_doc
@relations = []
Relation.find(relationids).each do |relation|
doc.root << relation.to_xml_node if relation.visible
@relations << relation if relation.visible
end
render :xml => doc.to_s
# Render the result
render :formats => [:xml]
end
end
end

View file

@ -22,12 +22,13 @@ module Api
end
def show
way = Way.find(params[:id])
@way = Way.find(params[:id])
response.last_modified = way.timestamp
response.last_modified = @way.timestamp
if way.visible
render :xml => way.to_xml.to_s
if @way.visible
# Render the result
render :formats => [:xml]
else
head :gone
end
@ -59,23 +60,22 @@ module Api
end
def full
way = Way.includes(:nodes => :node_tags).find(params[:id])
@way = Way.includes(:nodes => :node_tags).find(params[:id])
if way.visible
if @way.visible
visible_nodes = {}
changeset_cache = {}
user_display_name_cache = {}
doc = OSM::API.new.get_xml_doc
way.nodes.uniq.each do |node|
@nodes = []
@way.nodes.uniq.each do |node|
if node.visible
doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
@nodes << node
visible_nodes[node.id] = node
end
end
doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
render :xml => doc.to_s
# Render the result
render :formats => [:xml]
else
head :gone
end
@ -90,13 +90,10 @@ module Api
raise OSM::APIBadUserInput, "No ways were given to search for" if ids.empty?
doc = OSM::API.new.get_xml_doc
@ways = Way.find(ids)
Way.find(ids).each do |way|
doc.root << way.to_xml_node
end
render :xml => doc.to_s
# Render the result
render :formats => [:xml]
end
##
@ -106,13 +103,10 @@ module Api
def ways_for_node
wayids = WayNode.where(:node_id => params[:id]).collect { |ws| ws.id[0] }.uniq
doc = OSM::API.new.get_xml_doc
@ways = Way.where(:id => wayids, :visible => true)
Way.find(wayids).each do |way|
doc.root << way.to_xml_node if way.visible
end
render :xml => doc.to_s
# Render the result
render :formats => [:xml]
end
end
end

View file

@ -0,0 +1,8 @@
attrs = {
"minlat" => format("%.7f", bounds.min_lat),
"minlon" => format("%.7f", bounds.min_lon),
"maxlat" => format("%.7f", bounds.max_lat),
"maxlon" => format("%.7f", bounds.max_lon)
}
xml.bounds(attrs)

View file

@ -0,0 +1,8 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(:partial => "bounds", :object => @bounds) || "")
osm << (render(@nodes) || "")
osm << (render(@ways) || "")
osm << (render(@relations) || "")
end

View file

@ -0,0 +1,24 @@
attrs = {
"id" => node.id,
"visible" => node.visible,
"version" => node.version,
"changeset" => node.changeset_id,
"timestamp" => node.timestamp.xmlschema,
"user" => node.changeset.user.display_name,
"uid" => node.changeset.user_id
}
if node.visible
attrs["lat"] = node.lat
attrs["lon"] = node.lon
end
if node.tags.empty?
xml.node(attrs)
else
xml.node(attrs) do |nd|
node.tags.each do |k, v|
nd.tag(:k => k, :v => v)
end
end
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@nodes) || "")
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@node) || "")
end

View file

@ -0,0 +1,24 @@
attrs = {
"id" => old_node.node_id,
"visible" => old_node.visible,
"version" => old_node.version,
"changeset" => old_node.changeset_id,
"timestamp" => old_node.timestamp.xmlschema,
"user" => old_node.changeset.user.display_name,
"uid" => old_node.changeset.user_id
}
if old_node.visible
attrs["lat"] = old_node.lat
attrs["lon"] = old_node.lon
end
if old_node.tags.empty?
xml.node(attrs)
else
xml.node(attrs) do |nd|
old_node.tags.each do |k, v|
nd.tag(:k => k, :v => v)
end
end
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@elems) || "")
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@old_element) || "")
end

View file

@ -0,0 +1,19 @@
attrs = {
"id" => old_relation.relation_id,
"visible" => old_relation.visible,
"version" => old_relation.version,
"changeset" => old_relation.changeset_id,
"timestamp" => old_relation.timestamp.xmlschema,
"user" => old_relation.changeset.user.display_name,
"uid" => old_relation.changeset.user_id
}
xml.relation(attrs) do |r|
old_relation.relation_members.each do |m|
r.member(:type => m.member_type.downcase, :ref => m.member_id, :role => m.member_role)
end
old_relation.tags.each do |k, v|
r.tag(:k => k, :v => v)
end
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@elems) || "")
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@old_element) || "")
end

View file

@ -0,0 +1,19 @@
attrs = {
"id" => old_way.way_id,
"visible" => old_way.visible,
"version" => old_way.version,
"changeset" => old_way.changeset_id,
"timestamp" => old_way.timestamp.xmlschema,
"user" => old_way.changeset.user.display_name,
"uid" => old_way.changeset.user_id
}
xml.way(attrs) do |w|
old_way.nds.each do |n|
w.nd(:ref => n)
end
old_way.tags.each do |k, v|
w.tag(:k => k, :v => v)
end
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@elems) || "")
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@old_element) || "")
end

View file

@ -0,0 +1,19 @@
attrs = {
"id" => relation.id,
"visible" => relation.visible,
"version" => relation.version,
"changeset" => relation.changeset_id,
"timestamp" => relation.timestamp.xmlschema,
"user" => relation.changeset.user.display_name,
"uid" => relation.changeset.user_id
}
xml.relation(attrs) do |r|
relation.relation_members.each do |m|
r.member(:type => m.member_type.downcase, :ref => m.member_id, :role => m.member_role)
end
relation.tags.each do |k, v|
r.tag(:k => k, :v => v)
end
end

View file

@ -0,0 +1,7 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@nodes) || "")
osm << (render(@ways) || "")
osm << (render(@relations) || "")
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@relations) || "")
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@relations) || "")
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@relations) || "")
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@relations) || "")
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@relation) || "")
end

View file

@ -0,0 +1,19 @@
attrs = {
"id" => way.id,
"visible" => way.visible,
"version" => way.version,
"changeset" => way.changeset_id,
"timestamp" => way.timestamp.xmlschema,
"user" => way.changeset.user.display_name,
"uid" => way.changeset.user_id
}
xml.way(attrs) do |w|
way.nodes.each do |n|
w.nd(:ref => n.id)
end
way.tags.each do |k, v|
w.tag(:k => k, :v => v)
end
end

View file

@ -0,0 +1,6 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@nodes) || "")
osm << (render(@way) || "")
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@ways) || "")
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@way) || "")
end

View file

@ -0,0 +1,5 @@
xml.instruct!
xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << (render(@ways) || "")
end