0

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??

3

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

| improve this answer | |
  • @ahocevar. Thanks for edits and tips . I have also updated the fiddle – pavlos Feb 4 '17 at 10:22
  • Nice. It works. I was aware of the proj4js library but I thought the solution provided by the link in my answer would be sufficient. Any idea why it didn't work? – Stefanos Feb 5 '17 at 14:08
  • Well, seems that the doc your provide is not very clear. But you should better rely on live examples. -->openlayers.org/en/latest/examples/?q=projection. There are few of them which may help you. – pavlos Feb 5 '17 at 16:33
  • I just noticed that whatever I do, OpenLayers will by default render the WMS service with SRS 3857. Even if I set in my metadata section "wms_srs" 'ESPG:2100' and in LAYER metadata as well, OpenLayers will try the GetMap request with EPSG:3857 and if I've set to something else it will throw this error: "WMS server error. Invalid CRS given : CRS must be valid for all requested layers". So what's the point in reprojecting if I always have to advertise my WMS data in 3857 ? – Stefanos Feb 7 '17 at 0:01
  • Might be something wrong on the way you do it. You have varius options to deal with reprojection(client side, server side). check this example-->openlayers.org/en/latest/examples/… but you should better make a new question to demonstrate your case! – pavlos Feb 7 '17 at 8:53

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.