How can i extract coordinates from a kml file using Java












-1















I have a KML file https://files.fm/u/nbwf3trv and i need to fetch all coordinates and add it to a list.



Example: Required coordinates from KML file.
11.651548147201538,48.249088525772095,0 9.20654296875,50.00086069107056,0 7.010795159396929,51.45182674763414,0".



Please suggest.
Thanks.










share|improve this question





























    -1















    I have a KML file https://files.fm/u/nbwf3trv and i need to fetch all coordinates and add it to a list.



    Example: Required coordinates from KML file.
    11.651548147201538,48.249088525772095,0 9.20654296875,50.00086069107056,0 7.010795159396929,51.45182674763414,0".



    Please suggest.
    Thanks.










    share|improve this question



























      -1












      -1








      -1








      I have a KML file https://files.fm/u/nbwf3trv and i need to fetch all coordinates and add it to a list.



      Example: Required coordinates from KML file.
      11.651548147201538,48.249088525772095,0 9.20654296875,50.00086069107056,0 7.010795159396929,51.45182674763414,0".



      Please suggest.
      Thanks.










      share|improve this question
















      I have a KML file https://files.fm/u/nbwf3trv and i need to fetch all coordinates and add it to a list.



      Example: Required coordinates from KML file.
      11.651548147201538,48.249088525772095,0 9.20654296875,50.00086069107056,0 7.010795159396929,51.45182674763414,0".



      Please suggest.
      Thanks.







      java automation maps kml google-earth






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 6:52







      Nikhil Vernekar

















      asked Nov 20 '18 at 6:15









      Nikhil VernekarNikhil Vernekar

      177




      177
























          1 Answer
          1






          active

          oldest

          votes


















          1














          JAK (Java API for KML) is a simple and powerful Java API to parse and/or create KML files from scratch.



          https://github.com/micromata/javaapiforkml



          Here is a snippet of Java code to extract the coordinates from a KML file. In this case it's a placemark and a polygon geometry. If the KML file had multiple placemarks then you would iterate over all the placemarks.



          JAXBContext jc = JAXBContext.newInstance(Kml.class);

          // create KML reader to parse arbitrary KML into Java Object structure
          Unmarshaller u = jc.createUnmarshaller();
          Kml kml = (Kml) u.unmarshal(new File("test.kml"));

          Placemark placemark = (Placemark) kml.getFeature();
          Polygon geom = (Polygon) placemark.getGeometry();
          LinearRing linearRing = geom.getOuterBoundaryIs().getLinearRing();
          List<Coordinate> coordinates = linearRing.getCoordinates();
          for (Coordinate coordinate : coordinates) {
          System.out.println(coordinate.getLongitude());
          System.out.println(coordinate.getLatitude());
          System.out.println(coordinate.getAltitude());
          }


          If the first feature in the KML is a Folder then you can cast a Folder to the object then iterate over the features.



          Folder doc = (Folder) kml.getFeature();
          List<Feature> features = doc.getFeature();
          for(Feature f : features) {
          ...
          }





          share|improve this answer


























          • Thanks for the help. I tried the code. But it says "java.lang.ClassCastException: de.micromata.opengis.kml.v_2_2_0.Folder cannot be cast to de.micromata.opengis.kml.v_2_2_0.Placemark".

            – Nikhil Vernekar
            Nov 21 '18 at 10:57













          • And also i want the coordinates from <Placemark id="1.5.2"> . Thanks in advance.

            – Nikhil Vernekar
            Nov 21 '18 at 10:58






          • 1





            If the first element in KML is a Folder (see update in answer with Folder) then change the casting to a Folder element and iterate over its children. Likewise your placemarks may have different geometries so will need to test for geometry type if you have mixed types. Best to download the source and generate javadoc from it to view the API calls.

            – JasonM1
            Nov 22 '18 at 1:26













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387255%2fhow-can-i-extract-coordinates-from-a-kml-file-using-java%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          JAK (Java API for KML) is a simple and powerful Java API to parse and/or create KML files from scratch.



          https://github.com/micromata/javaapiforkml



          Here is a snippet of Java code to extract the coordinates from a KML file. In this case it's a placemark and a polygon geometry. If the KML file had multiple placemarks then you would iterate over all the placemarks.



          JAXBContext jc = JAXBContext.newInstance(Kml.class);

          // create KML reader to parse arbitrary KML into Java Object structure
          Unmarshaller u = jc.createUnmarshaller();
          Kml kml = (Kml) u.unmarshal(new File("test.kml"));

          Placemark placemark = (Placemark) kml.getFeature();
          Polygon geom = (Polygon) placemark.getGeometry();
          LinearRing linearRing = geom.getOuterBoundaryIs().getLinearRing();
          List<Coordinate> coordinates = linearRing.getCoordinates();
          for (Coordinate coordinate : coordinates) {
          System.out.println(coordinate.getLongitude());
          System.out.println(coordinate.getLatitude());
          System.out.println(coordinate.getAltitude());
          }


          If the first feature in the KML is a Folder then you can cast a Folder to the object then iterate over the features.



          Folder doc = (Folder) kml.getFeature();
          List<Feature> features = doc.getFeature();
          for(Feature f : features) {
          ...
          }





          share|improve this answer


























          • Thanks for the help. I tried the code. But it says "java.lang.ClassCastException: de.micromata.opengis.kml.v_2_2_0.Folder cannot be cast to de.micromata.opengis.kml.v_2_2_0.Placemark".

            – Nikhil Vernekar
            Nov 21 '18 at 10:57













          • And also i want the coordinates from <Placemark id="1.5.2"> . Thanks in advance.

            – Nikhil Vernekar
            Nov 21 '18 at 10:58






          • 1





            If the first element in KML is a Folder (see update in answer with Folder) then change the casting to a Folder element and iterate over its children. Likewise your placemarks may have different geometries so will need to test for geometry type if you have mixed types. Best to download the source and generate javadoc from it to view the API calls.

            – JasonM1
            Nov 22 '18 at 1:26


















          1














          JAK (Java API for KML) is a simple and powerful Java API to parse and/or create KML files from scratch.



          https://github.com/micromata/javaapiforkml



          Here is a snippet of Java code to extract the coordinates from a KML file. In this case it's a placemark and a polygon geometry. If the KML file had multiple placemarks then you would iterate over all the placemarks.



          JAXBContext jc = JAXBContext.newInstance(Kml.class);

          // create KML reader to parse arbitrary KML into Java Object structure
          Unmarshaller u = jc.createUnmarshaller();
          Kml kml = (Kml) u.unmarshal(new File("test.kml"));

          Placemark placemark = (Placemark) kml.getFeature();
          Polygon geom = (Polygon) placemark.getGeometry();
          LinearRing linearRing = geom.getOuterBoundaryIs().getLinearRing();
          List<Coordinate> coordinates = linearRing.getCoordinates();
          for (Coordinate coordinate : coordinates) {
          System.out.println(coordinate.getLongitude());
          System.out.println(coordinate.getLatitude());
          System.out.println(coordinate.getAltitude());
          }


          If the first feature in the KML is a Folder then you can cast a Folder to the object then iterate over the features.



          Folder doc = (Folder) kml.getFeature();
          List<Feature> features = doc.getFeature();
          for(Feature f : features) {
          ...
          }





          share|improve this answer


























          • Thanks for the help. I tried the code. But it says "java.lang.ClassCastException: de.micromata.opengis.kml.v_2_2_0.Folder cannot be cast to de.micromata.opengis.kml.v_2_2_0.Placemark".

            – Nikhil Vernekar
            Nov 21 '18 at 10:57













          • And also i want the coordinates from <Placemark id="1.5.2"> . Thanks in advance.

            – Nikhil Vernekar
            Nov 21 '18 at 10:58






          • 1





            If the first element in KML is a Folder (see update in answer with Folder) then change the casting to a Folder element and iterate over its children. Likewise your placemarks may have different geometries so will need to test for geometry type if you have mixed types. Best to download the source and generate javadoc from it to view the API calls.

            – JasonM1
            Nov 22 '18 at 1:26
















          1












          1








          1







          JAK (Java API for KML) is a simple and powerful Java API to parse and/or create KML files from scratch.



          https://github.com/micromata/javaapiforkml



          Here is a snippet of Java code to extract the coordinates from a KML file. In this case it's a placemark and a polygon geometry. If the KML file had multiple placemarks then you would iterate over all the placemarks.



          JAXBContext jc = JAXBContext.newInstance(Kml.class);

          // create KML reader to parse arbitrary KML into Java Object structure
          Unmarshaller u = jc.createUnmarshaller();
          Kml kml = (Kml) u.unmarshal(new File("test.kml"));

          Placemark placemark = (Placemark) kml.getFeature();
          Polygon geom = (Polygon) placemark.getGeometry();
          LinearRing linearRing = geom.getOuterBoundaryIs().getLinearRing();
          List<Coordinate> coordinates = linearRing.getCoordinates();
          for (Coordinate coordinate : coordinates) {
          System.out.println(coordinate.getLongitude());
          System.out.println(coordinate.getLatitude());
          System.out.println(coordinate.getAltitude());
          }


          If the first feature in the KML is a Folder then you can cast a Folder to the object then iterate over the features.



          Folder doc = (Folder) kml.getFeature();
          List<Feature> features = doc.getFeature();
          for(Feature f : features) {
          ...
          }





          share|improve this answer















          JAK (Java API for KML) is a simple and powerful Java API to parse and/or create KML files from scratch.



          https://github.com/micromata/javaapiforkml



          Here is a snippet of Java code to extract the coordinates from a KML file. In this case it's a placemark and a polygon geometry. If the KML file had multiple placemarks then you would iterate over all the placemarks.



          JAXBContext jc = JAXBContext.newInstance(Kml.class);

          // create KML reader to parse arbitrary KML into Java Object structure
          Unmarshaller u = jc.createUnmarshaller();
          Kml kml = (Kml) u.unmarshal(new File("test.kml"));

          Placemark placemark = (Placemark) kml.getFeature();
          Polygon geom = (Polygon) placemark.getGeometry();
          LinearRing linearRing = geom.getOuterBoundaryIs().getLinearRing();
          List<Coordinate> coordinates = linearRing.getCoordinates();
          for (Coordinate coordinate : coordinates) {
          System.out.println(coordinate.getLongitude());
          System.out.println(coordinate.getLatitude());
          System.out.println(coordinate.getAltitude());
          }


          If the first feature in the KML is a Folder then you can cast a Folder to the object then iterate over the features.



          Folder doc = (Folder) kml.getFeature();
          List<Feature> features = doc.getFeature();
          for(Feature f : features) {
          ...
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 12:44

























          answered Nov 20 '18 at 19:48









          JasonM1JasonM1

          12.4k41949




          12.4k41949













          • Thanks for the help. I tried the code. But it says "java.lang.ClassCastException: de.micromata.opengis.kml.v_2_2_0.Folder cannot be cast to de.micromata.opengis.kml.v_2_2_0.Placemark".

            – Nikhil Vernekar
            Nov 21 '18 at 10:57













          • And also i want the coordinates from <Placemark id="1.5.2"> . Thanks in advance.

            – Nikhil Vernekar
            Nov 21 '18 at 10:58






          • 1





            If the first element in KML is a Folder (see update in answer with Folder) then change the casting to a Folder element and iterate over its children. Likewise your placemarks may have different geometries so will need to test for geometry type if you have mixed types. Best to download the source and generate javadoc from it to view the API calls.

            – JasonM1
            Nov 22 '18 at 1:26





















          • Thanks for the help. I tried the code. But it says "java.lang.ClassCastException: de.micromata.opengis.kml.v_2_2_0.Folder cannot be cast to de.micromata.opengis.kml.v_2_2_0.Placemark".

            – Nikhil Vernekar
            Nov 21 '18 at 10:57













          • And also i want the coordinates from <Placemark id="1.5.2"> . Thanks in advance.

            – Nikhil Vernekar
            Nov 21 '18 at 10:58






          • 1





            If the first element in KML is a Folder (see update in answer with Folder) then change the casting to a Folder element and iterate over its children. Likewise your placemarks may have different geometries so will need to test for geometry type if you have mixed types. Best to download the source and generate javadoc from it to view the API calls.

            – JasonM1
            Nov 22 '18 at 1:26



















          Thanks for the help. I tried the code. But it says "java.lang.ClassCastException: de.micromata.opengis.kml.v_2_2_0.Folder cannot be cast to de.micromata.opengis.kml.v_2_2_0.Placemark".

          – Nikhil Vernekar
          Nov 21 '18 at 10:57







          Thanks for the help. I tried the code. But it says "java.lang.ClassCastException: de.micromata.opengis.kml.v_2_2_0.Folder cannot be cast to de.micromata.opengis.kml.v_2_2_0.Placemark".

          – Nikhil Vernekar
          Nov 21 '18 at 10:57















          And also i want the coordinates from <Placemark id="1.5.2"> . Thanks in advance.

          – Nikhil Vernekar
          Nov 21 '18 at 10:58





          And also i want the coordinates from <Placemark id="1.5.2"> . Thanks in advance.

          – Nikhil Vernekar
          Nov 21 '18 at 10:58




          1




          1





          If the first element in KML is a Folder (see update in answer with Folder) then change the casting to a Folder element and iterate over its children. Likewise your placemarks may have different geometries so will need to test for geometry type if you have mixed types. Best to download the source and generate javadoc from it to view the API calls.

          – JasonM1
          Nov 22 '18 at 1:26







          If the first element in KML is a Folder (see update in answer with Folder) then change the casting to a Folder element and iterate over its children. Likewise your placemarks may have different geometries so will need to test for geometry type if you have mixed types. Best to download the source and generate javadoc from it to view the API calls.

          – JasonM1
          Nov 22 '18 at 1:26




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387255%2fhow-can-i-extract-coordinates-from-a-kml-file-using-java%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory