Poul K. Sørensen
Poul K. Sørensen

Reputation: 17540

Parsing Projection WKT in OpenLayers 3

I am looking for a method (javascript) (external or internal part of ol3) that can parse a projection WKT to its proj4 text and create the projection.

 <SRS>PROJCS["GDA94 / MGA zone 53",GEOGCS["GDA94",DATUM["Geocentric_Datum_of_Australia_1994",SPHEROID["GRS 1980",6378137,298.2572221010002,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6283"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4283"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",135],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",10000000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","28353"]]</SRS>

Above shows an example of the XML element containing the SRS.

I have found out that if I can convert this to proj4 text. Then i can add it as a projection this way:

proj4.defs("EPSG:28353", "+proj=utm +zone=53 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs");

var def = proj4.defs(grid.srs);
var units = def.units;
        var proj = new ol.proj.Projection({
            code: grid.srs,
            units: units,
            axisOrientation: def.axis
        });
        proj.setExtent(/*...*/);
        ol.proj.addProjection(proj);
        var proj4Transform1 = proj4('EPSG:3857', grid.srs);
        ol.proj.addCoordinateTransforms(ol.proj.get('EPSG:3857'), proj,
            proj4Transform1.forward, proj4Transform1.inverse);
        var proj4Transform2 = proj4('EPSG:4326', grid.srs);
        ol.proj.addCoordinateTransforms(ol.proj.get('EPSG:4326'), proj,
            proj4Transform2.forward, proj4Transform2.inverse);

Is it possible to find the extend of the projection in the WKT also or should I look this up also external?

Upvotes: 2

Views: 1761

Answers (1)

ahocevar
ahocevar

Reputation: 5647

proj4js supports WKT strings too. You can throw what you have (excluding the <SRS> tag) directly at proj4js, using the same syntax:

proj4.defs("EPSG:28353", "PROJCS["GDA94 / MGA zone 53",GEOGCS["GDA94",DATUM["Geocentric_Datum_of_Australia_1994",SPHEROID["GRS 1980",6378137,298.2572221010002,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6283"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4283"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",135],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",10000000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","28353"]]");

Once you have defined a projection using proj4.defs(), it will be immediately be available in OpenLayers 3, and all transforms will be registered. So the proj4.defs() line is all you need unless you want to set additional options, like the projection extent in your case. There are two options to do this: 1) globally:

ol.proj.get('EPSG:28353').setExtent(*/ ... */);

or 2) just for one ol.proj.Projection instance that you use to e.g. configure your ol.View:

var projection = new ol.proj.Projection({
  code: 'EPSG:28353',
  extent: /* ... */
});

Upvotes: 2

Related Questions