5

I have two shapefiles that I am using for a verification processes. I am trying to clip some centroids to some buffers I made to reduce the amount of centroids. The code runs through, but with this centroid set, it clips to the first two buffers and then stops. I can clearly see that there are centroids in the other buffers. I am not quite sure why this is happening.

Any suggestions?

enter image description here

Centroid shapefile

Buffer

import geopandas
import glob
import os

Centroid_path = "D:/Random/Centroid_Clip/99"
Extent_path = "E:/99/verification/99Buffer.shp"

BigBuffer = geopandas.read_file(Extent_path)
temp = os.path.split(Extent_path)
save_name_plot = temp[1].split('Buffer.')[0]
os.mkdir('D:/Random/Point/' + save_name_plot)

for shape_file in glob.glob(Centroid_path + '/*.shp'):
    split_path = os.path.split(shape_file)
    save_name = split_path[1].split('.')[0]
    Centroid = geopandas.read_file(shape_file)

    try:
        Clipped = geopandas.clip(BigBuffer, Centroid)
        Clipped.to_file('D:/Random/Point/' + save_name_plot + '/' + str(save_name) + '.shp')
    except:
        print()

Edit 1 (removed try except block)

...

for shape_file in glob.glob(Centroid_path + '/*.shp'):
    split_path = os.path.split(shape_file)
    save_name = split_path[1].split('.')[0]
    Centroid = geopandas.read_file(shape_file)

    Clipped = geopandas.clip(BigBuffer, Centroid)
    Clipped.to_file('D:/Random/Point/' + save_name_plot + '/' + str(save_name) + '.shp')
    print()

Error

Traceback (most recent call last):
  File "D:/Random/Model_Points_In_Plots.py", line 27, in <module>
    Clipped.to_file('D:/Random/Point/' + save_name_plot + '/' + str(save_name) + '.shp')
  File "C:\Users\z\anaconda3\envs\random\lib\site-packages\geopandas\geodataframe.py", line 1086, in to_file
    _to_file(self, filename, driver, schema, index, **kwargs)
  File "C:\Users\z\anaconda3\envs\random\lib\site-packages\geopandas\io\file.py", line 330, in _to_file
    colxn.writerecords(df.iterfeatures())
  File "C:\Users\z\anaconda3\envs\random\lib\site-packages\fiona\collection.py", line 361, in writerecords
    self.session.writerecs(records, self)
  File "fiona/ogrext.pyx", line 1295, in fiona.ogrext.WritingSession.writerecs
RuntimeError: GDAL Error: Attempt to write non-multipoint (POINT) geometry to multipoint shapefile. 
 
 Failed to write record: {'id': '1', 'type': 'Feature', 'properties': {'bottom': 5512954.238399999, 'id': 1, 'left': -13724655.3911, 'right': -13724198.191100001, 'top': 5513411.438399999}, 'geometry': {'type': 'Point', 'coordinates': (-13724451.69110714, 5513680.37091026)}}

I printed out the centroid and buffer. Centroid is a Multipoint and Buffer is a Polygon. Not sure why it says it can't write a non-multipoint (POINT) to multipoint when my points are multipoints.

Edit 2 (multipart to singlepart) I found a similar problem from another post here. I used QGIS's multipart to singlepart tool and tried again. This time it resulted in the same error, but did happen to write the first two (similar to purple points in picture one).

9
  • I looked through the documentation and tried multiple solutions, but have come up with two results. One being the saved output is the BigBuffer and the other is the Centroid. geopandas.sjoin(Centroid, BigBuffer, how="left", op="contains")
    – Binx
    Jul 21, 2021 at 14:37
  • I am not verifying if the centroids land within the Buffer. I actually have another buffer layer within the main buffer layer that will be used. For this question, I am just after the clipped shapefile.
    – Binx
    Jul 21, 2021 at 14:40
  • The files you are sharing are not the ones in your screenshot
    – BERA
    Jul 26, 2021 at 18:57
  • Start by removing the try/except which is silencing any useful exceptions
    – mikewatt
    Jul 26, 2021 at 19:05
  • Check to make sure your feature geometries are consistent. Mixed multi-parts can cause issues. You may also wish to test behaviour when no points fall within the buffer.
    – CreekGeek
    Jul 26, 2021 at 19:54

1 Answer 1

4
+100

Basically your issue is this one guy here:

enter image description here

This centroid only has one point in it so it gets assigned as POINT and not MULTIPOINT like the others. To fix this you can cast everything to MULTIPOINT like this:

# Needs to be added to imports:
from shapely.geometry import Point, MultiPoint

clipped = geopandas.clip(BigBuffer, Centroid, keep_geom_type=True)
clipped["geometry"] = [MultiPoint([feature]) if type(feature) == Point \
                        else feature for feature in clipped["geometry"]]
clipped.to_file('Downloads/test/' + save_name_plot + '/' + str(save_name) + '.shp')
1
  • 1
    Wow! Honestly, I don't think I would have ever figured that out. SUPER appreciate finding that issue! I'll be able to award the bounty when the time required has passed.
    – Binx
    Jul 27, 2021 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.