Select to view content in your preferred language

Spatial Query in ArcGIS API for Python

2311
2
Jump to solution
02-22-2020 08:15 AM
NaufalIhsan1
Emerging Contributor

Hi guys,

Anyone has any idea about how to do a spatial query in ArcGIS API for python. Let's say I have a feature layer (point) and a web map with an extent. How to query all points inside the map extent?

Thank you.

0 Kudos
1 Solution

Accepted Solutions
AnttiKajanus
Regular Contributor

Here is a rough way of doing something like this. You should get started at least.

from arcgis.gis import GIS
import getpass

password = getpass.getpass("Enter password: ")

gis = GIS('organization', 'user', password)
print("Connected to: {}".format(gis.properties.urlKey + "." + gis.properties.customBaseUrl))
print("Connected as: {}".format(gis.users.me.username))

feature_layer_item = gis.content.get('bfb34d920bda46388e95a9d4e71e036b')
feature_layer_item

from arcgis.features import FeatureLayer

feature_layer = feature_layer_item.layers[0]
feature_layer

#<FeatureLayer url:"https://services2.arcgis.com/VDO6wzqjr9Wm8rpy/arcgis/rest/services/Water_hydrant_inspection/FeatureServer/0">

from arcgis.geometry import *

envelope = Envelope(feature_layer_item.extent)

XMin = envelope.x[0]
YMin = envelope.x[1]
XMax = envelope.y[0]
YMax = envelope.y[1]

query_json = {
    'rings' : [[
        [XMax,YMax], 
        [XMax,YMin], 
        [XMin,YMin],
        [XMin,YMax],
        [XMax,YMax]
        ]],
      'spatialReference' : {"wkid" : 4326}
    }
query_polygon = Polygon(query_json);
print(query_polygon)

# {'rings': [[[-61.64794109387762, 51.14199999999829], [-61.64794109387762, 13.865999999999516], [-131.08140371080557, 13.865999999999516], [-131.08140371080557, 51.14199999999829], [-61.64794109387762, 51.14199999999829]]], 'spatialReference': {'wkid': 4326}}

query_result = feature_layer.query(geometry_filter = filters.intersects(query_polygon))
print(len(query_result.features))
for feature in query_result.features:
    print(feature)
    
#1441
#results...‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

2 Replies
AnttiKajanus
Regular Contributor

Here is a rough way of doing something like this. You should get started at least.

from arcgis.gis import GIS
import getpass

password = getpass.getpass("Enter password: ")

gis = GIS('organization', 'user', password)
print("Connected to: {}".format(gis.properties.urlKey + "." + gis.properties.customBaseUrl))
print("Connected as: {}".format(gis.users.me.username))

feature_layer_item = gis.content.get('bfb34d920bda46388e95a9d4e71e036b')
feature_layer_item

from arcgis.features import FeatureLayer

feature_layer = feature_layer_item.layers[0]
feature_layer

#<FeatureLayer url:"https://services2.arcgis.com/VDO6wzqjr9Wm8rpy/arcgis/rest/services/Water_hydrant_inspection/FeatureServer/0">

from arcgis.geometry import *

envelope = Envelope(feature_layer_item.extent)

XMin = envelope.x[0]
YMin = envelope.x[1]
XMax = envelope.y[0]
YMax = envelope.y[1]

query_json = {
    'rings' : [[
        [XMax,YMax], 
        [XMax,YMin], 
        [XMin,YMin],
        [XMin,YMax],
        [XMax,YMax]
        ]],
      'spatialReference' : {"wkid" : 4326}
    }
query_polygon = Polygon(query_json);
print(query_polygon)

# {'rings': [[[-61.64794109387762, 51.14199999999829], [-61.64794109387762, 13.865999999999516], [-131.08140371080557, 13.865999999999516], [-131.08140371080557, 51.14199999999829], [-61.64794109387762, 51.14199999999829]]], 'spatialReference': {'wkid': 4326}}

query_result = feature_layer.query(geometry_filter = filters.intersects(query_polygon))
print(len(query_result.features))
for feature in query_result.features:
    print(feature)
    
#1441
#results...‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
NaufalIhsan1
Emerging Contributor

Hi Antti,

It works!! Thanks a lot!!

Regards,

Naufal

0 Kudos