views:

378

answers:

2

Hi, My goal is to display various shapes(polygons, points, linestring) on google maps by using data entered into a Postgis database dynamically(i mean by that we can see modifications in the map in real time). I was looking for a way to do this that used the spatial structure already provided in postgis(already designating if shape is a linestring or polygon, etc) instead of parsing out the coordinates and then re-entering spatial structure in google maps. I saw that google maps api is now compatible with kml data formats. And then I read that i have to convert postgis data to kml format. I've done some reading in the forums about the actual process of converting postgis data to kml via FWTools, but didn't see anything that would help me. I'm new to kml but am familiar with postgis and perl and PHP. Is there a tutorial for the process of converting postgis data to kml? Where can I get started? Thanks for any help

+1  A: 

You can get a textual representation of the spatial data from a Postgres DB using a text conversion function, like

SELECT AsText(MyGemoetry) from MyTable

then you parse the string, create your objects using various API functions - depending on the PostGIS geometry type - and append these object to the main GE plugin object in a DOM like way.

If you are familiar with JavaScript and have a fundamental knowledge of XML, a good start is http://code.google.com/apis/earth/documentation/reference/

Don't forget to specify unique ID's to your objects so you can find them later to drop/modify.

Maybe you can get some inspirations here, display the linked "locator.js" file and look at function PaintSubField(Coord) ... this is another way, bit crude but effective, avoiding to mess around with too many individual parent/child objects and structures

You also may want to consult sample applications and use the code playground for "rapid prototyping"

re "realtime" you need at least an event that you can link your generation/redraw routines to.

Good luck MikeD

MikeD
From my experience tcarobruce's solution works much better. Trying to parse the text by hand is overkill when there is already a function to do it.
Adam
@Adam: I tend to agree with you, except for the case that I want to get only small pieces of info from one Geo format and create another one. Usually Geoformats are overloaded with things I don't need (.... just gimme that LAT/LON ;-). This said, admittedly I didn't explore ST_AsKML() so far.
MikeD
+4  A: 

You can use PostGIS to convert to KML directly:

SELECT ST_AsKML(geometry) from MyTable;

ST_AsKML is one of several output formats, including WKT, GML, GeoJSON, etc.

To show dynamic data in Google Earth, a common pattern is to use KML with a NetworkLink element. Have the link's viewRefreshMode equal to onStop and Google Earth will make requests (to a URL served by PHP, presumably) with bounding box parameters attached. Use the bounding box to query features in the PostGIS database, and return results as kml. This is great if you have lots and lots of features, but only want to retrieve those in the region the user is looking at.

Depending on the complexity of your application, you may also want to look at GeoDjango. (Familiarity with PostGIS is a big head start!)

tcarobruce