source: subversion/applications/routing/pyroute/compress/decompress.py @ 20924

Last change on this file since 20924 was 5998, checked in by ojw, 12 years ago

storing nodes in ways. bit more detection of volatile values

  • Property svn:executable set to *
File size: 3.3 KB
Line 
1#!/usr/bin/python
2#----------------------------------------------------------------
3#
4#------------------------------------------------------
5# Usage:
6#
7#------------------------------------------------------
8# Copyright 2007, Oliver White
9#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22#------------------------------------------------------
23import sys
24import os
25from struct import *
26
27class BinaryOsm:
28  def __init__(self):
29    self.values = {}
30    pass
31   
32  def decode(self, filename, output):
33    if(not os.path.exists(filename)):
34      print "No such data file %s" % filename
35      return
36    Output = open(output, "w")
37    Input = open(filename, "rb")
38    self.readBinary(Input, Output)
39    Input.close()
40    Output.close()
41
42  def readBinary(self, Input, Output):
43    while(True):
44      type = Input.read(1)
45      if(type == 'N'):
46        self.readNode(Input, Output)
47      elif(type == 'W'):
48        self.readWay(Input, Output)
49      elif(type == 'X'):
50        print "Reached end of data!!!"
51        return
52      else:
53        print "Unknown type"
54
55  def readNode(self, Input, Output):
56    nid = unpack("L", Input.read(4))[0]
57    lat,lon = self.decodeLL(Input.read(8))
58    #print "Node %u at (%f,%f)"%(nid,lat,lon)
59    self.readTags(Input, Output)
60   
61  def readWay(self, Input, Output):
62    wid = unpack("L", Input.read(4))[0]
63    numNodes = unpack("H", Input.read(2))[0]
64    nodes = []
65    for i in range(numNodes):
66      nid = unpack('L',Input.read(4))[0]
67      nodes.append(nid)
68    #print "Read way %u, %u nodes" % (wid,numNodes)
69    self.readTags(Input, Output)
70
71  def readTags(self, Input, Output):
72    n = unpack('B', Input.read(1))[0]
73    if(n == 0):
74      return
75    for i in range(n):
76      k = self.readKV(Input,Output)
77      v = self.readKV(Input,Output)
78      #print "%s = %s" % (k,v)
79
80  def readKV(self, Input, Output):
81    ID = unpack('H', Input.read(2))[0]
82    if(ID == 0):
83      # one-off
84      lenX = unpack('B', Input.read(1))[0]
85      text = Input.read(lenX)
86      return(text.decode("utf-8"))
87   
88    if(ID == 1):
89      # New + store
90      NewID = unpack('H', Input.read(2))[0]
91      lenX = unpack('B', Input.read(1))[0]
92      X = Input.read(lenX)
93      X = X.decode("utf-8")
94      self.values[NewID] = X
95      return(X)
96     
97    else:
98      # ID that we should already know
99      return(self.values[ID])
100   
101   
102  def decodeLL(self,data):
103    iLat,iLon = unpack("II", data)
104    pLat = self.decodeP(iLat)
105    pLon = self.decodeP(iLon)
106    lat = pLat * 180.0 - 90.0
107    lon = pLon * 360.0 - 180.0
108    return(lat,lon)
109   
110  def decodeP(self,i):
111    p = float(i) / 4294967296.0
112    return(p)
113   
114# Parse the supplied OSM file
115if __name__ == "__main__":
116  print "Loading data..."
117  Binary = BinaryOsm()
118  Binary.decode(sys.argv[1], sys.argv[2])
Note: See TracBrowser for help on using the repository browser.