source: subversion/applications/utils/import/bulkupload/osm2change-python2.py @ 34714

Last change on this file since 34714 was 19988, checked in by balrog-kun, 10 years ago

Add the missing python2 version of the converter script.

  • Property svn:executable set to *
File size: 2.9 KB
Line 
1#! /usr/bin/python3
2# vim: fileencoding=utf-8 encoding=utf-8 et sw=4
3
4# Copyright (C) 2009 Jacek Konieczny <jajcus@jajcus.net>
5# Copyright (C) 2009 Andrzej Zaborowski <balrogg@gmail.com>
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
21"""
22Convert .osm files to osmChange 0.3 format.
23"""
24
25__version__ = "$Revision: 21 $"
26
27import os
28import sys
29import traceback
30
31import httplib
32
33import xml.etree.cElementTree as ElementTree
34
35def osmsort(tree, order):
36    list = tree[0:len(tree)]
37    list.sort(lambda x, y: order.index(x.tag) - order.index(y.tag))
38    tree[0:len(tree)] = list
39
40try:
41    if len(sys.argv) != 2:
42        sys.stderr.write("Synopsis:\n")
43        sys.stderr.write("    %s <file-name.osm>\n", (sys.argv[0],))
44        sys.exit(1)
45
46    filename = sys.argv[1]
47    if not os.path.exists(filename):
48        sys.stderr.write("File %r doesn't exist!\n" % (filename,))
49        sys.exit(1)
50    if filename.endswith(".osm"):
51        filename_base = filename[:-4]
52    else:
53        filename_base = filename
54
55    tree = ElementTree.parse(filename)
56    root = tree.getroot()
57    if root.tag != "osm" or root.attrib.get("version") != "0.6":
58        sys.stderr.write("File %s is not a v0.6 osm file!\n" % (filename,))
59        sys.exit(1)
60
61    output_attr = {"version": "0.3", "generator": root.attrib.get("generator")}
62    output_root = ElementTree.Element("osmChange", output_attr)
63    output_tree = ElementTree.ElementTree(output_root)
64
65    operation = {}
66    for opname in [ "create", "modify", "delete" ]:
67        operation[opname] = ElementTree.SubElement(output_root,
68                opname, output_attr)
69
70    for element in root:
71        if "id" in element.attrib and int(element.attrib["id"]) < 0:
72            opname = "create"
73        elif "action" in element.attrib:
74            opname = element.attrib.pop("action")
75        else:
76            continue
77        operation[opname].append(element)
78
79    # Does this account for all cases?  Also, is it needed?
80    # (cases like relations containing relations... is that allowed?)
81    #osmsort(operation["create"], [ "node", "way", "relation" ])
82    #osmsort(operation["delete"], [ "relation", "way", "node" ])
83
84    output_tree.write(filename_base + ".osc", "utf-8")
85except Exception, err:
86    sys.stderr.write(repr(err) + "\n")
87    traceback.print_exc(file = sys.stderr)
88    sys.exit(1)
Note: See TracBrowser for help on using the repository browser.