# Easy way in PyQGIS to convert a distance from meters to coordinates?

I am attempting to clip DEM rasters (USGS, in WGS 84) into a set of tiles measuring 2.56km on a side, in QGIS, using python.

So far, it appears that gdal_translate is working for me, using a bounding box in latitude/longitude coordinates. My problem is that I need to know how to convert 2.56 km into the equivalent distance in latitude and longitude. Thus far I've been getting by in other applications using the following formula, found online here: http://fmepedia.safe.com/articles/How_To/Calculating-accurate-length-in-meters-for-lat-long-coordinate-systems

``````rLat = math.radians(mapCenterLat)
metersPerDegreeLat = 111132.92 - 559.82 * math.cos(2 * rLat) + 1.175 * math.cos(4 * rLat)
metersPerDegreeLong = 111412.84 * math.cos(rLat) - 93.5 * math.cos(3 * rLat)
degreesPerMeterLong = 1.0 / metersPerDegreeLong
degreesPerMeterLat = 1.0 / metersPerDegreeLat
``````

It's black magic, but it seems like it's worked for me so far. However, in this instance when I plug in that equation, and multiply my resulting degreesPerMeter numbers times 2560, the resulting rectangle ends up being more like 3.45 km wide by 2.49 km high.

Clearly there must be a better way in QGIS to figure this out, without resorting to black magic, right?

• Are you using GDAL_Translate -projwin? If so in the most recent GDAL you can use -projwin_srs srs_def (eg: EPSG:4326 for WGS84/Geographic) and let GDAL_Translate do it for you. Note there will be some overlap between adjacent tiles (clips) as a rectangle becomes trapezoidal when projected but rasters must be rectangular so the clip is expanded slightly. – Michael Stimson Feb 28 '19 at 1:26
• Please removed the unrelated question. 1/3 arc second pixels are 9.2592592592592593e-05 degrees apart. What that means in the real word varies by where in the real world it is measured. – Vince Feb 28 '19 at 1:41
• Yes, I am using -projwin. Not sure what it looks like exactly to let GDAL_Translate do it for me... I don't currently have my map center point in meters, all I know is I want it to be at (-123.0,44.0) in coords. – chriscalef Feb 28 '19 at 1:44
• Hm, well this is interesting: while the tiles I created appear to be wider than they are tall, and using the scale bar decorator they definitely appear to be more than 3km wide... when I use the Measure tool, it says they are exactly 2.56 km wide! Is the measure tool that much more accurate than the scale bar? – chriscalef Feb 28 '19 at 2:10
• About the scale bar: A map drawn in flat long/lat has a variable scale in meters, the scale bar is fixed instead. Therefore the scale bar can be accurate just in one line of the map, probably its x axis. – Gabriel De Luca Feb 28 '19 at 4:33