Export selection from AGOL Feature Layer

1162
2
10-06-2021 09:23 AM
TNGStewardship
New Contributor III

I wish to export a selection (date range from January 1 2019 to January 1 2020) from a AGOL feature layer but am having issue with the end results - the attribute table does not match what I would get otherwise if exporting the same feature layer without the selection (as either a Shapefile and a File Geodatabase). Any ideas?

Here's what I've been trying so far:

from arcgis.gis import GIS
gis = GIS(url, user_name, password)
sql = "CreationDate > date'2019-01-01 00:00:00 AM' AND CreationDate < date'2020-01-01 00:00:00 AM'"
gis.content.get(itemid).layers[0].query(sql).save(folder_path, filename, encoding='utf-8')

#output is desired range but attributes get twisted (many strings abbreviated, GlobalID as '0's, dates as yyyy-mm-dd format).

I've also tried this but have had similar results:

from arcgis.gis import GIS
gis = GIS(url, user_name, password)
sql = "CreationDate > date'2019-01-01 00:00:00 AM' AND CreationDate < date'2020-01-01 00:00:00 AM'"
gis.content.get(itemid).layers[0].query(sql).features.sdf.spatial.to_featurelayer(output)

#output is similar to previous though GlobalIDs are presereved - however another step would be required to download (export?) the data (gis.content.get(itemid).export(title, export_format).download(folder_path)?) though this will download the entire dataset and not the selection.

Part of this solution is based on what was found on https://community.esri.com/t5/arcgis-api-for-python-questions/how-can-i-export-a-feature-layer-in-ag.... (Though this did not ultimately help me with my issue - mostly from the final line of 'features.sdf.spatial.to_featureclass('./output.shp')' where the output is a string and not an object, so nothing gets saved out.

Some screenshots:

Export as File Geodatabase:

Capture1.PNG

Export from script (notice 'GlobalID'):

Capture3.PNG

Export as File Geodatabase:

Capture2.PNG

Export from script (notice abbreviations and capitalizations):

Capture4.PNG

0 Kudos
2 Replies
emedina
New Contributor III

Hi,

I broke up the chain so you can better see what needs to happen:

  1. In your case, you start with an item.
  2. You access a layer in that item and query it with your predefined sql.
  3. The result is a FeatureSet object.
  4. Create an SEDF from the FeatureSet object.
  5. Use spatial.tofeatureclass() to save to a shapefile/Feature Class.

 

As you noted, you could actually stop at #3 and use FeatureSet's save method. In my experience, this method is not as reliable as working through a SEDF.

 

 

from arcgis.gis import GIS
gis = GIS(url, user_name, password)
sql = "CreationDate > date'2019-01-01 00:00:00 AM' AND CreationDate < date'2020-01-01 00:00:00 AM'"
item = gis.content.get(itemid)
fs = item.layers[0].query(sql)
sdf = fs.sdf
sdf.spatial.to_featureclass(location=r"c:\output_examples\your.gdb\test")

 

 

Can you tell us what happens when you try the above? It seems this is what you actually want? What I got out of your explanation is that you both want to download the data locally and publish the result as a Feature Layer.

0 Kudos
TNGStewardship
New Contributor III

Hi emedina,

Thanks for your response. Your suggestion almost got me to where I want to be though I'm still having issue with abbreviations ('FEED' instead of 'Feeding') and blank attributes in the output (shapefile) feature class, though I'm thinking that this is what is going to happen no matter what if converting a file geodatabase feature class to a shapefile. (I just did the feature class to shapefile conversion in ArcMap and got the same results, so I guess this is the results I'm stuck with.)

Anyways, thanks again for your response. Let me know if there is an easy way around maintaining the long form attribute information that was present in the file geodatabase.

0 Kudos