2

After i did an intersection with two shape using geopandas through the following topic (Intersecting two shape problem using geopandas). But it output a multi geometry type shape,which in futher step will impossible export to polygon. Results show as below:

for i in data:
   geom = i['geometry']
   print geom

Output:

POLYGON ((139.9125 40.592043, 139.925 40.592043, 139.925 40.58371, 139.9125 40.58371, 139.9125 40.592043))
POINT (139.9125 40.58371)
LINESTRING (139.9125 40.58371, 139.925 40.58371)

How can i exclude Point and LineString from multi-types into a single geometry type as polygon?

King-Zhao
  • 319
  • 1
  • 4
  • 11

1 Answers1

3

If you simply want to exclude all the geometries that are not polygons

for i in data:
   geom = i['geometry']
   if geom.geom_type=='Polygon':
      print geom

Return only Polygon geometries

data_ok= []
for i in data:
   if i['geometry'].geom_type=='Polygon':
       data_ok.append(i)  

or in one line with list comprehensions, in one line

 data_ok= [i for i in data if i['geometry'].geom_type=='Polygon']

And you can eliminate a loop in your original script

data = []
for index, orig in poly1.iterrows():
    for index2, ref in poly2.iterrows():      
        if ref['geometry'].intersects(orig['geometry']): 
             if ref['geometry'].intersection(orig['geometry']).geom_type=='Polygon':
                owdspd=orig['id']
                data.append({'geometry':ref['geometry'].intersection(orig['geometry']),'wdspd':owdspd})
gene
  • 54,868
  • 3
  • 110
  • 187
  • Thanks very much. I know this print will works, but how to return and set the intersected data only holds polygon geometry? – King-Zhao Mar 02 '17 at 12:43
  • With another list, for example, look above – gene Mar 02 '17 at 13:05
  • Yeah, thank you so much, i've got the problem solved, it works. But as usual, the output to_file method always show an error like this:
         ` self.session.start(self, **kwargs)`
         `File "fiona\ogrext.pyx", line 942, in fiona.ogrext.WritingSession.start (fiona/ogrext.c:16386)`
         `ValueError: Null layer`
    
    – King-Zhao Mar 03 '17 at 00:26
  • df = gpd.GeoDataFrame(data,columns=['geometry','wdspd']) df.to_file('./wdspd_intersection.shp')`
          `df.head()`
    
    – King-Zhao Mar 03 '17 at 00:30