2

I have a map service that has 2 layers. One is a polygon layer that contains a single large polygon, and the other is a point layer that contains many points located both inside and outside of the large polygon.

I've turned both layers into FeatureLayers:

var polygonFeatureLayer= new FeatureLayer(myPolygonLayer, {
    outFields: ["*"]
});

var pointFeatureLayer= new FeatureLayer(myPointLayer, {
    outFields: ["*"]
});

And I want to use a Geometry Service to find out how many points are located within the polygon.

geometryService = new GeometryService(myGeometryService);

var relationParams = new RelationParameters();
relationParams.geometries1 = myPolygonGeometry;
relationParams.geometries2 = myPointGeometries;
relationParams.relation = RelationParameters.SPATIAL_REL_WITHIN;

geometryService.relation(relationParams).then(addRelateResultsToMap);

But I can't figure out how to get the geometry of the features from the FeatureLayer.

I thought the most obiously way was to use the FeatureLayers's graphics property which supposedly returns an array of Graphics for each feature in the layer.

I then figured after that I'd use graphicsUtils method getGeometries() which supposedly returns an array of Geometries for each graphic in the array.

Hey! That sounds great! Exactly what I need, but I can't get it to work.

I did a simple test:

var polygonFeatureLayer= new FeatureLayer(myPolygonLayer, {
    outFields: ["*"]
});
map.addLayers([polygonFeatureLayer]);

polygonFeatureLayer.on("load", getPolygonGeometry);

function getPolygonGeometry(evt) {

   console.log(graphicsUtils.getGeometries(polygonFeatureLayer.graphics));

    }
}

The console just logs an empty array [].

In my mind this should work. Any idea why it doesn't? What do I need to do to get this to work?

EDIT

If you have a different approach, then I'll consider it, but it has to work knowing that both layers are part of a map service.

1
+100

First thing: you do not need a geometry service for this. You can use one if you really want, but it's an extra step that is not needed.

Here is what you need to do:

Step 1: query your polygon layer using the map service via QueryTask and Query

    var queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0");
    //initialize query
    var query = new Query();
    query.returnGeometry = true;
    query.outFields = ["field1", "field2", "field3"];
    query.where = "field1 =" + "'" + theValue + "'";
    //execute query
    queryTask.execute(query,getResults);

function getResults(featureSet) {
        //remove all graphics on the maps graphics layer
        var theGraphics = [];
        map.graphics.clear();
        var resultFeatures = featureSet.features;

        var theGeometry;
        //since you only have 1 feature in the layer...
        var graphic = resultFeatures[0];
        theGeometry = graphic.geometry;


    }

Step 2: Do the same thing for your point layer except set the geometry property of the query object to the result of you polygon layer query

var queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/1");
        //initialize query
        var query = new Query();
        query.geometry = theGeometry;//the geometry returned from first query
        query.returnGeometry = true;
        query.outFields = ["field1", "field2", "field3"];
        query.where = "field1 =" + "'" + theValue + "'";
        //execute query
        queryTask.execute(query,handleResults);

Make sure you change the final index value of the URL for each query task based on the index of each layer in the map.

If you really want to use a geometry service, you would NOT pass the resulting geometry of your first query into your second. Instead you would pass the resulting geometries of each separate query into the geometry service. It does not seem necessary to me in this instance.

  • Thanks, Brad, but I don't have a theValue to query. The polygon layer only has 1 polygon feature, and the point layer has about 20,000 point features, and I want to consider them all. Would I just set the where to 1=1 for both? Will I have trouble with the 20,000 point features? That's why I was considering the Geometry Service. – geogeogeo Dec 13 '16 at 15:30
  • You would just leave the "where" property blank. Also, you will have trouble with the 20,000. Look to see what your query return limit is on your ArcGIS Server (this can be changed but the default is 1000). Regardless, before you can use the geometry service you have to query the data for the geometries to pass in to the GS. If you can't change the default number of records returned by a query, you will have to query 1000 records at a time until you get them all. – Brad Dec 13 '16 at 15:46
  • I believe you're required to set a WHERE clause, or at least provide an OBJECTID of the feature you want. Go to the Query page of one of the map service layers you provided to see. I could use the OBJECTID to query the polygon layer, and then the returned polygon geometry to query the point layer like you had. Or I could create a Python geoprocessing service instead, but I was hoping to avoid it. I want the query to run on page load to populate some HTML with information. – geogeogeo Dec 13 '16 at 15:57
  • Have you tried it? Standard SQL works without a where clause, not sure why this wouldn't... It's just a way to filter your query. If you don't want it filtered just leave it blank. – Brad Dec 13 '16 at 16:17
  • Yeah, I've ran into this before, and the suggestion was to query where 1=1 to return all features. You can try it here sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/… Anyway, you've helped me think it out. I'm going to leave the question open for a bit to see if there are other answers. Thanks – geogeogeo Dec 13 '16 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.