Select to view content in your preferred language

Query and save an empty feature layer

530
2
Jump to solution
12-14-2023 09:08 AM
Labels (2)
JillianStanford
Occasional Contributor III

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

0 Kudos
1 Solution

Accepted Solutions
Clubdebambos
Occasional Contributor III

Hi @JillianStanford 

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.

 

~ learn.finaldraftmapping.com

View solution in original post

0 Kudos
2 Replies
Clubdebambos
Occasional Contributor III

Hi @JillianStanford 

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.

 

~ learn.finaldraftmapping.com
0 Kudos
JillianStanford
Occasional Contributor III

Hey @Clubdebambos ,

That worked perfectly.

Thank you!

Jill