Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

But i belive 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

share|improve this question
why dont u use ST_GeomFromKML? – Aragon Jan 2 '13 at 9:10
its probably the other way around.. From database to a KML file.. not from a KML into database.. – Christer Jan 2 '13 at 9:18
The result from 1 column as you can see in my post is a "as_kml" result. – Christer Jan 2 '13 at 9:27
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 '13 at 10:18
Good to know @AndreJoost, But how can i transform these coordinates to that coordinate system ? – Christer Jan 2 '13 at 10:27
up vote 2 down vote accepted

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 :)

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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