Search This Blog

Tuesday, April 5, 2011

Including Projections in OpenLayers

If you are using the OpenLayers viewer and you need to operate with the Projection of the map and layers, perhaps you notice that the behavior of the functions (e.g. OpenLayers.Bounds.transform) is not the expected. More specifically, it seems that they don't do anything at all.

The origin of that is that OpenLayers only includes the EPSG4326 and EPSG90013 projections. If you use another one, you'll have to include it using the proj4js library.



You can see all the necessary documentation in:
http://trac.osgeo.org/openlayers/wiki/Documentation/Dev/proj4js

Here there are the steps I followed:
  • First of all, I downloaded the most recent version of Proj4js.
  • Then, I included the corresponding Javascript file in my HTML page:
<script type="text/javascript" src="proj4js/proj4js-compressed.js></script>
Proj4js.defs["EPSG:23029"] = "+proj=utm +zone=29 +ellps=intl +units=m +no_defs";
  • Finally, I copied the Proj4 format of the projection in a Javascript file which I also included in my page:
<script type="text/javascript" src="proj4js/defs/EPSG23029.js></script>



And now all the operations over projections with the projection EPSG23029 work well.