Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: encoding decoding arbitrary Feature properties #4

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This project is licensed under the MIT License. See the LICENSE file for the ful
This project was bootstrapped with assistance from the OpenAI `o1-preview` model.
It was used to scaffold basic types and encoder/decoder functions according to [RFC 7946](https://datatracker.ietf.org/doc/html/rfc7946).
Manual edits were made to adjust the generated code to the actual Gleam language and to fix compilation errors.
Captured assistance session could be found in initial commit message.
Captured assistance session could be found in commit messages.

Other AI models from different providers were used to provide suggestions, generate initial code structures, and assist in problem-solving during the early stages of development.

Expand Down
91 changes: 53 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,30 @@
[![Package Version](https://img.shields.io/hexpm/v/gleojson)](https://hex.pm/packages/gleojson)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/gleojson/)

**gleojson** is a GeoJSON parsing and encoding library for Gleam, following the [RFC 7946](https://tools.ietf.org/html/rfc7946) specification. It provides types and utility functions to encode and decode GeoJSON objects such as Points, LineStrings, Polygons, and more.
**gleojson** is a comprehensive GeoJSON parsing and encoding library for Gleam, following the [RFC 7946](https://tools.ietf.org/html/rfc7946) specification. It provides robust types and utility functions to seamlessly encode and decode GeoJSON objects such as Points, LineStrings, Polygons, and more.

**Note:** This package is currently in development and has not reached version 1.0.0 yet. The API is considered unstable and may undergo breaking changes in future releases. Please use with caution in production environments and expect potential updates that might require code changes.

## Features

- Full support for all GeoJSON object types: Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, Feature, and FeatureCollection
- Flexible encoding and decoding of GeoJSON objects
- Custom property support for Feature and FeatureCollection objects
- Type-safe representation of GeoJSON structures

## Current Limitations

While **gleojson** aims to fully implement the GeoJSON specification (RFC 7946), some features are still under development. Key areas for future improvement include:

1. Coordinate validation
1. Antimeridian and pole handling
1. Bounding box support
1. Right-hand rule enforcement for polygon orientation
1. Position array length limitation
1. GeometryCollection usage recommendations

Despite these limitations, **gleojson** is fully functional for most common GeoJSON use cases.

## Installation

Add **gleojson** to your Gleam project:
Expand All @@ -17,56 +37,51 @@ gleam add gleojson

## Usage

Here's a basic example of how to use gleojson:

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

pub fn main() {
let json_string = "{
\"type\": \"Feature\",
\"geometry\": {
\"type\": \"Point\",
\"coordinates\": [125.6, 10.1]
},
\"properties\": {
\"name\": \"Dinagat Islands\"
}
}"
// Decode the JSON string into a GeoJSON object
let result = json.decode(from: json_string, using: gleojson.geojson_decoder)
case result {
Ok(geojson) -> {
// Successfully decoded GeoJSON
}
Error(errors) -> {
todo
// Handle decoding errors
// errors contains information about what went wrong during decoding
}
}

// Construct GeoJSON from types
let geojson = gleojson.GeoJSONFeatureCollection(
gleojson.FeatureCollection([
gleojson.Feature(
geometry: option.Some(gleojson.Point([1.0, 2.0])),
properties: option.None,
id: option.Some(gleojson.StringId("feature-id")),
),
]),
// Create a Point geometry
let point = gleojson.Point([125.6, 10.1])

// Create a Feature with the Point geometry
let feature = gleojson.Feature(
geometry: option.Some(point),
properties: option.None,
id: option.Some(gleojson.StringId("example-point"))
)

// Encode to JSON string
gleojson.encode_geojson(geojson) |> json.to_string
// Encode the Feature to GeoJSON
let geojson = gleojson.GeoJSONFeature(feature)
let encoded = gleojson.encode_geojson(geojson, gleojson.properties_null_encoder)

// Print the encoded GeoJSON
io.println(json.to_string(encoded))
}
```

Further documentation can be found at https://hexdocs.pm/gleojson.
For more advanced usage, including custom properties and decoding, see the [documentation](https://hexdocs.pm/gleojson).

## Development

```
To build and test the project:

```sh
gleam build
gleam test
```

## Contributing

Contributions to gleojson are welcome! Please feel free to submit a Pull Request. Before contributing, please review our [contribution guidelines](CONTRIBUTING.md).

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) and [NOTICE](NOTICE) files for more details.
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

Please see the [NOTICE](NOTICE) file for information about third party components and the use of AI assistance in this project.
2 changes: 1 addition & 1 deletion birdie_snapshots/feature_encode_decode.accepted
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
version: 1.2.3
title: feature_encode_decode
---
{"id":"feature-id","type":"Feature","geometry":{"type":"Point","coordinates":[1.0,2.0]},"properties":null}
{"id":"feature-id","type":"Feature","geometry":{"type":"Point","coordinates":[1.0,2.0]},"properties":{"name":"Test Point","value":42.0}}
5 changes: 0 additions & 5 deletions birdie_snapshots/featurecollection_encode_decode.accepted

This file was deleted.

5 changes: 5 additions & 0 deletions birdie_snapshots/real_life_feature.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
version: 1.2.3
title: real_life_feature
---
{"id":"yosemite","type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-119.5383,37.8651],[-119.5127,37.8777],[-119.4939,37.8685],[-119.5383,37.8651]]]},"properties":{"name":"Yosemite National Park","area_sq_km":3029.87,"year_established":1890,"is_protected":true}}
5 changes: 5 additions & 0 deletions birdie_snapshots/real_life_featurecollection.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
version: 1.2.3
title: real_life_featurecollection
---
{"type":"FeatureCollection","features":[{"id":"tokyo","type":"Feature","geometry":{"type":"Point","coordinates":[139.6917,35.6895]},"properties":{"name":"Tokyo","population":37435191,"timezone":"Asia/Tokyo","elevation":40.0}},{"id":"colorado-river","type":"Feature","geometry":{"type":"LineString","coordinates":[[-115.1728,36.1147],[-116.2139,36.5674],[-117.1522,36.6567]]},"properties":{"name":"Colorado River","length_km":2330.0,"countries":["USA","Mexico"]}}]}
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "gleojson"
version = "0.1.0"
version = "0.2.0"

description = "A Gleam library for encoding and decoding GeoJSON"
licences = ["MIT"]
Expand Down
Loading