2013-03-26 61 views
5

Tôi đang cố gắng phân tích cú pháp tệp Kml trong Java. Nguyên nhân tôi cần phải thực hiện tọa độ của Dấu vị trí, để tạo một poligon trong java và sử dụng nó.Trích xuất tọa độ từ tệp KML trong Java

Nhưng vấn đề của tôi là tôi đang sử dụng JAK thư viện này để phân tích cú pháp và tôi không thể trích xuất thông tin mà tôi muốn (tôi đọc "trợ giúp" trong trang chính thức, nhưng tôi đã không 't tìm thấy bất kỳ sự giúp đỡ giáp vấn đề của tôi)

tôi đang cố gắng để làm một cái gì đó như thế:

final Kml kml = Kml.unmarshal(new File("C:/Users/A556520/Documents/Proyectos/GeoFencing/res/labasa.kml")); 
final Document document = (Document)kml.getFeature();  
List<Feature> listafeatures = document.getFeature();   

Nhưng trong thời điểm này tôi không biết làm thế nào để trích xuất các tọa độ.

File Tôi đang cố gắng để phân tích này là một: la basa

Trả lời

9

Tiếp theo javadocs (unofficial), bạn cần phải kiểm tra - sử dụng instanceof - mỗi Feature dù là là một Placemark, nếu có dàn diễn viên với nó và nhận được các Geometry mà chính nó cần phải được kiểm tra cho dù đó là một Polygon, nếu có thì đúc nó. Sau đó đường dẫn đến tọa độ được như sau (chỉ vì nó đi kèm trong kml-file):

getOuterBoundaryIs > getlinearRing > getCoordinates 

Sau đây là cách nó trông giống như trong mã:

@Test 
public void parseKml() { 
    String src = "misctests/stackoverflow/kml/labasa.kml"; 
    InputStream is = getClass().getClassLoader().getResourceAsStream(src); 
    Assert.assertNotNull(is); 
    Kml kml = Kml.unmarshal(is); 
    Feature feature = kml.getFeature(); 
    parseFeature(feature); 
} 

private void parseFeature(Feature feature) { 
    if(feature != null) { 
     if(feature instanceof Document) { 
      Document document = (Document) feature; 
      List<Feature> featureList = document.getFeature(); 
      for(Feature documentFeature : featureList) { 
       if(documentFeature instanceof Placemark) { 
        Placemark placemark = (Placemark) documentFeature; 
        Geometry geometry = placemark.getGeometry(); 
        parseGeometry(geometry); 
       } 
      } 
     } 
    } 
} 

private void parseGeometry(Geometry geometry) { 
    if(geometry != null) { 
     if(geometry instanceof Polygon) { 
      Polygon polygon = (Polygon) geometry; 
      Boundary outerBoundaryIs = polygon.getOuterBoundaryIs(); 
      if(outerBoundaryIs != null) { 
       LinearRing linearRing = outerBoundaryIs.getLinearRing(); 
       if(linearRing != null) { 
        List<Coordinate> coordinates = linearRing.getCoordinates(); 
        if(coordinates != null) { 
         for(Coordinate coordinate : coordinates) { 
          parseCoordinate(coordinate); 
         } 
        } 
       } 
      } 
     } 
    } 
} 

private void parseCoordinate(Coordinate coordinate) { 
    if(coordinate != null) { 
     System.out.println("Longitude: " + coordinate.getLongitude()); 
     System.out.println("Latitude : " + coordinate.getLatitude()); 
     System.out.println("Altitude : " + coordinate.getAltitude()); 
     System.out.println(""); 
    } 
} 
+1

Tuyệt vời! nó hoạt động hoàn hảo! Tôi không thấy Javadocs,: S (xin lỗi!) Nhưng tôi tải xuống và tôi sẽ đọc nó. Tôi là "mới" trong Java, và có những điều mà tôi không hiểu chút nào, nhưng, tôi sẽ đọc các jdocs này, để hiểu được api;) Và một lần nữa ...... CẢM ƠN! !!! – Shudy

+0

Bạn được chào đón! Tôi phải nói rằng đây là một API lạ vì bạn phải tạo một 'Feature' để xem nó thực sự là gì,' instanceof' thực sự là bas practice trong 'OOP', tôi đoán những người đó biết họ phải đối phó với cái gì khi họ muốn tạo một API cho 'kml' và lược đồ kml' xml' buộc chúng thực hiện theo cách này. Một trợ giúp tốt cho bạn, sẽ xem xét một tính năng trong trình gỡ lỗi để xem những gì bên trong nó. Ngoài ra bạn phải lưu ý rằng có thể có các không gian tên xml khác được bao gồm trong một tài liệu kml như 'gs',' xal' và vân vân ... Chúng cũng được ghi lại trong javadoc. – A4L

1

Came qua bài này, do đó, đây là một phần của mã hàm tôi đã sử dụng trong ứng dụng của mình để trích xuất Tên điểm đánh dấu là & tọa độ từ một chuỗi kmlText.

