I am trying to project a points' shapefile on a multilinestrings' shapefile. (Project each point to the closest line)
Here's my code :
import geopandas as gpd
import time
start_time = time.time()
points4326 = gpd.GeoDataFrame.from_file('./Outputs/points.shp')
points = points4326.to_crs({'init': 'epsg:2269'})
lines = gpd.GeoDataFrame.from_file('./Shapefiles/lines.shp')
projected_points = gpd.GeoDataFrame(columns=['ID','Arrow','Comments','ZoneName','geometry'], crs={'init' :'epsg:2269'})
for index, row in points.iterrows():
pt = gpd.GeoSeries(row['geometry'])
# Project
projOnLines = lines.project(pt)
# print projOnLines
# Interpolate
ptInterpolate = lines.interpolate(projOnLines)
# print ptInterpolate
projected_points.set_value(index, 'geometry', ptInterpolate.iloc[0])
# print projected_points
# Set the value of each column of projected_points with the value of the corresponding row in the input points' shapefile
projected_points.set_value(index, 'ID', row['OBJECTID_l'])
projected_points.set_value(index,'Arrow',row['Arrow'])
projected_points.set_value(index,'Comments',row['Comments'])
projected_points.set_value(index,'ZoneName',row['ZoneName'])
print projected_points.head(5)
projected_points.to_file('./Outputs/pointsOnLines.shp')
print "time elapsed: {:.2f}s".format(time.time() - start_time)
If I just put :
projOnLines = lines.project(pt)
ptInterpolate = lines.interpolate(projOnLines)
I get this error :
ctypes.ArgumentError: argument 3: <type 'exceptions.TypeError'>: wrong type
The problem comes from the fact that projOnLines's result is a Geoseries, and interpolate expects one float. Here's what print projOnLines shows :
0 199.894
1 False
2 False
3 False
4 False
...
69955 False
69956 False
69957 False
dtype: object
Now If I take only that first value and pass it to interpolate like this :
ptInterpolate = lines.interpolate(projOnLines[0])
The script takes a long time to execute and when I finally get to display the result on QGIS I see all points are overlapped on one same location! (the attribute table has all features but they're somehow gathered in one place although I'm looping through all the points in the shapefile)
Any ideas?