2

I have a MapInfo TAB file which looks like this:

!table
!version 300
!charset WindowsLatin1

Definition Table
  File "hp.tif"
  Type "RASTER"
  (400000,1300000) (0,0) Label "TL",
  (500000,1300000) (10000,0) Label "TR",
  (500000,1200000) (10000,10000) Label "BR",
  (400000,1200000) (0,10000) Label "BL"
  CoordSys Earth Projection 8, 79, "m", -2, 49, 0.9996012717, 400000, -100000
  Units "m"

I also have an image file for the referenced area. I'm trying to convert this to an mbtiles file which is almost working but it's slightly out of alignment.

I'm obtaining the bounding box from this file which is 400000, 1200000, 500000, 1300000 which I'm then converting to latitude / longtiude which is -2.0018997431234, 60.670742105011, -0.17172476489587, 61.580961113449.

I'm then using the following code to convert this to an MBTiles grid using zoom 12:

$x = (int)floor((($longitude + 180) / 360) * pow(2, $zoom))
$y = pow(2, $zoom) - (int)floor((1 - log(tan(deg2rad($latitude)) + 1 / cos(deg2rad($latitude))) / pi()) / 2 * pow(2, $zoom)) - 1;

This produces a grid of 2025 - 2046 and 2921 - 2943.

So far so good. Next, I scale the associated image to 256px * tiles X x 256px * tiles Y and export 256x256 tiles mapped to the grid tiles/columns.

This produces an mbtiles file which works but is slightly out of alignment with the the real map.

I have attempted to export the same files using MapTiler Desktop and this produces an mbtiles file with the same grid of 2025 - 2046 and 2921 - 2943 but it aligns perfectly. I can't for the life of me figure out why.

What is the -100000 in the CoordSys Earth Projection line for? MapTiler shows the geographical location as 400000 10 0 1300000 0 -10. I'm wondering if the -10 is an offset or something that I need to take into account.

1
  • Just guessing but perhaps the native bbox gets rotated when converted into EPSG:4326 and therefore your lon/lat bounding box is not quite right.
    – user30184
    Commented Feb 5, 2020 at 13:47

1 Answer 1

1

The CoordSys clause exactly describes the British National Grid (EPSG:27700). -100000 is the false northing, and is correct.

10 and -10 describe the size of one pixel in the original coordinate system (Y is negative because bitmap coordinates go top-to-bottom).

If the corners of the original TIFF file do not lie exactly on MBTiles corners, then you cannot simply split the bitmap. You have to reproject the bitmap, and add margins (this is probably what MapTiler does).

8
  • Thanks for the clarification, that makes sense. I doubt the original TIFF is exactly on an MBTiles corner. How would I calculate the offset to re-align the tile? I've not worked with mapping before so I'm learning "on the job", any pointers would be greatfully received!
    – Dan
    Commented Feb 5, 2020 at 14:33
  • It's not a simple offset. Convert all four corner points to lat/long separately, and compare the coordinates.
    – CL.
    Commented Feb 5, 2020 at 14:41
  • I'm getting somewhere now I understand the problem. I have calculated the correct origin for the tiles and the difference in degrees. I'm not sure how I translate that difference into pixels to reproject the tile though.
    – Dan
    Commented Feb 5, 2020 at 14:54
  • For every tile, you have to project its corners back into BNG, and then into pixel coordinates, and then extract that (non-rectangular) part of the bitmap.
    – CL.
    Commented Feb 5, 2020 at 14:57
  • I'm getting way beyond my limited knownledge here. Do you know of any resources I could read to help understand what I need to do? I'm fine with the image manipulation but the mapping calculations I'm struggling with.
    – Dan
    Commented Feb 5, 2020 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.