[GIS] KML from PostGIS Geometry into Openlayers

kmlopenlayers-2postgis

im getting the KML value from my postgis database.. st_askml()

And the output is like this:

"<Polygon><outerBoundaryIs><LinearRing><coordinates>-99.999999990686774,48.499999959021807 -99.999999990686774,47.999999988824129 -99.500000020489097,47.999999988824129 -99.500000020489097,48.499999959021807 -99.999999990686774,48.499999959021807</coordinates></LinearRing></outerBoundaryIs></Polygon>"

Now i need to display them on a Openlayers Map i got.

Layer:
var KML_Layer = new OpenLayers.Layer.Vector('KML');

Formatting code:

function GetFeaturesFromKMLString (strKML) {
var format = new OpenLayers.Format.KML({
        'internalProjection': options.projections.source, //EPSG:3785/900913
        'externalProjection': options.projections.display //EPSG:4326
    });

return format.read(strKML);
};

Adding the feature to the layer:

KML_Layer.addFeatures(value.st_askml);

Debuggin info:

value.st_askml = The output format i pasted earlier in topic.

After running it trough the GetFeaturesFromKMLString() its set to [] (No value here)

How can i get this to display correctly on the map?

Best Answer

ST_AsKML() function doesnt return you valid kml file for openlayers or google earth. the result you have seen being formed from raw coordinates for kml file. you can achive this with following code which @BradHards has mentioned.

put your output between <Placemark>your output</Placemark>. you can try it whether works or not with google earth. i have tested it and has worked.

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
  <Style id="0">    
  </Style> 
    <Placemark>           
     <Polygon>
      <outerBoundaryIs>
       <LinearRing>
        <coordinates>
        -99.999999990686774,48.499999959021807 
        -99.999999990686774,47.999999988824129 
        -99.500000020489097,47.999999988824129 
        -99.500000020489097,48.499999959021807 
        -99.999999990686774,48.499999959021807
        </coordinates>
       </LinearRing>
      </outerBoundaryIs>
     </Polygon>
    </Placemark>
</Document>
</kml>

i hope it helps you...