Skip to content

Commit f8d9d13

Browse files
authored
Merge pull request #284 from jake-low/examples
Fix bug in geojson example, and rename some variables in examples for clarity
2 parents 31eb129 + 3b0df2b commit f8d9d13

11 files changed

+56
-59
lines changed

examples/amenity_list.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
This example shows how geometries from osmium objects can be imported
88
into shapely using the WKBFactory.
99
"""
10-
import osmium as o
10+
import osmium
1111
import sys
1212
import shapely.wkb as wkblib
1313

14-
wkbfab = o.geom.WKBFactory()
14+
wkbfab = osmium.geom.WKBFactory()
1515

16-
class AmenityListHandler(o.SimpleHandler):
16+
class AmenityListHandler(osmium.SimpleHandler):
1717

1818
def print_amenity(self, tags, lon, lat):
1919
name = tags.get('name', '')
@@ -33,7 +33,7 @@ def main(osmfile):
3333

3434
handler = AmenityListHandler()
3535

36-
handler.apply_file(osmfile, filters=[o.filter.KeyFilter('amenity')])
36+
handler.apply_file(osmfile, filters=[osmium.filter.KeyFilter('amenity')])
3737

3838
return 0
3939

examples/convert.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
This example shows how to write objects to a file.
55
"""
66

7-
import osmium as o
7+
import osmium
88

99
import sys
1010

@@ -13,9 +13,9 @@
1313
print("Usage: python convert.py <infile> <outfile>")
1414
sys.exit(-1)
1515

16-
writer = o.SimpleWriter(sys.argv[2])
16+
writer = osmium.SimpleWriter(sys.argv[2])
1717

18-
for obj in o.FileProcessor(sys.argv[1]):
18+
for obj in osmium.FileProcessor(sys.argv[1]):
1919
if obj.is_node():
2020
writer.add_node(obj)
2121
elif obj.is_way():

examples/convert_to_geojson.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import sys
77
import json
88

9-
import osmium as o
9+
import osmium
1010

11-
geojsonfab = o.geom.GeoJSONFactory()
11+
geojsonfab = osmium.geom.GeoJSONFactory()
1212

13-
class GeoJsonWriter(o.SimpleHandler):
13+
class GeoJsonWriter(osmium.SimpleHandler):
1414

1515
def __init__(self):
1616
super().__init__()
@@ -21,17 +21,17 @@ def __init__(self):
2121
def finish(self):
2222
print(']}')
2323

24-
def node(self, o):
25-
if o.tags:
26-
self.print_object(geojsonfab.create_point(o), o.tags)
24+
def node(self, n):
25+
if n.tags:
26+
self.print_object(geojsonfab.create_point(n), n.tags)
2727

28-
def way(self, o):
29-
if o.tags and not o.is_closed():
30-
self.print_object(geojsonfab.create_linestring(o), o.tags)
28+
def way(self, w):
29+
if w.tags and not w.is_closed():
30+
self.print_object(geojsonfab.create_linestring(w), w.tags)
3131

32-
def area(self, o):
33-
if o.tags:
34-
self.print_object(geojsonfab.create_multipolygon(o), o.tags)
32+
def area(self, a):
33+
if a.tags:
34+
self.print_object(geojsonfab.create_multipolygon(a), a.tags)
3535

3636
def print_object(self, geojson, tags):
3737
geom = json.loads(geojson)
@@ -48,7 +48,7 @@ def print_object(self, geojson, tags):
4848
def main(osmfile):
4949
handler = GeoJsonWriter()
5050

51-
handler.apply_file(osmfile,filters=[o.filter.EmptyTagFilter().apply_to(o.osm.NODE)])
51+
handler.apply_file(osmfile,filters=[osmium.filter.EmptyTagFilter().enable_for(osmium.osm.NODE)])
5252
handler.finish()
5353

5454
return 0

examples/create_nodecache.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import osmium as o
1+
import osmium
22
import sys
33

44
if len(sys.argv) != 3:
55
print("Usage: python create_nodecache.py <osm file> <node cache>")
66
exit(-1)
77

8-
reader = o.io.Reader(sys.argv[1], o.osm.osm_entity_bits.NODE)
8+
reader = osmium.io.Reader(sys.argv[1], osmium.osm.osm_entity_bits.NODE)
99

10-
idx = o.index.create_map("sparse_file_array," + sys.argv[2])
11-
lh = o.NodeLocationsForWays(idx)
10+
idx = osmium.index.create_map("sparse_file_array," + sys.argv[2])
11+
lh = osmium.NodeLocationsForWays(idx)
1212

13-
o.apply(reader, lh)
13+
osmium.apply(reader, lh)
1414

1515
reader.close()

