OpenLayers: Map doesn't render with a non 'standard' EPSG-code

前端 未结 2 417
借酒劲吻你
借酒劲吻你 2021-01-22 07:00

I have a problem with getting a map in EPSG 3031 to work.

From my research I have already learned that the parameters for the projection have to be defined in the javasc

相关标签:
2条回答
  • 2021-01-22 07:39

    Arcgis sources in non-global projections have non-standard tile grids. @RalphL's method will work if the correct tilegrid can be added, but it can be done automatically by defining it as a TileArcGISRest source. If you want to use OSM as a base layer reprojections such as EPSG:3857 which can't reach the poles to polar coordinates cause errors which OpenLayers doesn't handle and crashes. I get around that by defining transform functions which go via an intermediate projection which let the code catch any errors.

    function reprojectionErrorHandler(projections, opt_intermediate) {
    
      var intermediate = opt_intermediate || 'EPSG:4269';
    
      function transform(projA, projB) {
    
        return function (input, opt_output, opt_dimension) {
            var length = input.length;
            var dimension = opt_dimension !== undefined ? opt_dimension : 2;
            var output = opt_output !== undefined ? opt_output : new Array(length);
            var ll, point, i, j;
            try {
                for (i = 0; i < length; i += dimension) {
                    ll = ol.proj.transform([input[i], input[i + 1]], projA, intermediate);
                    point = ol.proj.transform([ll[i], ll[i + 1]], intermediate, projB);
                    output[i] = point[0];
                    output[i + 1] = point[1];
                    for (j = dimension - 1; j >= 2; --j) {
                        output[i + j] = input[i + j];
                    }
                }
            } catch (e) {}
            return output;
        };
    
      }
    
      if (Array.isArray(projections)) {
        for (i = 0; i < projections.length-1; i++) {
            for (j = i+1; j < projections.length; j++) {
                if (ol.proj.get(projections[i]).getCode() != ol.proj.get(projections[j]).getCode() &&
                    ol.proj.get(projections[i]).getCode() != ol.proj.get(intermediate).getCode() &&
                    ol.proj.get(projections[j]).getCode() != ol.proj.get(intermediate).getCode() ) {
    
                    ol.proj.addCoordinateTransforms(
                        projections[i],
                        projections[j],
                        transform(projections[i], projections[j]),
                        transform(projections[j], projections[i])
                    );
    
                    ol.proj.addCoordinateTransforms(
                        projections[j],
                        projections[i],
                        transform(projections[j], projections[i]),
                        transform(projections[i], projections[j])
                    );
    
                }
            }
        }
      }
    
    }
    
    proj4.defs("EPSG:3031", "+proj=stere +lat_0=-90 +lat_ts=-71 +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs");
    ol.proj.proj4.register(proj4);
    var proj3031 = ol.proj.get('EPSG:3031');
    
    reprojectionErrorHandler(['EPSG:3031', 'EPSG:3857'])
    
    var baseMapLayer = new ol.layer.Tile({
      source: new ol.source.OSM()
    });
    
    var esriArctic = new ol.layer.Tile({
      title: 'ESRI Imagery',
      type: 'base',
      zIndex: 0,
      opacity: 0.5,
      source: new ol.source.TileArcGISRest({
        url: 'https://services.arcgisonline.com/arcgis/rest/services/Polar/Antarctic_Imagery/MapServer'
      })
    });
    
    var map = new ol.Map({
      target: 'map',
      layers: [baseMapLayer, esriArctic],
      view: new ol.View({
        projection: proj3031,
        center: ol.proj.fromLonLat([0, -80], proj3031),
        zoom: 3
      })
    })
       html,
       body,
       #map {
         width: 100%;
         height: 100%;
         overflow: hidden;
       }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.5.0/proj4.js"></script>
    <link href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" rel="stylesheet"/>
    <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
    <body>
      <div id="map" class="map"></div>
    </body>

    Here's @RalphL's method centered on the right place with a tilegrid based on the full extent and maximum resolution listed here https://services.arcgisonline.com/arcgis/rest/services/Polar/Antarctic_Imagery/MapServer Unlike a normal EPSG:3857 tilegrid the tiles are not an exact fit for the extent

    proj4.defs("EPSG:3031", "+proj=stere +lat_0=-90 +lat_ts=-71 +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs");
    ol.proj.proj4.register(proj4);
    var proj3031 = ol.proj.get('EPSG:3031');
    
    var extent = [-3.369955099203E7, -3.369955099203E7, 3.369955099203E7, 3.369955099203E7];
    var maxResolution = 238810.81335399998;
    
    var resolutions = [];
    for (var i = 0; i < 24; i++) {
      resolutions[i] = maxResolution / Math.pow(2, i);
    }
    
    var esriArctic = new ol.layer.Tile({
      title: 'ESRI Imagery',
      type: 'base',
      zIndex: 0,
      source: new ol.source.XYZ({
        url: 'https://services.arcgisonline.com/arcgis/rest/services/Polar/Antarctic_Imagery/MapServer/tile/{z}/{y}/{x}',
        projection: proj3031,
        tileGrid: new ol.tilegrid.TileGrid({ extent: extent, resolutions: resolutions }),
      })
    });
    
    var map = new ol.Map({
      target: 'map',
      layers: [esriArctic],
      view: new ol.View({
        projection: proj3031,
        center: ol.proj.fromLonLat([0, -80], proj3031),
        zoom: 3
      })
    })
       html,
       body,
       #map {
         width: 100%;
         height: 100%;
         overflow: hidden;
       }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.5.0/proj4.js"></script>
    <link href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" rel="stylesheet"/>
    <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
    <body>
      <div id="map" class="map"></div>
    </body>

    The white hole at the pole in reprojections is mostly transparency and a quick fix would be to set the map div background in css to match the ice, for example

       .map {
         background-color: #e7e9f6;
       }
    

    However, a thin white rim is still visible.

    A better solution based on the layer spy eample https://openlayers.org/en/v4.6.5/examples/layer-spy.html might be to clip the real non-transparent white surround from the antarctic layer

    function reprojectionErrorHandler(projections, opt_intermediate) {
    
      var intermediate = opt_intermediate || 'EPSG:4269';
    
      function transform(projA, projB) {
    
        return function (input, opt_output, opt_dimension) {
            var length = input.length;
            var dimension = opt_dimension !== undefined ? opt_dimension : 2;
            var output = opt_output !== undefined ? opt_output : new Array(length);
            var ll, point, i, j;
            try {
                for (i = 0; i < length; i += dimension) {
                    ll = ol.proj.transform([input[i], input[i + 1]], projA, intermediate);
                    point = ol.proj.transform([ll[i], ll[i + 1]], intermediate, projB);
                    output[i] = point[0];
                    output[i + 1] = point[1];
                    for (j = dimension - 1; j >= 2; --j) {
                        output[i + j] = input[i + j];
                    }
                }
            } catch (e) {}
            return output;
        };
    
      }
    
      if (Array.isArray(projections)) {
        for (i = 0; i < projections.length-1; i++) {
            for (j = i+1; j < projections.length; j++) {
                if (ol.proj.get(projections[i]).getCode() != ol.proj.get(projections[j]).getCode() &&
                    ol.proj.get(projections[i]).getCode() != ol.proj.get(intermediate).getCode() &&
                    ol.proj.get(projections[j]).getCode() != ol.proj.get(intermediate).getCode() ) {
    
                    ol.proj.addCoordinateTransforms(
                        projections[i],
                        projections[j],
                        transform(projections[i], projections[j]),
                        transform(projections[j], projections[i])
                    );
    
                    ol.proj.addCoordinateTransforms(
                        projections[j],
                        projections[i],
                        transform(projections[j], projections[i]),
                        transform(projections[i], projections[j])
                    );
    
                }
            }
        }
      }
    
    }
    
    proj4.defs("EPSG:3031", "+proj=stere +lat_0=-90 +lat_ts=-71 +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs");
    ol.proj.proj4.register(proj4);
    var proj3031 = ol.proj.get('EPSG:3031');
    
    reprojectionErrorHandler(['EPSG:3031', 'EPSG:3857'])
    
    var baseMapLayer = new ol.layer.Tile({
      source: new ol.source.XYZ({
        url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
        maxZoom: 23
      })
    });
    
    var extent = [-3.369955099203E7, -3.369955099203E7, 3.369955099203E7, 3.369955099203E7];
    var maxResolution = 238810.81335399998;
    
    var resolutions = [];
    for (var i = 0; i < 24; i++) {
      resolutions[i] = maxResolution / Math.pow(2, i);
    }
    
    var esriArctic = new ol.layer.Tile({
      title: 'ESRI Imagery',
      type: 'base',
      zIndex: 0,
      source: new ol.source.XYZ({
        url: 'https://services.arcgisonline.com/arcgis/rest/services/Polar/Antarctic_Imagery/MapServer/tile/{z}/{y}/{x}',
        projection: proj3031,
        tileGrid: new ol.tilegrid.TileGrid({ extent: extent, resolutions: resolutions }),
      })
    });
    
    esriArctic.on('precompose', function(event) {
      radius = 4000000 / event.frameState.viewState.resolution;
      var ctx = event.context;
      var pixelRatio = event.frameState.pixelRatio;
      ctx.save();
      ctx.beginPath();
      position = map.getPixelFromCoordinate(ol.proj.transform([0, -90], 'EPSG:4326', 'EPSG:3031'));
      // only show a circle around the position
      ctx.arc(position[0] * pixelRatio, position[1] * pixelRatio, radius * pixelRatio, 0, 2 * Math.PI);
      ctx.clip();
    });
    
    // after rendering the layer, restore the canvas context
    esriArctic.on('postcompose', function(event) {
      var ctx = event.context;
      ctx.restore();
    });
    
    var map = new ol.Map({
      target: 'map',
      layers: [baseMapLayer, esriArctic],
      view: new ol.View({
        projection: proj3031,
        center: ol.proj.fromLonLat([0, -80], proj3031),
        zoom: 3
      })
    })
       html,
       body,
       #map {
         width: 100%;
         height: 100%;
         overflow: hidden;
       }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.5.0/proj4.js"></script>
    <link href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" rel="stylesheet"/>
    <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
    <body>
      <div id="map" class="map"></div>
    </body>

    0 讨论(0)
  • 2021-01-22 07:40

    Please have a look at the following snippet:

    proj4.defs("EPSG:3031", "+proj=stere +lat_0=-90 +lat_ts=-71 +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs");
    ol.proj.proj4.register(proj4);
    var proj3031 = ol.proj.get('EPSG:3031');
    
    var esriArctic = new ol.layer.Tile({
      title: 'ESRI Imagery',
      type: 'base',
      zIndex: 0,
      source: new ol.source.XYZ({
        url: 'https://services.arcgisonline.com/arcgis/rest/services/Polar/Antarctic_Imagery/MapServer/tile/{z}/{y}/{x}'
      })
    });
    
    var map = new ol.Map({
      target: 'map',
      layers: [esriArctic],
      view: new ol.View({
        center: ol.proj.fromLonLat([0, -80], proj3031),
        zoom: 3
      })
    })
       html,
       body,
       #map {
         width: 100%;
         height: 100%;
         overflow: hidden;
       }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.5.0/proj4.js"></script>
    <link href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" rel="stylesheet"/>
    <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
    <body>
      <div id="map" class="map"></div>
    </body>

    0 讨论(0)
提交回复
热议问题