Commit fd7d4d7c authored by sunyuhua's avatar sunyuhua

部分kml文件导入不成功

parent a7f0e7a2
...@@ -14,9 +14,15 @@ import java.util.HashMap; ...@@ -14,9 +14,15 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* KML格式的经纬度的转化方案
*/
public class KmlParserUtil { public class KmlParserUtil {
public static String parseKml(InputStream input) { public static String parseKml(InputStream input) {
Route route = new Route(); Route route = new Route();
SAXReader reader = new SAXReader(); SAXReader reader = new SAXReader();
...@@ -36,7 +42,14 @@ public class KmlParserUtil { ...@@ -36,7 +42,14 @@ public class KmlParserUtil {
placeMark = doc.selectNodes("//default:Folder/default:Placemark"); placeMark = doc.selectNodes("//default:Folder/default:Placemark");
for (Node node : selectNodes) { for (Node node : selectNodes) {
String lineString = node.getText(); String lineString = node.getText();
String[] array = lineString.split("\n"); Pattern CRLF = Pattern.compile("(\r\n|\r|\n|\n\r)");
Matcher m = CRLF.matcher(lineString);
String[] array=null;
if (m.find()) {
array = lineString.split("\n");
}else{
array = lineString.split(" ");
}
TracePoint tracePoint; TracePoint tracePoint;
for (String string : array) { for (String string : array) {
if (string.trim().length() == 0) { if (string.trim().length() == 0) {
...@@ -46,12 +59,13 @@ public class KmlParserUtil { ...@@ -46,12 +59,13 @@ public class KmlParserUtil {
String[] location = string.split(","); String[] location = string.split(",");
tracePoint.setLongitude(Double.parseDouble(location[0].trim())); tracePoint.setLongitude(Double.parseDouble(location[0].trim()));
tracePoint.setLatitude(Double.parseDouble(location[1].trim())); tracePoint.setLatitude(Double.parseDouble(location[1].trim()));
if (location.length >= 3) { if (location.length >= 3 && "0.0".equals(location[2].trim())) {
tracePoint.setAltitude(Float.parseFloat(location[2].trim())); tracePoint.setAltitude(Float.parseFloat(location[2].trim()));
} else { } else {
tracePoint.setAltitude(0); tracePoint.setAltitude(0);
} }
route.getTracePoint().add(tracePoint); route.getTracePoint().add(tracePoint);
} }
} }
} else { } else {
...@@ -60,7 +74,14 @@ public class KmlParserUtil { ...@@ -60,7 +74,14 @@ public class KmlParserUtil {
placeMark= doc.selectNodes("//default:Folder/default:Placemark"); placeMark= doc.selectNodes("//default:Folder/default:Placemark");
for (Node node : nodeList) { for (Node node : nodeList) {
String lineString = node.getText(); String lineString = node.getText();
String[] array = lineString.split(" "); Pattern CRLF = Pattern.compile("(\r\n|\r|\n|\n\r)");
Matcher m = CRLF.matcher(lineString);
String[] array=null;
if (m.find()) {
array = lineString.split("\n");
}else{
array = lineString.split(" ");
}
TracePoint tracePoint; TracePoint tracePoint;
if (array.length == 0) { if (array.length == 0) {
continue; continue;
...@@ -68,7 +89,7 @@ public class KmlParserUtil { ...@@ -68,7 +89,7 @@ public class KmlParserUtil {
tracePoint = new TracePoint(); tracePoint = new TracePoint();
tracePoint.setLongitude(Double.parseDouble(array[0].trim())); tracePoint.setLongitude(Double.parseDouble(array[0].trim()));
tracePoint.setLatitude(Double.parseDouble(array[1].trim())); tracePoint.setLatitude(Double.parseDouble(array[1].trim()));
if (array.length >= 3) { if (array.length >= 3 && "0.0".equals(array[2].trim())) {
tracePoint.setAltitude(Float.parseFloat(array[2].trim())); tracePoint.setAltitude(Float.parseFloat(array[2].trim()));
} else { } else {
tracePoint.setAltitude(0); tracePoint.setAltitude(0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment