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

Last change on this file since 7742 was 7742, checked in by ojw, 11 years ago

program to download and cache OSM data in tile-sized chunks, and run a
python server capable of rendering those into a slippy-map.

No pre-rendering, external dependancies, or databases should be
required. This should run in a standard python install.

File size: 2.2 KB
Line 
1#!/usr/bin/python
2#-----------------------------------------------------------------------------# Merge multiple OSM files (in memory) and save to another OSM file
3#-----------------------------------------------------------------------------# Copyright 2008, Oliver White
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
17#----------------------------------------------------------------------------
18from parseOsm import *
19
20def OsmMerge(dest, sources):
21  """Merge multiple OSM files together
22 
23  Usage:
24    OsmMerge('output_filename.osm',
25      ['input_file_1.osm',
26       'input_file_2.osm',
27       'input_file_3.osm'])
28  """
29 
30  ways = []
31  poi = []
32  node_tags = {}
33  nodes = {}
34 
35  # Trawl through the source files, putting everything into memory
36  for source in sources:
37    osm = parseOsm(source)
38   
39    ways = ways + osm.ways
40    for p in osm.poi:
41      node_tags[p['id']] = p['t']
42
43    for id,n in osm.nodes.items():
44      nodes[id] = n
45
46  # Store the result as an OSM XML file
47  f=open(dest, 'w')
48  f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
49  f.write("<osm version='0.5' generator='OsmMerge'>\n")
50 
51  for n,data in nodes.items():
52    (lat,lon) = nodes[n]
53    f.write("<node id='%d' lat='%f' lon='%f'>" % (n,lat,lon))
54    tags = node_tags.get(n, None)
55    if(tags):
56      for k,v in tags.items():
57        f.write("\n<tag k='%s' v='%s'/>" % (k,v))
58    f.write("</node>\n")
59   
60  for w in ways:
61    f.write("<way id='%d'>")
62    for k,v in w['t'].items():
63      f.write("\n<tag k='%s' v='%s'/>" % (k,v))
64    for n in w['n']:
65      f.write("\n<nd ref='%d'/>" % n)
66    f.write("</way>\n")
67 
68  f.write("</osm>\n")
69  f.close()
70 
Note: See TracBrowser for help on using the repository browser.