Moodle output to kml via php - Google Earth won't parse

Moodle output to kml via php - Google Earth won't parse

by Richard Treves -
Number of replies: 10
I've written a file that outputs .kml (xml readable by google earth/maps - attached).  If you create a database in Moodle 1.6 with a field title = 'Latitude' and one for 'Longitude' (case sensitive) and enter data (decimal degrees) it will produce kml readout.  You can have any other fields in the database too so long as you don't reuse the keywords Latitude and Longitude.  The placemarks in GE have a pop up description that is the URL of the database entry concerned, it works well with the internal browser of GE turned on.

To use it install and edit the variables commented in upper case for your site setup.

If I comment out the header (lines 2 and 3) it will produce a text file that I can save as .kml and then read into GE.  However, if I use the headers FF recognises it as .kml and boots GE which then spits the dummy with the message:

Open of file "[directory string]\nf_kml_out.kml" failed: Parse error: n at line 1, column 0

Opening the downloaded kml which it was trying to parse with notebook I found that it hadn't parsed (is that the term?) the .kml correctly, it had

"&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>"

type text rather than

"<?xml version="1.0" encoding="UTF-8"?>"

Which is probably where the GE parsing error comes from. Since I got a prototype php file reading a custom MySQL table rather than the Moodle tables working fine on another online server I assume its my XAMPP development server causing the problem.  I am running:
PHP Version 5.1.4,
MySQL 5.0.21,
Apache/2.2.2
on windows

I've already tried adding the .kml MIME type into Apache, that didn't work. 

I also can't get
$output = "<blah>1</blah>";
$output .= "<bleh>2</bleh>";
echo $output;
to work, hence the ugly code.

if someone could confirm it works elsewhere that would help, otherwise thanks for your attention in looking at this for me.

Rich
Average of ratings: -
In reply to Richard Treves

Re: Moodle output to kml via php - Google Earth won't parse

by Dan Stowell -
Rich -

This is an interesting idea. Yes, as you hint, the reason that your script doesn't work as expected is that it's outputting "quoted" XML rather than raw XML.

Line 38, for example, is

&lt;Folder&gt;<br>

whereas it should be

<Folder>

If you edit your file to make these changes (you need to change every outputted tag so it's not quoted), it should produce "real" XML.
In reply to Dan Stowell

Re: Moodle output to kml via php - Google Earth won't parse

by Richard Treves -
Hi Dan,

Did you install and run it on your own moodle 1.6 server to get this behaviour?  If so, thanks for that, its useful info.  Do you know your server handles .kml files?

I'll try and get echo statements to work, although, as I said, I'm having problems with them too sad

Rich
In reply to Richard Treves

Re: Moodle output to kml via php - Google Earth won't parse

by Dan Stowell -
Richard,

No, I didn't install it at all - just looked through the code. I have enough PHP/XML experience to be able to spot that kind of thing.

I suspect that you are not having problems with your echo statements. The issue is most likely that during your testing, you're looking at the output as if it was HTML. In your example:

$output = "<blah>1</blah>";
$output .= "<bleh>2</bleh>";
echo $output;

You probably tried this out and saw the output "12" - am I right? If so, this is because your web browser is treating the output as HTML and not displaying the raw tags. When you use your browser's "view source" option, you'll see that the output is correct after all.
In reply to Richard Treves

Re: latitude/longitude

by Dan Stowell -
Rich - you may also be interested in the latitude/longitude data type I've just released.
In reply to Dan Stowell

Re: latitude/longitude

by Richard Treves -
Cracked it!  Oooh, it feels good to scratch that itch!

My apologies, Dan, you were quite right, echo was working correctly and you had to look at source to see what it was outputting, I had realised this earlier as I said on the phone but in the confusion of
- what is/isn't html/xml
- do you need \<? like you need \"
I'd got confused with what I'd tested and what I hadn't (Dan and I discussed over the phone yesterday).

The actual error turned out to be that I'd managed to get the '<kml....' declaration before the '<?xml...' declaration and it should be the other way around of course. Doh!

For anyone else playing with .xml output for the first time, things I got stuck on:  in a php echo statement  if you want to echo double quotes within the statement using \ makes php recognise you're not closing the statement.  Thus
echo "<kml>"2.0"</kml>";  won't work
echo "<kml>\"2.0\"</kml>"; is the baby.

I also didn't realise that the browser will parse the message it gets from the server as if its .html, thus if you're trying to output .kml to test it open your .php file and look at the source code rather than whats on the page.

My first sucessful wander into the Moodle code....

Rich
In reply to Richard Treves

Re: latitude/longitude

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
echo '<kml>"2.0"</kml>';

Also works.
In reply to Tim Hunt

Re: latitude/longitude

by Richard Treves -
Thanks Tim, that's a simpler version.

And thanks to Dan too, forgot to say that earlier.

The outcome of this development is that within Google Earth a student can navigate around a map with placemarks of points that relate to entries in a Moodle DB.  By clicking the placemark on the map they get taken to the database entry concerned in the internal browser (which can have an image/audio/film clip file in it).  They can add/edit database entries of course.  To go from a database entry in the internal browser to the point on the map they have to look up the placemarks in the normal GE places bar which isn't so elegant but it works.

note this screenshot is mainly for the Google Earth community, they probably haven't heard of Moodle but I thought that you guys would all have heard of Google Earth. smile


Attachment screen_shot.jpg
In reply to Richard Treves

Re: latitude/longitude

by Dan Stowell -
Rich - This looks nice... I'm using a Mac and I don't seem to have an "internal browser". I've looked in the preferences, menus, etc - it's a windows-only feature?

My "latitude/longitude" data type (discussed in the other thread) now enables back-and-forth switching to Google Earth. I've got the KML export working nicely so that when you jump to Google Earth, the placemark has an associated URL that takes you right back to the Moodle data item you came from.

I'm not a member of the "Google Earth community", will there be many interested in this kind of thing?
In reply to Dan Stowell

Re: latitude/longitude

by Richard Treves -
"Rich - This looks nice... I'm using a Mac and I don't seem to have an "internal browser". I've looked in the preferences, menus, etc - it's a windows-only feature?"

dunno, have never seen GE on the Mac.  To access on windows GE v4: Tools>General>Show web results in external browser (should be off obviously)

"My "latitude/longitude" data type (discussed in the other thread) now enables back-and-forth switching to Google Earth. I've got the KML export working nicely so that when you jump to Google Earth, the placemark has an associated URL that takes you right back to the Moodle data item you came from."

yup neat.  That's what the my 'all the points' map does too.  There's probably a way of finding the placemark from the database via php for my map but I haven't looked at that yet.

I'll also work out how to incorporate your datatype into my file but I'm new to writing code for Moodle so I'll have to work out how to install your datatype, access it from the code, etc etc from moodle.docs before I can do that.

" I'm not a member of the "Google Earth community", will there be many interested in this kind of thing?"

Yes to the concept but I suspect less to the specific Moodle/GE mashup.  There are various projects similar to this going on already, someone is building something similar for joomla and a guy called valery35 (might have the number wrong) on the google earth community has taken a web based spreadsheet and mashed it with GE.  Check out 'Google Earth blog' and 'ogle earth' who are the 2 biggest blogs on this kind of stuff.

Do you have a particular project in mind for this?