gleojson

Types

Represents an altitude value, typically in meters above sea level.

Altitude can be positive (above sea level) or negative (below sea level).

pub type Alt {
  Alt(Float)
}

Constructors

  • Alt(Float)

Feature kind denominator for GeoJSON objects.

pub type Feature

Represents the ID of a Feature in GeoJSON.

The ID can be either a string or a number, as per the GeoJSON specification.

pub type FeatureId {
  StringId(String)
  NumberId(Float)
}

Constructors

  • StringId(String)
  • NumberId(Float)

Represents all possible GeoJSON objects as defined in the GeoJSON specification.

This type uses phantom types kind and properties to distinguish between different GeoJSON object types and to allow for custom property types.

pub type GeoJSON(kind, properties) {
  Point(coordinates: Position)
  MultiPoint(coordinates: List(Position))
  LineString(coordinates: List(Position))
  MultiLineString(coordinates: List(List(Position)))
  Polygon(coordinates: List(List(Position)))
  MultiPolygon(coordinates: List(List(List(Position))))
  GeometryCollection(geometries: List(GeoJSON(Geometry, Nil)))
  Feature(
    geometry: option.Option(GeoJSON(Geometry, Nil)),
    properties: option.Option(properties),
    id: option.Option(FeatureId),
  )
  FeatureCollection(features: List(GeoJSON(Feature, properties)))
}

Constructors

  • Point(coordinates: Position)
  • MultiPoint(coordinates: List(Position))
  • LineString(coordinates: List(Position))
  • MultiLineString(coordinates: List(List(Position)))
  • Polygon(coordinates: List(List(Position)))
  • MultiPolygon(coordinates: List(List(List(Position))))
  • GeometryCollection(geometries: List(GeoJSON(Geometry, Nil)))
  • Feature(
      geometry: option.Option(GeoJSON(Geometry, Nil)),
      properties: option.Option(properties),
      id: option.Option(FeatureId),
    )
  • FeatureCollection(features: List(GeoJSON(Feature, properties)))

Geometry kind denominator for GeoJSON objects.

pub type Geometry

Represents a latitude value in degrees.

Latitude values range from -90 to 90 degrees, with positive values indicating north and negative values indicating south of the Equator.

pub type Lat {
  Lat(Float)
}

Constructors

  • Lat(Float)

Represents a longitude value in degrees.

Longitude values range from -180 to 180 degrees, with positive values indicating east and negative values indicating west of the Prime Meridian.

pub type Lon {
  Lon(Float)
}

Constructors

  • Lon(Float)

Represents a geographic position in either 2D (longitude and latitude) or 3D (longitude, latitude, and altitude).

This type is used to define coordinates in GeoJSON objects.

