GDAL in PHP: Calculating Sum of Pixels and Retrieving Pixel Values

22 Views Asked by At

I'm working on a project where I need to use GDAL in PHP to analyze GeoTIFF files. Currently, I have a script that extracts information such as width and height from the GeoTIFF file. However, I'm struggling with two specific tasks and would appreciate any guidance:

Calculating the Sum of All Pixels: I need to calculate the sum of all pixel values within the GeoTIFF file. Could someone please provide guidance or a code snippet on how to achieve this using GDAL in PHP?

Retrieving Pixel Values at a Given Coordinate (x,y): Additionally, I would like to retrieve the pixel value at a specific coordinate (x,y) within the GeoTIFF file. How can I accomplish this using GDAL in PHP?

Here's a snippet of my current code for reference:

<?php
// Define the full path to the gdalinfo executable
$gdalinfoPath = "C:\Users\Media\anaconda3\envs\myenv\Library\bin\gdalinfo.exe"; // Update this with the correct path

// Define the path to your GeoTIFF file
$geotiffFile = "d.tif";

// Use GDAL command-line tools to read GeoTIFF information
$output = shell_exec("{$gdalinfoPath} " . escapeshellarg($geotiffFile) . " 2>&1");

// Output the information
echo $output . "<br>";


// Extract height, width from the output
$matches = array();
if (preg_match('/Size is (\d+), (\d+)/', $output, $matches)) {
    $width = $matches[1];
    $height = $matches[2];
    echo "Width: $width<br>";
    echo "Height: $height<br>";

    // Coordinates of the pixel you want to retrieve
    $pixelX = 2000;
    $pixelY = 2000;

    // Use GDAL command-line tools to get pixel value
    $output = shell_exec("{$gdallocationinfoPath} -valonly " . escapeshellarg($geotiffFile) . " $pixelX $pixelY 2>&1");

    // Output the pixel value
    echo "Pixel value at ($pixelX, $pixelY): $output";

}


?>

The output of this code is: Driver: GTiff/GeoTIFF Files: d.tif Size is 4539, 8150 Coordinate System is: GEOGCRS["WGS 84"...........Value=-3.4028235e+38 Metadata: RepresentationType=ATHEMATIC Width: 4539 Height: 8150

Notice: Undefined variable: gdallocationinfoPath in C:\Program Files (x86)\EasyPHP-5.3.9\www\pfe1702\handle-tiff-file\index.php on line 28 Pixel value at (2000, 2000): '-valonly' n'est pas reconnu en tant que commande interne ou externe, un programme exDutable ou un fichier de commandes.

0

There are 0 best solutions below