[GIS] openlayers display projection

coordinate systemdisplayopenlayers-2

I'm displaying coordinates in EPSG:900913 using ol.control.MousePosition() with OSM.
I'd like to convert these on the fly in another EPSG:2154.

I call in the header :

<script src="./proj4js-1-1-0/lib/proj4js-compressed.js" type="text/javascript"></script>

Then my html :

Proj4js.defs["EPSG:2154"] = "+proj=lcc +lat_1=49 +lat_2=44 +lat_0=46.5 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs";

var map = new OpenLayers.Map({
        div: "map",
        projection: "EPSG:900913",
        controls:[
                new OpenLayers.Control.LayerSwitcher({'ascending':false}),
                new OpenLayers.Control.ScaleLine({
                    'geodesic': "true"
                }),
                 new OpenLayers.Control.Navigation(),
                 new OpenLayers.Control.KeyboardDefaults(),
                 new OpenLayers.Control.MousePosition(
                     {displayProjection: new OpenLayers.Projection("EPSG:2154")}
                 )
                ]
        });

Best Answer

OpenLayers won't know the definitions of the projections just because of the included Proj4js script. The projections which OpenLayers 2 supports natively are the following: EPSG:4326, CRS:84, urn:ogc:def:crs:EPSG:6.6:4326, EPSG:900913, EPSG:3857, EPSG:102113 and EPSG:102100. If you have to use an other projection, you have to define the coordinate system manually before using it in OpenLayers. You can look up references for projections on spatailreference.org.

The definition of the EPSG:2154 is the following:

+proj=lcc +lat_1=49 +lat_2=44 +lat_0=46.5 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 

So before you can use this projection in OpenLayers, you have to define it in your code first like this:

Proj4js.defs["EPSG:2154"] = "+proj=lcc +lat_1=49 +lat_2=44 +lat_0=46.5 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs";

Or if you want to go the lazy way you can include Proj4js's predefined projection file as a script in your header:

<script src="http://spatialreference.org/ref/epsg/rgf93-lambert-93/proj4js/" type="text/javascript"></script>

I have created a fiddle to show the process. I have used OSM in it, which uses EPSG:900913, similar to EPSG:3857.

Related Question