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?
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).
geopandas.sjoin(Centroid, BigBuffer, how="left", op="contains")