aboutsummaryrefslogtreecommitdiff
path: root/upload/osm2change.py
blob: 8a836d240c1061510aa10027c11ddb0c0f423009 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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)