|
@@ -0,0 +1,105 @@
|
|
|
+package com.fdkankan.fusion.common.util;
|
|
|
+
|
|
|
+import com.fdkankan.geo.GeoTransformUtil;
|
|
|
+import org.locationtech.proj4j.ProjCoordinate;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.w3c.dom.Document;
|
|
|
+import org.w3c.dom.Element;
|
|
|
+import org.w3c.dom.NodeList;
|
|
|
+
|
|
|
+import javax.xml.parsers.DocumentBuilder;
|
|
|
+import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
+import java.io.File;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class ReadXmlUtil {
|
|
|
+
|
|
|
+ public static Logger logger = LoggerFactory.getLogger(ReadXmlUtil.class);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <?xml version="1.0" encoding="utf-8"?>
|
|
|
+ * <ModelMetadata version="1">
|
|
|
+ * <SRS>EPSG:32649</SRS>
|
|
|
+ * <SRSOrigin>767052,2475786,0</SRSOrigin>
|
|
|
+ * </ModelMetadata>
|
|
|
+ */
|
|
|
+
|
|
|
+ public static HashMap<String,String> getMapByXml(String path) {
|
|
|
+ try {
|
|
|
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
+ DocumentBuilder documentBuilder = factory.newDocumentBuilder();
|
|
|
+ Document document = documentBuilder.parse(new File(path));
|
|
|
+ Element root = document.getDocumentElement();
|
|
|
+ NodeList childNodes = root.getChildNodes();
|
|
|
+
|
|
|
+ HashMap<String,String> map = new HashMap<>();
|
|
|
+ for (int temp = 0; temp < childNodes.getLength(); temp++) {
|
|
|
+ org.w3c.dom.Node node = childNodes.item(temp);
|
|
|
+ if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
|
|
|
+ Element eElement = (Element) node;
|
|
|
+ // 获取元素中的文本内容
|
|
|
+ map.put(node.getNodeName(),eElement.getTextContent());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }catch (Exception e){
|
|
|
+ logger.info("getMapByXml-error",e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static HashMap<String, String> getLatMap(File file){
|
|
|
+ try {
|
|
|
+ File xmlPath = getXmlPath(file);
|
|
|
+ if(xmlPath == null){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ HashMap<String, String> resultMap = new HashMap<>();
|
|
|
+ HashMap<String, String> mapByXml = getMapByXml(xmlPath.getPath());
|
|
|
+ if(mapByXml != null && !mapByXml.isEmpty()){
|
|
|
+ String srs = mapByXml.get("SRS");
|
|
|
+ String sourceCrs = srs.replace("EPSG:","");
|
|
|
+ String srsOrigin = mapByXml.get("SRSOrigin");
|
|
|
+ String[] split = srsOrigin.split(",");
|
|
|
+ Double x = Double.valueOf(split[0]);
|
|
|
+ Double y = Double.valueOf(split[1]);
|
|
|
+ Double z = Double.valueOf(split[2]);
|
|
|
+
|
|
|
+ ProjCoordinate coordinate1 = new ProjCoordinate(x,y,z);
|
|
|
+ Double[] wgs84 = GeoTransformUtil.getWgs84(coordinate1, sourceCrs);
|
|
|
+ resultMap.put("wgs84",wgs84[0]+","+wgs84[1]+","+wgs84[2]);
|
|
|
+ ProjCoordinate coordinate2 = new ProjCoordinate(wgs84[0],wgs84[1],wgs84[2]);
|
|
|
+ Double[] gcj02 = GeoTransformUtil.wgs84ToGcj02(coordinate2);
|
|
|
+ resultMap.put("gcj02",+gcj02[0]+","+gcj02[1]+","+gcj02[2]);
|
|
|
+ }
|
|
|
+ return resultMap;
|
|
|
+ }catch (Exception e){
|
|
|
+ logger.info("getLatMap-error",e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static File getXmlPath(File localFile) {
|
|
|
+ try {
|
|
|
+ if(localFile == null || localFile.isFile()){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ File[] files = localFile.listFiles();
|
|
|
+ if(files == null || files.length == 0){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ for (File file : files) {
|
|
|
+ if(file.getName().contains(".xml")){
|
|
|
+ return file;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ logger.info("getXmlPath-error",e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|