Hello,
Can Graphics Layers be Queried? I cannot see anywhere that says that they cannot.
I know that CSV Layers cannot be queried.
Thanks,
Chris
In the 4.xyz API a layer view can be queried for a graphics layer the same way you would query the layer view for a feature layer:
<SPAN class="comment token"><SPAN>// source: </SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fdevelopers.arcgis.com%2Fjavascript%2Flatest%2Fapi-reference%2Fesri-layers-FeatureLayer.html" target="_blank">https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html</A></SPAN> <SPAN class="comment token">// returns all the graphics from the layer view</SPAN> view<SPAN class="punctuation token">.</SPAN><SPAN class="token function">whenLayerView</SPAN><SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">then</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">function</SPAN><SPAN class="punctuation token">(</SPAN>lyrView<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN> lyrView<SPAN class="punctuation token">.</SPAN><SPAN class="token function">watch</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"updating"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">function</SPAN><SPAN class="punctuation token">(</SPAN>val<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">if</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="operator token">!</SPAN>val<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">// wait for the layer view to finish updating</SPAN> lyrView<SPAN class="punctuation token">.</SPAN><SPAN class="token function">queryFeatures</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">then</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">function</SPAN><SPAN class="punctuation token">(</SPAN>results<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN> console<SPAN class="punctuation token">.</SPAN><SPAN class="token function">log</SPAN><SPAN class="punctuation token">(</SPAN>results<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// prints all the client-side graphics to the console</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</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>
You could pass a Query into layerView.queryFeatures with an extent geometry and the 'intersects' spatial relationship to query for features inside an extent. See Query | API Reference | ArcGIS API for JavaScript 4.3 for more details.
If you're using the 3.xyz API, my bet would be that you could easily write a query method yourself by accessing each graphic in a graphics layer and using the geometry engine to test each one. This is not an approach that scales especially well. I'm not sure if layerView.queryFeatures scales very well either.
Hi @RyanSutcliffe , did you manage to get this to work for querying a GraphicsLayer? Would you be open to sharing a code snippet? Thank you very much!
Taking a second look this morning I see how you can take a collection of graphics and set them as a source for a new FeatureLayer. That way you have access to queryFeatures and related functionality. I thought that a FeatureLayer required a REST endpoint or Map/FeatureService as it's source.
I could be wrong but I don't see how you can get a LayerView class from a GraphicsLayer. Seems a GraphicsLayer creates a GraphicsLayerView which only has queryGraphics(). The example and link above works only for a FeatureLayer. If either of you were indeed able to do the above would love to see a code sample of how you got this: specifically a LayerView from a GraphicsLayer in a map.
Thank you all for the feedback!
Chris,
The simple answer is No. But you could manually query the GL by looping through it's graphics and checking the graphics attributes.
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.