boost distance query from point to polygon
See original GitHub issueSeveral ways have been developed to boost distance queries from point to point and point to line (see here). However, I have not found a solution to fast distance query from point to polygon using R-Tree or cKDTree. Here, Geoff Boeing provides a way to boost spatial query, but looks like (to me) can only detect whether points are falling into the polygon using .intersect()
. My question is how to calculate the nearest distance from point gdf to polygon gdf quickly using R-Tree, spatial index, etc. I did some tests on small dataset.
# Load libraries
%matplotlib inline
import numpy as np
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
from geopandas import GeoSeries
from shapely.geometry import Point
# New York City
nyc = gpd.read_file(gpd.datasets.get_path('nybb'))
# Random points within the bounding box
np.random.seed(123)
xmin, xmax, ymin, ymax = 900000, 1080000, 120000, 280000
xc = (xmax - xmin) * np.random.random(20) + xmin
yc = (ymax - ymin) * np.random.random(20) + ymin
geopts = gpd.GeoDataFrame(GeoSeries([Point(x, y) for x, y in zip(xc, yc)]))
geopts.rename(columns={0:'geometry'}, inplace=True)
# Plot NYC and points
ax = nyc.boundary.plot(edgecolor='black')
geopts.plot(ax=ax, color='red')
# Distance from point to polygon
geopts['dist2poly'] = geopts.geometry.apply(lambda x: nyc.distance(x).min())
geopts
However, with big datasets, this approach can be time-consuming. Any suggestions to incorporate spatial index into this distance query? Thanks.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Using spatial index to boost distance query from point ...
Using a tolerance based bounding box to query RTree gives you a list of candidate polygons, then calculate the distance between the point...
Read more >[Boost-users] measuring distance between a point and a ...
I am currently trying to use the boost library in order to measure the shortest distance between a point and a list of...
Read more >Queries - 1.65.1
Queries using spatial predicates returns Value s which are related somehow to some Geometry - box, polygon, etc. Names of spatial predicates correspond...
Read more >Distance between Polygon and Point - postgis
Closed 8 years ago. Improve this question. I am working on a query to find the closest point between polygon and point. Can ......
Read more >Spatial Search | Apache Solr Reference Guide 7.0
Sort or boost scoring by distance between points, or relative area between rectangles. Generate a 2D grid of facet count numbers for heatmap...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@martinfleis Thanks, this worked. Forgive me if I bothered you too much, but I spent the weekend on some examples and still could not work out a solution. To make it simple, I was wondering how to get the same results just like the straightway way
geopts['dist2poly'] = geopts.geometry.apply(lambda x: nyc.distance(x).min())
did. Thanks, much appreciated.Glad you found a solution! Just to quickly elaborate on the example @martinfleis have above, it is important to note that if you set
num_results
to a small number, you may not get accurate results. If you use too large of a number, things will be very slow. There is a much more detailed discussion of this in #1271.