Wednesday, 1 August 2018

Adding a projection in OpenLayers


I am following these instructions in order to define my projection for my map. And then use the ol.proj.transform function to transform the coordinates. So my code snippet is the following:


  var myProjection = new ol.proj.Projection({
code: 'EPSG:2100',
extent: [104022.946289, 3850785.500488, 1007956.563293, 4624047.765686],
units: 'm'
});


ol.proj.addProjection(myProjections);

var center = new ol.proj.transform([21.767997, 39.556105], 'EPSG:4326', 'EPSG:2100');

However if I try to console.log(center) the coordinates are not transformed. I'm still getting the same coordinates which are


21.767997, 39.556105

Does this mean that the addProjection didn't work??



Answer



Yes, unfortunately, that means it didnt work.



So do the following:


include the proj4js library within your project:


https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.15/proj4.js


And then make ol3 aware about EPSG:2100 by adding this line:


proj4.defs("EPSG:2100","+proj=tmerc +lat_0=0 +lon_0=24 +k=0.9996 +x_0=500000 +y_0=0 +ellps=GRS80 +towgs84=-199.87,74.79,246.62,0,0,0,0 +units=m +no_defs");


To add the extent (if you need it), do


ol.proj.get('EPSG:2100').setExtent([104022.946289, 3850785.500488, 1007956.563293, 4624047.765686]);

Finally use your new "known" projection as follows


var center1 = ol.proj.transform([21.767997, 39.556105], 'EPSG:4326', 'EPSG:2100');



Or


var center1 = ol.proj.transform([21.767997, 39.556105], 'EPSG:4326', ol.proj.get('EPSG:2100'));


And here is a fiddle to see it in action


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...