ArcGIS Blog

Sharing and Collaboration

ArcGIS Runtime SDK for Java

Working with GeoJSON using ArcGIS Runtime SDK for Java

By Eric Bader

GeoJSON is an open standard for representing geographic features and their attributes. Based on the JSON (JavaScript Object Notation) format, GeoJSON inherits the advantages of being readable, simple and lightweight. Many GIS technologies and services now support GeoJSON.

Esri has supported the GeoJSON standard for many years through various solutions and initiatives such as the ArcGIS Open Data initiative and a number of Esri’s Open Source Javascript projects, just to name a few.

At its core, GeoJSON defines a type called Feature. A Feature has a geometry and optional properties. The geometry types supported here are Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon. Properties are basically name-value pairs. A set of features is represented by a FeatureCollection.

The following example represents the Los Angeles airport as a GeoJSON Feature. The feature is of type Point with a long-lat value of (-118.40, 33.93). The airport code LAX and its elevation 38 (meters) are specified as properties.

 { 
     "type": "Feature", 
     "geometry": {"type": "Point", "coordinates": [-118.40, 33.93]},
     "properties":
         { "code": "LAX", 
           "elevation": 38, 
         } 
}

Mapping GeoJSON Features to ArcGIS Runtime Java Features

The structure of a Feature in ArcGIS Runtime is very similar to that of a GeoJSON Feature – it consists of a Geometry and an optional map of name-value pairs called attributes. The properties of a GeoJSON feature can be directly mapped to the attributes of a ArcGIS Runtime feature. The mapping between the GeoJSON geometry and ArcGIS Runtime geometry is as per the following table:

Geometry types of GeoJSON and corresponding type in ArcGIS Runtime

Let’s get into a very simple yet practical example of ingesting GeoJSON using Java.

Adding GeoJSON features to an ArcGIS Runtime program

To consume GeoJSON in your Runtime Java applications, you’ll parse the GeoJSON data and create ArcGIS Runtime features from it. We have published an example of this for you on GitHub at https://github.com/Esri/arcgis-runtime-demo-java/tree/master/geojson

In this case, the GeoJsonParser object is used to parse GeoJSON data residing in a file and return the data as a collection of ArcGIS Runtime features. Though a file is used in this example, the source of the GeoJSON could be any sort of input stream that your Java program could read. The example below shows how to use the parser.

// create an instance of the parser
GeoJsonParser geoJsonParser = new GeoJsonParser();

// parse the input file in GeoJSON format
List features = geoJsonParser.parseFeatures(/path/geojson_file);

After the GeoJSON data is parsed into ArcGIS Runtime features, they can be added to a ArcGIS Runtime GraphicsLayer.

// add parsed features to a layer
GraphicsLayer graphicsLayer = new GraphicsLayer();

for (Feature f : features) {
  graphicsLayer.addGraphic(new Graphic(f.getGeometry(), f.getSymbol(), f.getAttributes()));
}

That’s about all there is to it. As we mentioned previously, we invite you to get the full source code for this example from GitHub.

Authored by Vijay Gandhi 

Share this article