Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
sg.LineString([(0, 0), (1, 1)]), None, sg.LineString([(1, 1), (2, 2)])
])
gp_rings = gp.array.from_shapely([
sg.LineString([(0, 0), (1, 1), (0, 0)]),
None,
sg.LineString([(1, 1), (2, 2), (1, 1)]),
])
gp_multilines = gp.array.from_shapely([
None,
sg.MultiLineString([[(0, 0), (1, 1)]]),
sg.MultiLineString([[(1, 1), (2, 2)]])
])
gp_polygons = gp.array.from_shapely([
None,
sg.Polygon([(0, 0), (1, 1), (0, 1)]),
sg.Polygon([(1, 1), (2, 2), (1, 0)])
])
gp_multipolygons = gp.array.from_shapely([
sg.MultiPolygon([sg.Polygon([(0, 0), (1, 1), (0, 1)])]),
sg.MultiPolygon([sg.Polygon([(1, 1), (2, 2), (1, 0)])]),
None
])
gpdf = gp.GeoDataFrame({
'a': [1, 2, 3],
'point': gp_points,
'b': [3, 4, 5],
'line': gp_lines,
def test_drop_geometry_column():
gp_points = gp.array.from_shapely([
sg.Point([0, 0]), sg.Point(1, 1)
])
gp_lines = gp.array.from_shapely([
sg.LineString([(0, 0), (1, 1)]), sg.LineString([(1, 1), (2, 2)])
])
gpdf = gp.GeoDataFrame({
'a': [1, 2],
'point': gp_points,
'b': [3, 4],
'line1': gp_lines,
'line2': gp_lines
}, geometry='line2')
# Import with no geometry column set
def st_point_array(draw, min_size=0, max_size=30, geoseries=False):
n = draw(st.integers(min_size, max_size))
points = []
for i in range(n):
x_mid = draw(st.floats(-50, 50))
y_mid = draw(st.floats(-50, 50))
point = (np.random.rand(2) - 0.5) * 5
point[0] = point[0] + x_mid
point[1] = point[1] + y_mid
points.append(sg.Point(point))
result = from_shapely(points)
if geoseries:
result = GeoSeries(result)
return result
def test_import_geopandas_with_none():
# Construct geopandas dataframe with column for each geometry type that includes
# Nones
gp_points = gp.array.from_shapely([
sg.Point([0, 0]), sg.Point(1, 1), None
])
gp_lines = gp.array.from_shapely([
sg.LineString([(0, 0), (1, 1)]), None, sg.LineString([(1, 1), (2, 2)])
])
gp_rings = gp.array.from_shapely([
sg.LineString([(0, 0), (1, 1), (0, 0)]),
None,
sg.LineString([(1, 1), (2, 2), (1, 1)]),
])
gp_multilines = gp.array.from_shapely([
None,
sg.MultiLineString([[(0, 0), (1, 1)]]),
sg.MultiLineString([[(1, 1), (2, 2)]])
])
gp_polygons = gp.array.from_shapely([
None,
sg.Polygon([(0, 0), (1, 1), (0, 1)]),
sg.Polygon([(1, 1), (2, 2), (1, 0)])
])
def test_import_geopandas_with_none():
# Construct geopandas dataframe with column for each geometry type that includes
# Nones
gp_points = gp.array.from_shapely([
sg.Point([0, 0]), sg.Point(1, 1), None
])
gp_lines = gp.array.from_shapely([
sg.LineString([(0, 0), (1, 1)]), None, sg.LineString([(1, 1), (2, 2)])
])
gp_rings = gp.array.from_shapely([
sg.LineString([(0, 0), (1, 1), (0, 0)]),
None,
sg.LineString([(1, 1), (2, 2), (1, 1)]),
])
gp_multilines = gp.array.from_shapely([
None,
sg.MultiLineString([[(0, 0), (1, 1)]]),
sg.MultiLineString([[(1, 1), (2, 2)]])
])
gp_polygons = gp.array.from_shapely([
def _ensure_geometry(data):
"""
Ensure the data is of geometry dtype or converted to it.
If input is a (Geo)Series, output is a GeoSeries, otherwise output
is GeometryArray.
"""
if is_geometry_type(data):
if isinstance(data, Series):
return GeoSeries(data)
return data
else:
if isinstance(data, Series):
out = from_shapely(np.asarray(data))
return GeoSeries(out, index=data.index, name=data.name)
else:
out = from_shapely(data)
return out
# if data is None and dtype is specified (eg from empty overlay
# test), specifying dtype raises an error:
# https://github.com/pandas-dev/pandas/issues/26469
kwargs.pop("dtype", None)
# Use Series constructor to handle input data
s = pd.Series(data, index=index, name=name, **kwargs)
# prevent trying to convert non-geometry objects
if s.dtype != object:
if s.empty:
s = s.astype(object)
else:
warnings.warn(_SERIES_WARNING_MSG, FutureWarning, stacklevel=2)
return s
# try to convert to GeometryArray, if fails return plain Series
try:
data = from_shapely(s.values)
except TypeError:
warnings.warn(_SERIES_WARNING_MSG, FutureWarning, stacklevel=2)
return s
index = s.index
name = s.name
self = super(GeoSeries, cls).__new__(cls)
super(GeoSeries, self).__init__(data, index=index, name=name, **kwargs)
self.crs = crs
self._invalidate_sindex()
return self
"""
Ensure the data is of geometry dtype or converted to it.
If input is a (Geo)Series, output is a GeoSeries, otherwise output
is GeometryArray.
"""
if is_geometry_type(data):
if isinstance(data, Series):
return GeoSeries(data)
return data
else:
if isinstance(data, Series):
out = from_shapely(np.asarray(data))
return GeoSeries(out, index=data.index, name=data.name)
else:
out = from_shapely(data)
return out
def to_geopandas(self):
"""
Convert a spatialpandas geometry array into a geopandas GeometryArray
Returns:
geopandas GeometryArray
"""
from geopandas.array import from_shapely
return from_shapely([el.to_shapely() for el in self])