mbolli/php-geobuf
Composer 安装命令:
composer require mbolli/php-geobuf
包简介
PHP library for the geobuf compact geospatial format
README 文档
README
PHP library for the geobuf compact geospatial format.
This is essentially a PHP port of the great pygeobuf.
Geobuf stores GeoJSON 6-8 times smaller and TopoJSON 2-3 times smaller. Depending on the $precision attribute, lossless compression is possible. More information about Geobuf is available in the JS implementation or the Python implementation.
Quick size comparison: An example 745 kB GeoJSON was converted to a 90 kB Geobuf file – more than 8 times less.
Beware: Experimental state – it works for my purposes but there probably are some bugs.
Installation
composer require mbolli/php-geobuf
Usage
The following methods are exposed:
Encoder
encode()reads a JSON string. Returns a geobuf-encoded string- string
$dataJsona JSON string
- string
encodeToFile()reads a JSON string and writes to a file. Returns the filesize of the resulting file or false- string
$filePathwhere to store the resulting geobuf file - string
$dataJsona JSON string
- string
encodeFileToBufFile()reads from a JSON file and writes to a file. Returns the filesize of the resulting file or false- string
$jsonFilepath to the JSON file - string
$geobufFilewhere to store the resulting geobuf file
- string
encodeFileToBuf()reads from a JSON file. Returns a geobuf-encoded string- string
$fileNamepath to the JSON file
- string
All encoding methods support the following two non-mandatory arguments:
- int
$precisionmax number of digits after the decimal point in coordinates, 6 by default (10 cm). - int
$dimnumber of dimensions in coordinates, 2 by default.
Decoder
decodeToArray()returns a PHP array- string
$encodedInputgeobuf input
- string
decodeFileToArray()returns a PHP array- string
$fileNamepath to the geobuf file
- string
decodeToJson()returns a JSON string- string
$encodedInputgeobuf input
- string
decodeFileToJson()returns a JSON string- string
$fileNamepath to the geobuf file
- string
decodeFileToJsonFile()writes to a file and returns the filesize of the resulting JSON file or false- string
$geobufFilepath to the geobuf file - string
$jsonFilewhere to store the resulting JSON file
- string
Example
<?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); }
Contribute
Pull requests are encouraged. Code style is enforced by 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
If the PR is about the Encoder or Decoder, please add a test JSON to the tests/geojson folder. The test suite will automatically pick it up and test it when executed.
Background: Proto compilation
Classes were generated by the proto compiler using this command:
bin/protoc --proto_path=src --php_out=build src/geobuf.proto
Used was this proto file, lightly modified from the mapbox/geobuf proto file for proto3 compatibility and automated namespace generation.
mbolli/php-geobuf 适用场景与选型建议
mbolli/php-geobuf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37.27k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 11 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mbolli/php-geobuf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mbolli/php-geobuf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 37.27k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: ISC
- 更新时间: 2021-11-07