mgold/elm-geojson/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

GeoJson

Decode GeoJson into an Elm data structure where you can operate on it further. Most of this module defines types that collectively define that data structure.

After using GeoJson.decoder you can either traverse the data structure directly (recommended if you're working with 2D positions and not using properties) or use Json.Decode.andThen to transform it into a more convenient representation specific to your use case (recommended if you find yourself with a lot of Maybes or impossible cases using the first approach). Also, GeoJSON objects may contain nonstandard top-level fields; you can run multiple decoders using Json.Decode.object2 the way you'd use map2.

An encode function is also provided, mostly for completeness and testing. Neither encoding nor decoding attempt to enforce minimum array lengths (excluding positions, whose minimum length is enforced by their type).

Decoder

decoder : Decoder GeoJson

Decode GeoJSON into Elm. The decoded value is expressed in the types defined by this module.

Elm Representation of GeoJSON

All union types are fully exposed so you can inspect them as necessary.

type alias GeoJson =
( GeoJsonObject, Maybe Bbox )

The root representation of GeoJSON in Elm. It consists of a GeoJsonObject and an optional Bbox.

= Geometry Geometry
| Feature FeatureObject
| FeatureCollection (List FeatureObject)

A GeoJsonObject contains the primary data, and is either a Geometry, a FeatureObject, or a list of FeatureObjects.

Note that the tag for FeatureObject is just Feature, to avoid a name collision.

type alias FeatureObject =
{ geometry : Maybe Geometry
, properties : Value
, id : Maybe String
}

A FeatureObject represents a geographic feature. The geometry field is allowed to have null instead of actual geometry, which is represented as Nothing. The properties may be any JSON object but no attempt is made to inspect it. The id is an optional "commonly used identifier". It is permitted by the RFC to be either a string or a number; if the latter this implementation converts it to a string.

= Point Position
| MultiPoint (List Position)
| LineString (List Position)
| MultiLineString (List (List Position))
| Polygon (List (List Position))
| MultiPolygon (List (List (List Position)))
| GeometryCollection (List Geometry)

The heart of GeoJSON: geometry objects. The union tags reflect the type field of the JSON, and carries the value of the coordinates field (or geometries for GeometryCollection).

The specification imposes minimum lengths for some of the arrays (lists in Elm). This representation does not express those guarantees, on the theory that you will likely be working with a valid GeoJson file rather than generating one of your own.

type alias Position =
( Float, Float, List Float )

A Position is the fundamental geometry construct. The specification represents positions as arrays of numbers that must contain at least two elements. This tuple allows the compiler to provide a similar guarantee, so that one avoids Maybes when working with a 2D dataset.

RFC 7946 suggests that positions not contain more than three elements, but this library accepts them. The RFC also states that "[t]he first two elements are longitude and latitude, or easting and northing, precisely in that order".

type alias Bbox =
List Float

A Bounding Box is represented as a simple list of floats. No attempt is made to validate that its length is twice the length of the geometrys' positions, or that low values are preceded by high values.

Encoding

encode : GeoJson -> Value

Encode GeoJSON into Elm. This is mostly for completeness and roundtrip testing.