3

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")}
                 )
                ]
        });
3

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.

  • 1
    Thx. I added <script src="spatialreference.org/ref/epsg/rgf93-lambert-93/proj4js" type="text/javascript"></script> in the header and got the message "Proj4js is not defined". I can't figure out the reason. But I have read this osgeo-org.1560.x6.nabble.com/…, sort of compatibility issue. Does it make sense ? – Leehan Sep 15 '14 at 12:09
  • Yes, it can make sense. Try to use Proj4js 1.x instead. When I'll have some time, I will try it, too. Maybe Proj4js 2.x's methods and output differs from 1.x versions and 2.x is only implemented in OL3. – Gabor Farkas Sep 15 '14 at 12:13
  • I tried with proj4js1.1.0 : it works. But now of course, 3857 is unknown : I have some problem to define 3857 with the proj4js.defs. Should it be written between <script> in the header ? – Leehan Sep 15 '14 at 12:36
  • You only have to include external resources in an individual <script> tag. EPSG:3857 is a default projection in OpenLayers 2 (in theory), so you only have to define EPSG:2154. If you use an external script file, you have to include it, otherwise you can define it in your code with the Proj4js.defs function. If nobody can get a working result until evening, I will take a look, too. – Gabor Farkas Sep 15 '14 at 13:09
  • 1
    Because I couldn't find any WMS map that uses EPSG:3857, I have used OSM, which uses EPSG:900913, similar to 3857 (wiki.openstreetmap.org/wiki/EPSG:3857). Are the transformed coordinates correct on the map? You can see the external resources I have used in the corresponding tab. jsfiddle.net/GFarkas/9cgz3Lcd – Gabor Farkas Sep 15 '14 at 15:09

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.