def test_expected_shape_geo_interface(typ, points, parts, expected): """ Assert that calling __geo_interface__ on arbitrary input Shape works as expected. """ shape = shapefile.Shape(typ, points, parts) geoj = shape.__geo_interface__ assert geoj == expected
def test_empty_shape_geo_interface(): """ Assert that calling __geo_interface__ on a Shape with no points or parts raises an Exception. """ shape = shapefile.Shape() with pytest.raises(Exception): shape.__geo_interface__
def shapely_to_pyshp(shapelygeom): # first convert shapely to geojson try: shapelytogeojson = shapely.geometry.mapping except: import shapely.geometry shapelytogeojson = shapely.geometry.mapping geoj = shapelytogeojson(shapelygeom) # create empty pyshp shape record = shapefile.Shape() # set shapetype if geoj["type"] == "Null": pyshptype = 0 elif geoj["type"] == "Point": pyshptype = 1 elif geoj["type"] == "LineString": pyshptype = 3 elif geoj["type"] == "Polygon": pyshptype = 5 elif geoj["type"] == "MultiPoint": pyshptype = 8 elif geoj["type"] == "MultiLineString": pyshptype = 3 elif geoj["type"] == "MultiPolygon": pyshptype = 5 record.shapeType = pyshptype # set points and parts if geoj["type"] == "Point": record.points = geoj["coordinates"] record.parts = [0] elif geoj["type"] in ("MultiPoint","Linestring"): record.points = geoj["coordinates"] record.parts = [0] elif geoj["type"] in ("Polygon"): record.points = geoj["coordinates"][0] record.parts = [0] elif geoj["type"] in ("MultiPolygon","MultiLineString"): index = 0 points = [] parts = [] for eachmulti in geoj["coordinates"]: points.extend(eachmulti[0]) parts.append(index) index += len(eachmulti[0]) record.points = points record.parts = parts return record