I'm trying to create KML files manually (XML format) by extracting data from a postGIS database with "geography" values in a column.

But i believe i need to transform the projection of each row in the database from EPSG:4326 to EPSG: 900913.. How can i do that?


 map = new OpenLayers.Map({
maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
zoom: 0,
projection: "EPSG:900913",
displayProjection: new OpenLayers.Projection("EPSG:4326"),
div: "map_canvas",
controls: [
                new OpenLayers.Control.Navigation(),
                new OpenLayers.Control.PanZoomBar(),
                new OpenLayers.Control.MousePosition(),
                new OpenLayers.Control.LayerSwitcher()

var KML = new OpenLayers.Layer.Vector("KML", {
        strategies: [new OpenLayers.Strategy.Fixed()],
        protocol: new OpenLayers.Protocol.HTTP({
            url: "assets/text.kml",
            format: new OpenLayers.Format.KML({
                extractStyles: false, 
                extractAttributes: true

  • Coordinate system in database: (one "Area") / Polygon.

-179.999995473772 -89.9999977368861,-89.9999954737723 -89.9999977368861,4.52622771263123e- 06 -89.9999977368861,90.0000045262277 -89.9999977368861,179.999995473772 -89.9999977368861,179.999995473772 89.9999977368861,90.0000045262277 89.9999977368861,4.52622771263123e-06 89.9999977368861,-89.9999954737723 89.9999977368861,-179.999995473772 89.9999977368861,-179.999995473772 -89.9999977368861

Current error:

Uncaught Bad LineString point coordinates: -179.999995473772

  • 1
    why dont u use ST_GeomFromKML?
    – urcm
    Jan 2, 2013 at 9:10
  • its probably the other way around.. From database to a KML file.. not from a KML into database..
    – Christer
    Jan 2, 2013 at 9:18
  • The result from 1 column as you can see in my post is a "as_kml" result.
    – Christer
    Jan 2, 2013 at 9:27
  • 1
    You might run into problems if your data exceeds 85.0511 to the North and South. North and South pole are not allowed in projection 900913.
    – AndreJ
    Jan 2, 2013 at 10:18
  • Good to know @AndreJoost, But how can i transform these coordinates to that coordinate system ?
    – Christer
    Jan 2, 2013 at 10:27

2 Answers 2


i found a way to do it:

´format: new OpenLayers.Format.KML({
                extractStyles: true, 
                extractAttributes: true,
                maxDepth: 2,
                'internalProjection': options.projections.source, //EPSG:3785/900913
                'externalProjection': options.projections.display //EPSG:4326

Hope this helps anyone else :)

  • How do you get the options object?
    – Alexis_A
    Apr 7, 2017 at 10:54
  • 1
    Hi Alexis_A, this post is 4 years old, but if i remember correcly, options is just a custom object, (the one you use when intiializing Map(). (see original question) - It seems like you can use .GetOptions() as well to get the current options. (dev.openlayers.org/docs/files/OpenLayers/Layer-js.html)
    – Christer
    Apr 7, 2017 at 11:02

To calculate the map center from a given pair of lat/lon coordinates, I use the following in Openlayers:

               var lonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
            map.setCenter (lonLat, zoom);

Maybe this function can help you with KML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.