9 Replies - 8230 Views - Last Post: 06 February 2012 - 06:05 PM Rate Topic: -----

#1 mido91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-February 11

How can get data from a KML file?

Posted 05 February 2012 - 09:40 PM

Hi everyone,

Basically I get a KML file via a specific URL which contains some Longitude and Latitude values I need in order to hook up to a drawable image using overlays on Google maps running on the android platform. I have a similar example of what I need but can't seem to be able to get it working to suit my situation

In the below code, it performs a GET request to get the KML file via the URL
    private String[] getDirectionData(String srcPlace, String destPlace) {
        
        String urlString = "http://www.scribblemaps.com/maps/kml/20001.kml";
        
        
        
        Log.d("URL", urlString);
        Document doc = null;
        HttpURLConnection urlConnection = null;
        URL url = null;
        String pathConent = "";
        try {

            url = new URL(urlString.toString());
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.connect();
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(urlConnection.getInputStream());

        } catch (Exception e) {
        }

        NodeList nl = doc.getElementsByTagName("LineString");
        for (int s = 0; s < nl.getLength(); s++) {
            Node rootNode = nl.item(s);
            NodeList configItems = rootNode.getChildNodes();
            for (int x = 0; x < configItems.getLength(); x++) {
                Node lineStringNode = configItems.item(x);
                NodeList path = lineStringNode.getChildNodes();
                pathConent = path.item(0).getNodeValue();
            }
        }
        String[] tempContent = pathConent.split(" ");
        return tempContent;
    }


This is the data that the code is interested in:
<LineString><altitudeMode>clampToGround</altitudeMode>
<coordinates>-2.384764899505565,51.372219297926534,0 -2.3842928307189437,51.372781890066285,0 -2.3853442566527816,51.373089973308595,0 -2.3860523598327132,51.37216571736219,0 -2.3851511376037093,51.371817442165565,0 -2.3847005264892274,51.37227287842815,0</coordinates></LineString></Placemark></Document></kml>


as you can see above the values are listed with the latitude first, however the code converts the values the other way round as it should be the longitude first then the latitude.
and as you can see below the code is broken up into longitude and latitude to perform various actions.
 public void getRouteDirection(Location location){

	String pairs[] = getDirectionData("englishcombe", "bathwick");
        String[] lngLat = pairs[0].split(",");
        
        
     // STARTING POINT
        GeoPoint startGP = new GeoPoint(
                (int) (Double.parseDouble(lngLat[1]) * 1E6), (int) (Double
                        .parseDouble(lngLat[0]) * 1E6));

        myMC = myMapView.getController();
        geoPoint = startGP;
        myMC.setCenter(geoPoint);
        myMC.setZoom(15);
        myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP));
        
        
        
     // NAVIGATE THE PATH
        GeoPoint gp1;
        GeoPoint gp2 = startGP;

        for (int i = 1; i < pairs.length; i++) {
            lngLat = pairs[i].split(",");
            gp1 = gp2;
            // watch out! For GeoPoint, first:latitude, second:longitude
            gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),
                    (int) (Double.parseDouble(lngLat[0]) * 1E6));
            myMapView.getOverlays().add(new DirectionPathOverlay(gp1, gp2));
            Log.d("xxx", "pair:" + pairs[i]);
            
        }
            
     // END POINT
        myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2));
        
        myMapView.getController().animateTo(startGP);
        myMapView.setBuiltInZoomControls(true);
        myMapView.displayZoomControls(true);
		}
		



And what I basically need the code to do, is break up the below data in a very similar way that the original code does
</Point></Placemark><Placemark scribbleType="6"><styleUrl>#style_0</styleUrl><Point>
<coordinates>-2.385408629669139,51.36864265765638,0</coordinates></Point></Placemark><Placemark scribbleType="6"><styleUrl>#style_0</styleUrl><Point><coordinates>-2.384807814849823,51.37197818489322,0</coordinates></Point></Placemark></Document></kml>



