3

I'm trying to load an image on a Leaflet map and to display it at the right place using the world information exported by QGIS.

For image overlays, Leaflet needs the coordinates of the upper left point and of the lower right point (boundaries):

var imageUrl = 'http://www.lib.utexas.edu/maps/historical/newark_nj_1922.jpg',
imageBounds = [[40.712216, -74.22655], [40.773941, -74.12544]]; L.imageOverlay(imageUrl, imageBounds).addTo(map);

In QGIS, when I export my image, I always use the 1:25000 scale and 72 DPI, which is a constant in my workflow.

enter image description here

It outputs a world file (PGW) that contains the information:

8.86711729729699094 //xscale
0 //yskew
0 //xskew
-8.86711729729699094 //yscale
491355.98836270271567628 //xpos
6600748.93414135184139013 //ypos

Knowing all that information, is there a way to convert the world file information into geographical coordinates I could use in Leaflet?

4
  • Change your project CRS to EPSG:4326 before you export your image.
    – user2856
    Commented Oct 3, 2019 at 21:27
  • wow ! It works ! But it distorts my image which is now stretched on the X axis. Any chance to get the best of both ?
    – gordie
    Commented Oct 4, 2019 at 7:14
  • Not really, a geographic coordinate system isn't very good for displaying data other than near the equator.
    – user2856
    Commented Oct 4, 2019 at 8:07
  • Change your leaflet coordinate system maybe.
    – user2856
    Commented Oct 4, 2019 at 8:10

1 Answer 1

1

I found out ! No need to check DPIs or map scale, all required informations are contained in the world file. Here, I'm parsing a PGW file sent through a form. I'm also using the proj4php library to handle projections.

public function get_overlay_bounds_from_pgw($pgwfile,$imagewidth,$imageheight){

    $filename = isset($pgwfile['name']) ? $pgwfile['name'] : null;
    if ( !$filename ) return new WP_Error('tdp_empty_pgw_file','Missing PGW file');

    $arr_file_type = wp_check_filetype( basename( $filename ) );
    if ( !$arr_file_type || ($arr_file_type['ext'] != 'pgw') ) return new WP_Error('tdp_invalid_pgw_file','Invalid PGW file');

    $content = file_get_contents($pgwfile['tmp_name']);
    $lines = explode(PHP_EOL, $content);
    if ( count($lines) < 6 ) return new WP_Error('tdp_invalid_pgw_file','Invalid PGW file');

    //https://en.wikipedia.org/wiki/World_file
    $world = array(
        'xscale'=>  $lines[0],
        'yskew'=>   $lines[1],
        'xskew'=>   $lines[2],
        'yscale'=>  $lines[3],
        'xpos'=>    $lines[4],
        'ypos'=>    $lines[5],
    );

    // Initialise Proj4
    $proj4 = new Proj4php();
    // Create two different projections.
    $projQgis    = new Proj('EPSG:3857', $proj4);
    $projLeaflet  = new Proj('EPSG:4326', $proj4);

    //
    $topleft = new Point($world['xpos'],$world['ypos'], $projQgis);
    $topleft = $proj4->transform($projLeaflet, $topleft);

    $xmeters = $imagewidth * $world['xscale'];
    $ymeters = $imageheight * $world['yscale'];

    $bottomright = new Point($world['xpos'] + $xmeters,$world['ypos'] + $ymeters, $projQgis);
    $bottomright = $proj4->transform($projLeaflet, $bottomright);

    $bounds = array(
        'north'=>   $topleft->x,
        'west'=>    $topleft->y,
        'south'=>   $bottomright->x,
        'east'=>    $bottomright->y,
    );

    return $bounds;
}

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.