aboutsummaryrefslogtreecommitdiff
path: root/upload/osm2change.py
diff options
context:
space:
mode:
authorAndrew Harvey <andrew@alantgeo.com.au>2021-07-04 20:48:39 +1000
committerAndrew Harvey <andrew@alantgeo.com.au>2021-07-04 20:48:39 +1000
commitdeffe3e80b786f5782c6915e3adabf855bd54ac4 (patch)
tree23bd569e18dc64b07ce9204eb67a929776d57e2f /upload/osm2change.py
parent7f59a0a9a9a8d3a739a08bbb0336fbf9aaf8b85d (diff)
directly commit upload.py as some local changes were made
Diffstat (limited to 'upload/osm2change.py')
-rwxr-xr-xupload/osm2change.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/upload/osm2change.py b/upload/osm2change.py
new file mode 100755
index 0000000..8a836d2
--- /dev/null
+++ b/upload/osm2change.py
@@ -0,0 +1,85 @@
+#! /usr/bin/env python3
+
+# Copyright (C) 2009 Jacek Konieczny <jajcus@jajcus.net>
+# Copyright (C) 2009 Andrzej Zaborowski <balrogg@gmail.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+"""
+Convert .osm files to osmChange 0.6
+"""
+
+import os
+import sys
+import traceback
+
+import http
+
+import xml.etree.cElementTree as ElementTree
+
+def osmsort(tree, order):
+ list = tree[0:len(tree)]
+ list.sort(lambda x, y: order.index(x.tag) - order.index(y.tag))
+ tree[0:len(tree)] = list
+
+try:
+ if len(sys.argv) != 2:
+ sys.stderr.write("Synopsis:\n")
+ sys.stderr.write(" %s <file-name.osm>\n" % (sys.argv[0],))
+ sys.exit(1)
+
+ filename = sys.argv[1]
+ if not os.path.exists(filename):
+ sys.stderr.write("File %r doesn't exist!\n" % (filename,))
+ sys.exit(1)
+ if filename.endswith(".osm"):
+ filename_base = filename[:-4]
+ else:
+ filename_base = filename
+
+ tree = ElementTree.parse(filename)
+ root = tree.getroot()
+ if root.tag != "osm" or root.attrib.get("version") != "0.6":
+ sys.stderr.write("File %s is not a v0.6 osm file!\n" % (filename,))
+ sys.exit(1)
+
+ output_attr = {"version": "0.6", "generator": root.attrib.get("generator")}
+ output_root = ElementTree.Element("osmChange", output_attr)
+ output_tree = ElementTree.ElementTree(output_root)
+
+ operation = {}
+ for opname in [ "create", "modify", "delete" ]:
+ operation[opname] = ElementTree.SubElement(output_root,
+ opname, output_attr)
+
+ for element in root:
+ if "id" in element.attrib and int(element.attrib["id"]) < 0:
+ opname = "create"
+ elif "action" in element.attrib:
+ opname = element.attrib.pop("action")
+ else:
+ continue
+ operation[opname].append(element)
+
+ # Does this account for all cases? Also, is it needed?
+ # (cases like relations containing relations... is that allowed?)
+ #osmsort(operation["create"], [ "node", "way", "relation" ])
+ #osmsort(operation["delete"], [ "relation", "way", "node" ])
+
+ output_tree.write(filename_base + ".osc", "utf-8")
+except Exception as err:
+ sys.stderr.write(repr(err) + "\n")
+ traceback.print_exc(file = sys.stderr)
+ sys.exit(1)