Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for converting geojson to csv. Closes #183.
- Loading branch information
Showing
5 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/env python | ||
|
||
from cStringIO import StringIO | ||
import json | ||
|
||
from csvkit import CSVKitWriter | ||
|
||
def geojson2csv(f, key=None, **kwargs): | ||
""" | ||
Convert a GeoJSON document into CSV format. | ||
""" | ||
document = f.read() | ||
js = json.loads(document) | ||
|
||
if not isinstance(js, dict): | ||
raise TypeError('JSON document is not valid GeoJSON: Root element is not an object.') | ||
|
||
if 'type' not in js: | ||
raise TypeError('JSON document is not valid GeoJSON: No top-level "type" key.') | ||
|
||
if js['type'] != 'FeatureCollection': | ||
raise TypeError('Only GeoJSON with root FeatureCollection type is supported. Not %s' % js['type']) | ||
|
||
if 'features' not in js: | ||
raise TypeError('JSON document is not a valid FeatureCollection: No top-level "features" key.') | ||
|
||
features = js['features'] | ||
|
||
features_parsed = [] # tuples in the format (id, properties, geometry) | ||
property_set = set() | ||
|
||
for feature in features: | ||
geoid = feature.get('id', None) | ||
|
||
properties = feature.get('properties') or {} | ||
property_set.update(properties.keys()) | ||
|
||
geometry = json.dumps(feature['geometry']) | ||
|
||
features_parsed.append((geoid, properties, geometry)) | ||
|
||
header = ['id'] | ||
fields = sorted(list(property_set)) | ||
header.extend(fields) | ||
header.append('geojson') | ||
|
||
o = StringIO() | ||
writer = CSVKitWriter(o) | ||
|
||
writer.writerow(header) | ||
|
||
for geoid, properties, geometry in features_parsed: | ||
row = [geoid] | ||
|
||
for field in fields: | ||
row.append(properties.get(field, None)) | ||
|
||
row.append(geometry) | ||
|
||
writer.writerow(row) | ||
|
||
output = o.getvalue() | ||
o.close() | ||
|
||
return output | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
id,prop0,prop1,geojson | ||
,value0,,"{""type"": ""Point"", ""coordinates"": [102.0, 0.5]}" | ||
,value0,0.0,"{""type"": ""LineString"", ""coordinates"": [[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]]}" | ||
,value0,{u'this': u'that'},"{""type"": ""Polygon"", ""coordinates"": [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]}" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [102.0, 0.5] | ||
}, | ||
"properties": { | ||
"prop0": "value0" | ||
} | ||
}, { | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[102.0, 0.0], | ||
[103.0, 1.0], | ||
[104.0, 0.0], | ||
[105.0, 1.0] | ||
] | ||
}, | ||
"properties": { | ||
"prop0": "value0", | ||
"prop1": 0.0 | ||
} | ||
}, { | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Polygon", | ||
"coordinates": [ | ||
[ | ||
[100.0, 0.0], | ||
[101.0, 0.0], | ||
[101.0, 1.0], | ||
[100.0, 1.0], | ||
[100.0, 0.0] | ||
] | ||
] | ||
}, | ||
"properties": { | ||
"prop0": "value0", | ||
"prop1": { | ||
"this": "that" | ||
} | ||
} | ||
}] | ||
} | ||
|
This file contains 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
c13ea6e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!