0

I'm trying to create a csv from a query of a feature layer hosted in ArcGIS Online. The csv only needs to contain one field, where the query is met, geometry is not needed. I'm having issues getting the queried data moved into a dataframe and exported into a csv. From what I've read, you can use the SpatiallyEnabledDataframe to query and export data from a feature, but I'm getting stuck generating the dataframe.

from arcgis.gis import GIS
from arcgis.features import FeatureLayer
from arcgis.features import manage_data
from datetime import date, time
import pandas as pd
from arcgis.features import SpatialDataFrame

gis = GIS("https://------", username="---------", password="---------")
item_id='---------'
get = gis.content.get(item_id)
feeder = get.layers[0]

sdf = pd.DataFrame.spatial.from_layer(feeder)
df = feeder.query(where="feeder_status = 'Closed'").sdf
df.head()

This throws the following error: AttributeError: type object 'DataFrame' has no attribute 'spatial'

Is this error thrown because pandas is looking for the features geometry?

1 Answer 1

0

I made it work by using Pandas dataframes. The code below explains how to connect to your content in AGOL, query a layer, and output it to your local drive with a timestamp name.

#import relevant modules
import csv, os,time,shutil
from arcgis.gis import GIS
import time
import pandas as pd

#connect and login to AGOL. Gets feeder summary layer.
gis = GIS("https://YOUR_ENTERPRISE.maps.arcgis.com", username="ENTER USERNAME", password="ENTER PASSWORD")
f= gis.content.get('CONTENT ID')

#queryies content of interest
sdf = f.layers[0].query(where="field_name = 'Yes'", 
                              out_fields='field_name',return_distinct_values=True).df
sdf.field_name  #tests actual output

#exports the query to csv where field_name is 'Yes'. CSV name has a date/time timestamp

timestr = time.strftime("%Y_%m_%d-%I_%M_%p")
url = (r'C:\Export_Location')     
export_name = str(url)+str("Yes_list.csv")
sdf['field_name'].str.slice(start=3).to_csv(export_name,sep=',',index=False)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.