Please comment how to decode a GeoJson file in ReasonML? I try to decode coordinates without "field latitude & longitude" in decoder, but I cannot find any information how to parse the field coordinates in the JSON file.
GeoJson file
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
131.469670264,
33.3158712032
]
},
"properties": {
"index": 0,
"alias": "海地獄-別府市",
"name": "Umi-Jigoku",
"image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/7T1aXG9Q3CAtEbwqFm3Nlw/o.jpg"
}
JsonDecoder (bs-json) in ReasonML
[@genType]
type properties = {
index: int,
alias: string,
name: string,
image_url: string,
geometry: coordinates,
}
and coordinates = {
latitude: float,
longitude: float,
};
let places = "../json/alljapan.json";
module Decode = {
let coordinates = json =>
Json.Decode.{
latitude: json |> field("latitude", float),
longitude: json |> field("longitude", float),
};
let properties = json =>
Json.Decode.{
index: json |> field("index", int),
alias: json |> field("alias", string),
name: json |> field("name", string),
image_url: json |> field("image_url", string),
geometry: json |> field("geometry", coordinates),
};
};
let line = places |> Json.parseOrRaise |> Decode.line;
At work, we wrote a library called GeoReason with the goal of providing Reason types, constructors, encoders, and decoders (along with some helper functions such as
eq) for GeoJSON data structures, following the RFC-7946 spec.I haven't tried using the library with React Native, but I assume it should work anywhere you can compile Reason to JS.
Overview
Js.Dict.t(Js.Json.t)Usage
Assuming you have a JSON value of type
Js.Json.t, and you've installed GeoReason following the instructions in the README, you could decode and use your data like this:As you can see, matching on all of the things a GeoJSON value could be is a fairly involved process, so it depends a bit on what you're hoping to do with the values. I've added some helpers to the library, such as
GeoJSON.getPolygons, which will attempt to get a list of polygons for any kind of GeoJSON value (returning an empty list if there were no polygons). Feel free to open issues if there are additional helpers that you'd find useful.