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

  • 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? May 30 '13 at 11:34
  • Does this sample show what you want to do: openlayers.org/dev/examples/wfs-reprojection.html? May 30 '13 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
    May 30 '13 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
    Jun 1 '13 at 15:51

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.

  • 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
    Jun 1 '13 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
    Jun 4 '13 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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