stephenberry / glaze

Extremely fast, in memory, JSON and interface library for modern C++
MIT License
1.13k stars 113 forks source link

Guidance on translating GeoJSON from/to Boost Geometry #1341

Open pentatonick opened 3 hours ago

pentatonick commented 3 hours ago

Hi Stephen,

Thank you for the awesome library, however I am struggling to write GeoJSON from Boost Geometry and need guidance.

Given a generic Point of dimension N and using the design rationale defined in Boost Geometry docs, where the components of the points are accessed through free functions boost::geometry::get<N>(Point p) how to model writing it to JSON to have an output such as for a 3D point:

{
    "type" : "Point",
    "coordinates" : [3.4,2.3,1.2]
}

The current hurdles are:

  1. Custom Read/Write example in README.md https://github.com/stephenberry/glaze/blob/main/README.md?plain=1#L360 uses member functions and not free functions.
  2. The examples for custom writing & reading (https://github.com/stephenberry/glaze/blob/main/docs/custom-serialization.md) has a JSON identifier which can't be resolved in version 3.4.1.

So please, can you give some guidance on how one would model generic Points, Linestrings, MultiLinestrings, etc... of Boost Geometry with Glaze in order to produce GeoJSON geometries? https://datatracker.ietf.org/doc/html/rfc7946

I currently work around the custom serialization with the generic json type glz::json_t json; and parse the strings into my model classes, but I believe you would be able to provide a more elegant way to solve the problem with a generic implementation, for which I am eager to learn.

Thank you,

Reference of a Linestring:

{
    "type" : "LineString",
    "coordinates" : [
      [13.5, 43, 35.8],
      [10.73, 59.92,3.0]
    ]
}

Reference of a MultiLinestring:

{
    "type" : "MultiLineString",
    "coordinates" : [
      [[12.1, 40.3], [11.42, 60.34]],
      [[43.5, 9.8], [-32.23, 23.40]]
    ]
}