将具有多个特征的 Kml 转换为 Geojson
Convert Kml with multiple features to Geojson
我使用此代码将具有单一特征的 kml 文件转换为 GeoJson 文件。
String kmlToGeoJson(String fileName)
throws IOException, ParserConfigurationException, SAXException, XMLStreamException {
FileInputStream reader = new FileInputStream(fileName);
PullParser parser = new PullParser(new KMLConfiguration(),reader, SimpleFeature.class);
FeatureJSON fjson = new FeatureJSON();
FileWriter tmp = new FileWriter(fileName + ".geojson");
BufferedWriter writer = new BufferedWriter(tmp);
SimpleFeature simpleFeature = (SimpleFeature) parser.parse();
while (simpleFeature != null) {
fjson.writeFeature(simpleFeature, writer);
simpleFeature = (SimpleFeature) parser.parse();
}
return "success";
}
但是,当我使用具有多项功能的 Kml 文件时:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>KmlFile</name>
<Style id="west_campus_style">
<IconStyle>
<Icon>
<href>https://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png
</href>
</Icon>
</IconStyle>
<BalloonStyle>
<text>$[video]</text>
</BalloonStyle>
</Style>
<Placemark>
<name>Google West Campus 1</name>
<styleUrl>#west_campus_style</styleUrl>
<ExtendedData>
<Data name="video">
<value><![CDATA[<iframe width="480" height="360"
src="https://www.youtube.com/embed/ZE8ODPL2VPI" frameborder="0"
allowfullscreen></iframe><br><br>]]></value>
</Data>
</ExtendedData>
<Point>
<coordinates>-122.0914977709329,37.42390182131783,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Google West Campus 2</name>
<styleUrl>#west_campus_style</styleUrl>
<ExtendedData>
<Data name="video">
<value><![CDATA[<iframe width="480" height="360"
src="https://www.youtube.com/embed/nb4gvrNrDWw" frameborder="0"
allowfullscreen></iframe><br><br>]]></value>
</Data>
</ExtendedData>
<Point>
<coordinates>-122.0926995893311,37.42419403634421,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Google West Campus 3</name>
<styleUrl>#west_campus_style</styleUrl>
<ExtendedData>
<Data name="video">
<value><![CDATA[<iframe width="480" height="360"
src="https://www.youtube.com/embed/0hhiEjf7_NA" frameborder="0"
allowfullscreen></iframe><br><br>]]></value>
</Data>
</ExtendedData>
<Point>
<coordinates>-122.0922532985281,37.42301710721216,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
我得到了这个 Geojson 文件:
{ "type":"Feature",
"geometry":{
"type":"Point",
"coordinates":[-122.0915,37.4239,0.0]},
"properties":{"name":"Google West Campus 1",
"visibility":true,
"open":true,
"Style":"FeatureTypeStyleImpl[ name=name, [], rules=<1> (<RuleImpl> null\n)]"},
"id":"fid--579a589e_150ad9a2e4f_-8000"
}
{"type":"Feature",
"geometry":{
"type":"Point",
"coordinates":[-122.0927,37.4242,0.0]},
"properties":{
"name":"Google West Campus 2",
"visibility":true,
"open":true,
"Style":"FeatureTypeStyleImpl[ name=name, [], rules=<1>(<RuleImpl> null\n)]"},
"id":"fid--579a589e_150ad9a2e4f_-7fff"
}
{"type":"Feature",
"geometry":{
"type":"Point",
"coordinates":[-122.0923,37.423,0.0]},
"properties":{"name":"Google West Campus 3",
"visibility":true,
"open":true,
"Style":"FeatureTypeStyleImpl[ name=name, [], rules=<1>(<RuleImpl> null\n)]"},
"id":"fid--579a589e_150ad9a2e4f_-7ffe"}
{"type":"Feature",
"properties":
{"name":"KmlFile",
"visibility":true,
"open":true,
"name":"KmlFile",
"Feature":"[]"},
"id":"fid--579a589e_150ad9a2e4f_-7ffd"}
这就像整个 kml 文件被认为是一个单一的特征,特征之间没有逗号。
我如何才能将整个文件视为一个多特征文件
我尝试用 SimpleFeatureCollection.class
替换 SimpleFeature.class
,但是返回的 Geojson 文件是空的。
我认为您需要这样的东西来生成 FeatureCollection:
FileInputStream reader = new FileInputStream(args[0]);
PullParser parser = new PullParser(new KMLConfiguration(), reader, SimpleFeature.class);
FeatureJSON fjson = new FeatureJSON();
FileWriter tmp = new FileWriter(args[0] + ".geojson");
BufferedWriter writer = new BufferedWriter(tmp);
ArrayList<SimpleFeature> features = new ArrayList<>();
SimpleFeature simpleFeature = (SimpleFeature) parser.parse();
while (simpleFeature != null) {
System.out.println(simpleFeature);
features.add(simpleFeature);
simpleFeature = (SimpleFeature) parser.parse();
}
SimpleFeatureCollection fc = DataUtilities.collection(features);
fjson.writeFeatureCollection(fc, System.out);
我使用此代码将具有单一特征的 kml 文件转换为 GeoJson 文件。
String kmlToGeoJson(String fileName)
throws IOException, ParserConfigurationException, SAXException, XMLStreamException {
FileInputStream reader = new FileInputStream(fileName);
PullParser parser = new PullParser(new KMLConfiguration(),reader, SimpleFeature.class);
FeatureJSON fjson = new FeatureJSON();
FileWriter tmp = new FileWriter(fileName + ".geojson");
BufferedWriter writer = new BufferedWriter(tmp);
SimpleFeature simpleFeature = (SimpleFeature) parser.parse();
while (simpleFeature != null) {
fjson.writeFeature(simpleFeature, writer);
simpleFeature = (SimpleFeature) parser.parse();
}
return "success";
}
但是,当我使用具有多项功能的 Kml 文件时:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>KmlFile</name>
<Style id="west_campus_style">
<IconStyle>
<Icon>
<href>https://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png
</href>
</Icon>
</IconStyle>
<BalloonStyle>
<text>$[video]</text>
</BalloonStyle>
</Style>
<Placemark>
<name>Google West Campus 1</name>
<styleUrl>#west_campus_style</styleUrl>
<ExtendedData>
<Data name="video">
<value><![CDATA[<iframe width="480" height="360"
src="https://www.youtube.com/embed/ZE8ODPL2VPI" frameborder="0"
allowfullscreen></iframe><br><br>]]></value>
</Data>
</ExtendedData>
<Point>
<coordinates>-122.0914977709329,37.42390182131783,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Google West Campus 2</name>
<styleUrl>#west_campus_style</styleUrl>
<ExtendedData>
<Data name="video">
<value><![CDATA[<iframe width="480" height="360"
src="https://www.youtube.com/embed/nb4gvrNrDWw" frameborder="0"
allowfullscreen></iframe><br><br>]]></value>
</Data>
</ExtendedData>
<Point>
<coordinates>-122.0926995893311,37.42419403634421,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Google West Campus 3</name>
<styleUrl>#west_campus_style</styleUrl>
<ExtendedData>
<Data name="video">
<value><![CDATA[<iframe width="480" height="360"
src="https://www.youtube.com/embed/0hhiEjf7_NA" frameborder="0"
allowfullscreen></iframe><br><br>]]></value>
</Data>
</ExtendedData>
<Point>
<coordinates>-122.0922532985281,37.42301710721216,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
我得到了这个 Geojson 文件:
{ "type":"Feature",
"geometry":{
"type":"Point",
"coordinates":[-122.0915,37.4239,0.0]},
"properties":{"name":"Google West Campus 1",
"visibility":true,
"open":true,
"Style":"FeatureTypeStyleImpl[ name=name, [], rules=<1> (<RuleImpl> null\n)]"},
"id":"fid--579a589e_150ad9a2e4f_-8000"
}
{"type":"Feature",
"geometry":{
"type":"Point",
"coordinates":[-122.0927,37.4242,0.0]},
"properties":{
"name":"Google West Campus 2",
"visibility":true,
"open":true,
"Style":"FeatureTypeStyleImpl[ name=name, [], rules=<1>(<RuleImpl> null\n)]"},
"id":"fid--579a589e_150ad9a2e4f_-7fff"
}
{"type":"Feature",
"geometry":{
"type":"Point",
"coordinates":[-122.0923,37.423,0.0]},
"properties":{"name":"Google West Campus 3",
"visibility":true,
"open":true,
"Style":"FeatureTypeStyleImpl[ name=name, [], rules=<1>(<RuleImpl> null\n)]"},
"id":"fid--579a589e_150ad9a2e4f_-7ffe"}
{"type":"Feature",
"properties":
{"name":"KmlFile",
"visibility":true,
"open":true,
"name":"KmlFile",
"Feature":"[]"},
"id":"fid--579a589e_150ad9a2e4f_-7ffd"}
这就像整个 kml 文件被认为是一个单一的特征,特征之间没有逗号。
我如何才能将整个文件视为一个多特征文件
我尝试用 SimpleFeatureCollection.class
替换 SimpleFeature.class
,但是返回的 Geojson 文件是空的。
我认为您需要这样的东西来生成 FeatureCollection:
FileInputStream reader = new FileInputStream(args[0]);
PullParser parser = new PullParser(new KMLConfiguration(), reader, SimpleFeature.class);
FeatureJSON fjson = new FeatureJSON();
FileWriter tmp = new FileWriter(args[0] + ".geojson");
BufferedWriter writer = new BufferedWriter(tmp);
ArrayList<SimpleFeature> features = new ArrayList<>();
SimpleFeature simpleFeature = (SimpleFeature) parser.parse();
while (simpleFeature != null) {
System.out.println(simpleFeature);
features.add(simpleFeature);
simpleFeature = (SimpleFeature) parser.parse();
}
SimpleFeatureCollection fc = DataUtilities.collection(features);
fjson.writeFeatureCollection(fc, System.out);