Copilot commented on code in PR #2067: URL: https://github.com/apache/sedona/pull/2067#discussion_r2190412771
########## python/sedona/geopandas/sindex.py: ########## @@ -0,0 +1,141 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import numpy as np +from pyspark.sql import DataFrame as PySparkDataFrame + + +class SpatialIndex: + """ + A wrapper around Sedona's spatial index functionality. + """ + + def __init__(self, geometry, index_type="strtree", column_name=None): + """ + Initialize the SpatialIndex with geometry data. + + Parameters + ---------- + geometry : np.array of Shapely geometries, PySparkDataFrame column, or PySparkDataFrame + index_type : str, default "strtree" + The type of spatial index to use. + column_name : str, optional + The column name to extract geometry from if `geometry` is a PySparkDataFrame. + """ + + if isinstance(geometry, np.ndarray): + self.geometry = geometry + self._dataframe = None + self._is_spark = False + elif isinstance(geometry, PySparkDataFrame): + if column_name is None: + raise ValueError( + "column_name must be specified when geometry is a PySparkDataFrame" + ) + self.geometry = geometry[column_name] + self._dataframe = geometry + self._is_spark = True + else: + raise TypeError( + "Invalid type for `geometry`. Expected np.array or PySparkDataFrame." + ) + + self.index_type = index_type + + def query(self, geometry, predicate=None, sort=False): + """ + Query the spatial index for geometries that intersect the given geometry. + + Parameters + ---------- + geometry : Shapely geometry + The geometry to query against the spatial index. + predicate : str, optional + Spatial predicate to filter results (e.g., 'intersects', 'contains'). + sort : bool, optional, default False + Whether to sort the results. + + Returns + ------- + list + List of indices of matching geometries. + """ + # Placeholder for range query using Sedona + raise NotImplementedError("This method is not implemented yet.") + + def nearest(self, geometry, k=1, return_distance=False): + """ + Find the nearest geometry in the spatial index. + + Parameters + ---------- + geometry : Shapely geometry + The geometry to find the nearest neighbor for. + k : int, optional, default 1 + Number of nearest neighbors to find. + return_distance : bool, optional, default False + Whether to return distances along with indices. + + Returns + ------- + list or tuple + List of indices of nearest geometries, optionally with distances. + """ + # Placeholder for KNN query using Sedona + raise NotImplementedError("This method is not implemented yet.") + + def intersection(self, bounds): + """ + Find geometries that intersect the given bounding box. + + Parameters + ---------- + bounds : tuple + Bounding box as (min_x, min_y, max_x, max_y). + + Returns + ------- + list + List of indices of matching geometries. + """ + raise NotImplementedError("This method is not implemented yet.") + + @property + def size(self): + """ + Get the size of the spatial index. + + Returns + ------- + int + Number of geometries in the index. + """ + if self._is_spark: + return self.geometry.count() Review Comment: In Spark mode, `self.geometry` is a Column, not a DataFrame, so calling `count()` on it will fail. Use `self._dataframe.count()` to get the number of rows. ```suggestion return self._dataframe.count() ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
