[GIS] Transforming polygon using Proj4js

openlayers-2proj

I am reading the Proj4js User Guide but cannot quite grasp it. I import the library

<script src="lib/proj4js-combined.js"></script>

Then,

function init() {

    (...) // reading from GML string - one polygon in EPSG:3031
    var features = format.read(strGML);

    Proj4js.defs['EPSG:3031'] = '+proj=stere +lat_0=-90 +lat_ts=-71 +lon_0=0 +k=1 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs';

    var source = new OpenLayers.Projection('EPSG:3031');
    var wgs84 = new OpenLayers.Projection('WGS84');

    OpenLayers.Projection.transform(features[0], source, wgs84);

    (...) // show polygon on map

}

Well, this doesn't work. Reading of GML string works fine as the polygon shows correctly on map if the coordinates are in WGS84 (and without the transformation, of course). But when I put in the string with other coordinates and try to transform it doesn't work. Is it possible to transform a polygon at once or do I have to do it point-by-point (and how would I do it)? Thanks

EDIT:
My GML string loks like this:

<gml:featureMember xmlns:gml="http://www.opengis.net/gml xsi:schemaLocation="http://www.opengis.net/gml http://schemas.opengis.net/gml/3.1.1/profiles/gmlsfProfile/1.0.0/gmlsf.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <feature:feature xmlns:feature="http://example.com/feature">
          <feature:geometry>
               <gml:Polygon>
                     <gml:exterior>
                           <gml:LinearRing>
                                 <gml:posList>591674.39 5022898.05 545682.5 4722908.1 571701.44 5322909.29 651691.25 5022904.6 591674.39 5022898.05
                                 </gml:posList>
                            </gml:LinearRing>
                     </gml:exterior>
               </gml:Polygon>
          </feature:geometry>
     </feature:feature>
</gml:featureMember>

Should I, instead of

OpenLayers.Projection.transform(features[0], source, wgs84); 

write

OpenLayers.Projection.transform(features[0].geometry, source, wgs84);

or even something else?

Best Answer

Since you want to transform the coordinates of the feature, you need to apply the transformation to the feature geometry.

//like this:
features[0].geometry.transform(source, wgs84);

//instead of:
//OpenLayers.Projection.transform(features[0].geometry, source, wgs84);