source: subversion/applications/rendering/pyrender/OsmMerge.py @ 8177

Last change on this file since 8177 was 8177, checked in by spaetz, 11 years ago

actually write out xml file in UTF-8 encoding

File size: 2.2 KB
Line 
1#!/usr/bin/python
2#----------------------------------------------------------------------------
3# Merge multiple OSM files (in memory) and save to another OSM file
4#----------------------------------------------------------------------------
5# Copyright 2008, Oliver White
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 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#---------------------------------------------------------------------------
20import codecs
21from parseOsm import *
22
23def OsmMerge(dest, sources):
24  """Merge multiple OSM files together
25 
26  Usage:
27    OsmMerge('output_filename.osm',
28      ['input_file_1.osm',
29       'input_file_2.osm',
30       'input_file_3.osm'])
31  """
32 
33  ways = []
34  poi = []
35  node_tags = {}
36  nodes = {}
37 
38  # Trawl through the source files, putting everything into memory
39  for source in sources:
40    osm = parseOsm(source)
41   
42    ways = ways + osm.ways
43    for p in osm.poi:
44      node_tags[p['id']] = p['t']
45
46    for id,n in osm.nodes.items():
47      nodes[id] = n
48
49  # Store the result as an OSM XML file
50  f=codecs.open(dest, mode='w', encoding='utf-8')
51  f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
52  f.write("<osm version='0.5' generator='OsmMerge'>\n")
53 
54  for n,data in nodes.items():
55    (lat,lon) = nodes[n]
56    f.write("<node id='%d' lat='%f' lon='%f'>" % (n,lat,lon))
57    tags = node_tags.get(n, None)
58    if(tags):
59      for k,v in tags.items():
60        f.write("\n<tag k='%s' v='%s'/>" % (k,v))
61    f.write("</node>\n")
62   
63  for w in ways:
64    f.write("<way id='%d'>")
65    for k,v in w['t'].items():
66      f.write("\n<tag k='%s' v='%s'/>" % (k,v))
67    for n in w['n']:
68      f.write("\n<nd ref='%d'/>" % n)
69    f.write("</way>\n")
70 
71  f.write("</osm>\n")
72  f.close()
73 
Note: See TracBrowser for help on using the repository browser.