You can use the shape function of Shapely:
from shapely.geometry import shape
c = fiona.open('data/boroughs/boroughs_n.shp')
pol = c.next()
geom = shape(pol['geometry'])
and a MultiPolygon is a list of Polygons,so
Multi = MultiPolygon([shape(pol['geometry']) for pol in fiona.open('data/boroughs/boroughs_n.shp')])
Example with one of my data:
# the dictionaries
for pol in fiona.open('poly.shp'):
print pol['geometry']
{'type': 'Polygon', 'coordinates': [[(249744.23153029341, 142798.16434689672), (250113.79108725351, 142132.95714436853), (250062.62130244367, 141973.76225829343), (249607.77877080048, 141757.71205576291), (249367.77424759799, 142304.68402918623), (249367.77424759799, 142304.68402918623), (249744.23153029341, 142798.16434689672)]]}
{'type': 'Polygon', 'coordinates': [[(249175.78991730965, 142292.53526406409), (249367.77424759799, 142304.68402918623), (249607.77877080048, 141757.71205576291), (249014.45396077307, 141876.13484290778), (249175.78991730965, 142292.53526406409)]]}
{'type': 'Polygon', 'coordinates': [[(249026.74622412826, 142549.13626160321), (249223.42243781092, 142496.89414234375), (249175.78991730965, 142292.53526406409), (249026.74622412826, 142549.13626160321)]]}
...
and
# MultiPolygon from the list of Polygons
Multi = MultiPolygon([shape(pol['geometry']) for pol in fiona.open('poly.shp')])
Multi.wkt
'MULTIPOLYGON (((249744.2315302934148349 142798.1643468967231456, 250113.7910872535139788 142132.9571443685272243, 250062.6213024436729029 141973.7622582934272941, 249607.7787708004761953 141757.7120557629095856, 249367.7742475979903247 142304.6840291862317827, 249367.7742475979903247 142304.6840291862317827, 249744.2315302934148349 142798.1643468967231456)), ((249175.7899173096520826 142292.5352640640921891, 249367.7742475979903247 142304.6840291862317827, 249607.7787708004761953 141757.7120557629095856, 249014.4539607730694115 141876.1348429077770561, 249175.7899173096520826 142292.5352640640921891)), ((249026.7462241282628383 142549.1362616032129154, 249223.4224378109211102 142496.8941423437499907, 249175.7899173096520826 142292.5352640640921891, 249026.7462241282628383 142549.1362616032129154)), ((249244.9338986824732274 142733.5202119307068642, 249744.2315302934148349 142798.1643468967231456, 249367.7742475979903247 142304.6840291862317827, 249367.7742475979903247 142304.6840291862317827, 249367.7742475979903247 142304.6840291862317827, 249175.7899173096520826 142292.5352640640921891, 249223.4224378109211102 142496.8941423437499907, 249244.9338986824732274 142733.5202119307068642)), ((249870.8182051893090829 142570.3083320840960369, 250034.3015973484434653 142613.6706442178401630, 250152.6146321419219021 142438.5058914067049045, 250015.3392731740023009 142310.1704097116598859, 249870.8182051893090829 142570.3083320840960369)))'
see also Append support for MultiPolygons in shapefiles
A MultiLineString is a list of lines:
from shapely.geometry import MultiLineString, mapping, shape
coords = [((0, 0), (1, 1)), ((-1, 0), (1, 0))]
lines = MultiLineString(coords)
print lines
MULTILINESTRING ((0 0, 1 1), (-1 0, 1 0))
for line in lines:
print line
LINESTRING (0 0, 1 1)
LINESTRING (-1 0, 1 0)
# convert to GeoJSON format:
print mapping(lines)
{'type': 'MultiLineString', 'coordinates': (((0.0, 0.0), (1.0, 1.0)), ((-1.0, 0.0), (1.0, 0.0)))}
# convert from GeoJSON to shapely
print shape(mapping(lines))
MULTILINESTRING ((0 0, 1 1), (-1 0, 1 0))
It is the same when you read a shapefile with Fiona:
import fiona
from shapely.geometry import shape
with fiona.open(path_to_road_network_shapefile) as copy_shpfile:
for feature in copy_shpfile:
geom = feature['geometry'])
# geom in GeoJSON format -> {'type': 'MultiLineString', 'coordinates': (((0.0, 0.0), (1.0, 1.0)), ((-1.0, 0.0), (1.0, 0.0)))}
if geom['type'] == 'MultiLineString':
# convert to shapely geometry
shapely_geom = shape(geom) # = MULTILINESTRING ((0 0, 1 1), (-1 0, 1 0))
for lines in shapely_geom:
print lines
New
A shapefile has no MultiLineString schema geometries: a shapefile that indicates LineString in its schema may yield either LineString or MultiLineString features (simple list of LineStrings)
However, it is the same algorithm:
# open the original shapefile
with fiona.open('multiline.shp') as source:
# create the new shapefile
with fiona.open('lines.shp','w', driver='ESRI Shapefile',crs=source.crs,schema=source.schema) as ouput:
# iterate the features of the original shapefile
for elem in source:
# iterate the list of geometries in one element (split the MultiLineString)
for line in shape(elem['geometry']):
# write the line to the new shapefile
ouput.write({'geometry':mapping(line),'properties':elem['properties']})
Best Answer
Your
capital_pt
is thecoords
attribute of the originalcapital
shapely geometry object. In itself this is not a shapely geometry, rather a sequence of tuples of flots which are the point objects. Instead you should be usingGiven your case where you're trying to calculate distances for all points in a dataset opened by fiona, a quick example might help also: