mbolli/php-geobuf

PHP 库,用于 geobuf 紧凑地理空间格式

v0.1.4 2022-12-19 08:50 UTC

This package is auto-updated.

Last update: 2024-08-31 11:16:40 UTC


README

php-geobuf test suite Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

PHP 库,用于 geobuf 紧凑地理空间格式。

这基本上是伟大的pygeobuf的 PHP 版本。

Geobuf 存储的 GeoJSON 比原始小 6-8 倍,TopoJSON 比原始小 2-3 倍。根据 $precision 属性,可以实现无损压缩。有关 Geobuf 的更多信息,请参阅JS 实现Python 实现

快速大小比较:一个示例 745 kB 的 GeoJSON 转换成了 90 kB 的 Geobuf 文件——减少了 8 倍以上。

注意:实验状态 - 它适用于我的目的,但可能存在一些错误。

安装

composer require mbolli/php-geobuf

用法

以下方法被暴露

编码器

  • encode() 读取一个 JSON 字符串。返回一个 geobuf 编码的字符串
    • string $dataJson 一个 JSON 字符串
  • encodeToFile() 读取一个 JSON 字符串并将其写入文件。返回结果文件的文件大小或 false
    • string $filePath 存储结果 geobuf 文件的位置
    • string $dataJson 一个 JSON 字符串
  • encodeFileToBufFile() 从 JSON 文件读取并写入文件。返回结果文件的文件大小或 false
    • string $jsonFile JSON 文件的路径
    • string $geobufFile 存储结果 geobuf 文件的位置
  • encodeFileToBuf() 从 JSON 文件读取。返回一个 geobuf 编码的字符串
    • string $fileName JSON 文件的路径

所有编码方法都支持以下两个非必填参数

  • int $precision 坐标中十进制点后最大的数字个数,默认为 6(10 厘米)。
  • int $dim 坐标中的维度数,默认为 2。

解码器

  • decodeToArray() 返回一个 PHP 数组
    • string $encodedInput geobuf 输入
  • decodeFileToArray() 返回一个 PHP 数组
    • string $fileName geobuf 文件的路径
  • decodeToJson() 返回一个 JSON 字符串
    • string $encodedInput geobuf 输入
  • decodeFileToJson() 返回一个 JSON 字符串
    • string $fileName geobuf 文件的路径
  • decodeFileToJsonFile() 写入文件并返回结果 JSON 文件的文件大小或 false
    • string $geobufFile geobuf 文件的路径
    • string $jsonFile 存储结果 JSON 文件的位置

示例

<?php

require_once('./vendor/autoload.php');

$jsonFile = './my.geojson';
$geobufFile = basename($jsonFile) . '.geobuf';
try {
    // encodes a json string to geobuf
    $geobufString = \MBolli\PhpGeobuf\Encoder::encode(
        file_get_contents($jsonFile), // (string) a json string 
        6, // (int) precision: max number of digits after the decimal point in coordinates, 6 by default
        2 // (int) dimensions: number of dimensions in coordinates, 2 by default.
    );
    
    // decodes a geobuf file to json
    $jsonString = \MBolli\PhpGeobuf\Decoder::decodeToJson(
        file_get_contents($geobufFile) // (string) expects a geobuf string
    )

} catch (\MBolli\PhpGeobuf\GeobufException $e) {
    var_dump($e);
} catch (\Throwable $e) {
    var_dump($e);
}

贡献

鼓励提交拉取请求。代码风格由 PHP-CS-Fixer 强制执行。

composer run lint # lint source files and show problems (read-only)
composer run lint-diff # lint source files and show diff to the files fixed state (read-only)
composer run fix # lint source files and fix the problems
composer run test # execute all tests
composer run analyse # run phpstan static analyzer

如果 PR 是关于编码器或解码器,请将测试 JSON 添加到 tests/geojson 文件夹。测试套件将在执行时自动选择并测试它。

背景:Proto 编译

类是通过proto 编译器使用以下命令生成的

bin/protoc --proto_path=src --php_out=build src/geobuf.proto

使用了此 proto 文件,略微修改了mapbox/geobuf proto 文件,以实现 proto3 兼容性和自动化命名空间生成。