I'm trying to create a proj4 custom projection for use with my openlayers map. However, when after creating the projection with proj4 I am unable to use it with openlayers projections because ol.proj.get("EPSG:42304") returns a null value.

Here is the relevant code:

proj4.defs("EPSG:42304", "+title=Atlas of Canada, LCC +proj=lcc +lat_1=49 +lat_2=77 +lat_0=49 +lon_0=-95 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs");
var proj_42304 = ol.proj.get('EPSG:42304');

The error I am receiving in the console is this:

Uncaught TypeError: Cannot read property 'setExtent' of null

I am using OpenLayers 3.20.1 and Proj4 2.2 for reference. I also have my proj4 script imported after I import the openlayers3 library on my HTML page.

If I change ol.proj.get('EPSG:42304') to ol.proj.get('EPSG:900913') it no longer returns null, which leads me to think it's an error with creating the custom projection.

I am a co-op student and this is my first time working with proj4.

  • Are you getting an error message about it being null? If so, please update your question to include the full error message. Are you using logging to determine that it is null? (eg, by console.log())? If so, please update your question to include the logging code, and the logging output. If you are using some other means to determine that it is null, please update your post to include this information. – Son of a Beach Mar 2 '17 at 3:33
  • Hey, yes I am getting an error message about it being null. I will update my post to be more clear, thank you! – Cole Boothman Mar 2 '17 at 17:44
  • Why don'tyou use the atest version of proj4js: github.com/proj4js/proj4js/releases ? – AndreJ Mar 2 '17 at 18:09
  • Hi Andre, I ended up downloading and using the latest version of proj4js and that fixed the problem! thank you – Cole Boothman Mar 2 '17 at 18:45
  • @ColeBoothman please self-answer your question, for the benefit of future users. – AndreJ Mar 2 '17 at 19:30

I managed to fix my problem and just wanted to post the answer incase someone runs into this problem in the future.

I ended up downloading the latest version of proj4js and this fixed the problem. OpenLayers recognized my custom projection after this.


Does it work if you omit the +title parameter? Like the following:

proj4.defs("EPSG:42304", "+proj=lcc +lat_1=49 +lat_2=77 +lat_0=49 +lon_0=-95 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs");

I can't see 'title' in the parameter list at http://proj4.org/parameters.html#parameter-list . Omitting it results in a string that perfectly matches the definition for EPSG:42304 given from the "proj4" link at http://spatialreference.org/ref/sr-org/6754/ .

  • I took a look at the documentation you linked, thank you! I tried taking out the title and using the definition above but it didn't work unfortunately – Cole Boothman Mar 2 '17 at 17:49

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.