gleojson

Functions for working with GeoJSON data.

This module provides types and functions for encoding and decoding GeoJSON data. It supports all GeoJSON object types including Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, Feature, and FeatureCollection.

Usage

To use this module, you can import it in your Gleam code:

import gleojson

Then you can use the provided functions to encode and decode GeoJSON data.

Types

pub type Feature(properties) {
  Feature(
    geometry: option.Option(Geometry),
    properties: option.Option(properties),
    id: option.Option(FeatureId),
  )
}

Constructors

  • Feature(
      geometry: option.Option(Geometry),
      properties: option.Option(properties),
      id: option.Option(FeatureId),
    )
pub type FeatureCollection(properties) {
  FeatureCollection(features: List(Feature(properties)))
}

Constructors

  • FeatureCollection(features: List(Feature(properties)))
pub type FeatureId {
  StringId(String)
  NumberId(Float)
}

Constructors

  • StringId(String)
  • NumberId(Float)
pub type GeoJSON(properties) {
  GeoJSONGeometry(Geometry)
  GeoJSONFeature(Feature(properties))
  GeoJSONFeatureCollection(FeatureCollection(properties))
}

Constructors

  • GeoJSONGeometry(Geometry)
  • GeoJSONFeature(Feature(properties))
  • GeoJSONFeatureCollection(FeatureCollection(properties))
pub type Geometry {
  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(Geometry))
}

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(Geometry))
pub type Position =
  List(Float)

Functions

pub fn encode_geojson(
  geojson: GeoJSON(a),
  properties_encoder: fn(a) -> 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([0.0, 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 geojson = gleojson.GeoJSONFeature(feature)
  
  let encoded = gleojson.encode_geojson(geojson, custom_properties_encoder)
  io.println(json.to_string(encoded))
}
pub fn geojson_decoder(
  properties_decoder: fn(Dynamic) -> Result(a, List(DecodeError)),
) -> fn(Dynamic) -> Result(GeoJSON(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.GeoJSONFeature(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, ", "))
    }
  }
}

Note: This function expects a valid GeoJSON structure. Invalid or incomplete GeoJSON data will result in a decode error.

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.

Returns

Always returns Ok(Nil).

Example

import gleojson
import gleam/json
import gleam/result

pub fn main() {
  let json_string = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[0.0,0.0]},\"properties\":null}"
  
  let decoded = 
    json.decode(
      from: json_string,
      using: gleojson.geojson_decoder(gleojson.properties_null_decoder)
    )
  
  // The "properties" field in the decoded Feature will be None
}
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.

Returns

A JSON null value.

Example

import gleojson
import gleam/json
import gleam/option

pub fn main() {
  let point = gleojson.Point([0.0, 0.0])
  let feature = gleojson.Feature(
    geometry: option.Some(point),
    properties: option.None,
    id: option.None
  )
  let geojson = gleojson.GeoJSONFeature(feature)
  
  let encoded = gleojson.encode_geojson(geojson, gleojson.properties_null_encoder)
  // The "properties" field in the resulting JSON will be null
}
Search Document