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.
Here is a rough way of doing something like this. You should get started at least.
<SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>gis <SPAN class="keyword token">import</SPAN> GIS <SPAN class="keyword token">import</SPAN> getpass password <SPAN class="operator token">=</SPAN> getpass<SPAN class="punctuation token">.</SPAN>getpass<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Enter password: "</SPAN><SPAN class="punctuation token">)</SPAN> gis <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'organization'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'user'</SPAN><SPAN class="punctuation token">,</SPAN> password<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Connected to: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>gis<SPAN class="punctuation token">.</SPAN>properties<SPAN class="punctuation token">.</SPAN>urlKey <SPAN class="operator token">+</SPAN> <SPAN class="string token">"."</SPAN> <SPAN class="operator token">+</SPAN> gis<SPAN class="punctuation token">.</SPAN>properties<SPAN class="punctuation token">.</SPAN>customBaseUrl<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Connected as: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>gis<SPAN class="punctuation token">.</SPAN>users<SPAN class="punctuation token">.</SPAN>me<SPAN class="punctuation token">.</SPAN>username<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> feature_layer_item <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'bfb34d920bda46388e95a9d4e71e036b'</SPAN><SPAN class="punctuation token">)</SPAN> feature_layer_item <SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>features <SPAN class="keyword token">import</SPAN> FeatureLayer feature_layer <SPAN class="operator token">=</SPAN> feature_layer_item<SPAN class="punctuation token">.</SPAN>layers<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> feature_layer <SPAN class="comment token">#<FeatureLayer url:"https://services2.arcgis.com/VDO6wzqjr9Wm8rpy/arcgis/rest/services/Water_hydrant_inspection/FeatureServer/0"></SPAN> <SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>geometry <SPAN class="keyword token">import</SPAN> <SPAN class="operator token">*</SPAN> envelope <SPAN class="operator token">=</SPAN> Envelope<SPAN class="punctuation token">(</SPAN>feature_layer_item<SPAN class="punctuation token">.</SPAN>extent<SPAN class="punctuation token">)</SPAN> XMin <SPAN class="operator token">=</SPAN> envelope<SPAN class="punctuation token">.</SPAN>x<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> YMin <SPAN class="operator token">=</SPAN> envelope<SPAN class="punctuation token">.</SPAN>x<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> XMax <SPAN class="operator token">=</SPAN> envelope<SPAN class="punctuation token">.</SPAN>y<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> YMax <SPAN class="operator token">=</SPAN> envelope<SPAN class="punctuation token">.</SPAN>y<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> query_json <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="string token">'rings'</SPAN> <SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN> <SPAN class="punctuation token">[</SPAN>XMax<SPAN class="punctuation token">,</SPAN>YMax<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN>XMax<SPAN class="punctuation token">,</SPAN>YMin<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN>XMin<SPAN class="punctuation token">,</SPAN>YMin<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN>XMin<SPAN class="punctuation token">,</SPAN>YMax<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN>XMax<SPAN class="punctuation token">,</SPAN>YMax<SPAN class="punctuation token">]</SPAN> <SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'spatialReference'</SPAN> <SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"wkid"</SPAN> <SPAN class="punctuation token">:</SPAN> <SPAN class="number token">4326</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN> query_polygon <SPAN class="operator token">=</SPAN> Polygon<SPAN class="punctuation token">(</SPAN>query_json<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>query_polygon<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># {'rings': [[[-61.64794109387762, 51.14199999999829], [-61.64794109387762, 13.865999999999516], [-131.08140371080557, 13.865999999999516], [-131.08140371080557, 51.14199999999829], [-61.64794109387762, 51.14199999999829]]], 'spatialReference': {'wkid': 4326}}</SPAN> query_result <SPAN class="operator token">=</SPAN> feature_layer<SPAN class="punctuation token">.</SPAN>query<SPAN class="punctuation token">(</SPAN>geometry_filter <SPAN class="operator token">=</SPAN> filters<SPAN class="punctuation token">.</SPAN>intersects<SPAN class="punctuation token">(</SPAN>query_polygon<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>query_result<SPAN class="punctuation token">.</SPAN>features<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> feature <SPAN class="keyword token">in</SPAN> query_result<SPAN class="punctuation token">.</SPAN>features<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>feature<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#1441</SPAN> <SPAN class="comment token">#results...</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Hi Antti,It works!! Thanks a lot!!
Regards,
Naufal
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.