Skip to content

Instantly share code, notes, and snippets.

@zerebubuth
Created June 14, 2017 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerebubuth/d27c608734ca141ac248ef0e8ce63f3a to your computer and use it in GitHub Desktop.
Save zerebubuth/d27c608734ca141ac248ef0e8ce63f3a to your computer and use it in GitHub Desktop.
require 'xml/libxml'
require 'set'
require 'open-uri'
node_ids = Set.new
way_ids = Set.new
rel_ids = Set.new
ARGV.each do |file|
data = XML::Parser.file(file).parse
data.find('//node').each do |n|
node_ids.add(n.attributes['id'].to_i)
end
data.find('//way').each do |n|
way_ids.add(n.attributes['id'].to_i)
end
data.find('//relation').each do |n|
rel_ids.add(n.attributes['id'].to_i)
end
end
doc = XML::Document.new
osm_change = XML::Node.new("osmChange")
osm_change.attributes['version'] = "0.6"
doc.root = osm_change
def add_element(doc, type, id)
begin
http = open("http://api.openstreetmap.org/api/0.6/#{type}/#{id}")
data = XML::Parser.io(http).parse
elt = data.find("//#{type}")[0]
modify = doc.import(XML::Node.new("modify"))
modify << doc.import(elt)
doc.root << modify
rescue OpenURI::HTTPError
elt = XML::Node.new(type)
elt.attributes['id'] = id.to_s
delete = XML::Node.new("delete")
delete << elt
doc.root << delete
end
end
node_ids.each do |node_id|
add_element(doc, "node", node_id)
end
way_ids.each do |way_id|
add_element(doc, "way", way_id)
end
rel_ids.each do |rel_id|
add_element(doc, "relation", rel_id)
end
puts doc.to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment