Select to view content in your preferred language

FeatureLayer.query() with a Spatial Filter Issue

05-20-2024 04:12 PM
Labels (3)
New Contributor

I am trying to use the GIS library to extract parcel data for my City from our County's data. I'm using a spatial filter, but for some reason I am not getting all the data returned. There are random holes in my data. We are interested in about 15% of the records, so I'd rather not have to extract all the data to get what we need.

import arcpy
from arcgis.gis import GIS
from arcgis.geometry import Geometry
from arcgis.features import FeatureLayer
from arcgis.geometry import filters
import pandas as pd

# Define the geometry
geometry = Geometry({
    "rings": [
            [-121.261, 44.2128],
            [-121.2618, 44.3237],
            [-121.0991, 44.3257],
            [-121.1004, 44.212]
    "spatialReference": {"wkid": 4326}

# Connect to the GIS
gis = GIS()

# URL of the Feature Layer
url = 'https://[service url]/FeatureServer/0'

feature_layer = FeatureLayer(url)

query_result = feature_layer.query(geometry_filter=filters.intersects(geometry), out_fields='*', return_geometry=True)

# Convert the query result to a DataFrame
df = query_result.sdf

# Save the DataFrame to a CSV file
output_csv = r'C:\Users\dummy\Desktop\output.csv'
df.to_csv(output_csv, index=False)


Screenshot 2024-05-20 160926.png

0 Kudos
1 Reply
Esri Regular Contributor

My guess is you are hitting the max record limit as I don't see logic to page through results. The good news is that getting all records should be relatively simple now. In more recent versions of the API, the query method has a "return_all_records" param you can set to True and the method will automatically keep calling the service until all records that satisfy the where clause are found.