Help with Create FeatureSet.FromJSON

8109
1
03-12-2013 12:40 PM
FionaHatfield
New Contributor
Hi,
I have been checking out available samples and other threads about this topic, and would appreciate any guidance the community has to offer.

I am trying to add a layer to my map based upon a JSON string.
I believe it can be done, based on other threads etc...
here is the piece that is giving me trouble:

var nStr:String = "[{"json_featuretype":"rejected(49)","SolutionLon":"-96.7678697","Amplitude":"0","SolutionLat":"53.7175418","Date_Time":"11/10/2012 12:09:19","CloudGround":"0","json_ogc_wkt_crs":"GEOGCS[\"WGS84 Lat/Longs\",DATUM[\"WGS_1984\",SPHEROID[\"World Geodetic System of 1984, GEM 10C\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433],AUTHORITY[\"EPSG\",\"4326\"]]","json_geometry":{"type":"Point","coordinates":[-96.7678697,53.7175418]}}]";
     
var nFeatureSet:FeatureSet = FeatureSet.fromJSON(nStr);
var nLayerDetails:LayerDetails = new LayerDetails();

var nFeatureCollection:FeatureCollection = new FeatureCollection(nFeatureSet,nLayerDetails);
var nFeatureLayer:FeatureLayer = new FeatureLayer();
nFeatureLayer.featureCollection = nFeatureCollection;
nFeatureLayer.mode = "MODE_SNAPSHOT";
nFeatureLayer.id = "Lightning";
nFeatureLayer.name = "Lightning";

map.addLayer(nFeatureLayer);



I'm not sure what it is that I am doing wrong.
Is it my JSON?

any insight would be greatly appreciated.

My environment:
arcgis flex api 3.1
flex 4.6
Tags (2)
0 Kudos
1 Reply
IvanBespalov
Occasional Contributor III
Hi, Fiona

1 - Where ESRI team works with FeatureSet?
Right, when receiving the query results.
So the pattern/example of FeatureSet in JSON = server query response in JSON format. It described in ArcGIS Server REST API help.

2 - Sample
My JSON
{
  "displayFieldName" : "description",
  "fieldAliases" : {
    "objectid" : "Object ID",
    "type" : "Type",
    "description" : "Description"
  },
  "geometryType" : "esriGeometryPoint",
  "spatialReference" : {
    "wkid" : 4326
  },
  "fields" : [
    {
      "name" : "objectid",
      "type" : "esriFieldTypeOID",
      "alias" : "Object ID"
    },
    {
      "name" : "type",
      "type" : "esriFieldTypeInteger",
      "alias" : "Type"
    },
    {
      "name" : "description",
      "type" : "esriFieldTypeString",
      "alias" : "Description",
      "length" : 1073741822
    }
  ],
  "features" : [
    {
      "attributes" : {
        "objectid" : 2010,
        "type" : 3,
        "description" : "xzc"
      },
      "geometry" : {
        "x" : -117.53253785200002,
        "y" : 34.244472716000075
      }
    },
    {
      "attributes" : {
        "objectid" : 2011,
        "type" : 0,
        "description" : "adf"
      },
      "geometry" : {
        "x" : -100.62294522999991,
        "y" : -15.344261442999937
      }
    }
  ]
}

JSON from ESRI help
{
"displayFieldName" : "AREANAME",

"fieldAliases" : {
    "ST" : "ST",
    "POP2000" : "Population - 2000",
    "AREANAME" : "City Name"

},

"fields" : [
{
    "name" : "ST",
    "alias" : "ST",
    "type" : "esriFieldTypeString",
    "length" : 2
},
{

    "name" : "POP2000",
    "alias" : "Population - 2000",
        "type" : "esriFieldTypeInteger"
},

{
    "name" : "AREANAME",
    "alias" : "City Name",
    "type" : "esriFieldTypeString",
    "length" : 255
}
],

"geometryType" : "esriGeometryPoint",
"spatialReference" : {"wkid" : 4326},
"features" : [
    {
    "attributes" : {
        "ST" : "CA",
        "POP2000" : 3694820,
        "AREANAME" : "Los Angeles"
    },
    "geometry" : { "x" : -118.37, "y" : 34.086 }
    },
    {
    "attributes" : {
        "ST" : "CA",
        "POP2000" : 461522,
        "AREANAME" : "Long Beach"
    },
    "geometry" : { "x" : -118.15, "y" : 33.80 }
    }
]
}

