ENH: Overload from_dict to accept crs and geometry (#1619)

This commit is contained in:
sangarshanan
2020-12-28 14:57:28 +01:00
committed by GitHub
parent 7d3136b834
commit 8383e17a83
2 changed files with 36 additions and 0 deletions
+26
View File
@@ -397,6 +397,32 @@ class GeoDataFrame(GeoPandasBase, DataFrame):
except Exception:
pass
@classmethod
def from_dict(cls, data, geometry=None, crs=None, **kwargs):
"""
Construct GeoDataFrame from dict of array-like or dicts by
overiding DataFrame.from_dict method with geometry and crs
Parameters
----------
data : dict
Of the form {field : array-like} or {field : dict}.
geometry : str or array (optional)
If str, column to use as geometry. If array, will be set as 'geometry'
column on GeoDataFrame.
crs : str or dict (optional)
Coordinate reference system to set on the resulting frame.
kwargs : key-word arguments
These arguments are passed to DataFrame.from_dict
Returns
-------
GeoDataFrame
"""
dataframe = super().from_dict(data, **kwargs)
return GeoDataFrame(dataframe, geometry=geometry, crs=crs)
@classmethod
def from_file(cls, filename, **kwargs):
"""Alternate constructor to create a ``GeoDataFrame`` from a file.
+10
View File
@@ -479,6 +479,16 @@ class TestDataFrame:
assert_frame_equal(self.df2.loc[5:], self.df2.cx[:, 5:])
assert_frame_equal(self.df2.loc[5:], self.df2.cx[5:, 5:])
def test_from_dict(self):
data = {"A": [1], "geometry": [Point(0.0, 0.0)]}
df = GeoDataFrame.from_dict(data, crs=3857)
assert df.crs == "epsg:3857"
assert df._geometry_column_name == "geometry"
data = {"B": [1], "location": [Point(0.0, 0.0)]}
df = GeoDataFrame.from_dict(data, geometry="location")
assert df._geometry_column_name == "location"
def test_from_features(self):
nybb_filename = geopandas.datasets.get_path("nybb")
with fiona.open(nybb_filename) as f: