Convert GeoJSON string to Elasticsearch Geometry

Viewed 309

I have a GeoJSON string like this

{
            "type": "Polygon",
            "coordinates": [
              [
                [
                  121.4,
                  33.99831528411953
                ],
                [
                  120.32770211588034,
                  33.09540949748297
                ],
                [
                  121.4,
                  32.20168471588049
                ],
                [
                  122.47229788411967,
                  33.09540949748297
                ],
                [
                  121.4,
                  33.99831528411953
                ]
              ]
            ]
          }

I want transform it to Elasticsearch Geometry or ShapeBuilder.

But I didn't find a convenient way.

Has anyone encountered similar problems?

Or have tool classe can do this.

Thank you very much

2 Answers

I find a solution like

 String valueAsString = mapper.writeValueAsString(value);

        GeometryParser parser = new GeometryParser(true, true, true);
        XContentParser contentParser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, valueAsString);
        contentParser.nextToken();
        Geometry geometry = parser.parse(contentParser);

but I dont know this is right or not.

Here is how you can do this:

// read your GeoJSON string from a file
String geojson = ...;

// parse the GeoJSON
JsonXContent geoJsonParser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, geojson);

// create a shape builder (of type PolygonBuilder since the GeoJSON represents a polygon)
PolygonBuilder shapeBuilder = (PolygonBuilder) ShapeBuilder.parse(geoJsonParser);
Related