{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "827e9aae-17df-4df6-9441-d9a6486d4b84", "metadata": { "tags": [] }, "outputs": [], "source": [ "import rastereasy" ] }, { "cell_type": "markdown", "id": "41eb0042-eb52-443f-8197-8db22a836331", "metadata": {}, "source": [ "# Pixel vs geographical coordinates " ] }, { "cell_type": "code", "execution_count": 2, "id": "88b2b818-aae1-4b74-b615-52284d368655", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on method pixel2latlon in module rastereasy.rastereasy:\n", "\n", "pixel2latlon(i, j) method of rastereasy.rastereasy.Geoimage instance\n", " Convert pixel coordinates to geographic coordinates (latitude, longitude).\n", "\n", " This method transforms a pixel location (row, col) in the image to the\n", " corresponding point defined by its latitude and longitude.\n", "\n", " Parameters\n", " ----------\n", " i : int\n", " Row index (vertical position) in the image\n", " j : int\n", " Column index (horizontal position) in the image\n", "\n", " Returns\n", " -------\n", " tuple of float\n", " The geographic coordinates as (latitude, longitude)\n", "\n", " Examples\n", " --------\n", " >>> # Convert pixel coordinates to geographic location\n", " >>> row, col = 500, 700\n", " >>> latitude, longitude = image.pixel2latlon(row, col)\n", " >>> print(f\"Pixel ({row}, {col}) is at lat/lon: ({latitude}, {longitude})\")\n", " >>>\n", " >>> # Find coordinates of image corners\n", " >>> nw_lat, nw_lon = image.pixel2latlon(0, 0) # Northwest corner\n", " >>> se_lat, se_lon = image.pixel2latlon(image.shape[0]-1, image.shape[1]-1) # Southeast\n", " >>> print(f\"Image covers from ({nw_lat}, {nw_lon}) to ({se_lat}, {se_lon})\")\n", "\n", " Notes\n", " -----\n", " - The image must be georeferenced (have valid CRS and transform)\n", " - Pixel coordinates typically start at (0, 0) in the upper-left corner of the image\n", " - For most projections, latitude increases going north and longitude increases going east\n", "\n", "Help on method latlon2pixel in module rastereasy.rastereasy:\n", "\n", "latlon2pixel(coord_lat, coord_lon) method of rastereasy.rastereasy.Geoimage instance\n", " Convert geographic coordinates (latitude, longitude) to pixel coordinates.\n", "\n", " This method transforms a point defined by its latitude and longitude to\n", " the corresponding pixel location (row, col) in the image.\n", "\n", " Parameters\n", " ----------\n", " coord_lat : float\n", " Latitude of the point\n", " coord_lon : float\n", " Longitude of the point\n", "\n", " Returns\n", " -------\n", " tuple of int\n", " The pixel coordinates as (row, col) or (i, j)\n", "\n", " Examples\n", " --------\n", " >>> # Convert a geographic location to pixel coordinates\n", " >>> latitude, longitude = 42.36, -71.06 # Boston, MA\n", " >>> row, col = image.latlon2pixel(latitude, longitude)\n", " >>> print(f\"This location is at pixel ({row}, {col})\")\n", " >>>\n", " >>> # Check if a specific location is within the image extent\n", " >>> row, col = image.latlon2pixel(latitude, longitude)\n", " >>> in_bounds = (0 <= row < image.shape[0]) and (0 <= col < image.shape[1])\n", " >>> print(f\"Location is within image: {in_bounds}\")\n", "\n", " Notes\n", " -----\n", " - The image must be georeferenced (have valid CRS and transform)\n", " - If the point is outside the image extent, the function will still return pixel coordinates, but they may be outside the valid image dimensions\n", " - Row (i) corresponds to the vertical position (along latitude)\n", " - Column (j) corresponds to the horizontal position (along longitude)\n", "\n" ] } ], "source": [ "name_im='./data/demo/sentinel.tif'\n", "image=rastereasy.Geoimage(name_im)\n", "help(image.pixel2latlon)\n", "help(image.latlon2pixel)" ] }, { "cell_type": "code", "execution_count": 3, "id": "de8e3433-47e6-4c72-b24d-10008f12a81d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Coord : 3 19 (lat,lon) = 7.085895234623746 38.3469798575148\n", "Coord : 80 25 (lat,lon) = 7.078931083837443 38.34753293909697\n", "----\n", "(lat,lon) = 7.085895234623746 38.3469798575148 Coord : 3 19\n", "(lat,lon) = 7.078931083837443 38.34753293909697 Coord : 80 25\n", "diff ppixels : 77 6 - diff lat lon : -0.0069641507863034136 0.0005530815821686019\n" ] } ], "source": [ "row1 = 3\n", "col1 = 19\n", "row2 = 80\n", "col2 = 25\n", "lat1,lon1=image.pixel2latlon(row1,col1)\n", "lat2,lon2=image.pixel2latlon(row2,col2)\n", "print('Coord : ',row1,col1,' (lat,lon) = ', lat1,lon1)\n", "print('Coord : ',row2,col2,' (lat,lon) = ', lat2,lon2)\n", "print('----')\n", "est_row1,est_col1=image.latlon2pixel(lat1,lon1)\n", "est_row2,est_col2=image.latlon2pixel(lat2,lon2)\n", "\n", "print('(lat,lon) = ', lat1,lon1,'Coord : ',est_row1,est_col1)\n", "print('(lat,lon) = ', lat2,lon2,'Coord : ',est_row2,est_col2)\n", "\n", "print('diff ppixels : ',row2-row1,col2-col1,' - diff lat lon : ',lat2 - lat1,lon2 - lon1)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.3" } }, "nbformat": 4, "nbformat_minor": 5 }