2

I'm working on a map viewer project and used openlayers 2 before this. Now I have to use OpenLayers 3 and map viewer app should support many different projections because I have wms and wfs layers from different sources and projections. I've found examples that use openlayers2 and proj4js. But I couldn't find explicit example for using ol3 and proj4js. What is your suggestion?

Haktan Aydın
  • 581
  • 1
  • 6
  • 21

2 Answers2

11

It seems that sometimes, like when using OpenLayers 3 and proj4 in Angular 2 using webpack, one needs to explicitly tell OL3 where to find it:

import * as proj4 from "proj4";
import * as ol from "openlayers";
...
ol.proj.setProj4(proj4);

When all is fine, then after defining a projection, ol.proj.get should return it:

proj4.defs("EPSG:28992", "...");
if (!ol.proj.get('EPSG:28992')) {
    console.error("Failed to register projection in OpenLayers");
    ...
}
Arjan
  • 22,808
  • 11
  • 61
  • 71
  • 1
    Spent 2 days trying to figure out why proj4.defs wasn't setting my custom projection. About to give up and a random last ditch search led me here, and what do you know.... setProj4(...) was the bloody solution. NONE OF THE OL DOCS Mention this :-( – shawty Aug 20 '17 at 22:20
  • PPS: This is with the current ver of OL4 that's in NPM at the date of this comment being written, so it's still the same even since OL3 – shawty Aug 20 '17 at 22:21
  • Seeing/using this if statement helped me trouble shoot, even though this wasn't directly related to my issue. – loctrice Aug 24 '17 at 16:34
  • With openlayers 4 and typescript, I can't seem to get a reference to either the ol object or ol.proj, anyone having this working? – chrismarx May 23 '18 at 22:55
  • 1
    Ah whoops, I'm on OL5 beta, where it looks like they either got rid of setProj4, or haven't implemented it yet, but you can call register manually and it works again: http://openlayers.org/en/beta/apidoc/module-ol_proj_proj4.html – chrismarx May 24 '18 at 00:02
  • 1
    @chrismarx I found the following in a branch in which I tried to migrate from OL4 to using modules (`npm install ol` rather than `npm install openlayers`) and OL5 beta: `import setProj4 from 'ol.proj.projection'`. However, I stopped that migration as I could not get the typings to work, so I might not have tested that specific part. Also, maybe your approach is actually to be preferred. – Arjan May 24 '18 at 09:27
3

This example demonstrates how to use proj4js with ol3: http://openlayers.org/en/v3.3.0/examples/wms-image-custom-proj.html

Basically using proj4js in OpenLayers 3 works transparently. You need to throw the srs definition at proj4js first, and then you can use the projection right away:

proj4.defs("EPSG:21781","+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1 +x_0=600000 +y_0=200000 +ellps=bessel +towgs84=660.077,13.551,369.344,2.484,1.783,2.939,5.66 +units=m +no_defs");
var zurich = ol.proj.transform([8.55, 47.366667], 'EPSG:4326', 'EPSG:21781');
ahocevar
  • 5,448
  • 15
  • 29
Alexandre Dubé
  • 2,769
  • 1
  • 18
  • 30
  • For more background information, you can also read http://ahocevar.net/2014/07/10/proj4js-2-2-x-with-ol3.html. – ahocevar Mar 22 '15 at 20:15