Is it possible to convert GeoJSON to KML format using geotools. I am trying to figure out this using Java but I can get results only using node js.
[GIS] GeoJSON to KML conversion
geojsongeotoolskml
Related Solutions
I have followed the resource pointed out by @iant and.. Here's the solution:
public static void toShp(File geojson) throws IOException {
File shpFile = new File("test.shp");
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", shpFile.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
ShapefileDataStore shpDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
InputStream in = new FileInputStream(geojson);
int decimals = 15;
GeometryJSON gjson = new GeometryJSON(decimals);
FeatureJSON fjson = new FeatureJSON(gjson);
FeatureCollection fc = fjson.readFeatureCollection(in);
SimpleFeatureType type = (SimpleFeatureType) fc.getSchema();
shpDataStore.createSchema(type);
Transaction transaction = new DefaultTransaction("create");
String typeName = shpDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = shpDataStore.getFeatureSource(typeName);
if (featureSource instanceof FeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(fc);
transaction.commit();
} catch (Exception ex) {
ex.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
} else {
System.out.println(typeName + " does not support read/write access");
}
}
The trick is to get a list of layers in the KML that you can than iterate over individually.
A linux solution (bash) could look like...
for layer in "$(ogrinfo -ro -so -q file.kml | cut -d ' ' -f 2)"
do
ogr2ogr -f "GeoJSON" "file_${layer}.json" file.kml "${layer}"
done
You might also have an issue of spaces ' ' in the kml layer name.
Those can be substituted (removed or replaced) on the fly with bash:
$ layer1="Site - 123"
$ echo ${layer1// /}
$ Site-123
$ echo ${layer1} | sed 's/ //g'
$ Site-123
Because of this, I highly recommend experimenting with the output of ogrinfo -ro -so -q file.kml | cut -d ' ' -f 2)
to make sure it treats the layers in the list of that kml properly. Try to make adjustments to the code so that the spaces don't split up the list too much.
You can see that cut -d ' ' -f 2
will look for spaces (-d ' '
) and separate the output into fields where they are found. Then it takes the second field (-f 2
) and uses that as the layer name.
Here's an example of the output on a sample sqlite database:
% ogrinfo -ro -so -q ca_bs.sqlite
1: bs_1250009_0 (Point)
2: bs_2060009_0 (Point)
3: bs_2010009_2 (Multi Polygon)
4: bs_2380009_2 (Multi Polygon)
5: bs_2240009_1 (Multi Line String)
6: bs_2310009_1 (Multi Line String)
% ogrinfo -ro -so -q ca_bs.sqlite | cut -d ' ' -f 2
bs_1250009_0
bs_2060009_0
bs_2010009_2
bs_2380009_2
bs_2240009_1
bs_2310009_1
So this should get pretty close with the sample you provided:
$ ogrinfo -ro -so -q test.kml | cut -d ':' -f2 | sed 's/^ //g'
Shapes
Set 1
Set 2
Best Answer
GeoTools will read GeoJSON and write KML quite easily: