source: subversion/applications/utils/import/bulkupload/change2diff2.py @ 34714

Last change on this file since 34714 was 19408, checked in by avar, 10 years ago

Add bulk upload scripts from http://openstreetmap.pl/balrog/bulkupload/ by balrog-k1n, he doesn't have svn access so I'm commiting this for him

File size: 4.6 KB
Line 
1#! /usr/bin/python2
2# vim: fileencoding=utf-8 encoding=utf-8 et sw=4
3
4# Copyright (C) 2009 Andrzej Zaborowski <balrogg@gmail.com>
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
20"""
21Generate a .diff.xml file (the response from the server after a diff upload)
22from an uploaded changeset file (downloadable through
23http://www.openstreetmap.org/api/0.6/changeset/<id>/download) -- this is
24useful if the network connection broke after uploading the changeset but
25before receiving the server response.
26"""
27
28__version__ = "$Revision: 21 $"
29
30import os
31import sys
32import traceback
33import codecs
34import locale
35import subprocess
36
37import httplib
38
39import xml.etree.cElementTree as ElementTree
40
41import locale, codecs
42locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
43encoding = locale.getlocale()[1]
44sys.stdout = codecs.getwriter(encoding)(sys.stdout, errors = "replace")
45sys.stderr = codecs.getwriter(encoding)(sys.stderr, errors = "replace")
46
47try:
48    this_dir = os.path.dirname(__file__)
49    version = subprocess.Popen(["svnversion", this_dir], stdout = subprocess.PIPE).communicate()[0].strip()
50    if len(sys.argv) != 3:
51        print >>sys.stderr, u"Synopsis:"
52        print >>sys.stderr, u"    %s <old-file-name.osc> <new-file-name.osc>"
53        sys.exit(1)
54
55    oldtree = ElementTree.parse(sys.argv[1])
56    oldroot = oldtree.getroot()
57    if oldroot.tag != "osmChange" or (oldroot.attrib.get("version") != "0.3" and
58            oldroot.attrib.get("version") != "0.6"):
59        print >>sys.stderr, u"File %s is not a v0.3 osmChange file!" % (sys.argv[1],)
60        sys.exit(1)
61
62    old_ids = []
63    for operation in oldroot:
64        if operation.tag == "create":
65            for element in operation:
66                old_ids.append(int(element.attrib.get("id")))
67
68    filename = sys.argv[2]
69    if not os.path.exists(filename):
70        print >>sys.stderr, u"File %r doesn't exist!" % (filename,)
71        sys.exit(1)
72    if filename.endswith(".osc"):
73        filename_base = filename[:-4]
74    else:
75        filename_base = filename
76
77    tree = ElementTree.parse(filename)
78    root = tree.getroot()
79    if root.tag != "osmChange" or (root.attrib.get("version") != "0.3" and
80            root.attrib.get("version") != "0.6"):
81        print >>sys.stderr, u"File %s is not a v0.3 osmChange file!" % (filename,)
82        sys.exit(1)
83
84    diff_attr = {"version": "0.6", "generator": root.attrib.get("generator")}
85    diff_root = ElementTree.Element("diffResult", diff_attr)
86    diff_tree = ElementTree.ElementTree(diff_root)
87
88    # Note this is broken, it assumes the nodes in the resulting osmChange
89    # are in the same order they were in the osmChange sent to the server
90    # and that the negative IDs there started at -1 and were increasing by
91    # -1 with each new element.
92    # A better idea (but still wrong) would be to parse the input osmChange
93    # xml at the same time and assume that the elements in input and output
94    # come in the same order, possibly with additional checks (lat/lon..)
95    old_id = 0
96    for operation in root:
97        for element in operation:
98            attr = {}
99            # TODO: at least make sure the element type matches!!
100            if operation.tag == "create":
101                attr["old_id"] = str(old_ids[old_id])
102                attr["new_id"] = element.attrib.get("id")
103                attr["new_version"] = element.attrib.get("version")
104                old_id += 1
105            elif operation.tag == "modify":
106                attr["old_id"] = element.attrib.get("id")
107                attr["new_id"] = element.attrib.get("id")
108                attr["new_version"] = element.attrib.get("version")
109            elif operation.tag == "delete":
110                attr["old_id"] = element.attrib.get("id")
111            else:
112                print "unknown operation", operation.tag
113                sys.exit(-1)
114            diff = ElementTree.SubElement(diff_root, element.tag, attr)
115
116    diff_tree.write(filename_base + ".diff.xml", "utf-8")
117
118except Exception,err:
119    print >>sys.stderr, repr(err)
120    traceback.print_exc(file=sys.stderr)
121    sys.exit(1)
Note: See TracBrowser for help on using the repository browser.