<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
FONT-SIZE: 10pt;
FONT-FAMILY:Tahoma
}
</style>
</head>
<body class='hmmessage'>Shad,<BR> <BR>I worked with Python for this target.<BR> <BR>I made two python scripts, postgis_kml_by_ogr.py and filelayer.py, the first one calls the other:<BR> <BR>--------- postgis_kml_by_ogr.py ---<BR># use:<BR># cMng = MngPostGis(host='csr-pascoal', dbname='uf_ac', user='userpostgis', pwd='userini2008')<BR># sSql = "SELECT DISTINCT m.nome, ST_Transform(m.the_geom, 4326) FROM municipio m, aeroporto a WHERE ST_Contains(m.the_geom, a.the_geom);"<BR># cMng.WriteLayerKML("c:\\kml\\teste.kml", 0)<BR># <BR>#<BR># <BR># Dependece Class:<BR>#- filelayerkml.py<BR>#<BR>#<BR># Last revision 18/3/2008<BR>from osgeo import ogr<BR>from util_ogr import filelayer<BR>import os, sys<BR>import os.path<BR>class MngPostGis:<BR> def __init__(self, host, dbname, user, pwd):<BR> self.__ds = ogr.Open("PG: host=%s dbname=%s user=%s password=%s" % (host, dbname, user, pwd))<BR> self.__lyr = ""<BR> def __del__(self):<BR> if isinstance(self.__lyr, ogr.Layer): self.__ds.ReleaseResultSet(self.__lyr)<BR> def SetLayerBySql(self, sSql, spatialFilter=""):<BR> if isinstance(self.__lyr, ogr.Layer): self.__ds.ReleaseResultSet(self.__lyr)<BR> if spatialFilter == "": self.__lyr = self.__ds.ExecuteSQL(sSql)<BR> else: self.__lyr = self.__ds.ExecuteSQL(sSql, spatialFilter)<BR> def WriteLayerKML(self, sFileKML, idFieldName):<BR> cFileLayerKML = filelayer.FileLayerKML(sFileKML)<BR> while True:<BR> feat = self.__lyr.GetNextFeature()<BR> if not feat: break<BR> cFileLayerKML.WriteFeature(feat.GetField(idFieldName), feat.GetGeometryRef().ExportToKML())<BR> def GetTotalFeatures(self):<BR> if isinstance(self.__lyr, ogr.Layer): return self.__lyr.GetFeatureCount()<BR> else: return 0<BR><BR>--------- postgis_kml_by_ogr.py ---<BR> <BR>---- filelayer.py ---<BR># use:<BR># cFileLyrKml = FileLayerKML("c:\\kml\\teste.kml")<BR># cFileLyrKml.WriteFeature( "Value of Attribute", sKMLGeometries):<BR># <BR>#<BR># Last revision 18/3/2008<BR>import os, sys<BR>import os.path<BR>class FileLayerKML:<BR> def __init__(self, sFileKML):<BR> # Params: <NameFileOut> <BR> self.__CreateFileOut(sFileKML)<BR> self.__WriteHeader()<BR> def __del__(self):<BR> self.__WriteEnd()<BR> self.__FileOut.flush()<BR> self.__FileOut.close()<BR> def __CreateFileOut(self, NameFile):<BR> try:<BR> self.__FileOut = file(NameFile, 'w')<BR> except IOError, error:<BR> print "Error File %s not be create.\nMenssage" % (NameFile, error)<BR> sys.exit()<BR> def __WriteHeader(self):<BR> sLine = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"<BR> self.__FileOut.write(sLine)<BR> sLine = "<kml xmlns=\"<A href="http://earth.google.com/kml/2.2/%22%3E/n" target=_blank>http://earth.google.com/kml/2.2\">\n</A>"<BR> self.__FileOut.write(sLine)<BR> sLine = "<Document>\n<name>%s</name>\n" % self.__FileOut.name<BR> self.__FileOut.write(sLine)<BR> def __WriteEnd(self):<BR> sLine = "</Document>\n</kml>"<BR> self.__FileOut.write(sLine)<BR> def WriteFeature(self, sName, sKMLGeometries):<BR> sLine = "<Placemark><name>%s</name>\n%s\n</Placemark>\n" % (sName, sKMLGeometries)<BR> self.__FileOut.write(sLine)<BR><BR>---- filelayer.py ---<BR> <BR> <BR><BR><BR><BR>
<BLOCKQUOTE>
<HR id=EC_stopSpelling>
From: shadkeene@hotmail.com<BR>To: fwtools@lists.maptools.org<BR>Date: Wed, 2 Apr 2008 11:16:19 -0700<BR>Subject: [FWTools] convert postgis data to kml<BR><BR>
<META content="Microsoft SafeHTML" name=Generator>
<STYLE>
.ExternalClass .EC_hmmessage P
{padding:0px;}
.ExternalClass body.EC_hmmessage
{font-size:10pt;font-family:Tahoma;}
</STYLE>
Hi,<BR>My goal is to display various shapes(polygons, points, linestring) on google maps by using data entered into a postgis database. 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 FWTools can convert postgis data to kml format. <BR>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 language. Is there a tutorial for the process of converting postgis data to kml? Where can I get started? Thanks for any help,<BR>Shad<BR><BR>
<DIV><FONT style="COLOR: rgb(0,0,255)" size=1><SPAN style="FONT-STYLE: italic; FONT-FAMILY: Lucida Handwriting,Cursive"></SPAN></FONT><EM><FONT face="Lucida Handwriting" color=#0000ff size=1><BR></FONT></EM></DIV><BR>
<HR>
More immediate than e-mail? <A href="http://www.windowslive.com/messenger/overview.html?ocid=TXT_TAGLM_WL_Refresh_instantaccess_042008" target=_blank>Get instant access with Windows Live Messenger.</A> </BLOCKQUOTE><br /><hr />Notícias direto do New York Times, gols do Lance, videocassetadas e muitos outros vídeos no MSN Videos! <a href='http://video.msn.com/?mkt=pt-br' target='_new'>Confira já!</a></body>
</html>