Reprojection with EPSG.io Search

Demonstrates client-side raster reprojection of OSM to arbitrary projection

This example shows client-side raster reprojection capabilities from OSM (EPSG:3857) to arbitrary projection by searching in EPSG.io database.

<!DOCTYPE html>
<html>
  <head>
    <title>Reprojection with EPSG.io Search</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.6.4/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://openlayers.org/en/v4.6.4/build/ol.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.4.4/proj4.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <form class="form-inline">
      <label for="epsg-query">Search projection:</label>
      <input type="text" id="epsg-query" placeholder="4326, 27700, US National Atlas, Swiss, France, ..." class="form-control" size="50" />
      <button id="epsg-search" class="btn">Search</button>
      <span id="epsg-result"></span>
      <div>
        <label for="render-edges">
          Render reprojection edges
          <input type="checkbox" id="render-edges">
        </label>
      </div>
    </form>
    <script>
      var map = new ol.Map({
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          })
        ],
        target: 'map',
        view: new ol.View({
          projection: 'EPSG:3857',
          center: [0, 0],
          zoom: 1
        })
      });


      var queryInput = document.getElementById('epsg-query');
      var searchButton = document.getElementById('epsg-search');
      var resultSpan = document.getElementById('epsg-result');
      var renderEdgesCheckbox = document.getElementById('render-edges');

      function setProjection(code, name, proj4def, bbox) {
        if (code === null || name === null || proj4def === null || bbox === null) {
          resultSpan.innerHTML = 'Nothing usable found, using EPSG:3857...';
          map.setView(new ol.View({
            projection: 'EPSG:3857',
            center: [0, 0],
            zoom: 1
          }));
          return;
        }

        resultSpan.innerHTML = '(' + code + ') ' + name;

        var newProjCode = 'EPSG:' + code;
        proj4.defs(newProjCode, proj4def);
        var newProj = ol.proj.get(newProjCode);
        var fromLonLat = ol.proj.getTransform('EPSG:4326', newProj);

        // very approximate calculation of projection extent
        var extent = ol.extent.applyTransform(
            [bbox[1], bbox[2], bbox[3], bbox[0]], fromLonLat);
        newProj.setExtent(extent);
        var newView = new ol.View({
          projection: newProj
        });
        map.setView(newView);
        newView.fit(extent);
      }


      function search(query) {
        resultSpan.innerHTML = 'Searching ...';
        fetch('https://epsg.io/?format=json&q=' + query).then(function(response) {
          return response.json();
        }).then(function(json) {
          var results = json['results'];
          if (results && results.length > 0) {
            for (var i = 0, ii = results.length; i < ii; i++) {
              var result = results[i];
              if (result) {
                var code = result['code'], name = result['name'],
                    proj4def = result['proj4'], bbox = result['bbox'];
                if (code && code.length > 0 && proj4def && proj4def.length > 0 &&
                    bbox && bbox.length == 4) {
                  setProjection(code, name, proj4def, bbox);
                  return;
                }
              }
            }
          }
          setProjection(null, null, null, null);
        });
      }


      /**
       * Handle click event.
       * @param {Event} event The event.
       */
      searchButton.onclick = function(event) {
        search(queryInput.value);
        event.preventDefault();
      };


      /**
       * Handle change event.
       */
      renderEdgesCheckbox.onchange = function() {
        map.getLayers().forEach(function(layer) {
          if (layer instanceof ol.layer.Tile) {
            var source = layer.getSource();
            if (source instanceof ol.source.TileImage) {
              source.setRenderReprojectionEdges(renderEdgesCheckbox.checked);
            }
          }
        });
      };
    </script>
  </body>
</html>