jonathanbyrn
jonathanbyrn

Reputation: 787

converting WGS84 vectors for import onto EPSG 3857 tiles in openlayers

I am importing bounding boxes I have converted to geojson WGS84 polygons but I am getting a strange projection error (see below):

misaligned bounding boxes

I assume this is from me importing it as WGS 84 onto a 3857 projection (I could be wrong!). I tried setting the view to WGS 84 but the map is then blank with no error messages. Should I do an "on the fly" transformation to 3857 or should I convert it to 3857 beforehand?

Upvotes: 1

Views: 831

Answers (2)

jonathanbyrn
jonathanbyrn

Reputation: 787

The problem was that I reprojected the max and min coordinates of my bounding box to WGS84, not all the coordinates separately

previously I was only doing two reprojections and combining the result:

minlat, minlon = transform(localsrs, wgs84, minx, miny)
maxlat, maxlon = transform(localsrs, wgs84, maxx, maxy)
polygon = [[minlon, minlat], [maxlon, minlat], [maxlon, maxlat],
          [minlon, maxlat], [minlon, maxlat]]

I didn't realise that the change in latitude (even only over 100 metres) is going to have an effect on the projected longitude. I now do 4 reprojections for my bounding box computation.

a, b = transform(localsrs, wgs84, minx, miny)
c, d = transform(localsrs, wgs84, minx, maxy)
e, f = transform(localsrs, wgs84, maxx, maxy)
g, h = transform(localsrs, wgs84, maxx, miny)

polygon = [[a, b], [c, d], [e, f], [g, h], [a, b]]

Thanks Dan for pointing me in the right direction!

Upvotes: 0

Dan
Dan

Reputation: 13160

It looks like you computed the bounding boxes before reprojecting. Since a bounding box is the min and max x and y coordinates, it is only meaningful in one SRS, since it depends on the direction of the axes, which might be different (think about it). Your options are either

  • Reproject underlying data, then compute bounding boxes in the final SRS
  • Compute bounding boxes, convert them to polygons, then reproject. (They won't be bounding boxes now, however)

Upvotes: 1

Related Questions