Skip to content

Commit

Permalink
Accept None and python list in GeoSeries constructor (#686)
Browse files Browse the repository at this point in the history
This PR augments `GeoSeries` constructor with `None` and a python list, and punt the objects to geopandas. A test case for geoseries contructor is also added.

Authors:
  - Michael Wang (https://github.com/isVoid)

Approvers:
  - H. Thomson Comer (https://github.com/thomcom)

URL: #686
  • Loading branch information
isVoid authored Sep 22, 2022
1 parent e9a2faa commit c0ac4f5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
8 changes: 5 additions & 3 deletions python/cuspatial/cuspatial/core/geoseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from functools import cached_property
from numbers import Integral
from typing import Tuple, TypeVar, Union
from typing import Optional, Tuple, TypeVar, Union

import cupy as cp
import geopandas as gpd
Expand Down Expand Up @@ -46,14 +46,16 @@ class GeoSeries(cudf.Series):

def __init__(
self,
data: Union[gpd.GeoSeries, Tuple, T, pd.Series, GeoColumn],
data: Optional[
Union[gpd.GeoSeries, Tuple, T, pd.Series, GeoColumn, list]
],
index: Union[cudf.Index, pd.Index] = None,
dtype=None,
name=None,
nan_as_null=True,
):
# Condition data
if isinstance(data, pd.Series):
if data is None or isinstance(data, (pd.Series, list)):
data = gpGeoSeries(data)
# Create column
if isinstance(data, GeoColumn):
Expand Down
35 changes: 35 additions & 0 deletions python/cuspatial/cuspatial/tests/test_geoseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy as np
import pandas as pd
import pytest
from geopandas.testing import assert_geoseries_equal
from shapely.affinity import rotate
from shapely.geometry import (
LineString,
Expand Down Expand Up @@ -358,6 +359,39 @@ def test_loc(gs):
assert_eq_geo(gsslice, cugsslice.to_geopandas())


@pytest.mark.parametrize(
"data",
[
None,
[],
[
Point(1, 1),
MultiPoint([(2, 2), (3, 3)]),
Point(4, 4),
Polygon([(0, 0), (1, 1), (0, 1), (0, 0)]),
],
gpd.GeoSeries(
[
MultiLineString(
[[(0, 0), (1, 2), (1, 0)], [(-1, -1), (-1, 3), (0, 0)]]
),
MultiPolygon(
[
Polygon([(0, 0), (1, 1), (0, 1), (0, 0)]),
Polygon([(2, 2), (2, 3), (3, 3), (2, 2)]),
]
),
]
),
],
)
def test_construction_from_foreign_object(data):
cugs = cuspatial.GeoSeries(data)
gps = gpd.GeoSeries(data)

assert_geoseries_equal(cugs.to_geopandas(), gps)


def test_shapefile_constructor():
host_dataframe = gpd.read_file(
gpd.datasets.get_path("naturalearth_lowres")
Expand All @@ -370,6 +404,7 @@ def test_shapefile_constructor():
gs.to_file("naturalearth_lowres_polygon")
data = cuspatial.read_polygon_shapefile("naturalearth_lowres_polygon")
cus = cuspatial.GeoSeries(data)

assert_eq_geo(gs.reset_index(drop=True), cus.to_geopandas())


Expand Down

0 comments on commit c0ac4f5

Please sign in to comment.