Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEA]: Allow to find the K nearest points to a polygon (using quadtree) #1324

Open
fpenarruApr opened this issue Jan 17, 2024 · 5 comments
Open
Labels
External Issues filed by people outside the team feature request New feature or request Python Related to Python code

Comments

@fpenarruApr
Copy link

fpenarruApr commented Jan 17, 2024

Is this a new feature, an improvement, or a change to existing functionality?

Improvement to existing functionality

How would you describe the priority of this feature request

Low priority, just an improvement

Please provide a clear description of problem you would like to solve.

Right now,
cuspatial.quadtree_point_to_nearest_linestring
allows to find the nearest point. It will be useful to find the N closest points to a feature (point, polygon or linestring)
Something similar to this article with Postgis:

https://www.crunchydata.com/blog/a-deep-dive-into-postgis-nearest-neighbor-search

SELECT loc.*, t.name, t.geom
  FROM locations loc
  JOIN LATERAL (SELECT name, geom FROM geonames gn
      ORDER BY loc.geom <-> gn.geom LIMIT 10) AS t ON true;

Describe any alternatives you have considered

No response

Additional context

No response

@fpenarruApr fpenarruApr added the feature request New feature or request label Jan 17, 2024
@GPUtester GPUtester added Needs Triage Need team to review and classify External Issues filed by people outside the team labels Jan 17, 2024
@GPUtester
Copy link
Contributor

Hi @fpenarruApr!

Thanks for submitting this issue - our team has been notified and we'll get back to you as soon as we can!
In the mean time, feel free to add any relevant information to this issue.

@isVoid isVoid added libcuspatial Relates to the cuSpatial C++ library and removed Needs Triage Need team to review and classify labels Jan 18, 2024
@jarmak-nv
Copy link
Contributor

Hey @fpenarruApr - Thanks for taking the time for a feature req here on cuSpatial!

quadtree_point_to_nearest_linestring() might do what you need already, just want to check to see if you've tried this out for your needs.

The function returns a cuDF dataframe with 3 columns:

  1. point_index - this is the index of your input points that the distance calc corresponds to
  2. linestring_index - the index of your input lines that the distance calc corresponds to
  3. distance - the distance itself in whatever your projection units are (side note, check out cuProj if you need to accelerate WGS84 <-> UTM!)

I think you can replicate

SELECT loc.*, t.name, t.geom
  FROM locations loc
  JOIN LATERAL (SELECT name, geom FROM geonames gn
      ORDER BY loc.geom <-> gn.geom LIMIT 10) AS t ON true;

With something similar to

distances = cuspatial.quadtree_point_to_nearest_linestring(linestring_quad_pairs, 
                                                           quadtree,
                                                           point_indices, 
                                                           points, 
                                                           linestrings)
top_n_distances = distances.sort(distances.distance)[:n]

That assumes the input points, or input linestrings are static (ie you're comparing a single point to a lot of different linestrings)

For any given point I think you'd need to do something similar to:

index_of_point_p = point_p_idx
top_n_distances_for_point_p = distances[distances.point_index == index_of_point_p].sort(distances.distance)[:n]

@isVoid isVoid added Python Related to Python code and removed libcuspatial Relates to the cuSpatial C++ library labels Jan 18, 2024
@fpenarruApr
Copy link
Author

Hi @jarmak-nv

Thanks for your help.
I think your suggestions are not going to solve my problem. top_n_distances will have the minimum distances between different points and polygons, and this is not what I want. Let me clarify my problem.
Suppose points representing hospitals, and polygons representing buildings. A typical Facility Problem will be to find the 3 or 4 closest hospitals to each building.
So, the solution can be a dataframe with the same number of rows that buildings, and some fields like

id_building, id_hospital1, dist_hospital1, id_hospital2, dist_hospital2, id_hospital3, dist_hospital3

Later, we can assign colors to each polygon according to distances or some coefficients and see which areas could be with a bad service.
I hope now my problem is more clear.
Thank you very much for your help! :-)

@harrism
Copy link
Member

harrism commented Jan 22, 2024

Thanks for the great example!

@jarmak-nv
Copy link
Contributor

@fpenarruApr Yeah I see that now! Definitely will leave this feature request open since it's not something we directly support.

We'll have to get creative here, I'll test a few things out this week and post some ideas 🌐

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
External Issues filed by people outside the team feature request New feature or request Python Related to Python code
Projects
Status: Todo
Development

No branches or pull requests

5 participants