4

I am using Python. I have a shapefile with polygons and a CSV with points. My goal is to assign to the points all the possible polygons within a distance of 10 kilometers.

I am able to assign the closest polygon to the points (see code below), but I am not able to assign polygons that are not the closest ones but they are within the distance of 10 kilometers.

This is what I am trying:

import numpy as np
import pandas as pd
import geopandas as gpd

wcea_crs = CRS.from_string('esri:54034')

shapefile_polygons = gpd.read_file(data_directory)
shapefile_polygons_reprojected = shapefile_polygons.to_crs(wcea_crs)

points = pd.read_csv(data_directory)
points_gis = gpd.GeoDataFrame(
    points, geometry=gpd.points_from_xy(x=points.longitude, y=points.latitude))
points_gis.crs = "EPSG:4326"
points_gis_reprojected = points_gis.to_crs(wcea_crs)

total_join = gpd.sjoin_nearest(points_gis_reprojected, shapefile_polygons_reprojected, how='left', max_distance = 10000, distance_col = True)

I would like to ask for suggestions to obtain the result I am trying to get.

1 Answer 1

4

You can buffer the polygons (or the points) and then spatial join. It should be very fast.

import geopandas as gpd

pointdf = gpd.read_file(r"C:\GIS\data\Bakgrundskartor\Topografi_250\data\byggnadsverk_sverige.gpkg", layer="byggnadsanlaggningspunkt")
pointdf["pointid"] = range(pointdf.shape[0]) #Create a unique id for each point
print(pointdf.pointid.duplicated().any())
#False
polydf = gpd.read_file(r"C:\GIS\data\Bakgrundskartor\Topografi_250\data\naturvard_sverige.gpkg", layer="skyddadnatur")

polydf.geometry = polydf.buffer(10000)

#Join the polygons to the points
pointdf_join_poly = pointdf.sjoin(polydf, how="left")

#Because one point can intersect many polygons those points are duplicated
print(pointdf_join_poly.pointid.duplicated().any())
#True

I dont know what you want to do next, groupby? Pivot?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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