TransWikia.com

Determine if a polygon feature is within a projection range in Openlayer 4

Geographic Information Systems Asked by Elena Politi on December 29, 2020

This question comes following a previous one that I posted yesterday (link).

I have to show on a World Ocean Base map (projected as EPSG:3857) a number of areas. These are contained in a series of geojson files. Some of them are in the Antarctic. Hence I developed a switcher to change projection, from EPSG:3857 to Orthographic South.

The function is as follows

//=========================================
//=========================================  
// *****CHANGE OF PROJECTION*****
//=========================================
//=========================================

//CREATES A CIRCLE CANVAS TO CROP THE ORTHOGRAPHIC PROJECTION IN A PERFECT CIRCLE
var circle = new ol.geom.Circle([0, 0], 6370997);
var style = new ol.style.Style({
  fill: new ol.style.Fill({
  color: "black"
  })
});
var createCircle = function(e) {
    var vectorContext = e.vectorContext;
    e.context.globalCompositeOperation = "destination-in";
    vectorContext.setStyle(style);
    vectorContext.drawCircle(circle);
    e.context.globalCompositeOperation = "source-over";
}
var viewProjSelect = document.getElementById('view-projection');

//FUNCTION TO MANAGE THE CHANGE OF PROJECTION
function updateViewProjection() {
        
        var newProj = viewProjSelect.value;

        if(newProj && newProj=="ORTHO-SOUTH") {
        var newView = new ol.View({
        projection: newProj,
        center: [0,-90],
        zoom: 0,
        minZoom: 0        
        });
        map.setView(newView);

          function updateLayer(layer) {
                if (layer.getSource) {
                    var source = layer.getSource();
                    if (source && source.getSource) {
                        // cluster source
                        source = source.getSource();
                    }
                    if (source && source.forEachFeature) {
                        source.forEachFeature(function(feature){
                            feature.getGeometry().transform(mercatorProjection, newProj);
                      });
                    }
                }
          } 

            map
            .on('postcompose', createCircle);
            map.getLayers().forEach(function(layer) { updateLayer(layer) });
  

        }
          else if (newProj && newProj=="EPSG:3857") {
            var newView = new ol.View({
            projection: newProj,
            center: [0,0],
            zoom: 2,
            minZoom: 1
            });
            map.setView(newView);
          function updateLayer(layer) {
                if (layer.getSource) {
                    var source = layer.getSource();
                    if (source && source.getSource) {
                        // cluster source
                        source = source.getSource();
                    }
                    if (source && source.forEachFeature) {

                        source.forEachFeature(function(feature){
                            
                            feature.getGeometry().transform("ORTHO-SOUTH", mercatorProjection);
                        });
                    }
                }
          } 
            map
            .un('postcompose', createCircle);
            map.getLayers().forEach(function(layer) { updateLayer(layer) });

        }
    };

//Handle change of projection

viewProjSelect.onchange = function() {
      updateViewProjection();
  };

The switcher works well if I use only features which boundaries are within the range of the Orthographic South projection. If I add areas outside that ranges (which are perfectly visible on the Mercator projection) they mess up the function (the author who helped me in developing the above function mentioned this problem in his answer). I searched on the forum and found that a possible solution is to use

ol.extent.containsExtent(extent2, extent1)

I tried the following solution, that doesn’t work

var orthoExtent = [-6370997, -6370997, 6370997, 6370997];
var featExtent = feature.getGeometry().getExtent();

if (ol.extent.containsExtent(orthoExtent, featExtent)) {
  feature.getGeometry().transform(mercatorProjection, newProj);
}
else {//feature is not projected}

Any suggestion? My idea is, in order to avoid the mess up, to select only the features that are within the range of the OrthoGraphic projection and excluding the other ones (that are not visible anyway). If I change back to the mercator projection, then everything can be mapped again.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP