[OpenLayers-Users] OpenLayers.Projection and transform of features

Arnd Wippermann arnd.wippermann at web.de
Tue Jun 24 18:54:16 EDT 2008


Hi,

i have a problem with OpenLayers.Projection and transform of features. 

I want to create a KML-file (GML, ...) from the features of a vector layer
and at once transform the geometry from the map projection to an other
projection (see the function features2KML). 

But this works only, when I have drawn the features with the editing tools
or added features to the map with OpenLayers.Layer.GML without the parameter
projection (no transformation of the geometry). 

With features from an OpenLayers.Layer.GML etc., added to the map with
parameter projection (works great!), features2KML() returns always the KML
(GML, ...) with the geometry in the map projection. 

The same is for vector layers with features added with
formats[type].read(element.value).

In short: I am not able to transform features, that are added to the map
with projection transformation, from the map projection (fromProjection) to
an other projection (toProjection).

I hope someone can help. I use OL 2.6 stable.

Mit freundlichen Grüssen

Arnd Wippermann
http://gis.ibbeck.de/ginfo/


function features2KML()
{
    fromProjection = winFeatures.document.getElementById('epsgFrom').value;
    toProjection   = winFeatures.document.getElementById('epsgTo').value;

    var vlyr = map.layers[map.aktLayer];
    var msg = "";

    if(vlyr.isVector && typeof vlyr.isVector != 'undefined')
    {
        if(vlyr.features.length>0)
        {
            var type =
winFeatures.document.getElementById("formatType").value;

            var features = vlyr.features;
            if(features.constructor != Array)
                features = [features];

            var myArr = [];
            for(var i=0; i<features.length; ++i)
            {   myArr[i] = features[i].clone();
                myArr[i].geometry.transform(new
OpenLayers.Projection(fromProjection), new
OpenLayers.Projection(toProjection));
            }

            try{
                var derString = formats[type].write(myArr, true);
                msg = derString;
            }catch(e){
                msg = "formats[type].write(myArr, true)\r\n<br>" +
e.message;
            }

        }
        else
            msg = vlyr.name + " enthält keine features";
    }
    else
        msg = vlyr.name + " ist kein Vektorlayer";

    return msg;
}

Mit freundlichen Grüssen

Arnd Wippermann
http://gis.ibbeck.de/ginfo/





More information about the Users mailing list