Select to view content in your preferred language

Query and deleting feature in a AGOL layer

1244
1
02-26-2019 04:29 PM
GregThistle
Emerging Contributor

I am trying to query and export features to a CSV file from an AGOl layer, and then have these features deleted from the layer aftwerwards: My code goes 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('a6be839d462d4441802abad0728cc857')
icy_layers = icy_conditions.layers
icy_reports = icy_layers[0]
Status_query = icy_reports.query(where="Status='Cleared'")
output_file = icy_conditions.export(title=icy_reports,export_format="CSV",parameters=Status_query)
output_file.download(r'\\vm-gis-catalog\Web_GIS\AGOL\IceReporting\Archived_Reports')
if icy_reports.query(Status_query):
icy_reports.delete_features()

The script will run and export the features, but it does not select the records I want, it just exports all records in the layer. Can anyone help with regards to the export function and how you can incorporate querying a layer into this function?

Thanks!

0 Kudos
1 Reply
simoxu
by MVP Regular Contributor
MVP Regular Contributor

There are multiple ways to do it. You can create a replica or do a query then use pandas dataframe to export the result to a CSV file.

Please try if the following code works:

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('a6be839d462d4441802abad0728cc857')
icy_layers = icy_conditions.layers
icy_reports = icy_layers[0]
Status_query = icy_reports.query(where="Status='Cleared'")

# use pandas dataframe to export the data into csv
# you have chance to reshape your data, for example, which fields to be exported
sdf_temp = Status_query.sdf

# specify the fields you want to export if needed
#refined_sdf = sdf_temp[["field1","fields",...]]

# save to a csv file in a particular folder
sdf_temp.to_csv(r'c:\temp\xyz.csv')

# delete features permantly, use with great causion!!
#icy_reports.delete_features(where="Status='Cleared'")

delete_features function is very powerful, but very dangerous. make sure you backup your data before deleting any records, here is the doc:

arcgis.features module — arcgis 1.5.3 documentation 

Hope it helps

0 Kudos