Skip to content

Commit 2634d1a

Browse files
committed
#67 Add a proper API for checking if a point is within the bounding area of it's CRS
1 parent 1ad806c commit 2634d1a

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Unreleased]
44

5+
## [5.10.0] - 2024-08-23
6+
### Added
7+
- Added a new method `isWithinCRSBoundingArea()` on most `Point` objects to check if they lie within the bounds
8+
of their defined CRS
9+
510
## [5.9.2] - 2024-08-18
611
### Changed
712
- Split declaration of the `Projected::EPSG_*` constants into multiple files internally to aid with static analysis
@@ -340,8 +345,9 @@ Initial release of this fork (based off of v2.3 of original)
340345
- Eastings and northings are rounded to 1m, and lat/long to 5dp (approx 1m) to avoid any misconceptions that precision is the same thing as accuracy.
341346
- When calculating surface distances, a more accurate mean radius is now used rather than that derived from historical definitions of a nautical mile
342347

343-
[Unreleased]: https://github.com/dvdoug/PHPCoord/compare/v5.9.2..master
348+
[Unreleased]: https://github.com/dvdoug/PHPCoord/compare/v5.10.0..master
344349

350+
[5.10.0]: https://github.com/dvdoug/PHPCoord/compare/v5.9.2..v5.10.0
345351
[5.9.2]: https://github.com/dvdoug/PHPCoord/compare/v5.9.1..v5.9.2
346352
[5.9.1]: https://github.com/dvdoug/PHPCoord/compare/v5.9.0..v5.9.1
347353
[5.9.0]: https://github.com/dvdoug/PHPCoord/compare/v5.8.0..v5.9.0

src/CoordinateOperation/AutoConversion.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ public function convert(Compound|Geocentric|Geographic2D|Geographic3D|Projected|
7474
return $point;
7575
}
7676

77+
/**
78+
* Calculates if the point as constructed actually lies within the bounding box of the CRS.
79+
*/
80+
public function isWithinCRSBoundingArea(): bool
81+
{
82+
$pointAsWGS84 = $this->getPointForBoundaryCheck(); // some obscure CRSs might not be able to convert
83+
84+
return !$pointAsWGS84 instanceof GeographicValue || $this->crs->getBoundingArea()->containsPoint($pointAsWGS84);
85+
}
86+
7787
/**
7888
* @return array<int, array{operation: string, name: string, source_crs: string, target_crs: string, accuracy: float, in_reverse: bool}>
7989
*/

tests/Geometry/BoundingAreaTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
namespace PHPCoord\Geometry;
1010

1111
use PHPCoord\CoordinateOperation\GeographicValue;
12+
use PHPCoord\CoordinateReferenceSystem\Geographic2D;
13+
use PHPCoord\CoordinateReferenceSystem\Projected;
1214
use PHPCoord\Datum\Datum;
15+
use PHPCoord\Point\GeographicPoint;
16+
use PHPCoord\Point\ProjectedPoint;
1317
use PHPCoord\UnitOfMeasure\Angle\Degree;
18+
use PHPCoord\UnitOfMeasure\Length\Metre;
1419
use PHPUnit\Framework\TestCase;
1520

1621
class BoundingAreaTest extends TestCase
@@ -102,4 +107,36 @@ public function testNetherlandsBufferedCorrectly(): void
102107
$polygon = BoundingArea::createFromExtentCodes(['urn:ogc:def:area:EPSG::1275']);
103108
self::assertTrue($polygon->containsPoint(new GeographicValue(new Degree(50.965613067768), new Degree(5.8249181759236), null, Datum::fromSRID(Datum::EPSG_WORLD_GEODETIC_SYSTEM_1984_ENSEMBLE))));
104109
}
110+
111+
public function testBoundaryChecking(): void
112+
{
113+
$newYorkInED50 = GeographicPoint::create(
114+
latitude: new Degree(40.689167),
115+
longitude: new Degree(-74.044444),
116+
crs: Geographic2D::fromSRID(Geographic2D::EPSG_ED50)
117+
);
118+
119+
$newYorkInNAD83 = GeographicPoint::create(
120+
latitude: new Degree(40.689167),
121+
longitude: new Degree(-74.044444),
122+
crs: Geographic2D::fromSRID(Geographic2D::EPSG_NAD83_2011)
123+
);
124+
125+
$newYorkInCorrectUTM = ProjectedPoint::createFromEastingNorthing(
126+
easting: new Metre(580741),
127+
northing: new Metre(4504692),
128+
crs: Projected::fromSRID(Projected::EPSG_WGS_84_UTM_ZONE_18N)
129+
);
130+
131+
$newYorkInOSGB = ProjectedPoint::createFromEastingNorthing(
132+
easting: new Metre(580741),
133+
northing: new Metre(4504692),
134+
crs: Projected::fromSRID(Projected::EPSG_OSGB36_BRITISH_NATIONAL_GRID)
135+
);
136+
137+
self::assertFalse($newYorkInED50->isWithinCRSBoundingArea());
138+
self::assertTrue($newYorkInNAD83->isWithinCRSBoundingArea());
139+
self::assertTrue($newYorkInCorrectUTM->isWithinCRSBoundingArea());
140+
self::assertFalse($newYorkInOSGB->isWithinCRSBoundingArea());
141+
}
105142
}

0 commit comments

Comments
 (0)