Mamed
Mamed

Reputation: 772

Check if the point within a polygon (speed up)

I have 2 data frames. 1) data - long and lat points 2) border = shapefile of a city


I need to check which points are within the shapefile and save them. Here is my code to do that:

Data

city = pd.read_csv("D:...path.../data.csv")
crs = {'init':'epsg:4326'}
geometry = [Point(xy) for xy in zip(city.longitude,city.latitude)]
city_point = gpd.GeoDataFrame(city,crs=crs,geometry=geometry)

Border

border = gpd.read_file("C:...path.../border.shp")
border_gdf = gpd.GeoDataFrame(border, geometry='geometry')

Final check

city_point['inside'] = city_point['geometry'].apply(border_gdf.contains)
city_point = city_point[city_point.inside != True]

Libraries

import numpy as np
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point, Polygon

Upvotes: 0

Views: 146

Answers (1)

Christoph Rieke
Christoph Rieke

Reputation: 757

city_point[city_point.geometry.within(border_gdf.iloc[0].geometry)]

Upvotes: 1

Related Questions