I have two shapefiles, one is a 1x1 km grid displayed as polygons and the other one is a street network displayed as linestrings. I want to cut the linestrings where they intersect the boundaries of a polygon and add the data from the respective gridcell to the resulting linestrings (similar to "Intersect" operation in ArcGIS). I searched for a solution for a while now, but I cannot find an appropriate answer for this problem. Is there a way to do this operation using Python?
I tried something with geopandas now and it seems to be working now! :) after some more searching around, i found this and i got the setup from there intersecting two shapefiles from Python or command line
import geopandas as gpd
grid_raw = gpd.read_file(r"fliepath")
grid_g = grid_raw[98597:98697].copy()
street_raw = gpd.read_file(r"filepath")
street_g = street_raw[99:125].copy()
data = []
print("Start")
for index, streets in street_g.iterrows():
for index2, grid in grid_g.iterrows():
if street['geometry'].intersects(grid['geometry']) is True:
data.append({'geometry': street['geometry'].intersection(grid['geometry']), 'gridcode':grid['GRIDCODE'], 'emission': grid['emissions'], 'oneway': street['oneway'], 'length':street['geometry'].intersection(grid['geometry']).length})
print('cut')
else:
data.append({'geometry': street['geometry'], 'gridcode':grid['GRIDCODE'], 'emission': grid['emissions'], 'oneway': street['oneway'], 'length':street['geometry'].length})
int_data = gpd.GeoDataFrame(data,columns=['geometry', 'gridcode', 'emission','oneway','length'])
#int_data.to_file('intersection.shp')
int_data.head()
geopandas
is a pretty good entry point for these types of vector operations.