Select to view content in your preferred language

unable to get geometry of featureset from query result

6192
9
12-09-2010 10:50 AM
KenDoman
Frequent Contributor
I'm trying to figure out what is going wrong.  This function posts the results of a query on the map, and provides different symbology based on whether it is a point, line, or polygon.  The problem is, my code stops working when I try to access the geometry type of the features in the featureset.  All attempts to get either the geometryType or the geometry.type either return undefined, or don't return at all.

function queryShowResults(featureSet) {
    //show results on map
    map.graphics.clear();
    var markerSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 10, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 0, 0]), 1), new dojo.Color([0, 255, 0, 0.25]));
    var lineSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASH, new dojo.Color([255, 0, 0]), 1);
    var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([255, 0, 0]), 2), new dojo.Color([255, 255, 0, 0.25]));
    //QueryTask returns a featureSet.  Loop through features in the featureSet and add them to the map.
    for (var i = 0, il = featureSet.features.length; i < il; i++) {
        //Get the current feature from the featureSet.
        //Feature is a graphic
        var graphic = featureSet.features;
        switch (graphic.geometry.type) {
            case "point":
                alert("point");
                graphic.setSymbol(markerSymbol);
                break;
            case "polyline":
                alert("polyline");
                graphic.setSymbol(lineSymbol);
                break;
            case "polygon":
                alert("polygon");
                graphic.setSymbol(polygonSymbol);
                break;
        }
        //Add graphic to the map graphics layer.
        map.graphics.add(graphic);
    }
        
}
0 Kudos
9 Replies
derekswingley1
Deactivated User
Try using featureSet.geometryType in your switch statement.
0 Kudos
KenDoman
Frequent Contributor
Thanks.  I tried it, and it didn't work.  When I test for it, featureSet.geometryType comes back undefined.

Also, the query.returnGeometry = true for the querytask, so it can't be that.
0 Kudos
KenDoman
Frequent Contributor
If it helps, here is the code for the function that calls my query.

When I test the graphic.geometry, it comes back null.  That may be why the graphic.geometry.type will grind the code to a halt.  I don't know why it would be null, though, because I set the returnGeometry = true.

function queryExecute(layerLoc, layerID, qText) {
    // layerLoc is location of map file, starting with "http:/"...
    // layerID is the layer number the query is performed on.
    var queryTask = new esri.tasks.QueryTask(layerLoc + layerID);
    var queryParams = new esri.tasks.Query();
    queryParams.returnGeometry = true;
    queryParams.outFields = ["*"];
    queryParams.where = qText;
    queryTask.execute(queryParams, queryShowResults);
}

function queryShowResults(featureSet) {
     //show results on map
    map.graphics.clear();
    var markerSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 10, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 0, 0]), 1), new dojo.Color([0, 255, 0, 0.25]));
    var lineSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASH, new dojo.Color([255, 0, 0]), 1);
    var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([255, 0, 0]), 2), new dojo.Color([255, 255, 0, 0.25]));
     //QueryTask returns a featureSet.  Loop through features in the featureSet and add them to the map.
    for (var i = 0, il = featureSet.features.length; i < il; i++) {
        //Get the current feature from the featureSet.
         var graphic = featureSet.features;
        alert('graphic geometry: '+graphic.geometry);
        switch (graphic.geometry.type) {
            case "point":
                graphic.setSymbol(markerSymbol);
                break;
            case "polyline":
                graphic.setSymbol(lineSymbol);
                break;
            case "polygon":
                graphic.setSymbol(polygonSymbol);
                break;
        }
        //Add graphic to the map graphics layer.
        map.graphics.add(graphic);
    }
}
0 Kudos
GregCorradini
Emerging Contributor
Hey Raymond,
I used your callback function queryShowResults in a query without any changes (except replacing alert with console.log for firebug feeback) and it worked fine (See code block below). So the problem is somewhere else, not with this callback function logic.

Some questions:

1) What version of JavaScript API are you using? 2.1?

2) What Map Service version are you executing the query against? Is it a Map Service from 10.0 or 9.3.1?

3) To debug this it's good to start with a very simple query. What is query.where being set to when you are getting these errors. See what I was using below.

4) Are you using firebug? How do you know graphic.geometry is coming back null? I'm guessing the problem is with one of your query parameters and chances are nothing is coming back.

