From 558ce57f260ebd5c2a9e706a34818d9e3df3983a Mon Sep 17 00:00:00 2001 From: isVoid Date: Tue, 6 Feb 2024 16:48:20 -0800 Subject: [PATCH] explict on dtypes on dtype on several location --- python/cuspatial/cuspatial/core/geoseries.py | 24 +++++++++++++------ .../cuspatial/core/spatial/nearest_points.py | 7 +++--- python/cuspatial/cuspatial/core/trajectory.py | 10 ++++++++ .../test_linestring_bounding_boxes.py | 8 +++---- .../nearest_points/test_nearest_points.py | 7 +++--- 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/python/cuspatial/cuspatial/core/geoseries.py b/python/cuspatial/cuspatial/core/geoseries.py index d86434bb2..64a977ec5 100644 --- a/python/cuspatial/cuspatial/core/geoseries.py +++ b/python/cuspatial/cuspatial/core/geoseries.py @@ -698,7 +698,8 @@ def from_points_xy(cls, points_xy): GeoSeries: A GeoSeries made of the points. """ - coords_dtype = "f8" if len(points_xy) == 0 else None + coords_dtype = _check_coords_dtype(points_xy) + return cls( GeoColumn._from_points_xy(as_column(points_xy, dtype=coords_dtype)) ) @@ -710,8 +711,9 @@ def from_multipoints_xy(cls, multipoints_xy, geometry_offset): Parameters ---------- - points_xy: array-like - Coordinates of the points, interpreted as interleaved x-y coords. + multipoints_xy: array-like + Coordinates of the multipoints, interpreted as interleaved x-y + coords. geometry_offset: array-like Offsets indicating the starting index of the multipoint. Multiply the index by 2 results in the starting index of the coordinate. @@ -732,7 +734,7 @@ def from_multipoints_xy(cls, multipoints_xy, geometry_offset): 1 MULTIPOINT (2.00000 2.00000, 3.00000 3.00000) dtype: geometry """ - coords_dtype = "f8" if len(multipoints_xy) == 0 else None + coords_dtype = coords_dtype = _check_coords_dtype(multipoints_xy) return cls( GeoColumn._from_multipoints_xy( as_column(multipoints_xy, dtype=coords_dtype), @@ -750,7 +752,8 @@ def from_linestrings_xy( Parameters ---------- linestrings_xy : array-like - Coordinates of the points, interpreted as interleaved x-y coords. + Coordinates of the linestring, interpreted as interleaved x-y + coords. geometry_offset : array-like Offsets of the first coordinate of each geometry. The length of this array is the number of geometries. Offsets with a difference @@ -777,7 +780,7 @@ def from_linestrings_xy( 0 LINESTRING (0 0, 1 1, 2 2, 3 3, 4 4, 5 5) dtype: geometry """ - coords_dtype = "f8" if len(linestrings_xy) == 0 else None + coords_dtype = _check_coords_dtype(linestrings_xy) return cls( GeoColumn._from_linestrings_xy( as_column(linestrings_xy, dtype=coords_dtype), @@ -827,7 +830,7 @@ def from_polygons_xy( 0 POLYGON (0 0, 1 1, 2 2, 3 3, 4 4, 5 5) dtype: geometry """ - coords_dtype = "f8" if len(polygons_xy) == 0 else None + coords_dtype = _check_coords_dtype(polygons_xy) return cls( GeoColumn._from_polygons_xy( as_column(polygons_xy, dtype=coords_dtype), @@ -1488,3 +1491,10 @@ def distance(self, other, align=True): if other_is_scalar: res.index = self.index return res + + +def _check_coords_dtype(coords): + if hasattr(coords, "dtype"): + return coords.dtype + else: + return "f8" if len(coords) == 0 else None diff --git a/python/cuspatial/cuspatial/core/spatial/nearest_points.py b/python/cuspatial/cuspatial/core/spatial/nearest_points.py index 7b65155a8..9098d7864 100644 --- a/python/cuspatial/cuspatial/core/spatial/nearest_points.py +++ b/python/cuspatial/cuspatial/core/spatial/nearest_points.py @@ -1,5 +1,6 @@ import cupy as cp +import cudf from cudf.core.column import as_column import cuspatial._lib.nearest_points as nearest_points @@ -51,9 +52,9 @@ def pairwise_point_linestring_nearest_points( if len(points) == 0: data = { - "point_geometry_id": [], - "linestring_geometry_id": [], - "segment_id": [], + "point_geometry_id": cudf.Series([], dtype="i4"), + "linestring_geometry_id": cudf.Series([], dtype="i4"), + "segment_id": cudf.Series([], dtype="i4"), "geometry": GeoSeries([]), } return GeoDataFrame._from_data(data) diff --git a/python/cuspatial/cuspatial/core/trajectory.py b/python/cuspatial/cuspatial/core/trajectory.py index b62fead9f..be779313f 100644 --- a/python/cuspatial/cuspatial/core/trajectory.py +++ b/python/cuspatial/cuspatial/core/trajectory.py @@ -122,6 +122,16 @@ def trajectory_bounding_boxes(num_trajectories, object_ids, points: GeoSeries): 1 1.0 1.0 3.0 3.0 """ + if len(points) == 0: + return DataFrame( + { + "x_min": Series([], dtype=points.points.x.dtype), + "y_min": Series([], dtype=points.points.x.dtype), + "x_max": Series([], dtype=points.points.x.dtype), + "y_max": Series([], dtype=points.points.x.dtype), + } + ) + if len(points) > 0 and not contains_only_points(points): raise ValueError("`points` must only contain point geometries.") diff --git a/python/cuspatial/cuspatial/tests/spatial/bounding/test_linestring_bounding_boxes.py b/python/cuspatial/cuspatial/tests/spatial/bounding/test_linestring_bounding_boxes.py index 154f6a22c..73984782b 100644 --- a/python/cuspatial/cuspatial/tests/spatial/bounding/test_linestring_bounding_boxes.py +++ b/python/cuspatial/cuspatial/tests/spatial/bounding/test_linestring_bounding_boxes.py @@ -18,10 +18,10 @@ def test_linestring_bounding_boxes_empty(): result, cudf.DataFrame( { - "minx": cudf.Series([]), - "miny": cudf.Series([]), - "maxx": cudf.Series([]), - "maxy": cudf.Series([]), + "minx": cudf.Series([], dtype=np.float64), + "miny": cudf.Series([], dtype=np.float64), + "maxx": cudf.Series([], dtype=np.float64), + "maxy": cudf.Series([], dtype=np.float64), } ), ) diff --git a/python/cuspatial/cuspatial/tests/spatial/nearest_points/test_nearest_points.py b/python/cuspatial/cuspatial/tests/spatial/nearest_points/test_nearest_points.py index 9c94a1106..77280ac50 100644 --- a/python/cuspatial/cuspatial/tests/spatial/nearest_points/test_nearest_points.py +++ b/python/cuspatial/cuspatial/tests/spatial/nearest_points/test_nearest_points.py @@ -1,4 +1,5 @@ import geopandas as gpd +import pandas as pd import pytest import shapely from geopandas.testing import assert_geodataframe_equal @@ -73,9 +74,9 @@ def test_empty_input(): ) expected = gpd.GeoDataFrame( { - "point_geometry_id": [], - "linestring_geometry_id": [], - "segment_id": [], + "point_geometry_id": pd.Series([], dtype="i4"), + "linestring_geometry_id": pd.Series([], dtype="i4"), + "segment_id": pd.Series([], dtype="i4"), "geometry": gpd.GeoSeries(), } )