[OpenLayers-Users] reprojection of vector layer from wgs84 to sphericalmercator

Christopher Schmidt crschmidt at metacarta.com
Mon Jan 14 11:05:39 EST 2008


On Mon, Jan 14, 2008 at 10:09:46AM -0500, Mike Adair wrote:
> Note that this should work too, and allows you to handle points in 
> coordinate systems other than EPSG:4326:
> 
> - include proj4js in your app via the <script> tag

This isn't neccesary for spherical mercator: the reprojection code is
registered as a 'custom' projection so long as you have included
Layer.SphericalMercator in your build. 

> preFeatureInsert = function(feature) {
>     var src = new OpenLayers.Projection('EPSG:4326');
>     var dest = new OpenLayers.Projection('EPSG:900913');
>     for (var i = 0; i < feature.geometry.components.length; i++) {
> 	OpenLayers.Projection.transform(feature.geometry.components[i], src, dest);
>    }
> };

Geometries can project themselves: instead of looping through the
componetns, you should just do:

  feature.geometry.transform(src, dest);

This will transform in place.  

Regards,
-- 
Christopher Schmidt
MetaCarta



More information about the Users mailing list