0

I am using Python geopandas and I have two shapefiles. One of them is a grid file containing equal polygons. The other shapefile is a linestring file with several lines and its Attributes.

The idea is to do a multiplication with the intersection length of each individual line within a grid cell and finally sum up all multiplication results which belong to this certain grid cell. Each row is one grid cell in the grid shapefile. I need the length of each intersection which is inside a grid cell. So there might be only one grid cell, but multiple intersections within this certain grid cell.

This simple multiplication should be like this:

df['new_column_result']= line['intersection_length_inside _cell_i']*line['factor_x']

And afterwards use this multiplication to calculate the sum of all df['new_column_result'] that are in the cell.

Additionally, the grid cells which are not touching any lines at all should be removed and the two shapefiles should be merged as an output. This part I managed to do by using the sjoin function of geopandas. But I am not sure if it is suitable for my goals. Sadly at the moment I get the same grid cell (with the same grid cell ID) multiple times. I guess it is produced for each "intersection_length_inside _cell_i".

For joining this two dataframes(or shapefiles) I use:

intersections = geopandas.sjoin(gridshapefile, lineshapefile, how="inner", op='intersects')

enter image description here

0

Here is one way to do it (the simplified way ) :

let's suppose that in each shapefile you have a unique column called index, if you don't you can create it by reindexing the DataFrame

 lines.reset_index(inplace = True)
 grids.reset_index(inplace = True)

1 - first apply a spatial join between both of your shapefiles, with operation within to avoid extreme cases when a line only touches the border of a grid

sjoin = geopandas.sjoin(lines, grids, how='inner', op='within')

2- then for each grid get a list of lines geometries that are within it:

grids['lines_within_geoms'] = grids['index'].apply(lambda x: sjoin[sjoin['index_right'] == x]['geometry'].tolist())

3- same for the column factor_x

grids['lines_within_factors'] = grids['index'].apply(lambda x: sjoin[sjoin['index_right'] == x]['factor_x'].tolist())

4 - and at last combine shapely's intersection with length to get the final result

grids['result'] = grids.apply(lambda x: x['geometry'].intersection(x['lines_within_geoms'][i]).length * x['lines_within_factor'][i] for i in range(len(x['lines_within_geoms'])), axis = 1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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