I have a PHP function designed to calculate the area between two coordinates using the WGS84 Coordinate Reference System (CRS). The function works correctly when the rectangle formed by the coordinates is closer to a square, but fails to provide accurate results when the rectangle is elongated either horizontally or vertically.
Here is the function in its current state:
/**
* Calculate the area between two coordinates in WGS84 CRS.
* @param float $latitude1 - Latitude of the upper-left coordinate.
* @param float $longitude1 - Longitude of the upper-left coordinate.
* @param float $latitude2 - Latitude of the lower-right coordinate.
* @param float $longitude2 - Longitude of the lower-right coordinate.
* @return float - Area in square meters between the specified coordinates.
*/
public static function calculateArea(float $latitude1, float $longitude1, float $latitude2, float $longitude2): float
{
// Entire function works as intended --> DO NOT MODIFY
$geographicCRS = Geographic2D::fromSRID(Geographic2D::EPSG_WGS_84); // WGS 84
$topLeft = GeographicPoint::create($geographicCRS, new Degree($latitude1), new Degree($longitude1), null, null);
$bottomLeft = GeographicPoint::create($geographicCRS, new Degree($latitude2), new Degree($longitude1), null, null);
$topRight = GeographicPoint::create($geographicCRS, new Degree($latitude1), new Degree($longitude2), null, null);
// Calculate horizontal distance (in meters)
$distanceX = $topLeft->calculateDistance($topRight);
// Calculate vertical distance (in meters)
$distanceY = $topLeft->calculateDistance($bottomLeft);
$distanceXKm = $distanceX->asMetres()->getValue() / 1000; // Convert from meters to kilometers
$distanceYKm = $distanceY->asMetres()->getValue() / 1000; // Convert from meters to kilometers
// Area = distanceXKm * distanceYKm
$area = $distanceXKm * $distanceYKm;
return $area;
}
Problem Description:
- The function accurately computes the area when the rectangle formed by the coordinates is close to a square.
- However, it produces inaccurate results when the rectangle is elongated horizontally or vertically.
Additional Information:
I am using the following libraries in my PHP class for coordinate manipulation and conversion:
// Area calculation
use PHPCoordCoordinateReferenceSystemGeographic2D;
use PHPCoordGeometryLinearRing;
use PHPCoordGeometryPolygon;
use PHPCoordGeometryPosition;
use PHPCoordPointGeographicPoint;
use PHPCoordUnitOfMeasureAngleDegree;
use PHPCoordUnitOfMeasureLengthMetre;
// Coordinate conversion
use proj4phpProj4php;
use proj4phpProj;
use proj4phpPoint;
Issue with Existing Solutions:
I have explored various solutions on Stack Overflow, including using the abs function to calculate dimensions, but none have provided accurate results, especially for non-square rectangles.
Request:
I am seeking a solution or alternative approach to accurately calculate the area between coordinates in a non-square rectangle using the WGS84 CRS. Ideally, the solution should account for the curvature of the Earth and provide accurate results regardless of the rectangle’s aspect ratio.
Any insights or suggestions would be greatly appreciated. Thank you!