I have a KML File and I need convert to GeoJSON. I am using Geotools in Java for convert KML because GeoServer has problems to import KML. I can convert the files but GeoServer don't accept GeoJSON converted. When I import GeoJSON converted online, there are no problems.
Code in Java
I used this code Convert Kml with multiple features to Geojson
FileInputStream reader = new FileInputStream(fileTmp.getAbsolutePath());
PullParser parser = new PullParser(new KMLConfiguration(), reader, SimpleFeature.class);
FeatureJSON featureJSON = new FeatureJSON();
FileWriter fileWriter = new FileWriter(filename + ".geojson");
BufferedWriter writer = new BufferedWriter(fileWriter);
ArrayList<SimpleFeature> features = new ArrayList<>();
SimpleFeature simpleFeature = (SimpleFeature) parser.parse();
while (simpleFeature != null) {
LOG.info(simpleFeature);
features.add(simpleFeature);
simpleFeature = (SimpleFeature) parser.parse();
}
SimpleFeatureCollection featureCollectionUnreprojected = DataUtilities.collection(features);
featureJSON.writeFeatureCollection(featureCollectionUnreprojected, writer);
KML File
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>KML-Example</name>
<gx:CascadingStyle kml:id="__managed_style_1BBDE4D1D5209F676816">
<Style>
<IconStyle>
<Icon>
<href>https://earth.google.com/earth/rpc/cc/icon?color=1976d2&id=2000&scale=4</href>
</Icon>
<hotSpot x="64" y="128" xunits="pixels" yunits="insetPixels"/>
</IconStyle>
<LabelStyle>
</LabelStyle>
<LineStyle>
<color>ff2f2fd3</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>402f2fd3</color>
</PolyStyle>
<BalloonStyle>
<displayMode>hide</displayMode>
</BalloonStyle>
</Style>
</gx:CascadingStyle>
<gx:CascadingStyle kml:id="__managed_style_2EA205CEFB209F676816">
<Style>
<IconStyle>
<scale>1.2</scale>
<Icon>
<href>https://earth.google.com/earth/rpc/cc/icon?color=1976d2&id=2000&scale=4</href>
</Icon>
<hotSpot x="64" y="128" xunits="pixels" yunits="insetPixels"/>
</IconStyle>
<LabelStyle>
</LabelStyle>
<LineStyle>
<color>ff2f2fd3</color>
<width>6</width>
</LineStyle>
<PolyStyle>
<color>402f2fd3</color>
</PolyStyle>
<BalloonStyle>
<displayMode>hide</displayMode>
</BalloonStyle>
</Style>
</gx:CascadingStyle>
<StyleMap id="__managed_style_06849BAC89209F676816">
<Pair>
<key>normal</key>
<styleUrl>#__managed_style_1BBDE4D1D5209F676816</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#__managed_style_2EA205CEFB209F676816</styleUrl>
</Pair>
</StyleMap>
<Placemark id="0C1FAD446F209F671F2B">
<name>Teste</name>
<LookAt>
<longitude>-8.071672824072024</longitude>
<latitude>40.94572565095269</latitude>
<altitude>554.9936597887551</altitude>
<heading>0</heading>
<tilt>0</tilt>
<gx:fovy>35</gx:fovy>
<range>469307.9708133639</range>
<altitudeMode>absolute</altitudeMode>
</LookAt>
<styleUrl>#__managed_style_06849BAC89209F676816</styleUrl>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-80.32039884287052,25.79595095231043,-440.4448477037203 -40.16540389678969,-7.859693178090486,-3537.052188699258 -7.711086121502985,41.24065720616411,-5036.3287754869 -80.32039884287052,25.79595095231043,-440.4448477037203
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>
Expected
{
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"id":"0C1FAD446F209F671F2B",
"geometry":{
"type":"Polygon",
"coordinates":[
[
[
-80.32039884287052,
25.79595095231043,
-440.4448477037203
],
[
-40.16540389678969,
-7.859693178090486,
-3537.052188699258
],
[
-7.711086121502985,
41.24065720616411,
-5036.3287754869
],
[
-80.32039884287052,
25.79595095231043,
-440.4448477037203
]
]
]
},
"properties":{
"name":"Teste"
}
}
]
}
My result
{
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"geometry":{
"type":"Polygon",
"coordinates":[
[
[
-80.3204,
25.796,
-440.4448
],
[
-40.1654,
-7.8597,
-3537.0522
],
[
-7.7111,
41.2407,
-5036.3288
],
[
-80.3204,
25.796,
-440.4448
]
]
]
},
"properties":{
"name":"Teste",
"visibility":true,
"open":true,
"LookAt":{
"type":"Point",
"coordinates":[
-8.0717,
40.9457,
554.9937
]
}
}
},
{
"type":"Feature",
"properties":{
"name":"KML-Example",
"visibility":true,
"open":true
}
}
]
}