CSC220 Processing KML coordinates in Php

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 14:10, 27 September 2010 (UTC)


The Map on Google-Maps

  • KML example.png

The URL

The KML File

(you can get a copy of this file in your 220a-xx account by typing getcopy example1.klm)

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Walkmeter-Walk-20100829-0750.kml</name>
<Style id="currentPathStyle">
<LineStyle>
<color>aaff0000</color>
<width>4</width>
</LineStyle>
</Style>
<Style id="officialPathStyle">
<LineStyle>
<color>888d1baa</color>
<width>4</width>
</LineStyle>
</Style>
<Style id="bestIconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconBest.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="medianIconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMedian.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="worstIconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconWorst.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="currentIconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconCurrent.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="startIconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconStart.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="finishIconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconFinish.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="mile1IconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMile1.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="mile2IconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMile2.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="mile3IconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMile3.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="mile4IconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMile4.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="mile5IconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMile5.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="mile6IconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMile6.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="mile7IconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMile7.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="mile8IconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMile8.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="mile9IconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMile9.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="mile10IconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMile10.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="mile11IconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMile11.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="mile12IconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMile12.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="mile13IconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMile13.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="mile14IconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMile14.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Style id="mile15IconStyle">
<IconStyle><Icon><href>http://share.abvio.com/i/iconMile15.png</href></Icon>
<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Folder>
<name>Walk on Aug 29, 2010</name><open>1</open>
<Placemark>
<name>Start 7:50 AM</name>
<styleUrl>#startIconStyle</styleUrl>
<Point>
<coordinates>-72.69459750750001,42.34444296000000</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Finish 9:57 AM - Average 6:04 /mile</name>
<description>walk time 1:31:33, 15.09 miles, climb 1266 feet, stopped time 35:03</description>
<styleUrl>#finishIconStyle</styleUrl>
<Point>
<coordinates>-72.69433810000001,42.34412963333332</coordinates>
</Point>
</Placemark>
<Folder>
<name><![CDATA[Path]]></name>
<open>0</open>
<Placemark>
<name>Segment 1</name>
<styleUrl>#currentPathStyle</styleUrl>
<LineString>
<extrude>0</extrude>
<tessellate>1</tessellate>
<altitudeMode>clampToGround</altitudeMode>
<coordinates>
-72.69459750750001,42.34444296000000
-72.69435600000000,42.34400928333333
-72.69431068333333,42.34405935000000
...
-72.70036616666665,42.34702501666668
-72.69271225000001,42.34235086666666
-72.69267878333333,42.34254801666666
-72.69267330000000,42.34261633333334
-72.69409016666667,42.34391306666667
-72.69424906666666,42.34403235000000
-72.69433810000001,42.34412963333332
-72.69433810000001,42.34412963333332
</coordinates>
</LineString>
</Placemark>
</Folder>
</Folder>
<Folder>
<name>Splits</name>
<open>1</open>
<Placemark>
<name>Mile 01 - Average 9:30 /mile</name>
<styleUrl>#mile1IconStyle</styleUrl>
<Point>
<coordinates>-72.70065718058417,42.35034620333218</coordinates>
</Point>
</Placemark>
...
<Placemark>
<name>Mile 15 - Average 4:51 /mile</name>
<styleUrl>#mile15IconStyle</styleUrl>
<Point>
<coordinates>-72.69320545710393,42.34310336988481</coordinates>
</Point>
</Placemark>
</Folder>
</Document>
</kml>

(The complete file is available here.)

Reading Files in Php

  • Local files can be read very easily in Php, as follows:
 $lines = file( 'play.txt' );
 
 //--- print each line in a list ---
 print "<ul>\n";
 for ( $i=0; $i<count( $lines ); $i++ ) 
    print "<li>$i: $lines[$i]\n";
 print "</ul>\n";

  • The code above reads all the lines from the text file play.txt and displays them as a numbered list, using the $i index as a counter (the line numbering will start at 0).

Questions

Question 1
Write the code necessary to extract all the coordinates from the KML file and store them in two arrays, $x and $y, i.e. filter out all the lines of the form:
-72.70066539999999,42.35041920000000
and store the first number in array $x, and the second one in $y.


Question 2
Same question, but this time use functions.

...