so instead of this being performed
NodeList nl = doc.getElementsByTagName("LineString");
This should be performed
NodeList nl = doc.getElementsByTagName("Point");
to pull out the correct data and as you can see the comparison between value format of the LineString and the value format of the Point, as shown below
<coordinates>-2.384764899505565,51.372219297926534,0 -2.3842928307189437,51.372781890066285,0 -2.3853442566527816......

there are just comma that seperates between each value. From this (-2.384764899505565,51.372219297926534,0) the code loops and separates this value like this (51.372219, -2.384764) and so on to get the rest which is used to draw a path between all those coordinates, but in my case I need the code to perform this too but from a different KML layout >>
<coordinates>-2.385408629669139,51.36864265765638,0</coordinates> .... </coordinates></Point></Placemark><Placemark scribbleType="6"><styleUrl>#style_0</styleUrl><Point><coordinates>-2.384807814849823,51.37197818489322,0</coordinates>
as you can see the difference this is separated by coordinates and other unwanted stuff so once I use the code above it only collect the first block of values and can not reach the rest of the values as they are not all separated by commas. I need the code to carry on looping to get all the values surrounded in <coordinates> the code should stop looping once it has collected all the values and formatted them in such as way again: (51.372219, -2.384764) Latitude being first then the longitude.

Thank you ever so much to whom that read this.

Is This A Good Question/Topic? 0
  • +

Replies To: How can get data from a KML file?

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2640
  • View blog
  • Posts: 11,155
  • Joined: 20-September 08

Re: How can get data from a KML file?

Posted 06 February 2012 - 04:27 AM

Check out http://technojeeves....63-xpath-tester

You will be able to get the coordinates with the pattern "\\coordinates\text()"

If you make a Point class, a parse of the nodes returned will make it trivial to get the lat/long in the order you want
Was This Post Helpful? 1
  • +
  • -

#3 mido91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-February 11

Re: How can get data from a KML file?

Posted 06 February 2012 - 12:41 PM

View Postg00se, on 06 February 2012 - 04:27 AM, said:

Check out http://technojeeves....63-xpath-tester

You will be able to get the coordinates with the pattern "\\coordinates\text()"

If you make a Point class, a parse of the nodes returned will make it trivial to get the lat/long in the order you want

Thanks for reply, can you be bit a more precise please? I am not sure how this link applies to my situation, I think the code I provided just needs to be amended right?
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2640
  • View blog
  • Posts: 11,155
  • Joined: 20-September 08

Re: How can get data from a KML file?

Posted 06 February 2012 - 01:30 PM

I'm just saying that it will give you a better start.
Was This Post Helpful? 1
  • +
  • -

#5 mido91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-February 11

Re: How can get data from a KML file?

Posted 06 February 2012 - 01:34 PM

View Postg00se, on 06 February 2012 - 01:30 PM, said:

I'm just saying that it will give you a better start.

Cool man, I had a look at it, and I just still think the code can be amended in few lines of code to achieve what is needed, but I can't figure it out just yet. Thanks again for the contribution though
Was This Post Helpful? 0
  • +
  • -

#6 mido91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-February 11

Re: How can get data from a KML file?

Posted 06 February 2012 - 01:34 PM

View Postg00se, on 06 February 2012 - 01:30 PM, said:

I'm just saying that it will give you a better start.

Cool man, I had a look at it, and I just still think the code can be amended in few lines of code to achieve what is needed, but I can't figure it out just yet. Thanks again for the contribution though
Was This Post Helpful? 0
  • +
  • -

#7 mido91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-February 11

Re: How can get data from a KML file?

Posted 06 February 2012 - 01:34 PM

View Postg00se, on 06 February 2012 - 01:30 PM, said:

I'm just saying that it will give you a better start.

