Select to view content in your preferred language

Restrict feature layer using a user drawn polygon

757
5
07-19-2012 08:13 AM
JamesBerg
New Contributor III
I am creating a widget to query a feature layer and display the results on the map.  So far I have it filtering data using the definitionExpression when the user clicks the query button on my widget and now I am trying to implement the ability to draw a polygon on the map and when the user presses the query button it will only return points that intersect with the polygon.  I can see from the query page of the feature class that setting the input geometry yields the results I want but I cannot figure out how to do this in action script.  It seems like there should be some property on the feature layer like tLayer.Geometry but that doesn't seem to work.  Any ideas?
Tags (2)
0 Kudos
5 Replies
AustinDavis
New Contributor III
jberg,

From what I gather you are attempting to select a series of points from a layer and return them if they are contained by a polygon drawn by a user?
I doubt the following logic will be the most elegant way but my approach would be as follows using points stored in a GraphicsLayer:

var polygon:Polygon = new Polygon(); //This would be the user's created polygon.

for(var i:int = 0; i<=graphicsLayer.numGraphics;i++){
  if(!polygon.contains(MapPoint((graphicsLayer.graphicsProvider as Graphic).geometry)){
     graphic.visible = false;
  }else{
     graphic.visible = true;
  };
};
0 Kudos
JamesBerg
New Contributor III
Austin thanks for the suggestion.  I feel like there has to be a more elegant way.  I've tried the following but I am not able to get the points to show on the map and so I don't know if my info popup will work this way.  I get results in the feature class in the OnResutls call back but I can't get them to show.  The thing that is confusing to me is that there is a input geometry parameter to the REST API that you call but I can't see how to set it the same way that you specify the where clause or definitionExpression.

var layerName:String;
if ( configXML )
{
    layerName = configXML.layer;
}
else
{
    layerName = "Tickets";
}
var ticketLayer:FeatureLayer = map.getLayer(layerName) as FeatureLayer;

var queryTask:QueryTask = new QueryTask(ticketLayer.url);
var queryTickets:Query = new Query();

// geom is the geometry object that the user draws on the map
if ( geom != null )
{
    queryTickets.geometry = geom;
    queryTickets.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
    queryTickets.outSpatialReference = geom.spatialReference;
}
else
{
    queryTickets.outSpatialReference = map.spatialReference;
}

var graph:GraphicsLayer = new GraphicsLayer();
graph.name = "testGeo";
map.addLayer(graph);

//ticketLayer.queryFeatures(queryTickets, new AsyncResponder(onResult, onFault));
queryTask.execute(queryTickets, new AsyncResponder(onResult, onFault ));

function onResult(featureSet:FeatureSet, token:XMLList = null):void
{
    var tktSym:SimpleMarkerSymbol = new SimpleMarkerSymbol("triangle", 20, 0xFF0000);
    graph.symbol = tktSym;
    graph.graphicProvider = featureSet.features;
}

function onFault(info:Object, token:Object = null):void
{
    
}
0 Kudos
JorgeFiallega
New Contributor
I have the same issue.
I get the following errror
"Unsupported Query parameters for FeatureCollection"

Any advice on how to query a FeatureLayer with a FeatureCollection against a geometry ?

// build the featurelayer
var featureSet:FeatureSet = new FeatureSet(graphicCollection);
var layerDetails:LayerDetails = new LayerDetails();
var featureLayer:FeatureLayer = new FeatureLayer();
featureLayer.featureCollection = new FeatureCollection(featureSet, layerDetails);

//build the query based on a polygon I want to intesect with the features in the featurelayer
var query:Query = new Query();
query.geometry = mypolygon;
query.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
query.outSpatialReference = spatialReference;

//run the query
featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW,new AsyncResponder(onQueryResult2, onQueryFault2));

0 Kudos
DasaPaddock
Esri Regular Contributor
Jorge,

Can you use Austin's suggestion above to filter your graphics before you create you FeatureCollection?

You would need to use Polygon.contains():
http://resources.arcgis.com/en/help/flex-api/apiref/com/esri/ags/geometry/Polygon.html#contains()
0 Kudos
JorgeFiallega
New Contributor
That was simple.
Thanks a lot
0 Kudos