question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

boost distance query from point to polygon

See original GitHub issue

Several 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:open
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
shuai-zhoucommented, Jun 1, 2020

@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.

0reactions
adriangbcommented, Jun 6, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found