TransWikia.com

Find vertices of LineStrings within Polygon - Shapely

Geographic Information Systems Asked by Alex Jüstel on October 18, 2020

I would like to use Shapely to find the vertices that are located within the boundaries of a Shapely Polygon. So far, I could only manage to get it running by converting the LineString and the Polygon to a GeoPandas DataFrame and use the Geopandas within(...) function. Ideally, the function that I am looking for would return a list of Shapely Points that were either kept or are the ones that were within the Polygon.

Here a little code example of what I have done so far:

fault_ls and interface_ls are Shapely Linestrings.

# Creating a buffer around the fault trace
fault_polygon = fault_ls.buffer(radius)

# Creating GeoDataFrame from Polygon
fault_polygon_gdf = gpd.GeoDataFrame({'geometry': [fault_polygon]}, crs=crs)

# Create lists with X and Y coordinates from LineString
x = [i[0] for i in interface_ls.coords]
y = [i[1] for i in interface_ls.coords]

# Creating GeoDataFrame from LineString
interface_ls_gdf = gpd.GeoDataFrame(geometry=gpd.points_from_xy(x, y, crs=crs))

vertices_in = interface_ls_gdf[interface_ls_gdf.within(fault_polygon_gdf.loc[0, 'geometry'])]

vertices_out = interface_ls_gdf[~interface_ls_gdf.within(fault_polygon_gdf.loc[0, 'geometry'])]

vertices_in are the vertices within the buffer, vertices_out the ones outside that I want to keep.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP