1

I have included Proj4js in OpenLayers. I would like to create a function (so I can use a make a button). To change the projection of my map.

I have included in javascript

Proj4js.defs["EPSG:54008"] = "+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs";

And I have a added a GeoJSON layer.

        var world = new OpenLayers.Layer.Vector("World", {
            projection: new OpenLayers.Projection("EPSG:4326"),
            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: new OpenLayers.Protocol.HTTP({
                url: "data/worldcountries.geojson",
                format: new OpenLayers.Format.GeoJSON()
            })
        });     

How can I make a function so I can change the projection of the vector-layer.

Hope someone can help

4
  • What is the projection of your gojson data? what is the Projection of your map? Why do you need a function to change the projection of your vector layer? Are you referring to projecting your layer, so that it appears at the correct location on your map which has a different projection? Commented May 30, 2013 at 11:34
  • Does this sample show what you want to do: openlayers.org/dev/examples/wfs-reprojection.html? Commented May 30, 2013 at 11:43
  • My main question is why do you need reprojection on the client side? It costs a lot to process. If you can, just transform your original geojson data to the right projection and set your projection to EPSG:54008 in your vector layer.
    – ThomasG77
    Commented May 30, 2013 at 13:34
  • I would like to create a function so I can show people the difference in projections (and use it for educational purposes).
    – Stefan
    Commented Jun 1, 2013 at 15:51

1 Answer 1

2

Proj4js reprojects features in place, you just iterate through all the features of the layer and apply the transform method:

function reproject(layer, inputProjection, outputProjection) {
    var source = new Proj4js.Proj(inputProjection);
    var dest = new Proj4js.Proj(outputProjection);

    Proj4js.reportError = function (msg) {
        throw new Error('Whoops: ' + msg);
    };

    for (var i = 0; i < layer.features.length; i++) {
        var feature = layer.features[i];
        Proj4js.transform(source, dest, feature);
    }
}

All features on the layer layer are now reprojected.

2
  • I have a layer named layer in my init. When I use this function I get the error: 'TypeError: layer is undefined' . And example can be found here stefanvanderhoorn.nl/experiments/ol_projections2.html
    – Stefan
    Commented Jun 1, 2013 at 16:10
  • This would indicate a programming error, the best guess would be to use browser's debugger to track down the source of the problem. Since you've already accepted the answer I'm assuming you solved it anyway. BTW. In the page you linked you import Proj4js four times; it's not necessary - first three imports will be overwritten by the last one anyway.
    – kryger
    Commented Jun 4, 2013 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.