如何使用Java中的Geotools将KML转换为Geojson(Geoserver不接受我的转换结果)
我有一个kml文件,需要转换为geojson。 我正在使用Java中的Geotools进行转换KML,因为GeoServer有问题进口KML。我可以转换文件,但GeoServer不接受Geojson转换。 当我导入Geojson在线转换时,没有问题。
java中的代码
我使用了此代码 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文件
<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>
预期
{
"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"
}
}
]
}
我的结果
{
"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
}
}
]
}
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
}
}
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自从最后一个答案以来,事情已经继续前进,首选的方法是使用
datastore
s:但是,kmldatastore仅在一个功能中读取
击中流的末端。我无法检查您的KML是否有效,因为所有KML验证器都已消失。
您的问题是
kmldatastore
不支持,因此您可以提出针对它的问题,但毫无疑问,它何时将其修复。Things have moved on since that last answer, the prefered way to do this is using
DataStore
s:However the KMLDatastore only reads in one feature before
hitting the end of the stream. I can't check if your KML is valid as all the KML validators have gone.
Your problem is that the
KMLDataStore
is unsupported so while you can raise an issue against it there is no saying when it would be fixed.