Skip to content

Commit

Permalink
BUG: to_geoframe supports replacing old geoemtry (#822)
Browse files Browse the repository at this point in the history
* `to_geoframe` could replace old geoemtry

* support replacing old geometry

* Add notes
  • Loading branch information
Zeroto521 authored Dec 25, 2022
1 parent c908d96 commit 6569a81
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
15 changes: 3 additions & 12 deletions dtoolkit/geoaccessor/dataframe/to_geoframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,6 @@ def to_geoframe(
- Prime Meridian: Greenwich
"""

return (
gpd.GeoDataFrame(
# Avoid mutating the original DataFrame.
# https://github.com/geopandas/geopandas/issues/1179
df.copy(),
crs=crs,
geometry=geometry,
**kwargs,
)
if not isinstance(df, gpd.GeoDataFrame)
else df
)
# Use `.copy` to avoid mutating the original DataFrame.
# https://github.com/geopandas/geopandas/issues/1179
return gpd.GeoDataFrame(df.copy(), geometry=geometry, crs=crs, **kwargs)
22 changes: 22 additions & 0 deletions test/geoaccessor/dataframe/test_to_geoframe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import geopandas as gpd
import pandas as pd
from pandas.testing import assert_frame_equal
from shapely import Point

from dtoolkit.geoaccessor.dataframe import to_geoframe # noqa: F401

Expand All @@ -23,3 +25,23 @@ def test_original_dataframe_type():
df.to_geoframe(geometry=df["geom"])

assert_frame_equal(df, df_copy)


# https://github.com/Zeroto521/my-data-toolkit/issues/820
def test_replace_old_geoemtry():
df = gpd.GeoDataFrame(
{
"label": ["a", "b"],
"geometry": [Point(100, 1), Point(122, 50)],
},
crs=4326,
).to_geoframe(
geometry=gpd.GeoSeries(
[Point(0, 0), Point(1, 1)],
crs=3857,
),
)

assert df.geometry.x.tolist() == [0, 1]
assert df.geometry.y.tolist() == [0, 1]
assert df.crs == 3857

0 comments on commit 6569a81

Please # to comment.