Hi,
I have trouble converting geographic coordinates to image coordinates. My procedure works with most Sentinel-2 products I’ve tried. However, every once in a while, it fails. I’m using Python, but not snappy.
The procedure is as follows.
lat, lon coordinates -> utm coordinates -> inverse projection matrix multiplication -> image coordinates
This is an example that reproduces the issue, final X image coordinate is wrong:
Product data:
Product id: 05303463-cdab-4b95-bc65-5ebaf435d45e
Product name: S2B_MSIL2A_20201112T034009_N0214_R061_T49SBB_20201113T160805
Footprint:
"POLYGON((108.85214171500951 37.35213890138582,108.84875074421133 37.3419731357291,108.79949598951012 37.19538606239605,108.75143165356803 37.04857431885545,108.71390883771207 36.93518822112508,107.63258699686288 36.910119161555244,107.58802752132053 37.8980997878049,108.83567015867037 37.92768043331917,108.85214171500951 37.35213890138582))
Matrix for band B2,B3,B4 (resolution 10). Double checked with product metadata in /GRANULE/L2A_T49SBB_A019249_20201112T034005/MTD_TL.xml and SNAP-generated geotiff using rasterio:
with rio.open('full_image.tif') as dataset:
matrix = dataset.transform
| 10.00, 0.00, 199980.00|
| 0.00,-10.00, 4200000.00|
| 0.00, 0.00, 1.00|
Input data:
- lat, lon coordinates: (37.44, 107.64), somewhere in China.
- utm coordinates: 733558.139047, 4146957.284352
UTM conversion checked with Lat Long to UTM Converter
- inverse matrix:
| 0.10, 0.00,-19998.00|
| 0.00,-0.10, 420000.00|
| 0.00, 0.00, 1.00|
- pixel coordinates: x= 53357.81, y= 5304.271
Y coordinate seems about right, snap places it about halfway the image height. However, X is five times the image width (10980 pixels).
This is a minimal example code (Python 3.6)
from affine import Affine
import utm
matrix = Affine(10.00,0.0,199980.00,0.0,-10,4200000.00)
print("{}\n".format(matrix))
print("{}\n".format(~matrix))
lat = 37.44
lon = 107.64
UTMx, UTMy, zone, band = utm.from_latlon(lat,lon)
print("UTMx = {}, UTMy = {}".format(str(UTMx),str(UTMy)))
x, y = ~matrix*(UTMx, UTMy)
print("x = {}, y = {}".format(str(x),str(y)))
As I said, this code works most of the time, it just seems like the matrix in some products is not right, which can’t be the case, as SNAP/GPT seems to get it right
Thank you