1

I'm pulling point data by lat long from my SQLServer database via webservice and creating a JSON FeatureCollection object as the webservic response. When I attempt to create a FeatureLayer from this FeatureCollection, the geometry attribute of the features becomes null, while the attribute array of each feature and the layerdefinition comes through fine. Here's a sample of my code and JSON: JSON:

    {
"layerDefinition": {
    "geometryType": "esriGeometryPoint",
    "objectIdField": "ObjectID",
    "drawingInfo": {
        "renderer": {
            "type": "simple",
            "label": "",
            "description": "",
            "symbol": {
                "color": [
                    205,
                    212,
                    144
                ],
                "size": 50,
                "angle": 0,
                "xoffset": 0,
                "yoffset": 0,
                "type": "esriSMS",
                "style": "esriSLSSolid",
                "outline": {
                    "color": [
                        205,
                        212,
                        144
                    ],
                    "width": 0,
                    "type": "esriSLS",
                    "style": "esriSLSolid"
                }
            }
        }
    },
    "fields": [
        {
            "name": "ObjectID",
            "alias": "ObjectID",
            "type": "esriFieldTypeOID"
        }, ...some more fields...
     ]
     },
    "featureSet": {
    "features": [
        {
            "attributes": {
                "ObjectID": 1,...more attributes...
    },
            "geometry": {
                "type": "point",
                "x": -8242964.882933755,
                "y": 5071902.135513675,
                "spatialReference": {
                    "wkid": 102100
                }
            }
        },

Code:

    function onGetFeatureJsonReturn(msg) {

      var featureCollection = JSON.parse(msg.d);
      var featureLayer = new esri.layers.FeatureLayer(featureCollection, {
         id: 'HourlyDataLayer'
       });
       map.addLayers([featureLayer]);
    }
  • A clarification, because the question was long enough already! Using Chrome debugger I can see the featureCollection object looks fine, it has the features, with geometry. After I call the featureLayer constructor passing it the featureCollection object, the featureCollection object looks the same in the debugger, except that the geometry is null for all features. – karl Mar 2 '16 at 15:19
2

the wkid you're specifying is web mercator, but as you said, the coordinates you supply are actually lat/long (appropriately described by the wkid below).

"wkid": 4326

http://spatialreference.org/ref/epsg/wgs-84/

i'm not 100% positive that making the change above will resolve the null geometry problem, but its still certainly worth fixing.

  • I agree, it looks like the thing @Karl is ignoring as a minor problem is in fact THE problem! – Stephen Lead Mar 7 '16 at 22:26
  • OK, I've done full diligence on that now. Converted coordinates to web mercator in the webservice before creating the JSON. FeatureCollection still gets populated as an object with feature objects with geometry objects, then after the call to the FeatureLayer constructor the geometries in the FeatureCollection are still changed to null. I'm editing the question to reflect the new, corrected JSON. – karl Mar 8 '16 at 15:06
0

This doesn't explain why the geometries get set to null, and I'd still love to see an explanation of that, but I did solve the immediate problem by using only the layer definition and fields information from the JSON to create the feature layer, then looping through the features in the JSON to add them to an array of point graphics and adding that to the featureLayer using applyEdits. The trick is to parse the JSON features to a new object before creating the featureLayer. So the solution to my problem, though not the answer to the Constructor nullifying geometries, is this:

var spatialReference = new esri.SpatialReference({ wkid: 102100 });
var featureCollection = JSON.parse(msg.d);
var features = JSON.parse(msg.d).featureSet.features;
featureCollection.featureSet.features = [];
var pointFeatures = [];
var featureLayer = new esri.layers.FeatureLayer(featureCollection, {
    id: 'HourlyDataLayer'
});
map.addLayers([featureLayer]);

for (var i = 0; i < features.length; i++) {
    var feature = features[i];
    var symbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 8,
        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 geometry = new esri.geometry.Point(feature.geometry.x, feature.geometry.y, spatialReference);
    var graphic = new esri.Graphic(geometry, symbol);
    graphic.setInfoTemplate(infoTemplate);
    graphic.setAttributes(attr);
    pointFeatures.push(graphic);
}

featureLayer.applyEdits(pointFeatures, null, null);

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.