-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding native parsers between geojson and shapefiles
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .geojson_to_shp import geojson_to_shp | ||
from .shp_to_geojson import shp_to_geojson |
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,30 @@ | ||
import os | ||
import geojson | ||
import shapefile | ||
import multiprocessing as mp | ||
|
||
def geojson_to_shp(geojson_file_name,shp_file_name=None): | ||
""" | ||
Converts a geojson file into a shapefile. This is a serial process | ||
""" | ||
if shp_file_name == None: | ||
shp_file_name = geojson_file_name.split(os.path.sep)[-1].split(".")[0] | ||
|
||
# Open the GeoJSON and load it to memory | ||
geoj = geojson.load(open(geojson_file_name,"r")) | ||
|
||
w = shapefile.Writer() | ||
|
||
# Get the number of features | ||
features = len(geoj["features"]) | ||
|
||
i = 0 | ||
while i < features: | ||
# Paranoid to prevent exceptions u | ||
try: | ||
w.shape(geoj[i]["geometry"]) | ||
except: | ||
break | ||
i += 1 | ||
|
||
w.save('test.shp') |
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,39 @@ | ||
import os | ||
import json | ||
import shapefile | ||
import multiprocessing as mp | ||
|
||
def FeatureCollection(features): | ||
""" | ||
FeatureCollection creates a `FeatureCollection` with the | ||
features in `features` | ||
""" | ||
return {"type": "FeatureCollection","features": features} | ||
# return {"type": "FeatureCollection","properties":{"features": len(features)},"features": features} | ||
|
||
|
||
def Feature(iterator): | ||
""" | ||
Generates a feature provided the iterator `iterator` that | ||
implements the __geo__interface__ | ||
""" | ||
return {"type":"Feature", "geometry":iterator.__geo_interface__} | ||
|
||
|
||
def shp_to_geojson(shp_file_name,geojson_file_name=None): | ||
""" | ||
Converts a shp file into a geojson file. This | ||
uses multiprocessing to critically accelerate the process. | ||
""" | ||
# If no geojson_file_name is passed, use the shp_file_name without extension | ||
if geojson_file_name == None: | ||
geojson_file_name = shp_file_name.split(os.path.sep)[-1].split(".")[0] | ||
|
||
shpfile = shapefile.Reader(shp_file_name) | ||
|
||
pool = mp.Pool() | ||
features = pool.map(Feature,shpfile.iterShapes()) | ||
|
||
f = open(geojson_file_name,"w") | ||
f.write(json.dumps(FeatureCollection(features),indent=2)) | ||
f.close() |