source: subversion/applications/rendering/pyrender/parseOsm.py @ 8207

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

1)encode the xml values properlyin OsmMerge? (quotation marks in values broke the XML previously) 2)store ways in a dict, so we store their id with them. Previously OsmMerge? would only output '%d' (literally) as id in the xml

File size: 2.9 KB
Line 
1#!/usr/bin/python
2#----------------------------------------------------------------------------
3# Library for handling OpenStreetMap data
4#
5# Optimised for use in the pyrender system
6#
7# Handles:
8#   * Interesting nodes, with tags (store as list)
9#   * Ways, with tags (store as list)
10#   * Position of all nodes (store as hash)
11#----------------------------------------------------------------------------
12# Copyright 2008, Oliver White
13#
14# This program is free software: you can redistribute it and/or modify
15# it under the terms of the GNU General Public License as published by
16# the Free Software Foundation, either version 3 of the License, or
17# (at your option) any later version.
18#
19# This program is distributed in the hope that it will be useful,
20# but WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22# GNU General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program.  If not, see <http://www.gnu.org/licenses/>.
26#---------------------------------------------------------------------------
27import sys
28import os
29from xml.sax import make_parser, handler
30import xml
31
32class parseOsm(handler.ContentHandler):
33  def __init__(self, filename):
34    """Load an OSM XML file into memory"""
35    self.nodes = {}
36    self.ways = {}
37    self.poi = []
38    if(filename != None):
39      self.loadOsm(filename)
40
41  def loadOsm(self, filename):
42    """Load an OSM XML file into memory"""
43    if(not os.path.exists(filename)):
44      return
45    try:
46      parser = make_parser()
47      parser.setContentHandler(self)
48      parser.parse(filename)
49    except xml.sax._exceptions.SAXParseException:
50      print "Error loading %s" % filename
51   
52
53  def startElement(self, name, attrs):
54    """Handle XML elements"""
55    if name in('node','way','relation'):
56      self.tags = {}
57      self.isInteresting = False
58      self.waynodes = []
59      if name == 'node':
60        """Nodes need to be stored"""
61        id = int(attrs.get('id'))
62        self.nodeID = id
63        lat = float(attrs.get('lat'))
64        lon = float(attrs.get('lon'))
65        self.nodes[id] = (lat,lon)
66      elif name == 'way':
67        id = int(attrs.get('id'))
68        self.wayID = id
69    elif name == 'nd':
70      """Nodes within a way -- add them to a list"""
71      self.waynodes.append(int(attrs.get('ref')))
72    elif name == 'tag':
73      """Tags - store them in a hash"""
74      k,v = (attrs.get('k'), attrs.get('v'))
75     
76      # Test if a tag is interesting enough to make it worth
77      # storing this node as a special "point of interest"
78      if not k in ('created_by'): # TODO: better list of useless tags
79        self.tags[k] = v
80        self.isInteresting = True
81 
82  def endElement(self, name):
83    if name == 'way':
84      self.ways[self.wayID] = ({'t':self.tags, 'n':self.waynodes})
85    elif name == 'node':
86      if(self.isInteresting):
87        self.poi.append({'t':self.tags, 'id':self.nodeID})
88
Note: See TracBrowser for help on using the repository browser.