简体   繁体   中英

How to group points to polygon and then match it to the city area

I'm trying to group points to make it into a polygon and then match it into a city area. So I have a table consisting of grids in lat and long, and a city table consisting of city name, lat and long. Below is the image of the grids table

网格表

Below is the image of the grids covering the India map, show a portion of it to have a better look on the grids

带网格的印度地图

Below is the image of the city table

印度城市表

The result of the desired city area will then be put into the grids table

Follow up of this question would be -> after displaying the proper polygon of the city area (With the grids inside of the area), this will be later be matched with a table consisting of user location (lat, long) so that the query can be more faster rather than using haversine to find the distance of the user location to the nearest city.

Sorry to make this question complicated, i'm a bit curious on whether there's a faster way to query a code without using haversine function

The way I understand your question, you want all your points to be attributed to the city they fall within. This is a spatial join with geopandas ( very well explained in the geopandas doc ) :

merged = gpd.sjoin(grid_points, cities, how='left', op='within')

this will join the attributes of the cities polygons to the points.

It will indeed be a lot faster than computing the one to one distances because GeoPandas provides fast spatial joins with the C compiled RTree library.

If you want to attribute the closest city to a point that is not actually within one (eg. points in the blank zone of your screen print) , then it might be more complicated, and you could look into scipy's cKDTree which will give you the closest points. This could be used with the city polygons representative points, that you can get from shapely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM