-
Notifications
You must be signed in to change notification settings - Fork 11
/
skeletron-hadoop-mapper.py
executable file
·49 lines (38 loc) · 1.57 KB
/
skeletron-hadoop-mapper.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
#!/usr/bin/env python
'''
Test usage:
cat oakland-sample.json | ./skeletron-hadoop-mapper.py | sort | ./skeletron-hadoop-reducer.py > output.json
'''
from sys import stdin, stdout
from json import load, dumps
from itertools import product
from uuid import uuid1
import logging
logging.basicConfig(level=logging.INFO, format='%(levelname)08s - %(message)s')
from shapely.geometry import asShape
from Skeletron.output import generalize_geometry
from Skeletron.util import hadoop_feature_line
if __name__ == '__main__':
geojson = load(stdin)
pixelwidth = 20
for (feature, zoom) in product(geojson['features'], (12, 13, 14, 15, 16)):
id = str(uuid1())
prop = feature.get('properties', {})
geom = asShape(feature['geometry'])
try:
skeleton = generalize_geometry(geom, pixelwidth, zoom)
bones = getattr(skeleton, 'geoms', [skeleton])
prop.update(dict(zoomlevel=zoom, pixelwidth=pixelwidth))
if not skeleton:
logging.debug('Empty skeleton')
continue
except Exception, e:
logging.error(str(e))
continue
if id is None:
for (index, bone) in enumerate(bones):
logging.info('line %d of %d from %s' % (1 + index, len(bones), dumps(prop)))
print >> stdout, hadoop_feature_line(id, prop, bone)
else:
logging.info('%d-part multiline from %s' % (len(bones), dumps(prop)))
print >> stdout, hadoop_feature_line(id, prop, skeleton)