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

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

fix missing newlines

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 == 'nd':
67      """Nodes within a way -- add them to a list"""
68      self.waynodes.append(int(attrs.get('ref')))
69    elif name == 'tag':
70      """Tags - store them in a hash"""
71      k,v = (attrs.get('k'), attrs.get('v'))
72     
73      # Test if a tag is interesting enough to make it worth
74      # storing this node as a special "point of interest"
75      if not k in ('created_by'): # TODO: better list of useless tags
76        self.tags[k] = v
77        self.isInteresting = True
78 
79  def endElement(self, name):
80    if name == 'way':
81      self.ways.append({'t':self.tags, 'n':self.waynodes})
82    elif name == 'node':
83      if(self.isInteresting):
84        self.poi.append({'t':self.tags, 'id':self.nodeID})
85
Note: See TracBrowser for help on using the repository browser.