xobble/grid

一个小型库,用于在英制国家网格(OSNG - 测绘国家网格)、爱尔兰网格和海峡群岛网格参考及其笛卡尔(基准,东西,南北,精度)表示之间进行转换

1.04 2019-10-23 23:44 UTC

This package is auto-updated.

Last update: 2024-09-24 10:52:17 UTC


README

一个小型库,用于在英制国家网格、爱尔兰网格和海峡群岛网格参考及其笛卡尔表示之间进行转换

安装

$ composer require xobble/grid

使用

use Xobble\Grid\Cartesian;
use Xobble\Grid\Converter;
use Xobble\Grid\GridRef\ChannelIslandsGridRef;
use Xobble\Grid\GridRef\IrishGridRef;
use Xobble\Grid\GridRef\BritishGridRef;

// 1. Instantiate the converter with the grid types you want to support

$converter = new Converter([
    new IrishGridRef(),
    new BritishGridRef(),
    new ChannelIslandsGridRef(),
]);

// 2. Start converting

// British National Grid:
$cart1 = $converter->toCartesian('SX466827');        // EPSG:27700(246600, 82700) [100m]
$converter->toGridRef($cart1);                       // SX466827

$cart2 = new Cartesian(27700, 651409, 313177, 1);
$grid2 = $converter->toGridRef($cart2);              // TG5140913177
$converter->toCartesian($grid2);                     // EPSG:27700(651409, 313177) [1m]

// Irish Grid:
$converter->toCartesian('X622997');                  // EPSG:29902(262200, 99700) [100m]
 
// Channel Islands Grid:
$converter->toCartesian('WV305754');                 // EPSG:32630(530500, 5475400) [100m]

// An UnsupportedRefException will be triggered for unsupported grid references or cartesian coordinates:
$converter->toCartesian('AB22997');                  // UnsupportedRefException

// A GridRefException will be triggered if trying to convert badly constructed Cartesians:
//
// GridRefException: Accuracy + easting / northing mismatch
// (States 100m accuracy, but easting/northing have meter and tens accuracy digits): 
$badCart1 = new Cartesian(27700, 651409, 313122, 100);
$converter->toGridRef($badCart1);                    

// GridRefException: Accuracy must be a power of 10 with an integer exponent (e.g. 1, 10, 100, 1000...)
$badCart2 = new Cartesian(27700, 651400, 313100, 200);
$converter->toGridRef($badCart2) ;                  

支持的数据基准/网格

GridRef 接口

可以通过创建实现以下接口的类来支持其他网格参考系统。

use Xobble\Grid\Cartesian;

interface GridRef
{
    public function getDatum() : int;
    public function getGridReferenceName() : string;

    public function toCartesian(string $gridRef) : Cartesian;
    public function toGridRef(Cartesian $cartesian) : string;
}
use Xobble\Grid\Cartesian;
use Xobble\Grid\GridRef\BritishGridRef;
    
$grid = new BritishGridRef();
    
$grid->getGridReferenceName();                   // British National Grid
$grid->getDatum();                               // 27700
$grid->toCartesian('SR123456');                  // Returns a Cartesian - "EPSG:27700(112300, 145600) [100m]"
$grid->toCartesian('HL123456');                  // throws UnsupportedRefException
    
$cart1 = new Cartesian(27700, 651409, 313177, 1);
$cart2 = new Cartesian(29902, 651409, 313177, 1);
    
$ref1 = $grid->toGridRef($cart1);                // Returns a string - "TG5140913177"
$grid->toGridRef($cart2);                        // throws UnsupportedRefException
    
$grid->toCartesian($ref1);                       // EPSG:27700(651409, 313177) [1m]

BritishGridRef 和 IrishGridRef 构造函数可以可选地接受一个选项数组,可以用来配置哪些网格参考前缀被视为有效。'allowed_references' 选项可以设置为 null 以允许任何前缀,或设置为包含有效前缀字符串的数组。

use Xobble\Grid\GridRef\IrishGridRef;

$gridLimited = new IrishGridRef([
    'allowed_references' => [
        'A', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'L', 'M', 
        'N', 'O', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y',
    ]
]);

此外,默认情况下,BritishGridRef 还不允许 WA 和 WV 前缀,因为这些用于表示海峡群岛网格参考。然而,可以通过选项将其关闭

use Xobble\Grid\GridRef\BritishGridRef;

$gridNoCI = new BritishGridRef([
    'grid_exclude_channel_islands' => false, 
    'allowed_references' => null                     // Allow any prefix
]);   

笛卡尔类

use Xobble\Grid\Cartesian;

$cart = new Cartesian(27700, 651409, 313177, 1);
    
echo $cart->getDatum();    // 27700
echo $cart->getEasting();  // 651409
echo $cart->getNorthing(); // 313177
echo $cart->getAccuracy(); // 1
echo $cart;                // EPSG:27700(651409, 313177) [1m]