JSON as query result http://goo.gl/Eys4N
------------------
App with input panel. Run, paste JSON in panel and push "Add features" button
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" 
      xmlns:esri="http://www.esri.com/2008/ags">
 
 <s:layout>
  <s:VerticalLayout gap="10"
        paddingBottom="10"
        paddingLeft="10"
        paddingRight="10"
        paddingTop="10" />
 </s:layout>
 
 <fx:Script>
  <![CDATA[
   import com.esri.ags.FeatureSet;
   import com.esri.ags.Graphic;
   import com.esri.ags.geometry.Geometry;
   import com.esri.ags.symbols.SimpleFillSymbol;
   import com.esri.ags.symbols.SimpleLineSymbol;
   import com.esri.ags.symbols.SimpleMarkerSymbol;
   import com.esri.ags.utils.JSONUtil;
   
   import mx.controls.Alert;
   
   private var sms:SimpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_TRIANGLE, 22, 0x00FF00);
   private var sls:SimpleLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, 0xFF0000);
   private var sfs:SimpleFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_DIAGONAL_CROSS, 0x0000FF);
   
   protected function onAddButtonClick(event:MouseEvent):void
   {
    var jsonString:String = txtInput.text;
    if (jsonString.length > 0)
    {
     try
     {
      var jsonObj:* = JSONUtil.decode(jsonString);
      if (rbtnFeatureSetType.selected)
      {
       var featureSet:FeatureSet = FeatureSet.fromJSON(jsonObj);
       if (featureSet && featureSet.features)
       {
        addGraphicsToMap(featureSet.features);
       }
      }
      else
      {
       Alert.show("JSON string is valid", "Message");
       // TODO: complete code
      }
     }
     catch (error:Error)
     {
      trace(error.getStackTrace());
      Alert.show("The input string cannot be parsed", "Error");
     }
     
    }
    else
    {
     Alert.show("You must input valid json string in text area", "Error");
    }
   }
   
   private function addGraphicsToMap(graphics:Array):void
   {
    if (graphicsLayer)
    {
     var isModified:Boolean = false;
     for (var i:int; i < graphics.length; i++)
     {
      var graphic:Graphic = graphics;
      if (graphic && graphic.geometry)
      {
       var geometry:Geometry = graphic.geometry;
       
       switch (geometry.type)
       {
        case Geometry.MAPPOINT:
         graphic.symbol = sms;
         break;
        case Geometry.POLYLINE:
         graphic.symbol = sls;
         break;
        case Geometry.POLYGON:
         graphic.symbol = sfs;
         break;
       }
       
       trace("Added graphic id=" + graphicsLayer.add(graphic));
       
       isModified = true;
      }
     }
     if (isModified)
     {
      graphicsLayer.refresh();
     }
    }
   }
  ]]>
 </fx:Script>
 
 
 <fx:Script>
  <![CDATA[
   protected function onClearButtonClick(event:MouseEvent):void
   {
    if (graphicsLayer)
    {
     graphicsLayer.clear();
    }
   }
  ]]>
 </fx:Script>
 
 <fx:Declarations>
  
  <s:RadioButtonGroup id="rbtnGroup"/>
  
 </fx:Declarations>
 
 <s:Panel title="Map panel"
    width="100%"
    height="100%">
  
  <s:layout>
   <s:VerticalLayout gap="5"
         verticalAlign="middle"
         horizontalAlign="center" />
  </s:layout>
  
  <s:Button label="Clear graphics"
      click="onClearButtonClick(event)" />
  
  <esri:Map id="map">
   <esri:ArcGISTiledMapServiceLayer id="baseLayer"
            url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer" 
            alpha="0.7" />
   <esri:GraphicsLayer id="graphicsLayer" />
  </esri:Map>
  
 </s:Panel>
 
 <s:Panel title="Input panel"
    width="100%"
    height="100%">
  
  <s:layout>
   <s:VerticalLayout gap="5"
         verticalAlign="middle"
         horizontalAlign="center" 
         paddingBottom="5"/>
   
  </s:layout>
  
  <s:TextArea id="txtInput" 
     width="100%"
     height="100%" />
  
  <s:HGroup width="100%"
      horizontalAlign="center"
      gap="20">
   
   <s:RadioButton id="rbtnFeatureSetType" 
         label="is FeatureSet"
         selected="true"
         group="{rbtnGroup}"/>
   
   <s:RadioButton id="rbtnCustomType" 
         label="is not FeatureSet"
         group="{rbtnGroup}" />
   
   <s:Button label="Add features"
       click="onAddButtonClick(event)" />
   
  </s:HGroup>  
  
 </s:Panel>
 
</s:Application>


P.S. here similar discussion with sample for oldest API

P.S.2 The JSON Validator
0 Kudos