# GeoJSON

This driver implements read/write support for access to features encoded in GeoJSON format. The GeoJSON is a dialect based on the JavaScript Object Notation (JSON). The JSON is a lightweight plain text format for data interchange and GeoJSON is nothing other than its specialization for geographic content.

At the moment of writing this, GeoJSON is supported as output format of services implemented by FeatureServer, GeoServer and CartoWeb.

The OGR GeoJSON driver translates a GeoJSON encoded data to objects of OGR Simple Features model: Datasource, Layer, Feature, Geometry. The implementation is based on GeoJSON Specification draft, v5.0.

Starting with OGR 1.8.0, the GeoJSON driver can read the JSON output of Feature Service request following the GeoServices REST Specification, like implemented by ArcGIS Server REST API

## Datasource

The OGR GeoJSON driver accepts three types of sources of data:

- Uniform Resource Locator (URL) - a Web address to perform HTTP request
- Plain text file with GeoJSON data - identified from the file extension .geojson or .json
- Text passed directly and encoded in GeoJSON

## Layer

A GeoJSON datasource is translated to single OGRLayer object with pre-defined name OGRGeoJSON:

    ogrinfo -ro http://featureserver/data/.geojson OGRGeoJSON

It's also valid to assume that OGRDataSource::GetLayerCount() for GeoJSON datasource always returns 1.

Accessing Web Service as a datasource (ie. FeatureServer), each request will produce new layer. This behavior conforms to stateless nature of HTTP transaction and is similar to how Web browsers operate: single request == single page.

If a top-level member of GeoJSON data is of any other type than FeatureCollection, the driver will produce a layer with only one feature. Otherwise, a layer will consists of a set of features.

## Feature

The OGR GeoJSON driver maps each object of following types to new OGRFeature object: Point, LineString, Polygon, GeometryCollection, Feature.

According to the GeoJSON Specification, only the Feature object must have a member with name properties. Each and every member of properties is translated to OGR object of type of OGRField and added to corresponding OGRFeature object.

The GeoJSON Specification does not require all Feature objects in a collection must have the same schema of properties. If Feature objects in a set defined by FeatureCollection object have different schema of properties, then resulting schema of fields in OGRFeatureDefn is generated as union of all Feature properties.

It is possible to tell the driver to not to process attributes by setting environment variable ATTRIBUTES_SKIP=YES. Default behavior is to preserve all attributes (as an union, see previous paragraph), what is equal to setting ATTRIBUTES_SKIP=NO.

## Geometry

Similarly to the issue with mixed-properties features, the GeoJSON Specification draft does not require all Feature objects in a collection must have geometry of the same type. Fortunately, OGR objects model does allow to have geometries of different types in single layer - a heterogeneous layer. By default, the GeoJSON driver preserves type of geometries.

However, sometimes there is a need to generate homogeneous layer from a set of heterogeneous features. For this purpose, it's possible to tell the driver to wrap all geometries with OGRGeometryCollection type as a common denominator. This behavior may be controlled by setting environment variable GEOMETRY_AS_COLLECTION=YES (default is NO).

## Environment variables

- GEOMETRY_AS_COLLECTION - used to control translation of geometries: YES - wrap geometries with OGRGeometryCollection type
- ATTRIBUTES_SKIP - controls translation of attributes: YES - skip all attributes

## Example

How to dump content of .geojson file:

    ogrinfo -ro point.geojson

How to query features from remote service with filtering by attribute:

    ogrinfo -ro http://featureserver/cities/.geojson OGRGeoJSON -where "name=Warsaw"

How to translate number of features queried from FeatureServer to ESRI Shapefile:

    ogr2ogr -f "ESRI Shapefile" cities.shp http://featureserver/cities/.geojson OGRGeoJSON

Read the result of a FeatureService request against a GeoServices REST server:

    ogrinfo -ro -al "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Hydrography/Watershed173811/FeatureServer/0/query?where=objectid+%3D+objectid&outfields=*&f=json"

## See Also

- GeoJSON - encoding geographic content in JSON
- JSON - JavaScript Object Notation
- JSON-C - A JSON implementation in C
- [Gdal-dev] OGR GeoJSON Driver - driver announcement
- GeoServices REST Specification