geospatialpolygongeojsonhere-apimultipolygons

Converting Here API WKTShapeType Multipolygon To GeoJSON?


I have access to a data source that looks like:

"Shape": {
   "_type": "WKTShapeType",
   "Value": "MULTIPOLYGON (((-128.9102 50.82492, -128.90881 50.82422, -128.90717 50.82318, -128.90566 50.82344, -128.90458 50.82304, -128.90401 50.82154, -128.90497 50.82093..."
}

I would like to convert the above to a format that looks like:

{
  "shape": [[{"lat": 39.65451, "lng": -75.78862}, {"lat": 39.65476, "lng": -75.78862}, etc...
}

I am not sure what the formatting names are for the two (I believe the former is Multipolygon and the latter is GeoJSON)? The former comes from calling Here API's geocoder endpoint. The later is stored as a JSON file.

Are there any online tools that will help me do the conversion?


Solution

  • I ended up creating a custom parser built on top of the Wicket library.

    import wicket from 'wicket/wicket';
    
    const parser = (outerPolygon) => {
      const wkt = new wicket.Wkt();
      const parsePolygonData = (polygon) => {
        wkt.read(polygon);
        wkt.toJson();
        const polygons = wkt.components;
        for (let i = 0; i < polygons.length; i++) {
          for (let x = 0; x < polygons[i].length; x++) {
            if (wkt.type === 'multipolygon') {
              for (let y = 0; y < polygons[i][x].length; y++) {
                polygons[i][x][y].lat = polygons[i][x][y].y;
                delete polygons[i][x][y].y;
                polygons[i][x][y].lng = polygons[i][x][y].x;
                delete polygons[i][x][y].x;
              }
            } else {
              polygons[i][x].lat = polygons[i][x].y;
              delete polygons[i][x].y;
              polygons[i][x].lng = polygons[i][x].x;
              delete polygons[i][x].x;
            }
          }
          if (wkt.type === 'multipolygon') {
            polygons[i] = polygons[i][0];
          }
        }
        return polygons;
      };
      return parsePolygonData(outerPolygon);
    };
    

    Wicket reads and JSONifies the body I received via the Shape.Value in the Here endpoint response. I then rearranged the lats / lngs by giving each of those values a label.

    End result is something that looks like:

    [[{"lat": 39.65451, "lng": -75.78862}, {"lat": 39.65476, "lng": -75.78862}, etc...