8000 Support BYPOLYGON option for GEOSEARCH by KarthikSubbarao · Pull Request #1809 · valkey-io/valkey · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support BYPOLYGON option for GEOSEARCH #1809

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

Open
wants to merge 25 commits into
base: unstable
Choose a base branch
from

Conversation

KarthikSubbarao
Copy link
Member
@KarthikSubbarao KarthikSubbarao commented Mar 3, 2025

This PR implements the BYPOLYGON search (described here: #1755) for the GEOSEARCH and GEOSEARCHSTORE valkey commands

Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
…ords

Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
…s of GEOSEARCH

Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
…centroid

Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
…roid of polygon. We can choose to not support WITHDIST, COUNT, ASC/DESC for polygon if we want to. With this commit, we do support it

Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
…lean up

Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
@KarthikSubbarao KarthikSubbarao marked this pull request as draft March 3, 2025 05:38
Copy link
codecov bot commented Mar 3, 2025

Codecov Report

Attention: Patch coverage is 96.29630% with 4 lines in your changes missing coverage. Please review.

Project coverage is 71.01%. Comparing base (c9c49b4) to head (2890a8b).

Files with missing lines Patch % Lines
src/geo.c 92.85% 3 Missing ⚠️
src/geohash_helper.c 98.48% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #1809      +/-   ##
============================================
- Coverage     71.05%   71.01%   -0.04%     
============================================
  Files           123      123              
  Lines         66106    66201      +95     
============================================
+ Hits          46971    47014      +43     
- Misses        19135    19187      +52     
Files with missing lines Coverage Δ
src/commands.def 100.00% <ø> (ø)
src/geohash_helper.c 98.88% <98.48%> (-0.28%) ⬇️
src/geo.c 93.41% <92.85%> (-0.17%) ⬇️

... and 13 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

8000
@@ -280,3 +365,21 @@ int geohashGetDistanceIfInRectangle(double width_m,
*distance = geohashGetDistance(x1, y1, x2, y2);
return 1;
}

/* Check if a point is in a polygon using ray casting. */
int geohashGetDistanceIfInPolygon(double x1, double y1, double *xy, double (*vertices)[2], int num_vertices, double *distance) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to distinguish whether the point is on the edge?

Copy link
Member Author
@KarthikSubbarao KarthikSubbarao Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you , this is a good question. I thought about this during implementation.

It is unlikely that a point will be on the an edge of a polygon to the exact precision (except for the very clear cases) - so the BYPOLYGON search might not return any results even if we handle the "is a point on the edge" check.

You can let me know what you think based on your experience with GIS applications

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will look into the linked algorithm and will get back to you here in the next day or two

Copy link
Member Author
@KarthikSubbarao KarthikSubbarao Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current code in this PR is based on PNPOLY - Point Inclusion in Polygon Test by W. Randolph Franklin (WRF). See: https://wrfranklin.org/Research/Short_Notes/pnpoly.html

I read through the link you suggested (https://github.com/tair-opensource/TairGis/blob/main/src/spatial/polyraycast.c#L157).

  • From reading the code, the algorithm looks exactly the same as this PR with two additional cases handled: (1) it uses cross product to check if a point is on the line & segment (2) checks if a point is on the vertex.
  • It seems more computationally expensive to handle these two edge cases which will not be caught because it compares to the exact floating point precision since it using == (instead of eplison)

Specifically the linked code has edge case handling using == which will very rarely be true unless the point is exactly on the vertex or segment/line.
This seems unlikely to occur unless we are using test data or a geographically very small polygon and set of coordinates

	if (a.x == b.x && a.y == b.y) {
		if (p.x == a.x && p.y == a.y) {
			return RAY_ON;
		}
		return RAY_OUT;
	}
	// try to check if p is on line ab and p is on segment ab.
	if (((a.x <= p.x && p.x <= b.x) || (b.x <= p.x && p.x <= a.x)) && ((a.y <= p.y && p.y <= b.y) || (b.y <= p.y && p.y <= a.y)) && (p.y - a.y) * (b.x - a.x) == (p.x - a.x) * (b.y - a.y)) {
		return RAY_ON;
	} 

@madolson madolson self-assigned this Apr 8, 2025
@madolson madolson added the major-decision-pending Major decision pending by TSC team label Apr 8, 2025
…oint on vertex and point on segment/line

Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
…point on vertex and point on segment/line

Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
…nly use cartesian coordinates to calculate the centroid

Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
@madolson madolson added major-decision-approved Major decision approved by TSC team and removed major-decision-pending Major decision pending by TSC team labels Apr 21, 2025
@madolson
Copy link
Member

Major decision approved as long as there are no additional API, config, or info changes. Once the PR is closed we can merge it.

Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
Signed-off-by: KarthikSubbarao <karthikrs2021@gmail.com>
@KarthikSubbarao KarthikSubbarao marked this pull request as ready for review April 28, 2025 06:07
@KarthikSubbarao
Copy link
Member Author

@yangbodong22011 / @valkey-io/core-team - You can take a look when you have time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
major-decision-approved Major decision approved by TSC team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0