Archive for the ‘Code’ Category

KML writing PHP Class

Tuesday, March 6th, 2007

For a little project working on for Geograph (btw Second Birthday today!), that will end up producing quite complex (and repetitive) KML, I decided to take the plunge and create my own abstraction class.

There are already a few of these about, but I wanted something with just enough abstraction so don’t need xml in the php code, but without too much complexity, or a whole new syntax to learn. So the final output is basically a generic XML creation system, but tailored to KML generation, with a number of convenience methods for common bits of KML, (like outputting the right headers). This don’t do any schema enforcement, and assumes a good working knowledge of kml.

Anyway as the code is already GPL, thought would mention it here in case it useful to someone else.

Get the base class here (from the Geograph SVN repository)

There is also another file here, which extends basic the functionality, this is more specific to Geograph, but could still be useful.

And a few demo’s/tests used during development:

Demo1 – (view running demo – by default displays the KML, option to open in GE)
Demo2 – (view running demo)

And for a real world use of the code (been in active use for a week now)

Eagle eyed viewers might notice it claims kml2.0 but then goes on to allow use of 2.1 features. Yes I know this is ‘bad’ but is with reason. I have found to my expense that using 2.1 as the version actually has a few undesirable effects, eg making the Document visible in My Places tree, can’t mix and match styleUrl and style (to have common style but with a unique icon), and a few other subtle things. Whereas GE seems to allow the new features in a 2.0 kml file, I guess this is the GE equivalent to quirks and strict mode. (disclaimer: it’s a while since I tested this, so might be old news, I really should run some real world tests to confirm this is still the case)

KML (& NetworkLink) for Nearby & any Geo-WordPress!

Saturday, February 17th, 2007

Following the addition of GeoRSS, earlier today while out photographing some PhotoSpots (TBA), I had an even better (possibly) idea, now that my WordPress stores Lat/Long, why not output KML too! The icing on the cake is you can also wrap it in a NetworkLink so that peoples Google Earth will auto update. (I am surprised couldn’t find this done already!)

And if that isn’t enough then I also release the code under GPL :) – note its the first time written anything for WordPress, so might not be a tidy as could be!

Anyway see it in action on my series of PhotoSpots:

- as all these list the PhotoSpot category, each post includes a link to open the actual spot, so just click the world icon in the balloon to open :)

Download the Source or view the source: WordPress KML Generator and NetworkLink Wrapper

Installation: This is not a proper plugin, so just drop the .php files into the root of your WordPress folder and you should be done (but you can configure the icon to display if you wish by editing the wp-kml.php file)

Use: Normally you would only provide a link to the Wrapper – which in turn links to the feed itself, but the feed is useful like in the above example to plot directly on Google Maps. You can also specify a particular category to display like the above example or otherwise will include all geocoded posts.
Important: Requires the GeoPress extension be installed (to get geocode info)
Only tested on WordPress 2.0.1 – Works For Me – Your Mileage May Vary – no guarantee, implied or otherwise