Hi Greta,
You may have already figured this out, but it looks like you missed 'spatial' - it should read:
'xx_sdf.spatial.to_featureclass'
I see this working with the below example script using version 1.6.2 of the API. This particular example maintains the source projection:
from arcgis import GIS
from arcgis.features import FeatureLayerCollection, GeoAccessor, GeoSeriesAccessor
from arcgis.geometry import SpatialReference
import pandas as pd
def search_item(conn, item_name, item_type, flc=False):
search_results = conn.content.search(item_name, item_type=item_type)
proper_index = [i for i, s in enumerate(search_results) if '"' + item_name + '"' in str(s)]
found_item = search_results[proper_index[0]]
if flc == False:
get_item = conn.content.get(found_item.id)
return get_item
if flc == True:
flc = FeatureLayerCollection.fromitem(found_item)
return flc
def main():
conn = GIS("https://www.arcgis.com", "Username", "Password")
flc = search_item(conn, "Name_of_Feature_Layer", "Feature Layer", True)
layer = flc.layers[0]
epsg_code = layer.properties['extent']['spatialReference']['latestWkid']
feature_set = layer.query()
sdf = feature_set.sdf
srs = SpatialReference({"wkid":epsg_code})
sdf.spatial.project(srs)
sdf.spatial.to_featureclass(location="/home/test.shp")
if __name__ == '__main__':
main()
An alternative approach would be to use the fiona library instead of sdf to write the shapefile. The equivalent workflow is something like this:
- Get the EPSG code from the Spatial Reference in the Feature Layer properties (obtained as shown above).
- Run to_geojson on the Feature Set.
- Load and open the Feature Set geojson with fiona.ogrext.buffer_to_virtual_file
- Write to a new file using the Esri Shapefile driver:
- Here, I set the optional crs parameter equal to the result of Fiona's from_epsg with the EPSG code from before.
Hope this helps!
-Earl