| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,19 @@ |
| 1 |
+import shapefile |
|
| 2 |
+from json import dumps |
|
| 3 |
+ |
|
| 4 |
+sf = shapefile.Reader("pyshp/shapefiles/blockgroups")
|
|
| 5 |
+ |
|
| 6 |
+fields = sf.fields[1:] |
|
| 7 |
+field_names = [field[0] for field in fields] |
|
| 8 |
+ |
|
| 9 |
+buffer = [] |
|
| 10 |
+for sr in sf.shapeRecords(): |
|
| 11 |
+ atr = dict(zip(field_names, sr.record)) |
|
| 12 |
+ |
|
| 13 |
+ geom = sr.shape.__geo_interface__ |
|
| 14 |
+ buffer.append(dict(type="Feature", geometry=geom, properties=atr)) |
|
| 15 |
+ |
|
| 16 |
+geojson = open("pyshp-demo.geojson", "w")
|
|
| 17 |
+geojson.write(dumps({"type": "FeatureCollection",\
|
|
| 18 |
+"features": buffer}, indent=2) + "\n") |
|
| 19 |
+geojson.close() |