Tuesday 20 June 2017

spatial analyst - Error: Intersect 2 polygons in Python. Record's geometry not match: 'GeometryCollection' != 'Polygon'



I'd tried replicating intersecting two shapefiles from Python or command line by anna-magdalena which requires intersecting two shapes. I used @gene answer for that:


import os
from shapely.geometry import mapping, shape
import fiona

os.chdir('/media/dir')
# Create schema of values that new shape will contain
schema = {'geometry': 'Polygon','properties': {'area': 'float:13.3', 'id1': 'str', 'id3': 'str'}}

with fiona.open('13fiona.shp', 'w',driver='ESRI Shapefile', schema=schema) as output:

for c1 in fiona.open('1.shp'):
for c2 in fiona.open('3.shp'):
if shape(c1['geometry']c1).intersects(shape(c2['geometry'])):
area = shape(c1['geometry']).intersection(shape(c2['geometry'])).area
prop = {'area': area, 'id1' : c1['id'],'id3': c2['id']}
geom = mapping(shape(c1['geometry']).intersection(shape(c2['geometry'])))
output.write({'geometry':geom,'properties': prop})

And I got this error:


ValueError: Record's geometry type does not match collection schema's geometry type: 'GeometryCollection' != 'Polygon'


After check the structure of objects c1, c2 and geom I got this:


Structure of c1 polygon:


{'geometry': {'type': 'Polygon', 'coordinates': [[(-74.71213794799996, 1.2098971930000744), (-74.71 ...

and c2 polygon:


{'geometry': {'type': 'Polygon', 'coordinates': [[(-74.69988807399994, 1.2222...

I found that intersection polygon assigned to geom objetc have this structure:


{'type': 'GeometryCollection', 'geometries': [{'type': 'Point', 'coordinates': (-74.71039521399996, 1.2098918560000698)}, {'type': 'Point', 'coordinates': (-74.70929715499994, 1.2102729000000636)}, ...


The c1 and c2 shapes cand be found here I'm using shapely 1.6b2 version


Any advice?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...