[OpenLayers-Users] How to change geometry's projection back and forth

Lehtonen, Mika mika at digikartta.net
Mon Dec 8 09:57:03 EST 2008


Hi,
I am trying to limit the size of a drawn geometry (regular polygon). So 
after the feature is created, I jump to my limiting function which 
suppose to redraw the geometry taking into account the set limit. My map 
is in EPSG:900913 (spherical mercator) because of the Google base maps, 
but all my geometries are natively in EPSG:2393. So in order to 
accomplish some comparison I have to reproject the drawn polygon into 
2393, do the comparison and then reproject it back to 900913. The drawn 
geometry reprojects nicely to 2393 (with proj4js) but I can't get the 
new geometry reprojected back. What's wrong with this? Here is the 
essential:
                    geometria = event.feature;
                    kkj = geometria.geometry;
                    kkj.transform(new 
OpenLayers.Projection("EPSG:900913"), new 
OpenLayers.Projection("EPSG:2393"));
                    radius = 
OpenLayers.Number.limitSigDigs((kkj.getBounds().getHeight()/1000),2);
                    if (radius > 50) {
                    var scale = 50 / radius;
                    var x = kkj.getBounds().getCenterLonLat().lon;
                    var y = kkj.getBounds().getCenterLonLat().lat;
                    var origin = new OpenLayers.Geometry.Point(x,y);
                        kkj.resize(scale,origin,1);
alert(kkj);
                    kkj.transform(new 
OpenLayers.Projection("EPSG:2393"), new 
OpenLayers.Projection("EPSG:900913"));
alert(kkj);   /* this is exactly the same than it was before */
                    geometria.geometry = kkj;
                    vlayer.drawFeature(geometria);
                    // vlayer.redraw(true);
                    OpenLayers.Util.getElement('metapalkki').innerHTML = 
" Hakuympyrän säde voi olla max. 50 km!";
                    } else {
                    OpenLayers.Util.getElement('metapalkki').innerHTML = 
" Hakuympyrän säde on " + radius + "km";
                    }

If I do this without reprojection, the geometry limitation works, 
although the scale is a little bit wrong and my other comparisons don't 
work because they should be using 2393.

- mika -



More information about the Users mailing list