0

I'm trying to load an OpenLayers map with UTM projection. If I try to register custom projection, the map becomes blank.

proj4.defs('EPSG:32640', '+proj=utm +zone=40 +datum=WGS84 +units=m +no_defs');

let options = {};
options.code = 'EPSG:32640';
options.units = 'm';


options.extent = [
  -98570.85212537996,
  2468701.5790765425,
  683268.1076887846,
  2874585.9453238174,
];

options.worldExtent = [];

options.axisOrientation = 'enu';

options.global = false;


//options.metersPerUnit = projectionOptions.metersPerUnit;
options.getPointResolution = ()=>{};

let projection = new ol.proj.Projection(options);
//ol.proj.addProjection(projection); // if I uncomment this the map becomes blank
//let ext=ol.proj.get('EPSG:32640').getExtent();
debugger;
//let center= ol.extent.getCenter(ext);
var olview = new ol.View({
  center: [312234.8270497762, 2636116.2213047906],
  extent:options.extent,
  projection: 'EPSG:32640',
  zoom: 8,
});
var map = new ol.Map({
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM(),
    }),
  ],
  target: 'map',
  view: olview,
}); 

Plunker

ol.proj.addProjection(projection); // if I uncomment this line, map becomes blank
1

You have to set extent also on projection:

proj4.defs('EPSG:32640', '+proj=utm +zone=40 +datum=WGS84 +units=m +no_defs');

extent = [
  -98570.85212537996,
  2468701.5790765425,
  683268.1076887846,
  2874585.9453238174
];

ol.proj.get('EPSG:32640').setExtent(extent);

var olview = new ol.View({
  center: [312234.8270497762, 2636116.2213047906],
  extent: extent,
  projection: 'EPSG:32640',
  zoom: 0,
});

var map = new ol.Map({
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM(),
    }),
  ],
  target: 'map',
  view: olview,
});
0

When the projection definition is available from proj4 it's no longer a custom projection, you can simply get it from proj4 and set its extent

replace

let projection = new ol.proj.Projection(options);
ol.proj.addProjection(projection);

with

let projection = ol.proj.get(options.code);
projection.setExtent(options.extent);

Are your coordinates correct? This is what I getenter image description here

Here's part of a larger UTM 40 projection map displaying a zone 40 graticule. I've drawn an appropriate box formed by your coordinates on the screenshot. The previous result is what I would expect with those values.enter image description here

  • still no luck , my map still showing a different extent than expected – AhammadaliPK Nov 19 '18 at 7:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.