aboutsummaryrefslogtreecommitdiff
path: root/upload/change2diff2.py
blob: e6643d40720d9c20b47c0d6b8d85b863b55efab5 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#! /usr/bin/python2
# vim: fileencoding=utf-8 encoding=utf-8 et sw=4

# 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


"""
Generate a .diff.xml file (the response from the server after a diff upload)
from an uploaded changeset file (downloadable through
https://www.openstreetmap.org/api/0.6/changeset/<id>/download) -- this is
useful if the network connection broke after uploading the changeset but
before receiving the server response.
"""

__version__ = "$Revision: 21 $"

import os
import sys
import traceback
import codecs
import locale
import subprocess

import httplib

import xml.etree.cElementTree as ElementTree

import locale, codecs
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
encoding = locale.getlocale()[1]
sys.stdout = codecs.getwriter(encoding)(sys.stdout, errors = "replace")
sys.stderr = codecs.getwriter(encoding)(sys.stderr, errors = "replace")

try:
    this_dir = os.path.dirname(__file__)
    version = subprocess.Popen(["svnversion", this_dir], stdout = subprocess.PIPE).communicate()[0].strip()
    if len(sys.argv) != 3:
        print >>sys.stderr, u"Synopsis:"
        print >>sys.stderr, u"    %s <old-file-name.osc> <new-file-name.osc>"
        sys.exit(1)

    oldtree = ElementTree.parse(sys.argv[1])
    oldroot = oldtree.getroot()
    if oldroot.tag != "osmChange" or (oldroot.attrib.get("version") != "0.3" and
            oldroot.attrib.get("version") != "0.6"):
        print >>sys.stderr, u"File %s is not a v0.3 osmChange file!" % (sys.argv[1],)
        sys.exit(1)

    old_ids = []
    for operation in oldroot:
        if operation.tag == "create":
            for element in operation:
                old_ids.append(int(element.attrib.get("id")))

    filename = sys.argv[2]
    if not os.path.exists(filename):
        print >>sys.stderr, u"File %r doesn't exist!" % (filename,)
        sys.exit(1)
    if filename.endswith(".osc"):
        filename_base = filename[:-4]
    else:
        filename_base = filename

    tree = ElementTree.parse(filename)
    root = tree.getroot()
    if root.tag != "osmChange" or (root.attrib.get("version") != "0.3" and
            root.attrib.get("version") != "0.6"):
        print >>sys.stderr, u"File %s is not a v0.3 osmChange file!" % (filename,)
        sys.exit(1)

    diff_attr = {"version": "0.6", "generator": root.attrib.get("generator")}
    diff_root = ElementTree.Element("diffResult", diff_attr)
    diff_tree = ElementTree.ElementTree(diff_root)

    # Note this is broken, it assumes the nodes in the resulting osmChange
    # are in the same order they were in the osmChange sent to the server
    # and that the negative IDs there started at -1 and were increasing by
    # -1 with each new element.
    # A better idea (but still wrong) would be to parse the input osmChange
    # xml at the same time and assume that the elements in input and output
    # come in the same order, possibly with additional checks (lat/lon..)
    old_id = 0
    for operation in root:
        for element in operation:
            attr = {}
            # TODO: at least make sure the element type matches!!
            if operation.tag == "create":
                attr["old_id"] = str(old_ids[old_id])
                attr["new_id"] = element.attrib.get("id")
                attr["new_version"] = element.attrib.get("version")
                old_id += 1
            elif operation.tag == "modify":
                attr["old_id"] = element.attrib.get("id")
                attr["new_id"] = element.attrib.get("id")
                attr["new_version"] = element.attrib.get("version")
            elif operation.tag == "delete":
                attr["old_id"] = element.attrib.get("id")
            else:
                print "unknown operation", operation.tag
                sys.exit(-1)
            diff = ElementTree.SubElement(diff_root, element.tag, attr)

    diff_tree.write(filename_base + ".diff.xml", "utf-8")

except Exception,err:
    print >>sys.stderr, repr(err)
    traceback.print_exc(file=sys.stderr)
    sys.exit(1)