# GeoJSON

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

GeoJSON is supported as an output format of a number of services: FeatureServer, GeoServer, CartoWeb, etc.

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

Starting with OGR 1.8.0, the GeoJSON driver can read the JSON output of Feature Service requests following the GeoServices REST Specification, like implemented by ArcGIS Server REST API. Starting with OGR 2.0, the GeoJSON driver can scroll through such result sets that are spread over multiple pages (for ArcGIS servers >= 10.3). This is automatically enabled if URL does not contain an explicit resultOffset parameter. If it contains this parameter and scrolling is still desired, the FEATURE_SERVER_PAGING open option must be set to YES. The page size can be explicitly set with the resultRecordCount parameter (but is subject to a server limit). If it is not set, OGR will set it to the maximum value allowed by the server.

Starting with OGR 1.11, the GeoJSON driver can read the TopoJSON format

Starting with GDAL 2.1.0, the GeoJSON driver supports updating existing GeoJSON files. In that case, the default value for the NATIVE_DATA open option will be YES.

## 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 (i.e. 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.

If the NATIVE_DATA open option is set to YES, members at the level of the FeatureCollection will be stored as a serialized JSon object in the NATIVE_DATA item of the NATIVE_DATA metadata domain of the layer object (and "application/vnd.geo+json" in the NATIVE_MEDIA_TYPE of the NATIVE_DATA metadata domain).

## 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 to 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.

Schema detection will recognized fields of type String, Integer, Real, StringList, IntegerList and RealList. Starting with GDAL 2.0, Integer(Boolean), Date, Time and DateTime fields are also recognized.

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.

If the NATIVE_DATA open option is set to YES, the Feature JSon object will be stored as a serialized JSon object in the NativeData property of the OGRFeature object (and "application/vnd.geo+json" in the NativeMediaType property). On write, if a OGRFeature to be written has its NativeMediaType property set to "application/vnd.geo+json" and its NativeData property set to a string that is a serialized JSon object, then extra members of this object (i.e. not the "property" dictionary, nor the first 3 dimensions of geometry coordinates) will be used to enhance the created JSon object from the OGRFeature. See RFC 60 for more details.

## 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 a 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 GEOMETRY_AS_COLLECTION=YES in the environment (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

## Open options

(GDAL >= 2.0)

- FLATTEN_NESTED_ATTRIBUTES = YES/NO: Whether to recursively explore nested objects and produce flatten OGR attributes. Defaults to NO.
- NESTED_ATTRIBUTE_SEPARATOR = character: Separator between components of nested attributes. Defaults to '_'
- FEATURE_SERVER_PAGING = YES/NO: Whether to automatically scroll through results with a ArcGIS Feature Service endpoint.
- NATIVE_DATA = YES/NO: (GDAL >= 2.1) Whether to store the native JSon representation at FeatureCollection and Feature level. Defaults to NO. This option can be used to improve round-tripping from GeoJSON to GeoJSON by preserving some extra JSon objects that would otherwise be ignored by the OGR abstraction. Note that ogr2ogr by default enable this option, unless you specify its -noNativeData switch.
- ARRAY_AS_STRING = YES/NO: (GDAL >= 2.1) Whether to expose JSon arrays of strings, integers or reals as a OGR String. Default is NO. Can also be set with the OGR_GEOJSON_ARRAY_AS_STRING configuration option.

To explain FLATTEN_NESTED_ATTRIBUTES, consider the following GeoJSON fragment:

    {
      "type": "FeatureCollection",
      "features":
      [
        {
          "type": "Feature",
          "geometry": {
            "type": "Point",
            "coordinates": [ 2, 49 ]
          },
          "properties": {
            "a_property": "foo",
            "some_object": {
              "a_property": 1,
              "another_property": 2
            }
          }
        }
      ]
    }

"ogrinfo test.json -al -oo FLATTEN_NESTED_ATTRIBUTES=yes" reports:

    OGRFeature(OGRGeoJSON):0
      a_property (String) = foo
      some_object_a_property (Integer) = 1
      some_object_another_property (Integer) = 2
      POINT (2 49)

## Layer creation options

- WRITE_BBOX = YES/NO: (OGR >= 1.9.0) Set to YES to write a bbox property with the bounding box of the geometries at the feature and feature collection level. Defaults to NO.
- COORDINATE_PRECISION = int_number: (OGR >= 1.9.0) Maximum number of figures after decimal separator to write in coordinates. Default to 15. "Smart" truncation will occur to remove trailing zeros.
- SIGNIFICANT_FIGURES = int_number: (OGR >= 2.1) Maximum number of significant figures when writing floating-point numbers. Default to 17. If explicitly specified, and COORDINATE_PRECISION is not, this will also apply to coordinates.
- NATIVE_DATA=string. (OGR >= 2.1) Serialized JSon object that contains extra properties to store at FeatureCollection level.
- NATIVE_MEDIA_TYPE=string. (OGR >= 2.1) Format of NATIVE_DATA. Must be "application/vnd.geo+json", otherwise NATIVE_DATA will be ignored.

## VSI Virtual File System API support

Some features below require OGR >= 1.9.0.

The driver supports reading and writing to files managed by VSI Virtual File System API, which includes "regular" files, as well as files in the /vsizip/ (read-write), /vsigzip/ (read-write), /vsicurl/ (read-only) domains.

Writing to /dev/stdout or /vsistdout/ is also supported.

## Round-tripping of extra JSon members

See RFC 60 for more details.

Starting with GDAL 2.1, extra JSon members at the FeatureCollection, Feature or geometry levels that are not normally reflected in the OGR abstraction, such as the ones called "extra_XXXXX_member" in the below snippet, are by default preserved when executing ogr2ogr with GeoJSON both at the source and destination. This also applies to extra values in position tuples of geometries, beyond the 3rd dimension (100, 101 in the below example), if the transformation preserves the geometry structure (for example, reprojection is allowed, but not change in the number of coordinates).

    {
      "type": "FeatureCollection",
      "extra_fc_member": "foo",
      "features":
      [
        {
          "type": "Feature",
          "extra_feat_member": "bar",
          "geometry": {
            "type": "Point",
            "extra_geom_member": "baz",
            "coordinates": [ 2, 49, 3, 100, 101 ]
          },
          "properties": {
            "a_property": "foo",
          }
        }
      ]
    }

This behaviour can be turned off by specifying the -noNativeData switch of the ogr2ogr utility.

## 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