geokit/geojson
GeoJSON (RFC 7946) encoders and decoders for geokit geometries and for
features with properties of your own type. Encoders return a
json.Json for json.to_string, and decoders are for json.parse.
GeoJSON writes a position as [longitude, latitude]; a third number
(altitude) is accepted when decoding and dropped. Decoders reject
positions out of range, polygon rings that are open or have fewer
than 4 positions, and GeometryCollection.
Types
A GeoJSON feature. geometry is None for a feature whose geometry
is null.
pub type Feature(properties) {
Feature(
geometry: option.Option(geokit.Geometry),
properties: properties,
id: option.Option(FeatureId),
)
}
Constructors
-
Feature( geometry: option.Option(geokit.Geometry), properties: properties, id: option.Option(FeatureId), )
Values
pub fn feature_collection_decoder(
properties properties: decode.Decoder(p),
) -> decode.Decoder(List(Feature(p)))
Returns a decoder for a GeoJSON feature collection object, decoding
each feature’s properties with properties.
Examples
json.parse(
"{\"type\":\"FeatureCollection\",\"features\":[]}",
feature_collection_decoder(properties: decode.dynamic),
)
// -> Ok([])
json.parse(
"{\"type\":\"Feature\",\"features\":[]}",
feature_collection_decoder(properties: decode.dynamic),
)
// -> Error(json.UnableToDecode([decode.DecodeError(
// expected: "FeatureCollection",
// found: "String",
// path: ["type"],
// )]))
pub fn feature_collection_to_json(
features: List(Feature(p)),
properties properties: fn(p) -> json.Json,
) -> json.Json
Returns the GeoJSON feature collection object for features,
encoding each feature’s properties with properties.
Examples
[Feature(geometry: None, properties: Nil, id: None)]
|> feature_collection_to_json(properties: fn(_) { json.null() })
|> json.to_string
// -> "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":null,\"properties\":null}]}"
pub fn feature_decoder(
properties properties: decode.Decoder(p),
) -> decode.Decoder(Feature(p))
Returns a decoder for a GeoJSON feature object, decoding its
properties with properties. A null geometry decodes to None and
a missing or null id to None.
Examples
json.parse(
"{\"type\":\"Feature\",\"geometry\":null,\"properties\":{\"name\":\"Tokyo\"},\"id\":1}",
feature_decoder(properties: decode.at(["name"], decode.string)),
)
// -> Ok(Feature(geometry: None, properties: "Tokyo", id: Some(IntId(1))))
json.parse(
"{\"type\":\"Point\",\"coordinates\":[0,0]}",
feature_decoder(properties: decode.dynamic),
)
|> result.is_error
// -> True
Target specific behavior
On JavaScript a number id with no fractional part, such as 1.0,
decodes to IntId. On Erlang 1.0 decodes to FloatId(1.0).
pub fn feature_to_json(
feature: Feature(p),
properties properties: fn(p) -> json.Json,
) -> json.Json
Returns the GeoJSON feature object for feature, encoding its
properties with properties. A None geometry is written as null
and a None id is left out.
Examples
Feature(geometry: None, properties: "Tokyo", id: Some(IntId(1)))
|> feature_to_json(properties: fn(name) {
json.object([#("name", json.string(name))])
})
|> json.to_string
// -> "{\"type\":\"Feature\",\"geometry\":null,\"properties\":{\"name\":\"Tokyo\"},\"id\":1}"
pub fn geometry_decoder() -> decode.Decoder(geokit.Geometry)
Returns a decoder for a GeoJSON geometry object. Integer and fractional coordinates are both accepted.
Examples
json.parse(
"{\"type\":\"Point\",\"coordinates\":[139.5,35.5]}",
geometry_decoder(),
)
// -> Ok(Point(geokit.wrap_lat_lng(latitude: 35.5, longitude: 139.5)))
json.parse(
"{\"type\":\"Circle\",\"coordinates\":[139.5,35.5]}",
geometry_decoder(),
)
// -> Error(json.UnableToDecode([decode.DecodeError(
// expected: "Point, MultiPoint, LineString, MultiLineString, Polygon or MultiPolygon",
// found: "String",
// path: ["type"],
// )]))
pub fn geometry_to_json(geometry: geokit.Geometry) -> json.Json
Returns the GeoJSON geometry object for geometry. An open polygon
ring is closed by repeating its first position at the end.
geometry_decoder rejects the output for a ring
with fewer than 3 positions besides the closing one, or for a polygon
without rings.
Examples
geokit.wrap_lat_lng(latitude: 35.5, longitude: 139.5)
|> geokit.Point
|> geometry_to_json
|> json.to_string
// -> "{\"type\":\"Point\",\"coordinates\":[139.5,35.5]}"