-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_SpatiallyEnabledDataFrame.py
41 lines (34 loc) · 1.53 KB
/
to_SpatiallyEnabledDataFrame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import warnings
from arcgis.geometry import Geometry
from arcgis.features import GeoAccessor, GeoSeriesAccessor
from pandas import DataFrame
from geopandas import GeoDataFrame
def to_SpatiallyEnabledDataFrame(self, spatial_reference=None):
"""Returns an arcgis spatially-enabled data frame.
Arguments:
spatial_reference Either None or a spatial reference integer code.
If None, the spatial reference will be extracted
from the GeoDataFrame if it is defined using an
EPSG code.
Cribbed from from user jrmatchett on ESRI GeoNet:
https://community.esri.com/thread/228904-creating-updating-feature-layer-in-arcgis-online-from-geopandas-geodataframe
"""
try:
if not spatial_reference:
crs = self.crs
epsg_code = crs.to_epsg()
if epsg_code:
spatial_reference = {'wkid': epsg_code}
else:
spatial_reference = {'wkid': 4326}
warnings.warn('Unable to extract a spatial reference, assuming latitude/longitude (EPSG 4326).')
else:
spatial_reference = {'wkid': spatial_reference}
sdf = DataFrame(data=self.drop(self.geometry.name, axis=1))
sdf['SHAPE'] = [Geometry.from_shapely(g, spatial_reference) for g in self.geometry.tolist()]
sdf.spatial.set_geometry('SHAPE')
return sdf
except Exception as e:
print(e.args[0])
raise
GeoDataFrame.to_SpatiallyEnabledDataFrame = to_SpatiallyEnabledDataFrame