You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用PHPCoord将OS地图参考转换为WGS84经纬度?

修复PHPCoord中OSGB36转WGS84的SRID错误

你的错误根源在于用错了坐标系类型:WGS84(EPSG:4326)是地理坐标系(Geographic2D),但你却用Projected::fromSRID()去加载它,导致系统无法识别对应的SRID。

修正后的代码如下:

use PHPCoord\CoordinateReferenceSystem\Geographic2D;
use PHPCoord\CoordinateReferenceSystem\Projected;
use PHPCoord\ProjectedPoint;
use PHPCoord\UnitOfMeasure\Length\Metre;

// 构建OSGB36投影坐标系下的点
$from = ProjectedPoint::createFromEastingNorthing(
    Projected::fromSRID(Projected::EPSG_OSGB36_BRITISH_NATIONAL_GRID),
    new Metre(374600),
    new Metre(250400)
);

// 正确加载WGS84地理坐标系
$toCRS = Geographic2D::fromSRID(Geographic2D::EPSG_WGS_84);

// 执行转换
$to = $from->convert($toCRS);

// 输出经纬度(保留对应精度)
echo number_format($to->getLatitude()->getValue(), 7) . ', ' . number_format($to->getLongitude()->getValue(), 7);

运行这段代码会输出你预期的结果:52.1512930, -2.3726484,和你验证的结果一致。

额外补充:
如果需要直接处理OS地图参考(如SO746504),可以使用PHPCoord自带的OSGridReference类直接解析,无需手动转换为东距北距,示例代码如下:

use PHPCoord\OSGridReference;
use PHPCoord\CoordinateReferenceSystem\Geographic2D;

$gridRef = OSGridReference::fromString('SO746504');
$point = $gridRef->toProjectedPoint();

// 后续转换步骤
$toCRS = Geographic2D::fromSRID(Geographic2D::EPSG_WGS_84);
$to = $point->convert($toCRS);
echo number_format($to->getLatitude()->getValue(), 7) . ', ' . number_format($to->getLongitude()->getValue(), 7);

内容的提问来源于stack exchange,提问作者morgyface

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 03:57:20