[7742] | 1 | #!/usr/bin/python |
---|
[7803] | 2 | #---------------------------------------------------------------------------- |
---|
| 3 | # Merge multiple OSM files (in memory) and save to another OSM file |
---|
| 4 | #---------------------------------------------------------------------------- |
---|
| 5 | # Copyright 2008, Oliver White |
---|
[7742] | 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/>. |
---|
[7803] | 19 | #--------------------------------------------------------------------------- |
---|
[7742] | 20 | from parseOsm import * |
---|
| 21 | |
---|
| 22 | def OsmMerge(dest, sources): |
---|
| 23 | """Merge multiple OSM files together |
---|
| 24 | |
---|
| 25 | Usage: |
---|
| 26 | OsmMerge('output_filename.osm', |
---|
| 27 | ['input_file_1.osm', |
---|
| 28 | 'input_file_2.osm', |
---|
| 29 | 'input_file_3.osm']) |
---|
| 30 | """ |
---|
| 31 | |
---|
| 32 | ways = [] |
---|
| 33 | poi = [] |
---|
| 34 | node_tags = {} |
---|
| 35 | nodes = {} |
---|
| 36 | |
---|
| 37 | # Trawl through the source files, putting everything into memory |
---|
| 38 | for source in sources: |
---|
| 39 | osm = parseOsm(source) |
---|
| 40 | |
---|
| 41 | ways = ways + osm.ways |
---|
| 42 | for p in osm.poi: |
---|
| 43 | node_tags[p['id']] = p['t'] |
---|
| 44 | |
---|
| 45 | for id,n in osm.nodes.items(): |
---|
| 46 | nodes[id] = n |
---|
| 47 | |
---|
| 48 | # Store the result as an OSM XML file |
---|
| 49 | f=open(dest, 'w') |
---|
| 50 | f.write("<?xml version='1.0' encoding='UTF-8'?>\n") |
---|
| 51 | f.write("<osm version='0.5' generator='OsmMerge'>\n") |
---|
| 52 | |
---|
| 53 | for n,data in nodes.items(): |
---|
| 54 | (lat,lon) = nodes[n] |
---|
| 55 | f.write("<node id='%d' lat='%f' lon='%f'>" % (n,lat,lon)) |
---|
| 56 | tags = node_tags.get(n, None) |
---|
| 57 | if(tags): |
---|
| 58 | for k,v in tags.items(): |
---|
| 59 | f.write("\n<tag k='%s' v='%s'/>" % (k,v)) |
---|
| 60 | f.write("</node>\n") |
---|
| 61 | |
---|
| 62 | for w in ways: |
---|
| 63 | f.write("<way id='%d'>") |
---|
| 64 | for k,v in w['t'].items(): |
---|
| 65 | f.write("\n<tag k='%s' v='%s'/>" % (k,v)) |
---|
| 66 | for n in w['n']: |
---|
| 67 | f.write("\n<nd ref='%d'/>" % n) |
---|
| 68 | f.write("</way>\n") |
---|
| 69 | |
---|
| 70 | f.write("</osm>\n") |
---|
| 71 | f.close() |
---|
| 72 | |
---|