var query = new esri.tasks.Query();
 query.where = "1=1";
 query.returnGeometry = true;
  
 var queryTask = new esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/3");
 queryTask.execute(query, queryShowResults);
0 Kudos
HemingZhu
Frequent Contributor
have you put alert(featureSet.features.length) at the beginning of the function to test if there is any feature returns.

If it helps, here is the code for the function that calls my query.

When I test the graphic.geometry, it comes back null.  That may be why the graphic.geometry.type will grind the code to a halt.  I don't know why it would be null, though, because I set the returnGeometry = true.

function queryExecute(layerLoc, layerID, qText) {
    // layerLoc is location of map file, starting with "http:/"...
    // layerID is the layer number the query is performed on.
    var queryTask = new esri.tasks.QueryTask(layerLoc + layerID);
    var queryParams = new esri.tasks.Query();
    queryParams.returnGeometry = true;
    queryParams.outFields = ["*"];
    queryParams.where = qText;
    queryTask.execute(queryParams, queryShowResults);
}

function queryShowResults(featureSet) {
     //show results on map
    map.graphics.clear();
    var markerSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 10, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 0, 0]), 1), new dojo.Color([0, 255, 0, 0.25]));
    var lineSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASH, new dojo.Color([255, 0, 0]), 1);
    var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([255, 0, 0]), 2), new dojo.Color([255, 255, 0, 0.25]));
     //QueryTask returns a featureSet.  Loop through features in the featureSet and add them to the map.
    for (var i = 0, il = featureSet.features.length; i < il; i++) {
        //Get the current feature from the featureSet.
         var graphic = featureSet.features;
        alert('graphic geometry: '+graphic.geometry);
        switch (graphic.geometry.type) {
            case "point":
                graphic.setSymbol(markerSymbol);
                break;
            case "polyline":
                graphic.setSymbol(lineSymbol);
                break;
            case "polygon":
                graphic.setSymbol(polygonSymbol);
                break;
        }
        //Add graphic to the map graphics layer.
        map.graphics.add(graphic);
    }
}
0 Kudos
KenDoman
Frequent Contributor
I recently installed firebug and I'm still getting used to it.  I've been debugging it the old fashion way with alerts.

To answer some of the questions, I'm using the following:

JavaScript API v2.1
ArcGIS Server 10.0
ArcMap 10.0

I've added to my code to get the attributes, and I can get all the visible fields and field attributes in a table (like parcel ids, owners, addresses, etc.), but no geography.  I've set alerts to show graphic.geometry, and they return null.

Looking through the examples ESRI provides, I dug into their mapservers and found the fields had a supported operation: Query Layer, while my layers only supported Query.
0 Kudos
HemingZhu
Frequent Contributor
I recently installed firebug and I'm still getting used to it.  I've been debugging it the old fashion way with alerts.

To answer some of the questions, I'm using the following:

JavaScript API v2.1
ArcGIS Server 10.0
ArcMap 10.0

I've added to my code to get the attributes, and I can get all the visible fields and field attributes in a table (like parcel ids, owners, addresses, etc.), but no geography.  I've set alerts to show graphic.geometry, and they return null.

Looking through the examples ESRI provides, I dug into their mapservers and found the fields had a supported operation: Query Layer, while my layers only supported Query.


I had your problem exactly one time. i found out that my query task in not set as globe variable. I corrected it and it works fine ever since. like this:
     var queryTask;
     function init(){
         queryTask =new esri.....;
     }
0 Kudos
JesseVarner
Occasional Contributor
Hi,
I ran into a similar problem where the QueryTask was not returning geometries (but the IdentifyTask was!) I found this forum post which solved the problem for me:
http://forums.esri.com/Thread.asp?c=158&f=2421&t=273166#923332

Try checking to make sure the OBJECTID and SHAPE fields are checked on in the MXD/MSD for the map service -- this worked for me.

-Jesse
0 Kudos
KenDoman
Frequent Contributor
Hi,
I ran into a similar problem where the QueryTask was not returning geometries (but the IdentifyTask was!) I found this forum post which solved the problem for me:
http://forums.esri.com/Thread.asp?c=158&f=2421&t=273166#923332

Try checking to make sure the OBJECTID and SHAPE fields are checked on in the MXD/MSD for the map service -- this worked for me.

-Jesse


Thanks, Jesse.  This one worked.  I didn't have the shape field checked on the MXD.  And thanks to everybody else who helped me with this issue.

--Ken
0 Kudos