import rastereasy

Pixel vs geographical coordinates

name_im='./data/demo/sentinel.tif'
image=rastereasy.Geoimage(name_im)
help(image.pixel2latlon)
help(image.latlon2pixel)
Help on method pixel2latlon in module rastereasy.rastereasy:

pixel2latlon(i, j) method of rastereasy.rastereasy.Geoimage instance
    Convert pixel coordinates to geographic coordinates (latitude, longitude).

    This method transforms a pixel location (row, col) in the image to the
    corresponding point defined by its latitude and longitude.

    Parameters
    ----------
    i : int
        Row index (vertical position) in the image
    j : int
        Column index (horizontal position) in the image

    Returns
    -------
    tuple of float
        The geographic coordinates as (latitude, longitude)

    Examples
    --------
    >>> # Convert pixel coordinates to geographic location
    >>> row, col = 500, 700
    >>> latitude, longitude = image.pixel2latlon(row, col)
    >>> print(f"Pixel ({row}, {col}) is at lat/lon: ({latitude}, {longitude})")
    >>>
    >>> # Find coordinates of image corners
    >>> nw_lat, nw_lon = image.pixel2latlon(0, 0)  # Northwest corner
    >>> se_lat, se_lon = image.pixel2latlon(image.shape[0]-1, image.shape[1]-1)  # Southeast
    >>> print(f"Image covers from ({nw_lat}, {nw_lon}) to ({se_lat}, {se_lon})")

    Notes
    -----
    - The image must be georeferenced (have valid CRS and transform)
    - Pixel coordinates typically start at (0, 0) in the upper-left corner of the image
    - For most projections, latitude increases going north and longitude increases going east

Help on method latlon2pixel in module rastereasy.rastereasy:

latlon2pixel(coord_lat, coord_lon) method of rastereasy.rastereasy.Geoimage instance
    Convert geographic coordinates (latitude, longitude) to pixel coordinates.

    This method transforms a point defined by its latitude and longitude to
    the corresponding pixel location (row, col) in the image.

    Parameters
    ----------
    coord_lat : float
        Latitude of the point
    coord_lon : float
        Longitude of the point

    Returns
    -------
    tuple of int
        The pixel coordinates as (row, col) or (i, j)

    Examples
    --------
    >>> # Convert a geographic location to pixel coordinates
    >>> latitude, longitude = 42.36, -71.06  # Boston, MA
    >>> row, col = image.latlon2pixel(latitude, longitude)
    >>> print(f"This location is at pixel ({row}, {col})")
    >>>
    >>> # Check if a specific location is within the image extent
    >>> row, col = image.latlon2pixel(latitude, longitude)
    >>> in_bounds = (0 <= row < image.shape[0]) and (0 <= col < image.shape[1])
    >>> print(f"Location is within image: {in_bounds}")

    Notes
    -----
    - The image must be georeferenced (have valid CRS and transform)
    - If the point is outside the image extent, the function will still return pixel coordinates, but they may be outside the valid image dimensions
    - Row (i) corresponds to the vertical position (along latitude)
    - Column (j) corresponds to the horizontal position (along longitude)
row1 = 3
col1 = 19
row2 = 80
col2 = 25
lat1,lon1=image.pixel2latlon(row1,col1)
lat2,lon2=image.pixel2latlon(row2,col2)
print('Coord : ',row1,col1,'  (lat,lon) = ', lat1,lon1)
print('Coord : ',row2,col2,'  (lat,lon) = ', lat2,lon2)
print('----')
est_row1,est_col1=image.latlon2pixel(lat1,lon1)
est_row2,est_col2=image.latlon2pixel(lat2,lon2)

print('(lat,lon) = ', lat1,lon1,'Coord : ',est_row1,est_col1)
print('(lat,lon) = ', lat2,lon2,'Coord : ',est_row2,est_col2)

print('diff ppixels : ',row2-row1,col2-col1,' - diff lat lon : ',lat2 - lat1,lon2 - lon1)
Coord :  3 19   (lat,lon) =  7.085895234623746 38.3469798575148
Coord :  80 25   (lat,lon) =  7.078931083837443 38.34753293909697
----
(lat,lon) =  7.085895234623746 38.3469798575148 Coord :  3 19
(lat,lon) =  7.078931083837443 38.34753293909697 Coord :  80 25
diff ppixels :  77 6  - diff lat lon :  -0.0069641507863034136 0.0005530815821686019