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

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

Use the tile data server, instead of OSM API

File size: 3.1 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    self.divisor = float(2 ** 31)
39    if(filename != None):
40      self.loadOsm(filename)
41
42  def loadOsm(self, filename):
43    """Load an OSM XML file into memory"""
44    if(not os.path.exists(filename)):
45      return
46    try:
47      parser = make_parser()
48      parser.setContentHandler(self)
49      parser.parse(filename)
50    except xml.sax._exceptions.SAXParseException:
51      print "Error loading %s" % filename
52   
53
54  def startElement(self, name, attrs):
55    """Handle XML elements"""
56    if name in('node','way','relation'):
57      self.tags = {}
58      self.isInteresting = False
59      self.waynodes = []
60      if name == 'node':
61        """Nodes need to be stored"""
62        id = int(attrs.get('id'))
63        self.nodeID = id
64        lat = float(attrs.get('lat'))
65        lon = float(attrs.get('lon'))
66        self.nodes[id] = (lat,lon)
67      elif name == 'way':
68        id = int(attrs.get('id'))
69        self.wayID = id
70    elif name == 'nd':
71      """Nodes within a way -- add them to a list"""
72      node = {
73        'id': int(attrs.get('id')),
74        'lat': float(attrs.get('y')) / self.divisor, 
75        'lon': float(attrs.get('x')) / self.divisor}
76      self.waynodes.append(node)
77    elif name == 'tag':
78      """Tags - store them in a hash"""
79      k,v = (attrs.get('k'), attrs.get('v'))
80     
81      # Test if a tag is interesting enough to make it worth
82      # storing this node as a special "point of interest"
83      if not k in ('created_by'): # TODO: better list of useless tags
84        self.tags[k] = v
85        self.isInteresting = True
86 
87  def endElement(self, name):
88    if name == 'way':
89      self.ways[self.wayID] = ({'t':self.tags, 'n':self.waynodes})
90    elif name == 'node':
91      if(self.isInteresting):
92        self.poi.append({'t':self.tags, 'id':self.nodeID})
93
Note: See TracBrowser for help on using the repository browser.