pub type Position {
  Position2D(#(Lon, Lat))
  Position3D(#(Lon, Lat, Alt))
}

Constructors

  • Position2D(#(Lon, Lat))
  • Position3D(#(Lon, Lat, Alt))

Functions

pub fn encode_geojson(
  geojson: GeoJSON(a, b),
  properties_encoder: fn(b) -> Json,
) -> Json

Encodes a GeoJSON object into a JSON value.

This function takes a GeoJSON object and a properties encoder function, and returns a JSON representation of the GeoJSON object.

Arguments

  • geojson: The GeoJSON object to encode.
  • properties_encoder: A function that encodes the properties of Features and FeatureCollections.

Returns

A JSON representation of the GeoJSON object.

Example

import gleojson
import gleam/json
import gleam/option
import gleam/io

pub type CustomProperties {
  CustomProperties(name: String, value: Float)
}

pub fn custom_properties_encoder(props: CustomProperties) -> json.Json {
  json.object([
    #("name", json.string(props.name)),
    #("value", json.float(props.value)),
  ])
}

pub fn main() {
  let point = gleojson.Point(gleojson.position_2d(lon: 0.0, lat: 0.0))
  let properties = CustomProperties("Example", 42.0)
  let feature = gleojson.Feature(
    geometry: option.Some(point),
    properties: option.Some(properties),
    id: option.Some(gleojson.StringId("example-point"))
  )
  
  let encoded = gleojson.encode_geojson(feature, custom_properties_encoder)
  io.println(json.to_string(encoded))
}
pub fn encode_geometry(geometry: GeoJSON(Geometry, a)) -> Json

Encodes a GeoJSON geometry object into a JSON value.

This function is a convenience wrapper around encode_geojson for geometry objects.

pub fn geojson_decoder(
  properties_decoder: fn(Dynamic) -> Result(a, List(DecodeError)),
) -> fn(Dynamic) -> Result(GeoJSON(b, a), List(DecodeError))

Decodes a GeoJSON object from a dynamic value.

This function takes a dynamic value (typically parsed from JSON) and a properties decoder, and attempts to decode it into a GeoJSON object.

Arguments

  • properties_decoder: A function that decodes the properties of Features and FeatureCollections.

Returns

A function that takes a dynamic value and returns a Result containing either the decoded GeoJSON object or a list of decode errors.

Example

import gleojson
import gleam/json
import gleam/result
import gleam/dynamic
import gleam/io
import gleam/string

pub type CustomProperties {
  CustomProperties(name: String, value: Float)
}

pub fn custom_properties_decoder(
  dyn: dynamic.Dynamic,
) -> Result(CustomProperties, List(dynamic.DecodeError)) {
  dynamic.decode2(
    CustomProperties,
    dynamic.field("name", dynamic.string),
    dynamic.field("value", dynamic.float),
  )(dyn)
}

pub fn main() {
  let json_string = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[0.0,0.0]},\"properties\":{\"name\":\"Example\",\"value\":42.0}}"
  
  let decoded = 
    json.decode(
      from: json_string,
      using: gleojson.geojson_decoder(custom_properties_decoder)
    )
  
  case decoded {
    Ok(geojson) -> {
      // Work with the decoded GeoJSON object
      case geojson {
        gleojson.Feature(_, _, _) -> {
          io.println("Decoded a feature")
        }
        _ -> io.println("Decoded a different type of GeoJSON object")
      }
    }
    Error(errors) -> {
      // Handle decoding errors
      io.println("Failed to decode: " <> string.join(errors, ", "))
    }
  }
}
pub fn geometry_decoder() -> fn(Dynamic) ->
  Result(GeoJSON(Geometry, Nil), List(DecodeError))

Creates a decoder for GeoJSON geometry objects.

This function is a convenience wrapper around geojson_decoder for geometry objects.

pub fn position_2d(lon lon: Float, lat lat: Float) -> Position

Creates a 2D Position object from longitude and latitude values.

This function is a convenience helper for creating a Position object with two dimensions (longitude and latitude).

Arguments

  • lon: The longitude value as a Float.
  • lat: The latitude value as a Float.

Returns

A Position object representing a 2D coordinate.

Example

import gleojson

pub fn main() {
  let position = gleojson.position_2d(lon: 125.6, lat: 10.1)
  // Use this position in your GeoJSON objects, e.g., in a Point geometry
  let point = gleojson.Point(coordinates: position)
}
pub fn position_3d(
  lon lon: Float,
  lat lat: Float,
  alt alt: Float,
) -> Position

Creates a 3D Position object from longitude, latitude, and altitude values.

This function is a convenience helper for creating a Position object with three dimensions (longitude, latitude, and altitude).

Arguments

  • lon: The longitude value as a Float.
  • lat: The latitude value as a Float.
  • alt: The altitude value as a Float.

Returns

A Position object representing a 3D coordinate.

Example

import gleojson

pub fn main() {
  let position = gleojson.position_3d(lon: 125.6, lat: 10.1, alt: 100.0)
  // Use this position in your GeoJSON objects, e.g., in a Point geometry
  let point = gleojson.Point(coordinates: position)
}
pub fn properties_null_decoder(
  dyn: a,
) -> Result(Nil, List(DecodeError))

Decodes null properties for Features and FeatureCollections.

This is a utility function that can be used as the properties_decoder argument for geojson_decoder when you don’t need to decode any properties.

pub fn properties_null_encoder(props: a) -> Json

Encodes null properties for Features and FeatureCollections.

This is a utility function that can be used as the properties_encoder argument for encode_geojson when you don’t need to encode any properties.

Search Document