openlayersopenlayers-6proj4jsangular-openlayers

Openlayers - transform polygon from a specific projection like EPSG:28992


How can I add a simple polygon with a specific projection to my map?

The polygon is valid, I checked before.

coordinatesPolygonInRd = [ [ [173563, 441818], [173063, 441818], [173063, 444318],
      [173563, 444318], [173563, 441818] ] ];

This is how I tried to transform the polygon before putting it on my map:

Try 1:

let dutchProjection = new Projection({
  code: 'EPSG:28992',
  extent: [-285401.92, 22598.08, 595402.0, 903402.0],
  worldExtent: [3.2, 50.75, 7.22, 53.7],
  units: 'm'
});
addProjection(dutchProjection);

const geometry = new Polygon( this.coordinatesPolygonInRd).transform( 'EPSG:28992', this.map.getView().getProjection());
this.vectorLayer.getSource().addFeature(new Feature(geometry));

Try 2:

proj4.defs["EPSG:28992"] = "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000  +ellps=bessel  +towgs84=565.040,49.910,465.840,-0.40939,0.35971,-1.86849,4.0772 +units=m +no_defs";
register(proj4)
let dutchProjection = GetProjection('EPSG:28992');
const geometry = new Polygon( this.coordinatesPolygonInRd).transform( 'EPSG:28992', this.map.getView().getProjection());
this.vectorLayer.getSource().addFeature(new Feature(geometry));

Solution

  • Method 2 will work but your proj4 syntax is wrong, it should be

    proj4.defs("EPSG:28992","+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000  +ellps=bessel  +towgs84=565.040,49.910,465.840,-0.40939,0.35971,-1.86849,4.0772 +units=m +no_defs");
    

    You can optionally set projection extent, etc. after registering the proj4 definition.