Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to write out my geometry in gml format after it is drawn in OpenLayers. Actually it is working fine, I get the gml like this:

<gml:coordinates decimal="." cs="," ts=" ">8.62975388121127,46.34818691143132 8.761589818711352,46.590322794368646 9.124138646836665,46.43911409951752 8.805535131211348,46.24951148519901 8.62975388121127,46.34818691143132</gml:coordinates>

But unfortunately I need it to have the coordinate system (srsName) defined in it, like this:

<gml:Polygon srsName="EPSG:4326" >
<gml:coordinates decimal="." cs="," ts=" ">8.62975388121127,46.34818691143132 8.761589818711352,46.590322794368646 9.124138646836665,46.43911409951752 8.805535131211348,46.24951148519901 8.62975388121127,46.34818691143132</gml:coordinates>

Here is my code:

draw.events.register('featureadded', this, function(evt) {
   var feat1 = vectors.features;
   var gml = new OpenLayers.Format.GML({ 
                  format: OpenLayers.Format.GML.v3,
                  formatOptions: {
                       srsName: "urn:x-ogc:def:crs:EPSG:4326", 
   for (var i=0; i<feat1.length; i++) {
   var geomtransformed = feat1[i].geometry.transform(new OpenLayers.Projection("EPSG:3857"), new OpenLayers.Projection("EPSG:4326"));
   geomout = gml.buildGeometry.polygon.apply(gml,[geomtransformed]);

This is working but the srsName does not appear in the gml.

So could you please advise me if it is possible to transform vector feature to gml with writing the srsName into the gml, and if yes, how?

share|improve this question
Have you tried using GML.Base & srsname: dev.openlayers.org/docs/files/OpenLayers/Format/GML/… –  Devdatta Tengshe May 2 '13 at 9:57
unfortunately yes but maybe I am mixing something up in the code, so If you have a working code snippet could you show it? anyway, I have solved the problem with a workaround but it is a long way around it... –  ylka May 2 '13 at 10:31
add comment

1 Answer

I have solved my problem with a workaround, so this is not exactly the answer to my question, because originally I wanted this with javascript, but, well, maybe it helps someone in the future:

I needed the srsname in the gml geometry because I have to use the PostGIS function ST_GeomFromGML and it is not working without srsname.

Actually I'm passing the geometry to the database via wps, which is written in java, so I solved this problem by passing the geometry in WKT and then I convert it to GML in java. The package com.vividsolutions.jts.io.gml2.GMLWriter (jts-1.12.jar - comes with geoserver) provides a tool to set the srsname. The idea came from this question so the credit goes there.

Here is the code:

public static String wktToGml(String wktGeometry) throws ParseException, IOException {
       WKTReader wktR = new WKTReader();
       Geometry geom = wktR.read(wktGeometry);

       // write JTS to string
       GMLWriter gmlW = new GMLWriter();
       String gml = gmlW.write(geom);

       //return gml;
       return gml;


there was one more problem with this. The class GMLWriter writes the geometry out like this:

  <gml:Polygon srsName='EPSG:4326'>

but the PostGIS accepts only like this:

  <gml:Polygon srsName="EPSG:4326">

So I had to change the source code of this class, at the following line:

writer.write(" " + GMLConstants.GML_ATTR_SRSNAME + "=\"" + srsName + "\"");
share|improve this answer
add comment

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.