0

I am attempting to port an Openlayers2 site to OL5. The OL2 implementation has been working fine for years and does not have the issues described here.

I have most stuff working but am stumped by the following: the displayed mouse position, and the map.on events all report positions approx 5km south and 3km west of the true position on the map.

All maps are TMS grids in EPSG:2193. I allow display of mapposition coordinates in a range projections - including the NZ meter grid NZTM2000 (EPSG:2193) and lat/long WGS (EPSG:4326). But all coordinates are out by approximately 5km/3km or their degree equivalent in whatever projection I use. The magnitude of the error remains constant across all zoom levels and across the entire 2000km/3000km area of the map.

The relevant bits of code are below. Has anyone seen anything similar or got any suggestions of where to try looking/troubleshooting for mistakes? It's got me completely stumped.

proj4.defs('EPSG:2193', '+proj=tmerc +lat_0=0 +lon_0=173 +k=0.9996 +x_0=1600000 +y_0=10000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs');
ol.proj.proj4.register(proj4);

var view = new ol.View({
                         center: [1600000, 5500000],
                         zoom: 2,
                         projection: ol.proj.get('EPSG:2193'),
                         maxResolution: 4891.969809375,
                         numZoomLevels: 11
            });
            mpc= new ol.control.MousePosition({
                 coordinateFormat: createStringXY(current_projdp),
                 projection: ol.proj.get('EPSG:'+current_proj)
            });
            topo50_2009=new TileLayer({
                source: new XYZ({
                  projection: ol.proj.get('EPSG:2193'),
                  url: 'http://au.mapspast.org.nz/topo50/{z}/{x}/{-y}.png',
                  maxResolution: 4891.969809375,
                  numZoomLevels: 11
                }),
                name: 'NZTM Topo 2009',
                visible: true,
                projection: ol.proj.get('EPSG:2193'),
                maxResolution: 4891.969809375,
                numZoomLevels: 11

              });

      map_map = new Map({
        view: view,
        target: 'map',
        layers: [topo50_2009],
        controls: defaultControls().extend([  mpc ]),
      });

==

Regarding HTML - there is nothing on the page other than the map at this stage.

  <body>
    <div id="map" class="map"></div>
    <script> init_map(); </script>
  </body>
madpom
  • 1
  • 4
  • You must define the extent for the projection and/or define a custom tilegrid, otherwise the tilegrid will default to the extent used by EPSG:3857 – Mike Mar 28 '20 at 23:19

1 Answers1

0

Thanks Mike. If that had been an answer rather than a comment then I'd have marked it as a solution.

None of the values needed for tilegrid were specified in the openlayers2 app, so I guess it had been using defaults which were correct for all of the EPSG2193 TMS layers I have. And that ol5 has used different default values. I dug around in the openlayers2 web app using a debugger and found what values that had defaulted to. Applying them to an ol5 tilegrid (as follows) resolved the issue. The coordinates are now correct.

var origin=[-20037508, 20037508];
var resolutions=[156543.0339,
                            78271.51695,
                            39135.758475,
                            19567.8792375,
                            9783.93961875,
                            4891.969809375,
                            2445.9849046875,
                            1222.99245234375,
                            611.496226171875,
                            305.7481130859375,
                            152.87405654296876,
                            76.43702827148438,
                            38.21851413574219,
                            19.109257067871095,
                            9.554628533935547,
                            4.777314266967774];
var extent=[-20037508, -20037508, 20037508, 20037508];
var tilegrid=new ol.tilegrid.TileGrid({
        origin: origin,
        resolutions: resolutions,
        extent: extent});
topo50_2009=new TileLayer({
    source: new XYZ({
      projection: epsg2193,
      url: 'http://au.mapspast.org.nz/topo50/{z}/{x}/{-y}.png',
      maxResolution: 4891.969809375,
      numZoomLevels: 11,
      tileGrid: tilegrid
    }),
    name: 'NZTM Topo 2009',
    visible: false,
    projection: epsg2193,
    maxResolution: 4891.969809375,
    numZoomLevels: 11
  });
madpom
  • 1
  • 4