Copilot commented on code in PR #2067: URL: https://github.com/apache/sedona/pull/2067#discussion_r2189015961
########## python/sedona/geopandas/sindex.py: ########## @@ -0,0 +1,137 @@ +# 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, 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 GeoDataFrame. + """ + + if isinstance(geometry, np.ndarray): + self.geometry = geometry + elif isinstance(geometry, PySparkDataFrame): + if column_name is None: + raise ValueError("column_name must be specified for PySpark DataFrame.") + if column_name not in geometry.columns: + raise ValueError( + f"Column '{column_name}' does not exist in the DataFrame." + ) + self.geometry = geometry[column_name].values Review Comment: Accessing `.values` on a PySpark DataFrame column will not work, since `geometry[column_name]` yields a Column without a `.values` attribute. Consider collecting the column via `geometry.select(column_name).toPandas()[column_name].values` or using an RDD to extract an array. ```suggestion self.geometry = geometry.select(column_name).toPandas()[column_name].values ``` ########## python/tests/geopandas/test_sindex.py: ########## @@ -0,0 +1,75 @@ +# 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 unittest +import numpy as np +from shapely.geometry import Point, Polygon, LineString, box Review Comment: [nitpick] The import of `box` is unused in this test file. Consider removing it to clean up unnecessary imports. ```suggestion from shapely.geometry import Point, Polygon, LineString ``` ########## python/sedona/geopandas/geodataframe.py: ########## @@ -250,7 +250,7 @@ def _to_geopandas(self) -> gpd.GeoDataFrame | pd.Series: raise NotImplementedError("This method is not implemented yet.") @property - def geoindex(self) -> GeoIndex: + def sindex(self) -> SpatialIndex: # Implementation of the abstract method raise NotImplementedError("This method is not implemented yet.") Review Comment: [nitpick] The `GeoDataFrame.sindex` property is still raising `NotImplementedError`. To maintain consistency with GeoSeries, implement this method to return a `SpatialIndex` or clearly document that it's unsupported. ```suggestion def sindex(self) -> SpatialIndex | None: """ Returns a spatial index for the GeoDataFrame. The spatial index allows for efficient spatial queries. If the spatial index cannot be created (e.g., no geometry column is present), this property will return None. Returns: - SpatialIndex: The spatial index for the GeoDataFrame. - None: If the spatial index is not supported. """ if "geometry" in self.columns: # Assuming SpatialIndex can be constructed from the geometry column return SpatialIndex(self["geometry"]) return None ``` -- 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]
