... | ... |
@@ -1,6 +1,6 @@ |
1 | 1 |
#-*- coding: utf-8 -*- |
2 | 2 |
import shapefile |
3 |
-from json import dumps |
|
3 |
+import json |
|
4 | 4 |
|
5 | 5 |
path = "pyshp/shapefiles/blockgroups" |
6 | 6 |
sf = shapefile.Reader(path) |
... | ... |
@@ -16,5 +16,5 @@ for sr in sf.shapeRecords(): |
16 | 16 |
buffer.append(dict(type="Feature", geometry=geom, properties=atr)) |
17 | 17 |
|
18 | 18 |
geojson = open("pyshp-demo.geojson", "w") |
19 |
-geojson.write(dumps({"type": "FeatureCollection", "features": buffer}, indent=2, ensure_ascii=False)) |
|
19 |
+geojson.write(json.dumps({"type": "FeatureCollection", "features": buffer}, indent=2, ensure_ascii=False)) |
|
20 | 20 |
geojson.close() |
... | ... |
@@ -1,7 +1,9 @@ |
1 |
+#-*- coding: utf-8 -*- |
|
1 | 2 |
import shapefile |
2 | 3 |
from json import dumps |
3 | 4 |
|
4 |
-sf = shapefile.Reader("pyshp/shapefiles/blockgroups") |
|
5 |
+path = "pyshp/shapefiles/blockgroups" |
|
6 |
+sf = shapefile.Reader(path) |
|
5 | 7 |
|
6 | 8 |
fields = sf.fields[1:] |
7 | 9 |
field_names = [field[0] for field in fields] |
... | ... |
@@ -14,6 +16,5 @@ for sr in sf.shapeRecords(): |
14 | 16 |
buffer.append(dict(type="Feature", geometry=geom, properties=atr)) |
15 | 17 |
|
16 | 18 |
geojson = open("pyshp-demo.geojson", "w") |
17 |
-geojson.write(dumps({"type": "FeatureCollection",\ |
|
18 |
-"features": buffer}, indent=2) + "\n") |
|
19 |
+geojson.write(dumps({"type": "FeatureCollection", "features": buffer}, indent=2, ensure_ascii=False)) |
|
19 | 20 |
geojson.close() |
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() |