2

I am a beginner in javascript and I am working on a project using the ArcGIS javascript API. Since I am using shapefiles, I need a way to put them on the map automatically after a procedure. I have been suggested to convert the shapefile to a JSON string, which I did using ESRI.ArcGIS.ConversionTools.FeaturesToJSON. It gives me something like this:

{
    "displayFieldName": "",
    "fieldAliases": {
        "FID": "FID",
        "IDENTIFIER": "IDENTIFIER",
        "DISPLAY": "DISPLAY",
        "COLOUR": "COLOUR"
    },
    "geometryType": "esriGeometryPolyline",
    "spatialReference": {
        "wkid": 32186,
        "latestWkid": 32186
    },
    "fields": [{
        "name": "FID",
        "type": "esriFieldTypeOID",
        "alias": "FID"
    }, {
        "name": "IDENTIFIER",
        "type": "esriFieldTypeString",
        "alias": "IDENTIFIER",
        "length": 254
    }, {
        "name": "DISPLAY",
        "type": "esriFieldTypeString",
        "alias": "DISPLAY",
        "length": 254
    }, {
        "name": "COLOUR",
        "type": "esriFieldTypeDouble",
        "alias": "COLOUR"
    }],
    "features": [{
        "attributes": {
            "FID": 0,
            "IDENTIFIER": " ",
            "DISPLAY": "True",
            "COLOUR": 0
        },
        "geometry": {
            "paths": [
                [
                    [230203.36252065492, 5370086.4987341948],
                    [230195.42152014989, 5370082.7258209372],
                    [230229.82547157537, 5370076.1204356803],
                    [230230.58002531325, 5370076.9307560939],
                    [230231.34286282485, 5370077.8994241506],
                    [230231.94069129985, 5370079.0473612295],
                    [230232.4038794667, 5370080.3275831603],
                    [230232.78926402796, 5370081.3478309968],
                    [230233.07412446581, 5370082.266860785],
                    [230233.20385479546, 5370082.7032454694],
                    [230233.49509875989, 5370083.1654702397],
                    [230233.81149685301, 5370083.6553206556],
                    [230233.89962032362, 5370083.7566910088],
                    [230234.22350693849, 5370083.8735930277],
                    [230235.23812436999, 5370084.6618494503],
                    [230235.7265868854, 5370085.0375824366],
                    [230236.93800452136, 5370086.1774356738],
                    [230238.91537520697, 5370087.1114649111],
                    [230241.20344435755, 5370088.5726968031],
                    [230242.10950562957, 5370089.1200344963],
                    [230243.1274934996, 5370089.6938548377],
                    [230245.73496147123, 5370091.402549807],
                    [230247.58051963543, 5370092.6832000325],
                    [230247.85058683314, 5370092.9499494741],
                    [230247.97786052118, 5370093.1999530876],
                    [230248.02382869384, 5370093.3950914163],
                    [230247.90815344563, 5370093.5550719444],
                    [230247.83746374052, 5370094.7770727118],
                    [230251.59447348068, 5370093.8700312478],
                    [230256.07295378071, 5370094.5567273004],
                    [230271.93292641686, 5370107.4999685455],
                    [230284.57099391034, 5370120.0941871861],
                    [230283.04288481449, 5370122.6776164165],
                    [230273.76307012059, 5370120.3203884307],
                    [230267.85401282064, 5370118.7949806377],
                    [230267.04651756078, 5370118.1997346235],
                    [230266.42955295654, 5370117.4808098888],
                    [230260.8462208565, 5370117.1535397889],
                    [230253.55863216554, 5370125.3309558751],
                    [230252.9894960777, 5370132.4784455141],
                    [230254.132238747, 5370135.4647953082],
                    [230251.42881939228, 5370134.9411432538],
                    [230249.04957992013, 5370132.2041026689],
                    [230251.995854404, 5370124.8109164219],
                    [230261.60623797448, 5370126.380779258],
                    [230271.53357044971, 5370133.660332839],
                    [230271.13617243501, 5370130.7853333829],
                    [230268.41419420805, 5370096.8456510343]
                ]
            ]
        }
    }]
}

Now I am trying to put this on the map but it does not work. I tried a few ways but seriously, I don't understand much what I am doing. It crashes on the new featureLayer with a "cannot get property length of undefined".

I don't care that much about converting it in JSON or not, I need to have a function which can put a shapefile on a ArcGIS javascript map.

Here is the code:

// shapeJSON is the JSON string

var featureCollection = {
    "layerDefinition": null,
    "featureSet": {
        "features": [],
        "geometryType": "esriGeometryPolyline"
    }
};

featureCollection.layerDefinition = shapeJSON
addShapefileJSON(featureCollection);


var featureLayer = new esri.layers.FeatureLayer(featureCollection, {
       mode: esri.layers.FeatureLayer.MODE_ONDEMAND
});

map.addLayer(featureLayer);

1 Answer 1

1

If you look at my answer here: https://gis.stackexchange.com/a/183908/15647 You'll find a way of accomplishing this. You'll need to make sure your JSON is formatted correctly. It doesn't look quite right to me, but it can probably be tweaked, there's some JSON example in the question I linked to as well and all you really need in JSON is the Layer Definition, you're going to want to remove the features and add them in again individually in a loop to get the FeatureLayer constructor to work (also, you need to add the layer to the map before you add features to it). So basically, the JSON you need to pass to the FeatureCollection constructor should be look like this:

 {"layerDefinition": {
     "geometryType": "esriGeometryPolyline",
     "spatialReference": {
     "wkid": 32186,
     "latestWkid": 32186
   },
   "fields": [
    {
        "name": "FID",
        "type": "esriFieldTypeOID",
        "alias": "FID"
    },
    {
        "name": "IDENTIFIER",
        "type": "esriFieldTypeString",
        "alias": "IDENTIFIER",
        "length": 254
    },
    {
        "name": "DISPLAY",
        "type": "esriFieldTypeString",
        "alias": "DISPLAY",
        "length": 254
    },
    {
        "name": "COLOUR",
        "type": "esriFieldTypeDouble",
        "alias": "COLOUR"
    }
  ]},
"featureSet": {
"features": []
 }
}

Think I've matched all my brackets there!

2
  • Thanks! After a lot of work, I ended up succeding with points. I am trying to make it work with polygons and polylines. I am also working with the ESRI support for this. When I will have the complete solution, I will post it here.
    – Math
    Commented Apr 15, 2016 at 15:59
  • I noticed yours was polygons and not points when your JSON was edited for format. In theory, it should work similarly, but populating the feature geometry may not be straightforward.
    – karl
    Commented Apr 15, 2016 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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