Hello!
I am using ArcGIS for Python to save some sub layers in a hosted feature service to a feature geodatabase.
My code is as follows:
feature_layer_item = gis.content.get("xxxxxxxxxxxxxxx")
layer = feature_layer_item.layers[0]
featureSet = layer.query()
featureSet.save(arcpy.env.workspace, "MyLayer")
This works perfectly except when there are no features in the layer. If that's the case then the save function creates a table in the fgdb instead of an empty polygon feature class. If I add a bogus record to the layer and execute the same tool, a polygon feature class is created.
Has anyone encountered this? I assume it's because the query returns an empty feature set. I have tried setting the spatial_reference and geometry_type on the feature set, which looks like it should be possible but no luck.
Anybody have a workaround?
Thanks!
Jill
Solved! Go to Solution.
Using ArcPy
import arcpy
## url to the feature layer
fl = "https://services-eu1.arcgis.com/*******/arcgis/rest/services/County_Boundaries/FeatureServer/0"
## file geodatabase path
## NOTE you can use arcpy to create a geodatabase rather than having an existing
gdb = r"C:\Path\to\Documents\fgdb.gdb"
## path for saving output feature class
out_fc = "{0}\\exported_layer".format(gdb)
## create feature class from feature layer
## you can also use FeatureClassToFeatureClass in older versions of ArcGIS Pro
arcpy.conversion.ExportFeatures(fl, out_fc)
In the example above I have hardcoded the url but you could replace with the below.
fl = feature_layer_item.layers[0].url
You could also change the hardcoded "exported_layer" name to be the name of the layer you are exporting.
Using ArcPy
import arcpy
## url to the feature layer
fl = "https://services-eu1.arcgis.com/*******/arcgis/rest/services/County_Boundaries/FeatureServer/0"
## file geodatabase path
## NOTE you can use arcpy to create a geodatabase rather than having an existing
gdb = r"C:\Path\to\Documents\fgdb.gdb"
## path for saving output feature class
out_fc = "{0}\\exported_layer".format(gdb)
## create feature class from feature layer
## you can also use FeatureClassToFeatureClass in older versions of ArcGIS Pro
arcpy.conversion.ExportFeatures(fl, out_fc)
In the example above I have hardcoded the url but you could replace with the below.
fl = feature_layer_item.layers[0].url
You could also change the hardcoded "exported_layer" name to be the name of the layer you are exporting.