Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_convert(self):
self.assertEqual(DataFrameLike.convert(None), None)
data = {'c1': [4, 5, 6], 'c2': [6, 7, 8]}
xr_ds = xr.Dataset(data_vars=data)
pd_ds = pd.DataFrame(data=data)
gdf_ds = gpd.GeoDataFrame.from_features(read_test_features())
proxy_gdf_ds = GeoDataFrame.from_features(read_test_features())
self.assertIsInstance(DataFrameLike.convert(xr_ds), pd.DataFrame)
self.assertIsInstance(DataFrameLike.convert(pd_ds), pd.DataFrame)
self.assertIs(DataFrameLike.convert(pd_ds), pd_ds)
self.assertIsInstance(DataFrameLike.convert(gdf_ds), gpd.GeoDataFrame)
self.assertIs(DataFrameLike.convert(gdf_ds), gdf_ds)
self.assertIsInstance(DataFrameLike.convert(proxy_gdf_ds), GeoDataFrame)
self.assertIs(DataFrameLike.convert(proxy_gdf_ds), proxy_gdf_ds)
with self.assertRaises(ValidationError):
DataFrameLike.convert(42)
def to_geopandas(self, crs=None):
"""
Parameters
----------
crs : int or str
Optionally give an integer srid, or valid proj4 string to transform output to
Returns
-------
GeoDataFrame
This query as a GeoPandas GeoDataFrame.
"""
js = self.to_geojson(crs=crs)
gdf = geopandas.GeoDataFrame.from_features(js["features"])
gdf.crs = js["properties"]["crs"]
return gdf
The dataset being referenced.
"""
try:
assert 'examples.zip' in resource_listdir('geoplot.datasets', '')
except AssertionError:
raise IOError("The 'examples.zip' file packaging geoplot example datasets was not found.")
z = ZipFile(resource_stream('geoplot.datasets', 'examples.zip'))
if dname in ['boston-airbnb-listings', 'boston-zip-codes', 'contiguous-usa', 'dc-roads', 'la-flights',
'nyc-boroughs', 'napoleon-troop-movements',
'ny-census-partial', 'nyc-boroughs', 'nyc-collision-factors', 'nyc-fatal-collisions',
'nyc-injurious-collisions', 'nyc-parking-tickets-sample', 'nyc-police-precincts', 'usa-cities']:
with fiona.BytesCollection(z.read('geoplot-data/{0}.geojson'.format(dname))) as f:
crs = f.crs
gdf = gpd.GeoDataFrame.from_features(f, crs=crs)
return gdf
elif dname in ['obesity-by-state']:
return pd.read_csv(BytesIO(z.read("geoplot-data/{0}.tsv".format(dname))), sep='\t')
else:
raise ValueError('The provided dataset is not in the example datasets provided with geoplot.')
def lazy_data_frame(self):
features = self._features
if features is not None and self._lazy_data_frame is None:
crs = features.crs if hasattr(features, 'crs') else None
self._lazy_data_frame = geopandas.GeoDataFrame.from_features(features, crs=crs)
return self._lazy_data_frame
:param format: Format of output (default is GeoDataFrame)
:param epsg: EPSG code of projection to reproject output to
:return: Dataset in requested format
"""
if not format:
format = self.default_output
if self.data is None and self.features:
if type(self.features) == str:
self.features = json.loads(self.features)
features = self.features
if 'type' in features and features['type'] == 'FeatureCollection':
self.data = geopandas.GeoDataFrame.from_features(
self.features['features'])
else:
self.data = geopandas.GeoDataFrame.from_features(features)
if not self.data.crs:
if hasattr(self, 'crs'):
self.data.crs = self.crs
else:
self.get_epsg()
return self.transform_data(outformat=format, epsg=epsg)
crs=crs,
bounds=bounds,
bounds_crs=bounds_crs,
sortby=sortby,
pagesize=pagesize,
)
with ThreadPoolExecutor(max_workers=max_workers) as executor:
results = executor.map(make_request, param_dicts)
outjson = dict(type="FeatureCollection", features=[])
for result in results:
outjson["features"] += result
if not as_gdf:
return outjson
else:
gdf = gpd.GeoDataFrame.from_features(outjson)
gdf.crs = {"init": crs}
return gdf
def read_specific_rows():
with fiona.open(fn_suo,layer="suo") as c:
rows = (1,5,100)
df=gpd.GeoDataFrame.from_features([c[i] for i in rows])
print(df)
def load_geojson(input_data):
if isinstance(input_data, str):
# File name
data = geopandas.read_file(input_data)
elif isinstance(input_data, list):
# List of features
data = geopandas.GeoDataFrame.from_features(input_data)
elif isinstance(input_data, dict):
# GeoJSON object
if input_data.get('features'):
# From features
data = geopandas.GeoDataFrame.from_features(input_data['features'])
elif input_data.get('type') == 'Feature':
# From feature
data = geopandas.GeoDataFrame.from_features([input_data])
elif input_data.get('type'):
# From geometry
data = geopandas.GeoDataFrame.from_features([{
'type': 'Feature',
'properties': {},
'geometry': input_data
}])
else:
data = geopandas.GeoDataFrame()
else:
raise ValueError(
'''