1

I've added a tile base map and and a couple of vector layers using OpenLayers. I'm implementing a change map projection functionality. For that, I need to change map projection and all added layers .

changeLayerProjection(layer, oldProj, newProj) {
    if (layer instanceof olLayerGroup) {
      layer.getLayers().forEach(function(subLayer) {
        this.changeLayerProjection(subLayer, oldProj, newProj);
      });
    } else if (layer instanceof olTileLayer) {         
      var tileLoadFunction = layer.getSource().getTileLoadFunction();
      layer.getSource().setTileLoadFunction(tileLoadFunction);
    } else if (layer instanceof olLayerVector) {
      var currProj = layer.getSource().getProjection();
      var features = layer.getSource().getFeatures();
      if (currProj === null) {
        for (var i = 0; i < features.length; i += 1) {
          features[i].getGeometry().transform(oldProj, newProj);
        }
      }
    }
  }

But for the tile layer, I'm doing like this:

 if (layer instanceof olTileLayer) {         
          var tileLoadFunction = layer.getSource().getTileLoadFunction();
          layer.getSource().setTileLoadFunction(tileLoadFunction);
        } 

getTileLoadFunction will only return that function. I want to change tile layer projection as well. What is best way to do this?

  • 1
    I think just changing the projection of your map view would be just enough, ol should take care of the layers. have a look at this --> freeopengis.blogspot.com/2017/04/… – pavlos Nov 20 '18 at 9:18
  • 1
    Changing the view is sufficient for raster layers, OpenLayers will automatically reproject them. Note that vector tiles cannot be reprojected. Features geometry in vector layers will need to be transformed, that part of your code is correct. The position coordinates of any overlays will also need to be transformed. – Mike Nov 20 '18 at 10:02
  • then , how do i will reproject my Tile vectors, do i need to remove and add it back ? – AhammadaliPK Nov 20 '18 at 10:35
  • 1
    It's been raised as an OpenLayers development issue in the past and some possible workarounds have been suggested github.com/openlayers/openlayers/issues/4491 github.com/openlayers/openlayers/issues/6059 – Mike Nov 20 '18 at 16:08

Your Answer

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

Browse other questions tagged or ask your own question.