Skip to content

Instantly share code, notes, and snippets.

@zarch
Created January 9, 2013 11:23
Show Gist options
  • Save zarch/4492419 to your computer and use it in GitHub Desktop.
Save zarch/4492419 to your computer and use it in GitHub Desktop.
import sys
from imposm.parser import OSMParser
from time import time
from datetime import datetime
class NavTagsCounter(object):
turnrestrictions = 0
meanwayversion = 0
firstobject = datetime.max
lastobject = datetime.min
numcoords = numnodes = numways = numrelations = 0
maxnodeid = maxwayid = maxrelationid = 0
minnodeid = minwayid = minrelationid = sys.maxint
minlon = minlat = float(180.0)
maxlon = maxlat = float(-180.0)
def coord(self, coords):
for osmid, lon, lat, osmversion, osmtimestamp in coords:
self.numcoords += 1
objdate = datetime.utcfromtimestamp(osmtimestamp)
self.lastobject = self.lastobject if self.lastobject > objdate else objdate
self.firstobject = self.firstobject if self.firstobject < objdate else objdate
self.minnodeid = self.minnodeid if self.minnodeid < osmid else osmid
self.maxnodeid = self.maxnodeid if self.minnodeid > osmid else osmid
self.minlon = self.minlon if self.minlon < lon else lon
self.minlat = self.minlat if self.minlat < lat else lat
self.maxlon = self.maxlon if self.maxlon > lon else lon
self.maxlat = self.maxlat if self.maxlat > lat else lat
def node(self, nodes):
for osmid, tags, ref, osmversion, osmtimestamp in nodes:
self.numnodes += 1
def way(self, ways):
for osmid, tags, ref, osmversion, osmtimestamp in ways:
self.numways += 1
objdate = datetime.utcfromtimestamp(osmtimestamp)
self.meanwayversion = float(((self.numways - 1) * self.meanwayversion + osmversion) / float(self.numways))
self.lastobject = self.lastobject if self.lastobject > objdate else objdate
self.firstobject = self.firstobject if self.firstobject < objdate else objdate
self.minwayid = self.minwayid if self.minwayid < osmid else osmid
self.maxwayid = self.maxwayid if self.minwayid > osmid else osmid
def relation(self, relations):
for osmid, tags, refs, osmversion, osmtimestamp in relations:
self.numrelations += 1
objdate = datetime.utcfromtimestamp(osmtimestamp)
if 'type' in tags and tags['type'] == 'restriction':
self.turnrestrictions += 1
self.lastobject = self.lastobject if self.lastobject > objdate else objdate
self.firstobject = self.firstobject if self.firstobject < objdate else objdate
self.minrelationid = self.minrelationid if self.minrelationid < osmid else osmid
self.maxrelationid = self.maxrelationid if self.minrelationid > osmid else osmid
if __name__ == '__main__':
OSMFILE = 'molise.pbf'
t0 = time()
counter = NavTagsCounter()
p = OSMParser(concurrency=8,
coords_callback = counter.coord,
nodes_callback = counter.node,
ways_callback=counter.way,
relations_callback=counter.relation)
p.parse(OSMFILE)
t1 = time()
print '''number of coords: %d
nodes: %d
ways: %d
relations: %d
parsing time: %f''' % (counter.numcoords,
counter.numnodes,
counter.numways,
counter.numrelations,
t1 - t0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment