3

I have the following issue with transforming GeoJSON object into GML geometry object.

First, on backend in request I retrieve GeoJSON object. Then I use jackson library to transform it into java object. This java object is FeatureCollection from geojson-jackson library. So I have java object representing GeoJSON on backend and now I need to transform it into GML object and extract geometry from it.

From this:

{
  "featureCollection : {
    "type": "FeatureCollection",
    "features": [{
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [ [[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]] ]
      },
    }]
  }
}

I need to get this:

<gml:Rectangle srsName="urn:x-ogc:def:crs:EPSG:6.6:25833">
  <gml:exterior>
    <gml:LinearRing>
      <gml:coordinates>100.0,0.0 101.0,0.0 101.0,1.0 100.0,1.0 100.0,0.0</gml:coordinates>
    </gml:LinearRing>
  </gml:exterior>
</gml:Rectangle> 

The question, is there any existing library which allows transform from GeoJSON into GML object?

tyomka
  • 459
  • 2
  • 5
  • 14

3 Answers3

4

Geotools can probably do what you need. I would not have posted, since I have not done this, but a speculative recipe is better than "definitely not".

GeoTools can read GeoJSON and give you a JTS geometry or a Feature, which can then be used to generate GML using a org.geotools.xml.Encoder.

Use org.geotools.geojson.feature.FeatureJSON or org.geotools.geojson.geom.GeometryJSON to take a JSON string and give a Geometry instance, like this:

GeometryJSON gJson = new GeometryJSON();
jtsGeometry = gJson.readGeometryCollection(jsonInputStream)

Then configure an encoder to write GML.

org.geotools.xml.Encoder encoder = new org.geotools.xml.Encoder(...);
...
encoder.encode(jtsGeometry, qName, outputStream);

I hope this is enough to started. Perhaps if you get it working (or find it impossible) come back and edit this post.

David Lotts
  • 550
  • 5
  • 10
  • Hello. Can you provide an example about that? I am having issues with prefix when encoding a feature collection obtained by FeatureJSON. Thankyou. – Francesco Aug 10 '17 at 12:53
2

As far as i know, there is no such thing for java. ogr2ogr (a commandline utility) can do that:

converts simple features data between file formats

http://www.gdal.org/ogr2ogr.html

ogr2ogr -f "GML" destination.gml source.geojson

You can run that from java, see this question: How to run GDAL (ogr2ogr) in Java to convert Shapefiles to GeoJSON

Community
  • 1
  • 1
iH8
  • 27,722
  • 4
  • 67
  • 76
0

No, there is definetly not. You have to find another way to do that.

I suggest you to do that either manually or read it as file and save to variables.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183