[FWTools] convert postgis data to kml

Luiz Motta luizmottanet at hotmail.com
Wed Apr 2 14:51:42 EDT 2008


Shad, I worked with Python for this target. I made two python scripts, postgis_kml_by_ogr.py  and filelayer.py, the first one calls the other: --------- postgis_kml_by_ogr.py ---# use:#  cMng = MngPostGis(host='csr-pascoal',  dbname='uf_ac',  user='userpostgis',  pwd='userini2008')# 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);"# cMng.WriteLayerKML("c:\\kml\\teste.kml",  0)# ## #  Dependece Class:#-  filelayerkml.py### Last revision 18/3/2008from osgeo import ogrfrom util_ogr import filelayerimport os, sysimport os.pathclass MngPostGis: def __init__(self, host, dbname, user, pwd):  self.__ds = ogr.Open("PG: host=%s dbname=%s user=%s password=%s" % (host, dbname, user, pwd))  self.__lyr = "" def __del__(self):  if isinstance(self.__lyr, ogr.Layer): self.__ds.ReleaseResultSet(self.__lyr) def SetLayerBySql(self, sSql, spatialFilter=""):  if isinstance(self.__lyr, ogr.Layer): self.__ds.ReleaseResultSet(self.__lyr)  if spatialFilter == "": self.__lyr = self.__ds.ExecuteSQL(sSql)  else: self.__lyr = self.__ds.ExecuteSQL(sSql, spatialFilter) def WriteLayerKML(self, sFileKML, idFieldName):  cFileLayerKML = filelayer.FileLayerKML(sFileKML)  while True:   feat = self.__lyr.GetNextFeature()   if not feat: break   cFileLayerKML.WriteFeature(feat.GetField(idFieldName),  feat.GetGeometryRef().ExportToKML()) def GetTotalFeatures(self):  if isinstance(self.__lyr, ogr.Layer): return self.__lyr.GetFeatureCount()  else: return 0--------- postgis_kml_by_ogr.py --- ---- filelayer.py ---# use:#  cFileLyrKml  = FileLayerKML("c:\\kml\\teste.kml")#  cFileLyrKml.WriteFeature( "Value of Attribute",  sKMLGeometries):# ## Last revision 18/3/2008import os, sysimport os.pathclass FileLayerKML:        def __init__(self, sFileKML):                # Params:  <NameFileOut>                 self.__CreateFileOut(sFileKML)                self.__WriteHeader()        def __del__(self):                self.__WriteEnd()                self.__FileOut.flush()                self.__FileOut.close()        def __CreateFileOut(self, NameFile):                try:                        self.__FileOut = file(NameFile, 'w')                except IOError, error:                        print "Error File %s not be create.\nMenssage" % (NameFile, error)                        sys.exit()        def __WriteHeader(self):                sLine = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"                self.__FileOut.write(sLine)                sLine = "<kml xmlns=\"http://earth.google.com/kml/2.2\">\n"                self.__FileOut.write(sLine)                sLine = "<Document>\n<name>%s</name>\n" % self.__FileOut.name                self.__FileOut.write(sLine)        def __WriteEnd(self):                sLine = "</Document>\n</kml>"                self.__FileOut.write(sLine)        def WriteFeature(self, sName, sKMLGeometries):                sLine = "<Placemark><name>%s</name>\n%s\n</Placemark>\n" % (sName, sKMLGeometries)                self.__FileOut.write(sLine)---- filelayer.py ---  


From: shadkeene at hotmail.comTo: fwtools at lists.maptools.orgDate: Wed, 2 Apr 2008 11:16:19 -0700Subject: [FWTools] convert postgis data to kml


Hi,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.  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,Shad


More immediate than e-mail? Get instant access with Windows Live Messenger. 
_________________________________________________________________
Confira vídeos com notícias do NY Times, gols direto do Lance, videocassetadas e muito mais no MSN Video!
http://video.msn.com/?mkt=pt-br
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.maptools.org/pipermail/fwtools/attachments/20080402/303c3467/attachment.html


More information about the FWTools mailing list