Reconciling transforms by from_origin and from_bounds


ayr035@...
 

I am trying to reconcile the transforms that I get by using transform.from_origin and transform.from_bounds

bounds = BoundingBox(left=1679712.0, bottom=5860848.0, right=1862208.0, top=6029312.0)
width = math.ceil((bounds.right - bounds.left) / 100) # resolution of 100
height = math.ceil((bounds.bottom - bounds.top) / 100)
# resolution of 100

T_o = transform.from_origin(bounds.left, bounds.top, width, height)
Affine(1825.0, 0.0, 1679712.0,
      0.0, -1685.0, 6029312.0)

T_b = transform.from_bounds(*bounds, width, height)
Affine(99.99780821917808, 0.0, 1679712.0,
      0.0, -99.9786350148368, 6029312.0)

The transforms seem to differ quite significantly. If we compute the array bounds, I expect to recover the original bounds
transform.array_bounds(height, width, T_o) ->
(1679712.0, 3190087.0, 5010337.0, 6029312.0)   # bottom and right bounds are wrong?
transform.array_bounds(height, width, T_b) -> (
1679712.0, 5860848.0, 1862208.0, 6029312.0)   # These are my original bounds

Am I misusing transform.from_origin?


Luke
 
Edited

rasterio.transform.from_origin(westnorthxsizeysize)

Return an Affine transformation given upper left and pixel sizes.

https://rasterio.readthedocs.io/en/latest/api/rasterio.transform.html#rasterio.transform.from_origin

After changing your height calculation from bottom - top to top - bottom, for your coords I get similar transforms:

import math
from rasterio.transform import (from_origin, from_bounds, array_bounds)
from rasterio.coords import BoundingBox

bounds = BoundingBox(
left=1679712.0, bottom=5860848.0, right=1862208.0, top=6029312.0)
width = math.ceil((bounds.right - bounds.left) /
100) # resolution of 100
height = math.ceil((bounds.top - bounds.bottom) / 100) # resolution of 100

T_o = from_origin(bounds.left, bounds.top, 100, 100)

T_b = from_bounds(*bounds
, width, height)

print(T_o)
print(T_b)

print(array_bounds(height, width, T_o))
print(array_bounds(height, width, T_b))

| 100.00, 0.00, 1679712.00| | 0.00,-100.00, 6029312.00| | 0.00, 0.00, 1.00| | 100.00, 0.00, 1679712.00| | 0.00,-99.98, 6029312.00| | 0.00, 0.00, 1.00| (1679712.0, 5860812.0, 1862212.0, 6029312.0) (1679712.0, 5860848.0, 1862208.0, 6029312.0)