← Back to Pull requests

[GH-2454] : Implement binary predicate `relate` for Geopandas #2455

Merged
gauravbyte merged 4 files (179 lines) on Oct 28, 2025 05:30 UTC
View on GitHub
gauravbyte gauravbyte commented on Oct 26, 2025 22:31 UTC
Author

Resolves: #2454
Depends on: #2230

Did you read the Contributor Guide?

  • Yes, I have read the Contributor Rules and Contributor Development Guide
  • Is this PR related to a ticket?

  • Yes, and the PR name follows the format [GH-XXX] my subject. Closes #2454
  • What changes were proposed in this PR?


    Implement binary predicate relate for Geopandas with test cases

    How was this patch tested?


    Added tests

    Did this PR include necessary documentation updates?


  • Yes, I have updated the documentation.
  • Diff: 179 lines (+179 / -0) across 4 files Reviewer: petern48 Review duration: 1858m
    All Vouch review confidence checks passed
    Residual risk 0.05 = Change Risk (0.38) × (1 − Review Confidence (0.87)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.38 · 179 lines diff across 4 files
    0.38
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.90 · 5 substantive comment(s)
    0.90
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    87%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.311
    Real PR Conversation Timeline
    16 items
    petern48 petern48 commented on code on Oct 26, 2025 22:58 UTC
    Code Review
    python/tests/geopandas/test_geoseries.py:1
    @@ -1954,6 +1954,83 @@ def test_contains(self):
         def test_contains_properly(self):
             pass
     
    +    def test_relate(self):
    +        s = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(0, 0),
    +                LineString([(0, 0), (1, 1)]),
    +            ]
    +        )
    +        s2 = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(1, 1),
    +                LineString([(0, 0), (1, 1)]),
    +            ]
    +        )
    +        # "ABCDEFGHI" DE-9 Format
    +        # A Dimension of intersection
    +        # B Dimension of interior intersection
    +        # C Dimension of boundary intersection
    +        # D Interior of first geometry intersects exterior of second
    +        # E Exterior of first geometry intersects interior of second
    +        # F Boundary of first geometry intersects exterior of second
    +        # G Exterior of first geometry intersects boundary of second
    +        # H Exterior of first geometry intersects exterior of second
    +        # I Dimension of intersection for interiors
    +        # 0 = false, 1 = point, 2 = line, F = area
    +
    +        # 1. Test with single geometry
    +        point = Point(0, 0)
    +        result = s.relate(point)
    +        expected = pd.Series(["0FFFFFFF2", "0FFFFFFF2", "FF10F0FF2"])
    +        assert_series_equal(result.to_pandas(), expected)

            self.check_pd_series_equal(result, expected)
    

    While this works, it's actually preferred to use self.check_pd_series_equal() instead. You can read about why in this issue: https://github.com/apache/sedona/issues/2379.

    petern48 petern48 commented on code on Oct 26, 2025 22:59 UTC
    Code Review
    python/tests/geopandas/test_geoseries.py:1
    @@ -1954,6 +1954,83 @@ def test_contains(self):
         def test_contains_properly(self):
             pass
     
    +    def test_relate(self):
    +        s = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(0, 0),
    +                LineString([(0, 0), (1, 1)]),
    +            ]
    +        )
    +        s2 = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(1, 1),
    +                LineString([(0, 0), (1, 1)]),
    +            ]
    +        )
    +        # "ABCDEFGHI" DE-9 Format
    +        # A Dimension of intersection
    +        # B Dimension of interior intersection
    +        # C Dimension of boundary intersection
    +        # D Interior of first geometry intersects exterior of second
    +        # E Exterior of first geometry intersects interior of second
    +        # F Boundary of first geometry intersects exterior of second
    +        # G Exterior of first geometry intersects boundary of second
    +        # H Exterior of first geometry intersects exterior of second
    +        # I Dimension of intersection for interiors
    +        # 0 = false, 1 = point, 2 = line, F = area
    +
    +        # 1. Test with single geometry
    +        point = Point(0, 0)
    +        result = s.relate(point)
    +        expected = pd.Series(["0FFFFFFF2", "0FFFFFFF2", "FF10F0FF2"])
    +        assert_series_equal(result.to_pandas(), expected)
    +
    +        result = s.relate(s2)
    +        expected = pd.Series(["0FFFFFFF2", "FF0FFF0F2", "1FFF0FFF2"])
    +        assert_series_equal(result.to_pandas(), expected)

            self.check_pd_series_equal(result, expected)
    

    petern48 petern48 commented on code on Oct 26, 2025 22:59 UTC
    Code Review
    python/tests/geopandas/test_geoseries.py:1
    @@ -1954,6 +1954,83 @@ def test_contains(self):
         def test_contains_properly(self):
             pass
     
    +    def test_relate(self):
    +        s = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(0, 0),
    +                LineString([(0, 0), (1, 1)]),
    +            ]
    +        )
    +        s2 = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(1, 1),
    +                LineString([(0, 0), (1, 1)]),
    +            ]
    +        )
    +        # "ABCDEFGHI" DE-9 Format
    +        # A Dimension of intersection
    +        # B Dimension of interior intersection
    +        # C Dimension of boundary intersection
    +        # D Interior of first geometry intersects exterior of second
    +        # E Exterior of first geometry intersects interior of second
    +        # F Boundary of first geometry intersects exterior of second
    +        # G Exterior of first geometry intersects boundary of second
    +        # H Exterior of first geometry intersects exterior of second
    +        # I Dimension of intersection for interiors
    +        # 0 = false, 1 = point, 2 = line, F = area
    +
    +        # 1. Test with single geometry
    +        point = Point(0, 0)
    +        result = s.relate(point)
    +        expected = pd.Series(["0FFFFFFF2", "0FFFFFFF2", "FF10F0FF2"])
    +        assert_series_equal(result.to_pandas(), expected)
    +
    +        result = s.relate(s2)
    +        expected = pd.Series(["0FFFFFFF2", "FF0FFF0F2", "1FFF0FFF2"])
    +        assert_series_equal(result.to_pandas(), expected)
    +        # 2. Test with align=True (different indices)
    +        s3 = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(1, 1),
    +            ],
    +            index=range(1, 3),
    +        )
    +        s4 = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(1, 1),
    +            ],
    +            index=range(0, 2),
    +        )
    +        result = s3.relate(s4, align=True)
    +        expected = pd.Series([None, "FF0FFF0F2", None], index=[0, 1, 2])
    +        assert_series_equal(result.to_pandas(), expected)

            self.check_pd_series_equal(result, expected)
    

    petern48 petern48 commented on code on Oct 26, 2025 22:59 UTC
    Code Review
    python/tests/geopandas/test_geoseries.py:1
    @@ -1954,6 +1954,83 @@ def test_contains(self):
         def test_contains_properly(self):
             pass
     
    +    def test_relate(self):
    +        s = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(0, 0),
    +                LineString([(0, 0), (1, 1)]),
    +            ]
    +        )
    +        s2 = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(1, 1),
    +                LineString([(0, 0), (1, 1)]),
    +            ]
    +        )
    +        # "ABCDEFGHI" DE-9 Format
    +        # A Dimension of intersection
    +        # B Dimension of interior intersection
    +        # C Dimension of boundary intersection
    +        # D Interior of first geometry intersects exterior of second
    +        # E Exterior of first geometry intersects interior of second
    +        # F Boundary of first geometry intersects exterior of second
    +        # G Exterior of first geometry intersects boundary of second
    +        # H Exterior of first geometry intersects exterior of second
    +        # I Dimension of intersection for interiors
    +        # 0 = false, 1 = point, 2 = line, F = area
    +
    +        # 1. Test with single geometry
    +        point = Point(0, 0)
    +        result = s.relate(point)
    +        expected = pd.Series(["0FFFFFFF2", "0FFFFFFF2", "FF10F0FF2"])
    +        assert_series_equal(result.to_pandas(), expected)
    +
    +        result = s.relate(s2)
    +        expected = pd.Series(["0FFFFFFF2", "FF0FFF0F2", "1FFF0FFF2"])
    +        assert_series_equal(result.to_pandas(), expected)
    +        # 2. Test with align=True (different indices)
    +        s3 = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(1, 1),
    +            ],
    +            index=range(1, 3),
    +        )
    +        s4 = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(1, 1),
    +            ],
    +            index=range(0, 2),
    +        )
    +        result = s3.relate(s4, align=True)
    +        expected = pd.Series([None, "FF0FFF0F2", None], index=[0, 1, 2])
    +        assert_series_equal(result.to_pandas(), expected)
    +
    +        # 3. Test with align=False
    +        result = s3.relate(s4, align=False)
    +        expected = pd.Series(["0FFFFFFF2", "0FFFFFFF2"], index=range(1, 3))
    +        assert_series_equal(result.to_pandas(), expected)

            self.check_pd_series_equal(result, expected)
    

    petern48 petern48 commented on code on Oct 26, 2025 22:59 UTC
    Code Review
    python/tests/geopandas/test_geoseries.py:1
    @@ -1954,6 +1954,83 @@ def test_contains(self):
         def test_contains_properly(self):
             pass
     
    +    def test_relate(self):
    +        s = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(0, 0),
    +                LineString([(0, 0), (1, 1)]),
    +            ]
    +        )
    +        s2 = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(1, 1),
    +                LineString([(0, 0), (1, 1)]),
    +            ]
    +        )
    +        # "ABCDEFGHI" DE-9 Format
    +        # A Dimension of intersection
    +        # B Dimension of interior intersection
    +        # C Dimension of boundary intersection
    +        # D Interior of first geometry intersects exterior of second
    +        # E Exterior of first geometry intersects interior of second
    +        # F Boundary of first geometry intersects exterior of second
    +        # G Exterior of first geometry intersects boundary of second
    +        # H Exterior of first geometry intersects exterior of second
    +        # I Dimension of intersection for interiors
    +        # 0 = false, 1 = point, 2 = line, F = area
    +
    +        # 1. Test with single geometry
    +        point = Point(0, 0)
    +        result = s.relate(point)
    +        expected = pd.Series(["0FFFFFFF2", "0FFFFFFF2", "FF10F0FF2"])
    +        assert_series_equal(result.to_pandas(), expected)
    +
    +        result = s.relate(s2)
    +        expected = pd.Series(["0FFFFFFF2", "FF0FFF0F2", "1FFF0FFF2"])
    +        assert_series_equal(result.to_pandas(), expected)
    +        # 2. Test with align=True (different indices)
    +        s3 = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(1, 1),
    +            ],
    +            index=range(1, 3),
    +        )
    +        s4 = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(1, 1),
    +            ],
    +            index=range(0, 2),
    +        )
    +        result = s3.relate(s4, align=True)
    +        expected = pd.Series([None, "FF0FFF0F2", None], index=[0, 1, 2])
    +        assert_series_equal(result.to_pandas(), expected)
    +
    +        # 3. Test with align=False
    +        result = s3.relate(s4, align=False)
    +        expected = pd.Series(["0FFFFFFF2", "0FFFFFFF2"], index=range(1, 3))
    +        assert_series_equal(result.to_pandas(), expected)
    +
    +        # 4. Check that GeoDataFrame works too
    +        df_result = s.to_geoframe().relate(s2, align=False)
    +        expected = pd.Series(["0FFFFFFF2", "FF0FFF0F2", "1FFF0FFF2"])
    +        assert_series_equal(df_result.to_pandas(), expected)

            self.check_pd_series_equal(result, expected)
    

    petern48 petern48 commented on code on Oct 26, 2025 22:59 UTC
    Code Review
    python/tests/geopandas/test_geoseries.py:1
    @@ -1954,6 +1954,83 @@ def test_contains(self):
         def test_contains_properly(self):
             pass
     
    +    def test_relate(self):
    +        s = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(0, 0),
    +                LineString([(0, 0), (1, 1)]),
    +            ]
    +        )
    +        s2 = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(1, 1),
    +                LineString([(0, 0), (1, 1)]),
    +            ]
    +        )
    +        # "ABCDEFGHI" DE-9 Format
    +        # A Dimension of intersection
    +        # B Dimension of interior intersection
    +        # C Dimension of boundary intersection
    +        # D Interior of first geometry intersects exterior of second
    +        # E Exterior of first geometry intersects interior of second
    +        # F Boundary of first geometry intersects exterior of second
    +        # G Exterior of first geometry intersects boundary of second
    +        # H Exterior of first geometry intersects exterior of second
    +        # I Dimension of intersection for interiors
    +        # 0 = false, 1 = point, 2 = line, F = area
    +
    +        # 1. Test with single geometry
    +        point = Point(0, 0)
    +        result = s.relate(point)
    +        expected = pd.Series(["0FFFFFFF2", "0FFFFFFF2", "FF10F0FF2"])
    +        assert_series_equal(result.to_pandas(), expected)
    +
    +        result = s.relate(s2)
    +        expected = pd.Series(["0FFFFFFF2", "FF0FFF0F2", "1FFF0FFF2"])
    +        assert_series_equal(result.to_pandas(), expected)
    +        # 2. Test with align=True (different indices)
    +        s3 = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(1, 1),
    +            ],
    +            index=range(1, 3),
    +        )
    +        s4 = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(1, 1),
    +            ],
    +            index=range(0, 2),
    +        )
    +        result = s3.relate(s4, align=True)
    +        expected = pd.Series([None, "FF0FFF0F2", None], index=[0, 1, 2])
    +        assert_series_equal(result.to_pandas(), expected)
    +
    +        # 3. Test with align=False
    +        result = s3.relate(s4, align=False)
    +        expected = pd.Series(["0FFFFFFF2", "0FFFFFFF2"], index=range(1, 3))
    +        assert_series_equal(result.to_pandas(), expected)
    +
    +        # 4. Check that GeoDataFrame works too
    +        df_result = s.to_geoframe().relate(s2, align=False)
    +        expected = pd.Series(["0FFFFFFF2", "FF0FFF0F2", "1FFF0FFF2"])
    +        assert_series_equal(df_result.to_pandas(), expected)
    +
    +        # 5. touching_polygons and overlapping polygon case
    +        touching_poly_a = Polygon(((0, 0), (1, 0), (1, 1), (0, 1), (0, 0)))
    +        touching_poly_b = Polygon(((1, 0), (2, 0), (2, 1), (1, 1), (1, 0)))
    +        overlapping_poly_a = Polygon(((0, 0), (2, 0), (2, 2), (0, 2), (0, 0)))
    +        overlapping_poly_b = Polygon(((1, 1), (3, 1), (3, 3), (1, 3), (1, 1)))
    +        s5 = GeoSeries([touching_poly_a, overlapping_poly_a])
    +        s6 = GeoSeries([touching_poly_b, overlapping_poly_b])
    +        result = s5.relate(s6)
    +
    +        expected = pd.Series(["FF2F11212", "212101212"])
    +        assert_series_equal(result.to_pandas(), expected)

            self.check_pd_series_equal(result, expected)
    

    petern48 petern48 commented on code on Oct 26, 2025 23:05 UTC
    Code Review
    python/tests/geopandas/test_match_geopandas_series.py:1
    @@ -1073,6 +1078,21 @@ def test_contains(self):
         def test_contains_properly(self):
             pass
     
    +    def test_relate(self):
    +        for geom, geom2 in self.pairs:
    +            if self.contains_any_geom_collection(geom, geom2):
    +                continue

    This conditional skip is included in some test functions to work around a bug or difference in Sedona's behavior, but for this case, we can actually remove this, since the tests pass without it (I checked this branch out and tried it locally).

    petern48 petern48 commented on code on Oct 26, 2025 23:23 UTC
    Code Review
    python/sedona/spark/geopandas/geoseries.py:1
    @@ -79,6 +79,7 @@
             "is_valid_reason",
             "length",
             "make_valid",
    +        "relate",

    Just an fyi, we don't actually keep this list updated (in fact, I just filed an issue to remove this). It doesn't hurt to update it, so it's fine to leave this change here.

    petern48 petern48 commented on Oct 26, 2025 23:26 UTC
    Reviewer

    Welcome! Thanks for working on this. It looks great. I mainly just had two minor suggestions to improve testing, but otherwise it looks good.

    petern48 petern48 commented on Oct 26, 2025 23:34 UTC
    Contributor

    <img width="458" height="32" alt="image" src="https://github.com/user-attachments/assets/d7e6b93e-b866-415b-87fb-da99bfdccc82" />

    optional: I recommend setting up the pre-commit hook to auto-run the linter after each commit. It's as simple as running make checkinstall in the home directory (or running pip install pre-commit and pre-commit install). Otherwise, you'll have to remember to run pre-commit manually before submitting a PR (which I personally find easy to forget).

    gauravbyte gauravbyte commented on code on Oct 27, 2025 08:33 UTC
    Code Review
    python/sedona/spark/geopandas/geoseries.py:1
    @@ -79,6 +79,7 @@
             "is_valid_reason",
             "length",
             "make_valid",
    +        "relate",

    sure will fix it in seperate PR

    gauravbyte gauravbyte commented on code on Oct 27, 2025 08:39 UTC
    Code Review
    python/tests/geopandas/test_geoseries.py:1
    @@ -1954,6 +1954,83 @@ def test_contains(self):
         def test_contains_properly(self):
             pass
     
    +    def test_relate(self):
    +        s = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(0, 0),
    +                LineString([(0, 0), (1, 1)]),
    +            ]
    +        )
    +        s2 = GeoSeries(
    +            [
    +                Point(0, 0),
    +                Point(1, 1),
    +                LineString([(0, 0), (1, 1)]),
    +            ]
    +        )
    +        # "ABCDEFGHI" DE-9 Format
    +        # A Dimension of intersection
    +        # B Dimension of interior intersection
    +        # C Dimension of boundary intersection
    +        # D Interior of first geometry intersects exterior of second
    +        # E Exterior of first geometry intersects interior of second
    +        # F Boundary of first geometry intersects exterior of second
    +        # G Exterior of first geometry intersects boundary of second
    +        # H Exterior of first geometry intersects exterior of second
    +        # I Dimension of intersection for interiors
    +        # 0 = false, 1 = point, 2 = line, F = area
    +
    +        # 1. Test with single geometry
    +        point = Point(0, 0)
    +        result = s.relate(point)
    +        expected = pd.Series(["0FFFFFFF2", "0FFFFFFF2", "FF10F0FF2"])
    +        assert_series_equal(result.to_pandas(), expected)
    gauravbyte gauravbyte commented on code on Oct 27, 2025 08:40 UTC
    Code Review
    python/tests/geopandas/test_match_geopandas_series.py:1
    @@ -1073,6 +1078,21 @@ def test_contains(self):
         def test_contains_properly(self):
             pass
     
    +    def test_relate(self):
    +        for geom, geom2 in self.pairs:
    +            if self.contains_any_geom_collection(geom, geom2):
    +                continue
    gauravbyte gauravbyte commented on Oct 27, 2025 09:09 UTC
    Author

    Thanks for the review, @petern48!
    I’ve addressed your comments in 6f14d51
    , rerun the tests, and set up the pre-commit hook as suggested.
    Please review at your convenience.

    petern48 petern48 approved these changes on Oct 28, 2025 03:21 UTC
    Approved Reviewer

    Great PR, thanks for working on it!

    Reviewers
    Assignees
    gauravbyte gauravbyte
    Labels
    risk: low confidence: 87.4% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 38.5% (MEDIUM) · Higher = larger blast radius
    38.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 87.4% · Lower = rushed or shallow review
    87.4%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.8% · Safe to merge
    4.8%
    Repository