Cool man, I had a look at it, and I just still think the code can be amended in few lines of code to achieve what is needed, but I can't figure it out just yet. Thanks again for the contribution though
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2640
  • View blog
  • Posts: 11,155
  • Joined: 20-September 08

Re: How can get data from a KML file?

Posted 06 February 2012 - 04:47 PM

The following works fine for me:

	NodeList coordinates = XPathTest.process("http://www.scribblemaps.com/maps/kml/20001.kml", "//coordinates/text()");
	if(coordinates != null) {
	    for(int i = 0;i < coordinates.getLength();i++) {	
		Node n = coordinates.item(i);
		String[] points = n.getNodeValue().split("\\s+");
		for(String point : points) {
		    Scanner in = new Scanner(point);
		    in.useDelimiter(",");
		    if(in.hasNextDouble()) {
			Point p = new Point(in.nextDouble(), in.nextDouble(), in.nextDouble());
			System.out.println(p);
		    }
		}
	    }
	}


Output:
latitude=51.511858,longitude=-0.201640,height=0.000000
latitude=51.510769,longitude=-0.205191,height=0.000000
latitude=51.509641,longitude=-0.204344,height=0.000000
latitude=51.510676,longitude=-0.200664,height=0.000000
latitude=51.511063,longitude=-0.199955,height=0.000000
latitude=51.511851,longitude=-0.201629,height=0.000000
Was This Post Helpful? 1
  • +
  • -

#9 mido91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-February 11

Re: How can get data from a KML file?

Posted 06 February 2012 - 05:11 PM

View Postg00se, on 06 February 2012 - 04:47 PM, said:

The following works fine for me:

	NodeList coordinates = XPathTest.process("http://www.scribblemaps.com/maps/kml/20001.kml", "//coordinates/text()");
	if(coordinates != null) {
	    for(int i = 0;i < coordinates.getLength();i++) {	
		Node n = coordinates.item(i);
		String[] points = n.getNodeValue().split("\\s+");
		for(String point : points) {
		    Scanner in = new Scanner(point);
		    in.useDelimiter(",");
		    if(in.hasNextDouble()) {
			Point p = new Point(in.nextDouble(), in.nextDouble(), in.nextDouble());
			System.out.println(p);
		    }
		}
	    }
	}


Output:
latitude=51.511858,longitude=-0.201640,height=0.000000
latitude=51.510769,longitude=-0.205191,height=0.000000
latitude=51.509641,longitude=-0.204344,height=0.000000
latitude=51.510676,longitude=-0.200664,height=0.000000
latitude=51.511063,longitude=-0.199955,height=0.000000
latitude=51.511851,longitude=-0.201629,height=0.000000

That's quality thanks, but I need it to work for this URL "http://www.scribblemaps.com/maps/kml/2000.kml" rather than "http://www.scribblemaps.com/maps/kml/20001.kml". and it seems to be getting the wrong values for the longitude.

for the URL with (2000) its KML file includes:
<Point>
<coordinates>-2.3892710106505843,51.36731641666582,0</coordinates></Point></Placemark><Placemark scribbleType="6"><styleUrl>#style_0</styleUrl><Point><coordinates>-2.385408629669139,51.36864265765638,0</coordinates></Point></Placemark><Placemark scribbleType="6"><styleUrl>#style_0</styleUrl><Point><coordinates>-2.384807814849823,51.37197818489322,0</coordinates></Point></Placemark></Document></kml>
and I need the output to be:
lat=51.367316, lng=-2.389271
lat=51.368642, lng=--2.385408
etc.. if that makes sense, sorry for the misunderstanding, thanks :)

This post has been edited by mido91: 06 February 2012 - 05:22 PM

Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2640
  • View blog
  • Posts: 11,155
  • Joined: 20-September 08

Re: How can get data from a KML file?

Posted 06 February 2012 - 06:05 PM

Quote

but I need it to work for this URL "http://www.scribblemaps.com/maps/kml/2000.kml"


Works fine for me just the same
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1