I am trying to select a handful of features from an Enterprise Portal feature layer using the ArcGIS API for Python. Specifically I am trying to do a spatial selection, where the selected features are within a certain bounding box.
My code is based on the code snippets in the API reference documentation pages for the arcgis.features FeatureLayer query() function, the arcgis.geometry.filters module, and the arcgis.geometry Geometry and Polygon sections. Code is as follows (with exact names simplified for privacy reasons):
# Import Statements
from arcgis.gis import GIS
from arcgis.geometry import Geometry
from arcgis.geometry.filters import *
import arcpy
# Instantiate Connection to Portal
gis = GIS("portal_url", username="my_username")
# Get item from portal and set spatial reference
portalItem = gis.content.get('itemID')
portalLayer = portalItem.layers[0]
spatialReference = arcpy.SpatialReference(4326)
# Create a filter feature Geometry object
AOI = Geometry({
"rings" : [[-108.46, 35.12],[-108.48, 35.12],[-108.48, 35.18],[-108.46, 35.18],
[-108.46, 35.12]],
"spatialReference" : {"wkid" : 4326} })
# construct a geometry filter using the filter geometry
AOI_geom_filter = intersects(AOI, spatialReference)
# query a feature layer for features that meet filter criteria
subset = portallLayer.query(geometry_filter=AOI_geom_filter, outfields='OBJECTID, LAT, LON')
This results in an error:
Unable to complete operation.
'geometry' parameter is invalid.
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-71-bb0dabd89dd0> in <module>
9
10 # query a feature layer for features that meet filter criteria
---> 11 NMparcels = parcelLayer.query(geometry_filter=AOI_geom_filter, outfields='OBJECTID, LAT, LON')
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\layer.py in query(self, where, out_fields, time_filter, geometry_filter, return_geometry, return_count_only, return_ids_only, return_distinct_values, return_extent_only, group_by_fields_for_statistics, statistic_filter, result_offset, result_record_count, object_ids, distance, units, max_allowable_offset, out_sr, geometry_precision, gdb_version, order_by_fields, out_statistics, return_z, return_m, multipatch_option, quantization_parameters, return_centroid, return_all_records, result_type, historic_moment, sql_format, return_true_curves, return_exceeded_limit_features, as_df, **kwargs)
698
699 params['returnCountOnly'] = True
--> 700 record_count = self._query(url, params, raw=as_raw)
701 if 'maxRecordCount' in self.properties:
702 max_records = self.properties['maxRecordCount']
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\layer.py in _query(self, url, params, raw)
1397 """ returns results of query """
1398 result = self._con.post(path=url,
-> 1399 postdata=params, token=self._token)
1400 if 'error' in result:
1401 raise ValueError(result)
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py in post(self, path, postdata, files, ssl, compress, is_retry, use_ordered_dict, add_token, verify_cert, token, try_json, out_folder, file_name, force_bytes, add_headers)
1175 verify_cert=verify_cert, is_retry=True)
1176
-> 1177 self._handle_json_error(resp_json['error'], errorcode)
1178 return None
1179
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py in _handle_json_error(self, error, errorcode)
1196
1197 errormessage = errormessage + "\n(Error Code: " + str(errorcode) +")"
-> 1198 raise RuntimeError(errormessage)
1199
1200 class _StrictURLopener(request.FancyURLopener):
RuntimeError: Unable to complete operation.
'geometry' parameter is invalid.
(Error Code: 400)
I've done the following testing to try to figure out why this is erroring (given that I'm following the example in the documentation very closely):
- Checked the geometry of the AOI object (polygon type, correct coordinates), including checking that the coordinates are listed in the correct format and order when creating the AOI object.
- Visualized portalLayer in a map - it appears as it should, and covers the AOI
- Ran a non-geometric query (where='OBJECTID<10') on portalLayer (worked as expected)
- Look at the result of
AOI_geom_filter
- it's a dictionary that seems reasonable, although the documentation pages linked above don't actually say what intersects() should output.
Can anyone see what's missing and leading to the error?
I'm working on the ArcGIS Jupyter Notebook environment (ArcGIS 10.7.1) on Windows 10. I did note that running arcgis.__version__
said I have Version 1.6.1 instead of Version 1.8.2 which is what the documentation webpages are for.