-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxspeed-completeness.py
executable file
·68 lines (55 loc) · 2.26 KB
/
maxspeed-completeness.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python
# This script generates completeness statistics for maxspeed tags based on a
# given set of OpenStreetMap-data.
#
# Before running this script, you will need to load OSM-data into an
# PostGIS-database with osm2pgsql:
# sudo -u postgres createdb --encoding=UTF8 gis
# psql --dbname=gis -c "CREATE EXTENSION postgis;"
# psql --dbname=gis -c "CREATE EXTENSION hstore;"
# osm2pgsql --slim -d gis -C 2000 --hstore --number-processes 3 netherlands-latest.osm.pbf
#
# Usage: Simply run './maxspeed-completeness.py'.
from __future__ import division
import psycopg2
import sys
db = "gis"
user = "postgres"
con = None
try:
road_types = [
"motorway",
"motorway_link",
"trunk",
"trunk_link",
"primary",
"primary_link",
"secondary",
"secondary_link",
"tertiary",
"tertiary_link",
"residential",
"unclassified"]
con = psycopg2.connect(database=db, user=user)
cur = con.cursor()
# calculate total
cur.execute("SELECT COUNT(*) FROM planet_osm_line WHERE highway IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'residential', 'unclassified') AND NOT (tags->'maxspeed') is null;")
withmaxspeed = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) FROM planet_osm_line WHERE highway IN ('motorway', 'motorway_link', 'trunk', 'trunk_link', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'residential', 'unclassified');")
total = cur.fetchone()[0]
percentage = withmaxspeed / total * 100
print 'TOTAL', withmaxspeed, total, "%.2f" % percentage + '%'
# calculate for all road types
for road_type in road_types:
cur.execute("SELECT COUNT(*) FROM planet_osm_line WHERE highway = %s AND NOT (tags->'maxspeed') is null;", (road_type,))
withmaxspeed = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) FROM planet_osm_line WHERE highway = %s;", (road_type,))
total = cur.fetchone()[0]
percentage = withmaxspeed / total * 100
print road_type, withmaxspeed, total, "%.2f" % percentage + '%'
except psycopg2.DatabaseError, e:
print 'Error %s' % e
sys.exit(1)
finally:
if con:
con.close()