if (kmlText != null & kmlText.length() > 0) { 
    // Change case of relevant tags to match our search string case 
    kmlText = kmlText.replaceAll("(?i)<Placemark>", "<Placemark>") 
     .replaceAll("(?i)</Placemark>", "</Placemark>") 
     .replaceAll("(?i)<name>", "<name>") 
     .replaceAll("(?i)</name>", "</name>") 
     .replaceAll("(?i)<coordinates>", "<coordinates>") 
     .replaceAll("(?i)</coordinates>", "</coordinates>"); 
    // Get <Placemark> tag 
    String[] kmlPlacemarks = kmlText.split("</Placemark>"); 
    if (kmlPlacemarks.length > 0) { 
     for (Integer i = 0; i < kmlPlacemarks.length; i++) { 
      // Add '</Placemark>' to the end - actually not necessary 
      kmlPlacemarks[i] += "</Placemark>"; 
      if (kmlPlacemarks[i].indexOf("<Placemark>") > -1) 
       /* Trim front to start from '<Placemark>' 
       Otherwise additional tags may be in between leading 
       to parsing of incorrect values especially Name */ 
       kmlPlacemarks[i] = kmlPlacemarks[i].substring(kmlPlacemarks[i].indexOf("<Placemark>")); 
     } 
     String tmpPlacemarkName; 
     String tmpPlacemarkCoordinates; 
     for (String kmlPlacemark: kmlPlacemarks) 
      if ((kmlPlacemark.indexOf("<name>") > -1 && kmlPlacemark.indexOf("</name>") > -1) && 
        (kmlPlacemark.indexOf("<coordinates>") > -1 && kmlPlacemark.indexOf("</coordinates>") > -1)) { 
       tmpPlacemarkCoordinates = kmlPlacemark.substring(kmlPlacemark.indexOf("<coordinates>") + 13, kmlPlacemark.indexOf("</coordinates>")); 
       tmpPlacemarkName = kmlPlacemark.substring(kmlPlacemark.indexOf("<name>") + 6, kmlPlacemark.indexOf("</name>")); 
      } 
     } 
} 
0

Cảm ơn @ A4L Đây thực sự là một bản cập nhật và là một cách groovier hơn để làm điều tương tự Vì vậy, thay đổi cho Groovy và cũng cố gắng loại hơn ví dụ đưa ra cũng đào sâu bên trong một lớp tài liệu:

/** 
    * This starts the process and reads in the uk file 
    */ 
    public static void parseKml() { 
     def src = ServletContextHolder.servletContext.getRealPath("/KML/doc.kml") 
     InputStream is = new FileInputStream(src); 
     Kml kml = Kml.unmarshal(is); 
     Feature feature = kml.getFeature(); 
     parseFeature(feature); 
    } 

    /** 
    * This is step 2 of the process it figures out if it has a direct placemark mapping on kml 
    * or if this is part of some big folder structure 
    * @param feature 
    */ 
    public static void parseFeature(Feature feature) { 
     if(feature) { 
      if(feature instanceof Document) { 
       feature?.feature?.each { documentFeature-> 
        if(documentFeature instanceof Placemark) { 
         getPlacemark((Placemark) documentFeature) 
        } else if (documentFeature instanceof Folder) { 
         getFeatureList(documentFeature.feature) 
        } 
       } 
      } 
     } 
    } 


    /** 
    * This iterates over itself over and over again to gain access to placemarks within folders 
    * The uk map boundary was nested folders within folders 
    * @param features 
    * @return 
    */ 
    public static List<Feature> getFeatureList(List<Feature> features) { 
     features?.each { Feature f -> 
      if (f instanceof Folder) { 
       getFeatureList(f.getFeature()) 
      } else if (f instanceof Placemark) { 
       getPlacemark((Placemark) f) 
      } 
     } 
    } 

    /** 
    * This in short kicks off looking at a placemark it's name then parsing through each of its geometry points 
    * This controls the listener content or should I say builds it up from within this helper 
    * @param placemark 
    */ 
    public static void getPlacemark(Placemark placemark) { 
     Geometry geometry = placemark.getGeometry() 
     List results = parseGeometry(geometry) 
     GeoMapListener.update(placemark.name, results) 
    } 


    private static List parseGeometry(Geometry geometry) { 
     List results=[] 
     if(geometry != null) { 
      if(geometry instanceof Polygon) { 
       Polygon polygon = (Polygon) geometry; 
       Boundary outerBoundaryIs = polygon.getOuterBoundaryIs(); 
       if(outerBoundaryIs != null) { 
        LinearRing linearRing = outerBoundaryIs.getLinearRing(); 
        if(linearRing != null) { 
         List<Coordinate> coordinates = linearRing.getCoordinates(); 
         if(coordinates != null) { 
          for(Coordinate coordinate : coordinates) { 
           results << parseCoordinate(coordinate); 
          } 
         } 
        } 
       } 
      } else if (geometry instanceof LineString) { 
       LineString lineString = (LineString) geometry; 
       List<Coordinate> coordinates = lineString.getCoordinates(); 
       if (coordinates != null) { 
        for (Coordinate coordinate : coordinates) { 
         results << parseCoordinate(coordinate); 
        } 
       } 
      } 
     } 
     return results 
    } 

    private static Map parseCoordinate(Coordinate coordinate) { 
     Map results=[:] 
     if(coordinate) { 
      results.longitude= coordinate.longitude 
      results.latitude= coordinate.latitude 
      results.altitude= coordinate.altitude 
     } 
     return results 
    } 
Các vấn đề liên quan