ArcGIs API for Python - Export Item Parameters

2323
7
02-27-2019 03:51 PM
GregThistle
New Contributor II

Hello,

I am trying to export a layer into a CSV file from a quertied ArcGIs Online layer, and then delete those same features afterwards. My code is as follows:

import arcgis
from arcgis.gis import GIS
gis = GIS("https://arcgis.com", "User", "Password")
from arcgis.gis import *
import os
icy_conditions = gis.content.get('7b812b022e00426b8f5c14448aa55b73')
icy_layers = icy_conditions.layers
icy_reports = icy_layers[0]
output_file = icy_conditions.export(title=icy_reports,export_format="CSV",parameters=icy_reports.query(where="Status='3'"))
output_file.download(r'filepath')
icy_reports.delete_features(where="Status='3'")

I know the delete features works at the end, but I am having trouble getting the query in the parameters section of the export item function (show in cold). There is documentation on how to do this using the JavaScript API, but no example on how to do this in python. I've done a lot of guessing and checking and cannot get the syntax right. Could anyone please offer some help?   

0 Kudos
7 Replies
EarlMedina1
Occasional Contributor III

Hi Greg,

The paramaeters argument (or more accurately the REST operation being called: Export Item—ArcGIS REST API: Users, groups, and content | ArcGIS for Developers ) is expecting a JSON object is what's going here.

Something like this will work:

item.export(title="test",export_format="CSV",parameters={"layers" :[{"id" : 0, "where" : "year_ > 1990"}]})

For you, I'm not sure but I'll assume the export is from layer 0:

icy_conditions.export(title=icy_reports,export_format="CSV",parameters={"layers" :[{"id" : 0, "where" : "Status = '3'"}]})

-Earl

GregThistle
New Contributor II

Hi Earl,

I though that query syntax was only for javascript. That worked and does exactly what I want. Thank you very much.

0 Kudos
BrandonJameson
New Contributor

I am attempting to do that same thing, but am getting an exception:
My code:
itm = gis.content.get("1ec7cba5f3984de4bfa32321a07b8a64")
itm.export(title="PS_CSV",export_format="CSV",parameters={"layers":[{"id":0}]})

The exception:

Exception                                 Traceback (most recent call last)<ipython-input-32-143f02afd855> in <module>----> 1 itm.export(title="PS_CSV",export_format="CSV",parameters={"layers":[{"id":0}]})      2       3 #itm.export(title="PS_CSV",export_format="CSV")C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py in export(self, title, export_format, parameters, wait, enforce_fld_vis)   7339                                             job_type="export")   7340                 if status['status'] == 'failed':-> 7341                     raise Exception("Could not export item: %s" % self.itemid)   7342                 elif status['status'].lower() == "completed":   7343                     return export_item Exception: Could not export item: 1ec7cba5f3984de4bfa32321a07b8a64

Any ideas on why I am not able to export this?

0 Kudos
ChrisBuckmaster2
New Contributor III

Did you get this issue resolved?

I am also receiving a "Could not export item: %s" % self.itemid" error message when trying to export content from my ArcGIS Portal v10.6.

0 Kudos
WhitneyWeber
Occasional Contributor

Just to add more examples here, I wanted to export all of the layers and tables from one of my feature services and after testing exporting just the layers, just the tables, and then trying to do so at the same time, found that by just exporting the tables, the layers were included. Following is the successful syntax used:

output_file = observations.export(title="Observations", export_format="File Geodatabase", parameters= {"tables":[{"id":4},{"id":5},{"id":6},{"id":7},{"id":8},{"id":9},{"id":10}]})

And then, downloading:

output_file.download(work_dir)

0 Kudos
WhitneyWeber
Occasional Contributor

I was struggling with getting the query syntax correct with dates. The following works

  • observations.export(title="ObservationsWhere5", export_format="File Geodatabase", parameters={"layers" :[{"id" : 0, "where" : "CreationDate > '7/1/2020'"}]})
0 Kudos
WhitneyWeber
Occasional Contributor

Okay, so does anyone know how to export layers and tables? The following syntax exports the layers successfully, respecting the where_clause, but the tables do not export. I'm guessing I need something other than a comma in between the layers[] and tables[]

output_file = observations.export(title="Observations", export_format="File Geodatabase", parameters={"layers" :[{"id" : 0, "where" : where_clause},{"id" : 1, "where" : where_clause},{"id" : 2, "where" : where_clause},{"id" : 3, "where" : where_clause}], "tables":[{"id":4},{"id":5},{"id":6},{"id":7},{"id":8},{"id":9},{"id":10}]})

If I use the following syntax, both the layers and tables export successfully, but the where_clause is ignored:

output_file = observations.export(title="ObservationsTables", export_format="File Geodatabase", parameters= {"tables":[{"id":4, "where" : where_clause},{"id":5, "where" : where_clause},{"id":6, "where" : where_clause},{"id":7, "where" : where_clause},{"id":8, "where" : where_clause},{"id":9, "where" : where_clause},{"id":10, "where" : where_clause}]})

Turns out I was over complicating the issue - all can be done within "layers", whether layers or tables, so the proper syntax is:

output_file = observations.export(title="ObservationsTables", export_format="File Geodatabase", parameters= {"layers":[{"id":0, "where" : where_clause},{"id":1, "where" : where_clause},{"id":2, "where" : where_clause},{"id":3, "where" : where_clause},{"id":4, "where" : where_clause},{"id":5, "where" : where_clause},{"id":6, "where" : where_clause},{"id":7, "where" : where_clause},{"id":8, "where" : where_clause},{"id":9, "where" : where_clause},{"id":10, "where" : where_clause}]})

or, simplified:

param = {"layers" :[{"id" : 0, "where" : where_clause}, {"id" : 1, "where" : where_clause}, {"id" : 2, "where" : where_clause}, {"id" : 3, "where" : where_clause}, {"id" : 4, "where" : where_clause}, {"id" : 5, "where" : where_clause}, {"id" : 6, "where" : where_clause}, {"id" : 7, "where" : where_clause}, {"id" : 8, "where" : where_clause}, {"id" : 9, "where" : where_clause}, {"id" : 10, "where" : where_clause}]}

with:

output_file = observations.export(title="Observations", export_format="File Geodatabase", parameters=param)

Many thanks to Calvin Kwon, Esri, for the solution!