examples/filter_coastlines.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
we are interested in and remember the nodes required. Then, in a second
88
run all the relevant nodes and ways are written out.
99
"""
10-
import osmium as o
10+
import osmium
1111
import sys
1212

1313

@@ -16,25 +16,23 @@
1616
print("Usage: python filter_coastlines.py <infile> <outfile>")
1717
sys.exit(-1)
1818

19-
2019
# go through the ways to find all relevant nodes
2120
nodes = set()
2221
# Pre-filter the ways by tags. The less object we need to look at, the better.
23-
way_filter = o.filter.KeyFilter('natural')
22+
way_filter = osmium.filter.KeyFilter('natural')
2423
# only scan the ways of the file
25-
for obj in o.FileProcessor(sys.argv[1], o.osm.WAY).with_filter(way_filter):
24+
for obj in osmium.FileProcessor(sys.argv[1], osmium.osm.WAY).with_filter(way_filter):
2625
if obj.tags['natural'] == 'coastline':
2726
nodes.update(n.ref for n in obj.nodes)
2827

29-
3028
# go through the file again and write out the data
31-
writer = o.SimpleWriter(sys.argv[2])
29+
writer = osmium.SimpleWriter(sys.argv[2])
3230

3331
# This time the pre-filtering should only apply to ways.
34-
way_filter = o.filter.KeyFilter('natural').enable_for(o.osm.WAY)
32+
way_filter = osmium.filter.KeyFilter('natural').enable_for(osmium.osm.WAY)
3533

3634
# We need nodes and ways in the second pass.
37-
for obj in o.FileProcessor(sys.argv[1], o.osm.WAY | o.osm.NODE).with_filter(way_filter):
35+
for obj in osmium.FileProcessor(sys.argv[1], osmium.osm.WAY | osmium.osm.NODE).with_filter(way_filter):
3836
if obj.is_node() and obj.id in nodes:
3937
# Strip the object of tags along the way
4038
writer.add_node(obj.replace(tags={}))

examples/normalize_boolean.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
It changes all tag values 'yes/no' to '1/0'.
44
"""
55

6-
import osmium as o
6+
import osmium
77
import sys
88

9-
class BoolNormalizer(o.SimpleHandler):
9+
class BoolNormalizer(osmium.SimpleHandler):
1010

1111
def __init__(self, writer):
1212
super(BoolNormalizer, self).__init__()
@@ -44,23 +44,22 @@ def normalize(self, o):
4444
# and discard the tag list we just created.
4545
return o
4646

47-
def node(self, o):
48-
self.writer.add_node(self.normalize(o))
47+
def node(self, n):
48+
self.writer.add_node(self.normalize(n))
4949

50-
def way(self, o):
51-
self.writer.add_way(self.normalize(o))
50+
def way(self, w):
51+
self.writer.add_way(self.normalize(w))
5252

53-
def relation(self, o):
54-
self.writer.add_relation(self.normalize(o))
53+
def relation(self, r):
54+
self.writer.add_relation(self.normalize(r))
5555

5656

5757
if __name__ == '__main__':
5858
if len(sys.argv) != 3:
5959
print("Usage: python normalize_boolean.py <infile> <outfile>")
6060
sys.exit(-1)
6161

62-
63-
writer = o.SimpleWriter(sys.argv[2])
62+
writer = osmium.SimpleWriter(sys.argv[2])
6463
BoolNormalizer(writer).apply_file(sys.argv[1])
6564

6665
writer.close()

examples/osm_diff_stats.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Shows how to detect the different kind of modifications and how to
55
use the handler generator function instead of a handler class.
66
"""
7-
import osmium as o
7+
import osmium
88
import sys
99

1010
class Stats:
@@ -31,7 +31,7 @@ def outstats(self, prefix):
3131
def main(osmfile):
3232
stats = {t: Stats() for t in 'nwr'}
3333

34-
for obj in o.FileProcessor(osmfile):
34+
for obj in osmium.FileProcessor(osmfile):
3535
stats[obj.type_str()].add(obj)
3636

3737
stats['n'].outstats("Nodes")

examples/osm_file_stats.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
44
Shows how to write a handler for the different types of objects.
55
"""
6-
import osmium as o
6+
import osmium
77
import sys
88

9-
class FileStatsHandler(o.SimpleHandler):
9+
class FileStatsHandler(osmium.SimpleHandler):
1010

1111
def __init__(self):
1212
super(FileStatsHandler, self).__init__()

examples/osm_replication_stats.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
Shows how to detect the different kind of modifications.
66
"""
7-
import osmium as o
7+
import osmium as osmium
88
import sys
99
import datetime as dt
1010
import osmium.replication.server as rserv
@@ -30,7 +30,7 @@ def outstats(self, prefix):
3030
print("%s modified: %d" % (prefix, self.modified))
3131
print("%s deleted: %d" % (prefix, self.deleted))
3232

33-
class FileStatsHandler(o.SimpleHandler):
33+
class FileStatsHandler(osmium.SimpleHandler):
3434
def __init__(self):
3535
super(FileStatsHandler, self).__init__()
3636
self.nodes = Stats()

examples/road_length.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
44
Shows how to extract the geometry of a way.
55
"""
6-
import osmium as o
6+
import osmium
77
import sys
88

99
def main(osmfile):
1010
total = 0.0
1111
# As we need the way geometry, the node locations need to be cached.
1212
# This is enabled with the with_locations() function.
13-
for obj in o.FileProcessor(osmfile, o.osm.NODE | o.osm.WAY)\
13+
for obj in osmium.FileProcessor(osmfile, osmium.osm.NODE | osmium.osm.WAY)\
1414
.with_locations()\
15-
.with_filter(o.filter.KeyFilter('highway')):
15+
.with_filter(osmium.filter.KeyFilter('highway')):
1616
if obj.is_way():
1717
try:
18-
total += o.geom.haversine_distance(obj.nodes)
19-
except o.InvalidLocationError:
18+
total += osmium.geom.haversine_distance(obj.nodes)
19+
except osmium.InvalidLocationError:
2020
# A location error might occur if the osm file is an extract
2121
# where nodes of ways near the boundary are missing.
2222
print("WARNING: way %d incomplete. Ignoring." % obj.id)

0 commit comments

Comments
 (0)