Created
October 17, 2017 21:53
-
-
Save mvexel/129fe3160d565f05f43d879085bffdd6 to your computer and use it in GitHub Desktop.
Count objects in a series of OSM PBF files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Count objects in a series of OSM files. | |
Build a CSV file with object counts for a series of PBF | |
time slices generated from a history file | |
""" | |
from __future__ import print_function | |
import osmium as o | |
from os.path import exists | |
import csv | |
class FileStatsHandler(o.SimpleHandler): | |
"""Simple handler to count objects in OSM file.""" | |
def __init__(self): | |
"""Initialize.""" | |
super(FileStatsHandler, self).__init__() | |
self.nodes = 0 | |
self.ways = 0 | |
self.rels = 0 | |
def node(self, n): | |
"""Count nodes.""" | |
self.nodes += 1 | |
def way(self, w): | |
"""Count ways.""" | |
self.ways += 1 | |
def relation(self, r): | |
"""Count relations.""" | |
self.rels += 1 | |
if __name__ == '__main__': | |
h = FileStatsHandler() | |
result = [] | |
for year in range(2007, 2018): | |
for month in range(1, 13): | |
fname = "utah-{year}{month}01.osm.pbf".format( | |
year=year, | |
month=str(month).zfill(2)) | |
if exists(fname): | |
h.apply_file(fname) | |
print(year, month) | |
result.append([year, month, h.nodes, h.ways, h.rels]) | |
with open("/Users/martijnv/osm/planet/utah_counts.csv", "wb") as f: | |
writer = csv.writer(f) | |
writer.writerows(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment