Feature Layer query for multiple coordinates

01-28-2020 06:43 AM
New Contributor


I have a FeatureLayer that I'm trying to query for a bunch of coordinates. Is there any way to query the FeatureLayer once (from the WWW to my local machine) and then query it once per coordinate, instead of doing an HTTP request per coordinate?



0 Kudos
4 Replies
New Contributor III

If i understood your need correctly, you can use query with multipoint to get all the points in one go and then query the returned result set further. The api is a bit confusing for the query but you should get the idea from this. 

from arcgis.gis import GIS
from arcgis.features import FeatureLayer
from arcgis.geometry import *
import getpass

password = getpass.getpass("Enter password: ")
gis = GIS('org', 'me', password)

feature_layer_item = gis.content.get('bfb34d920bda46388e95a9d4e71e036b')
feature_layer = feature_layer_item.layers[0]

json = {"points":[
        "spatialReference":{"wkid":102100 }};

multi_point = MultiPoint(json)
for point in multi_point.coordinates():

query_result = feature_layer.query(geometry_filter = filters.contains(multi_point))
0 Kudos
New Contributor
I tested it and it doesn't seem to do what I want it to do.

Maybe to explain my use-case better: I have this feature layer (https://gis.wri.org/server/rest/services/LandMark/comm_ind_Documented/MapServer/1) that you can visualize graphically here.
I would like to query these layer for multiple locations; for example (LAT 36.357824, LNG-109.271351) and (LAT -14.681117, LNG 127.484020). Querying the points one by one I get the results I want, but the MultiPoint approach doesn't get me any results (seemingly because both points need to be  in a the same shape).

My code below:

feature_layer = arcgis.features.FeatureLayer(FEATURE_LAYER_comm_ind_Documented)
result = feature_layer.query(geometry_filter=arcgis.geometry.filters.contains(multi_point))

Point by point:
feature_layer = arcgis.features.FeatureLayer(FEATURE_LAYER_comm_ind_Documented)
point = arcgis.geometry.Point({'x': location.lng, 'y': location.lat, 'spatialReference': {'wkid': 4326}})
result = feature_layer.query(return_geometry='false', sr={'wkid': 4326}, geometry_filter=geom_filter).to_dict()
The reason why I would like to avoind the point-by-point approach is because I have thousands of locations, and I want to save round-trips to the MapServer.
I also tried downloading the layer to a GeoAccessor or DataFrame, but that takes forever.
Maybe what I want to do is not possible
0 Kudos
New Contributor III

I totally get that you want to do all in one query to avoid the ridiculous amount of HTTP calls when doing these kind of queries one by one. I think that what you are looking here is to use a correct spatial relationship.

Feature Service supports following spatial relationships on the query

  • esriSpatialRelIntersects —Part of an input feature is contained in the query geometry
  • esriSpatialRelContains —Part or all of an input feature is contained within the query geometry
  • esriSpatialRelCrosses —An input feature crosses the query geometry
  • esriSpatialRelEnvelopeIntersects —The envelope of an input feature intersects the envelope of the query geometry
  • esriSpatialRelOverlaps —An input feature overlaps the query geometry
  • esriSpatialRelTouches —An input feature touches the border of the query geometry
  • esriSpatialRelWithin—An input feature is completely enclosed by the query geometry

So instead of using contains you should use intersects instead. That should/could/maybe do the trick. 

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
from arcgis.geometry import *

feature_service_url = 'https://gis.wri.org/server/rest/services/LandMark/comm_ind_Documented/MapServer'
feature_service_root = FeatureLayerCollection(feature_service_url)
feature_layer = feature_service_root.layers[1]

# <FeatureLayer url:"https://gis.wri.org/server/rest/services/LandMark/comm_ind_Documented/MapServer/1">

multipoint_json = {"points":[
        "spatialReference":{"wkid":4326 }};

multipoint = MultiPoint(multipoint_json)

# True

query_result = feature_layer.query(
                geometry_filter = filters.intersects(multipoint), 
                out_fields = 'NAME')
print("Found {} results.".format(len(query_result.features)))
for feature in query_result.features:
# Found 3 results.
# {"attributes": {"OBJECTID": 30989, "Name": "Navajo Nation Reservation"}}
# {"attributes": {"OBJECTID": 106183, "Name": "FORREST RIVER"}}
# {"attributes": {"OBJECTID": 107290, "Name": "Balanggarra (Combined)"}}
New Contributor III

Whenever we try to use Multipoint directly against the query operation of a hosted feature service endpoint, it seems the feature service always returns the first matching feature, not all matching features. Would you be so kind and validate that maybe our Multipoint geometry is malformed?

0 Kudos