-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg2geojson.py
32 lines (22 loc) · 905 Bytes
/
pg2geojson.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
import datetime
import simplejson as json
def to_obj(cur, rows, geom_col):
""" Creates a feature collection as a dict. Assumes rows is a dict """
feature_collection = {'type': 'FeatureCollection', 'features': []}
for row in rows:
geom = row.pop(geom_col)
feature = {
'type': 'Feature',
'geometry': geom,
'properties': row,
}
feature_collection['features'].append(feature)
return feature_collection
def to_str(cur, rows, geom_col):
""" Creates a feature collection as a string. Assumes rows is a dict """
feature_collection = to_obj(cur, rows, geom_col)
def encode(obj):
if isinstance(obj, datetime.date):
return obj.isoformat()
raise TypeError(repr(obj) + " is not JSON serializable")
return json.dumps(feature_collection, indent=2, use_decimal=True, default=encode)