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.
Solved! Go to Solution.
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...
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...
Hi Antti,
It works!! Thanks a lot!!
Regards,
Naufal