Return sorted features from spatial FeatureLayer query

07-30-2021 06:43 PM
New Contributor III

I'm using ArcGIS for JavaScript 3.36.  I have the geometry for a route from the Directions widget.  Using the geometry, I query the FeatureLayer that contains the route linework to get the features that are contained in the geometry.  This works.

However, when the results come back from the query, the features are not in order from one end of the geometry to the other.  What determines the order that the features come back in when no order by fields are provided?  How can I ensure the features come back in order?  If that isn't possible, is there a function in the ArcGIS API for JavaScript to join the features in the order based on their geometry?

Thanks to anyone out there that has any input on this.

This is my query:



const query = new Query();
query.returnGeometry = true;
query.outSpatialReference = new SpatialReference(102100);
query.geometry = geometry;
query.spatialRelationship = Query.SPATIAL_REL_CONTAINS;
query.outFields = ["*"];
query.distance = 3;
query.units = "feet";
const prom = layers.routesFeatureLayer.queryFeatures(query);



0 Kudos
2 Replies
Occasional Contributor

When no order by fields are provided, the data will be returned in the same order it is grabbed from the table (which almost always means it is ordered by the object id).

If you want to do a join, you can use the union method from the geometry engine. That will turn them all into one feature, so they will be interconnected.

If you want to order the geometries but keep them separate, that's a bit more complicated. Ordering features based on geometry is very complicated. Assuming you're working with lines (since you mentioned roads), roads can curve back or circle around. If there are any additional features picked up from intersections, it can throw things for a loop as well.

Courtney Menikheim | Application Developer | @thecmenikheim
0 Kudos
New Contributor III

Hi @CourtneyMenikheim, thank you for your insight on this!  In my situation, I need the features ordered by their geometry.  I've found a convoluted workaround, but if I could just get all features on the route in order, it would be super!  I think I've seen that OBJECTID appears to be the default sort; and of course, this is not always in the order of the geometry.