Search code examples
gisopenlayers-3

How to convert extent from EPSG 3857 to EPSG 26331


I am trying to convert extent which is in 3857 projection into 26331 projection in OpenLayers by ol.proj.transformExtent(map.getView().calculateExtent(map.getSize()),'EPSG:3857','EPSG:26331')


Solution

  • You will first need to include the proj4js library

    <script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.6.2/proj4.js"></script>
    

    Then the EPSG:26331 proj4js definition from https://epsg.io/26331

    proj4.defs("EPSG:26331","+proj=utm +zone=31 +ellps=clrk80 +towgs84=-92,-93,122,0,0,0,0 +units=m +no_defs");
    ol.proj.proj4.register(proj4);  // if you are using OpenLayers 5 or 6
    
    var transformedExtent = ol.proj.transformExtent(map.getView().calculateExtent(map.getSize()),'EPSG:3857','EPSG:26331');