2

I want to find out the timezone a GPX track is in by looking up the first coordinate pair. I took the shapefile from Timezone Boundary Builder and created a PNG image via qgis from it (using the default projection, "WGS 84 – EPSG:4326"), with one color per defined timezone. I also have a color-timezone mapping.

I know the width and the height of the image (which represents all possible coordinates on earth), and I have a lat/lon pair. Now I "only" need to map the coordinate pair to the respective pixel (x/y) to get it's color, which then maps to the timezone I need.

But how do I calculate the mapping? I know how to do it using the Mercator projection (I wrote a function doing that some time ago: mercatorProjection, but I'm struggling to find respective code for EPSG:4326 (or WGS84).

6
  • In what projection is your timezone image? – TomazicM Jan 9 at 17:04
  • EPSG:4326 – thus I need to calculate this projection. qgis says "WGS 84 – EPSG:4326". I also added this to the text now to clarify it. – Tobias Leupold Jan 9 at 17:16
  • 1
    This then means you have simple linear cartesian math. Upper side of image has y=90, bottom has y=-90, left side has x=-180, right side has x=180. Conversion to image pixels is then just linear function, taking into account image size in pixels. – TomazicM Jan 9 at 18:02
  • @TomazicM: It's really that easy?! The Mercator projection was much more complicated. Thanks for this hint :-) Is there some reference one can read about this? I really found nothing. – Tobias Leupold Jan 9 at 18:19
  • 1
    It's easy because EPSG:4326 is not a projection. In your case [lat, lng] values are simply used on a flat surface, without any projection calculus. – TomazicM Jan 9 at 18:25
2

EPSG:4326 is not a projected coordinate system, but ellipsoidal coordinate system, using degrees for longitude and latitude coordinates. In your case these [lng, lat] values are simply used on a flat surface as cartesian coordinates.

This then means you have simple linear cartesian math. Upper side of image has y=90, bottom has y=-90, left side has x=-180, right side has x=180. Conversion to image pixels is then just linear function, taking into account image size in pixels.

0

Just to also leave the code here: This is how I map coordinates to the image (m_timezoneMap is a QImage created from the said PNG file):

const double width = m_timezoneMap.size().width();
const double height = m_timezoneMap.size().height();

// Scale the coordinates to the image size, relative to the image center
int mappedLon = std::round(lastCoordinates.lon() / 180.0 * (width / 2.0));
int mappedLat = std::round(lastCoordinates.lat() / 90.0 * (height / 2.0));

// Move the mapped coordinates to the left lower edge
mappedLon = width / 2 + mappedLon;
mappedLat = height - (height / 2 + mappedLat);

// Get the respective pixel's color
const auto timezoneColor = m_timezoneMap.pixelColor(mappedLon, mappedLat).name();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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