Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from KML file using node.js

Is it possible to get location data from KML file based on the latitude and longitude? When I am using npm node-geocoder that time I am getting a result which Google provided. But here I am having KML file from this file I need to get the result. Please guide me to get a result from KML file.

Below: the code I am using getting data from geocoder API.

var NodeGeocoder = require('node-geocoder');
var options = {
  provider: 'google',
  // Optional depending on the providers
  httpAdapter: 'https',
  formatter: 'json'
};

var geocoder = NodeGeocoder(options);

var kmllatitude =req.body.latitude;
var kmllong =req.body.longitude;


geocoder.reverse({lat:kmllatitude, lon:kmllong}, function(err, res) {
    console.log(err,"!!!!!!!!");
    console.log(res,"####");
});
like image 570
user3239284 Avatar asked Oct 18 '17 05:10

user3239284


People also ask

How do I read a KML file in node?

var fs = require('fs'), path = require('path'), xmlReader = require('read-xml'); var convert = require('xml-js'); // If your file is located in a different directory than this javascript // file, just change the directory path. var FILE = path. join(__dirname, './history. kml'); xmlReader.


1 Answers

I'm supposing you downloaded the KML file from your Google Location History.

Since KML uses a tag-based structure with nested elements and attributes, based on the XML standard, you can use the read-xml package to obtain the data from your KML file.

This is what your KML file should be like:

<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://www.opengis.net/kml/2.2' xmlns:gx='http://www.google.com/kml/ext/2.2'>
    <Document>
        <Placemark>
            <open>1</open>
            <gx:Track>
                <altitudeMode>clampToGround</altitudeMode>
                <when>2018-01-18T23:48:28Z</when>
                <gx:coord>-16.9800841 32.6660673 0</gx:coord>
                <when>2018-01-18T23:45:06Z</when>
                            ...
                <when>2013-12-05T09:03:41Z</when>
                <gx:coord>-16.9251961 32.6586912 0</gx:coord>
            </gx:Track>
        </Placemark>
    </Document>
</kml>

Then I convert the XML text to Javascript object/JSON text. You don't have to do this step, but for me, it's easier to do and to explain. You can do that by using the xml-js package.

Another thing you have to do is to split the value of this tag <gx:coord>-16.9251961 32.6586912 0</gx:coord> since you have first the longitude and then the latitude, inside the same tag.

var fs = require('fs'),
    path = require('path'),
    xmlReader = require('read-xml');

var convert = require('xml-js');

// If your file is located in a different directory than this javascript 
// file, just change the directory path.
var FILE = path.join(__dirname, './history.kml'); 

xmlReader.readXML(fs.readFileSync(FILE), function(err, data) {
    if (err) {
        console.error(err);
    }

    var xml = data.content;
    var result = JSON.parse(convert.xml2json(xml, {compact: true, spaces: 4}));

      // If your KML file is different than the one I provided just change 
      // result.kml.Document.Placemark['gx:Track']['gx:coord'].
      // As you can see it is similar with the KML file provided.
      for(var i = 0; i < result.kml.Document.Placemark['gx:Track']['gx:coord'].length; i++){
         var results = result.kml.Document.Placemark['gx:Track']['gx:coord'][i]._text;

         // As I said before you have to split the returned value.
         var coordinates = results.split(" ");
         var longitude = coordinates[0];
         var latitude = coordinates[1];
         console.log("lat/long: " + latitude + ", " + longitude);
      }
});

Hope it could help you!

like image 168
Ricardo Faria Avatar answered Sep 29 '22 08:09

Ricardo Faria