scott_f
scott_f

Reputation: 912

How to Determine Zoom Height to Request a Tile From a WMTS Server

I am setting up a WMTS client to consume tiles from tileserver.php.

To request a tile I need to pass in the identifier/layer, the zoom height, the col and row to the WMTS RESTful server and it will return the tile.

e.g. http://my ip.com/tileserver/tileserver.php?/index.json?/identifier/{z}/{x}/{y}.png

In my test environment I can manually choose a zoom level and my test code will correctly calculate the col(x) and row(y). I then can use this information to request the image.

This all works as expected.

However, now I need to workout how to programmatically determine the zoom height(z). E.g. as the user zooms in or out of an area, I will need to workout the appropriate zoom level to, in turn, request an image from the tile server at the zoom level that approximates what the user is expecting.

I assume, to workout the correct zoom level:

  1. Determine the current bounding box for the user (this will be model space in a cad program). I will use the bounding box to get the linear width / height of the location they are located (in CAD).

  2. Use the screen pixel size of the monitor to workout pixel per meter? Am I going in the right direction?

Can anyone help me sort out how to do this please?

Upvotes: 2

Views: 660

Answers (1)

scott_f
scott_f

Reputation: 912

Ok, it has finally soaked in as to what the answer is.

I will note it here encase someone else needs to work this out and they don't want to waste days of their life and their employers money in trying to sort this.

TileSpan is what I was after. The TileSpan is the width of the image (tile) that is returned from the tileserver.

I will include the basic calculations below.

I need to use the zoom level that generates a tile with a TileSpan that is just larger than the width of the target area.

I will iterate the tile matrices to discover the correct tile matrix to use.

  1. Get the Capabilities document from the tileserver
  2. Iterate the appropriate tile matrix set and examine each child element (tile matrix)
  3. Calculate the TileSpan for each tile matrix to find the tile matrix that contains the first TileSpan that is just larger than the area on interest.
  4. Calculate the col (x) and row (y) and use the zoom level (z) or Identifier as it is called in the Capabilities document to request the image.

Here are the Tile Matrix calculations for TileSpan to clarify:

See page 22: https://portal.ogc.org/files/?artifact_id=35326[OpenGIS Web Map Tile Service Implementation Standard][1]

pixelSpan = scaleDenominator × 0.00028  / metersPerUnit(crs);
tileSpanX = tileWidth × pixelSpan; 
tileSpanY = tileHeight × pixelSpan;
tileMatrixMaxX = tileMatrixMinX + tileSpanX × matrixWidth;
tileMatrixMinY = tileMatrixMaxY - tileSpanY × matrixHeight;

I will update this answer as I improve my understanding.

Upvotes: 1

Related Questions