PHPackages                             mbolli/php-geobuf - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. mbolli/php-geobuf

ActiveLibrary

mbolli/php-geobuf
=================

PHP library for the geobuf compact geospatial format

v0.2.0(1y ago)131.7k↓52.2%2ISCPHPPHP ^8.2CI failing

Since Nov 7Pushed 1y ago2 watchersCompare

[ Source](https://github.com/mbolli/php-geobuf)[ Packagist](https://packagist.org/packages/mbolli/php-geobuf)[ Fund](https://paypal.me/bolli)[ GitHub Sponsors](https://github.com/mbolli)[ RSS](/packages/mbolli-php-geobuf/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (4)Versions (7)Used By (0)

php-geobuf
==========

[](#php-geobuf)

[![php-geobuf test suite](https://github.com/mbolli/php-geobuf/actions/workflows/php-test.yml/badge.svg)](https://github.com/mbolli/php-geobuf/actions/workflows/php-test.yml) [![Latest Stable Version](https://camo.githubusercontent.com/43ae595c74be5d239f966e873c7a696b68f89d6d26a8d7785156233e05c15854/68747470733a2f2f706f7365722e707567782e6f72672f6d626f6c6c692f7068702d67656f6275662f76)](https://packagist.org/packages/mbolli/php-geobuf) [![Total Downloads](https://camo.githubusercontent.com/472f1d2f484079d759dd194432e20a43ee2858548a2addc5dc46c2755382d679/68747470733a2f2f706f7365722e707567782e6f72672f6d626f6c6c692f7068702d67656f6275662f646f776e6c6f616473)](https://packagist.org/packages/mbolli/php-geobuf) [![Latest Unstable Version](https://camo.githubusercontent.com/d93a1ac61dedca176f4aed96c9b95159128f3463b68ee7cf4dbcca1919daf0a0/68747470733a2f2f706f7365722e707567782e6f72672f6d626f6c6c692f7068702d67656f6275662f762f756e737461626c65)](https://packagist.org/packages/mbolli/php-geobuf) [![License](https://camo.githubusercontent.com/7c53b0286e7076322e9b1e78d9ebde2ba28759edecacac11e1d015fc124326d6/68747470733a2f2f706f7365722e707567782e6f72672f6d626f6c6c692f7068702d67656f6275662f6c6963656e7365)](https://packagist.org/packages/mbolli/php-geobuf) [![PHP Version Require](https://camo.githubusercontent.com/1d8fe3803b8a05a155016e9edaebd7d8a8033c87178b2ac71510e466e6649b43/68747470733a2f2f706f7365722e707567782e6f72672f6d626f6c6c692f7068702d67656f6275662f726571756972652f706870)](https://packagist.org/packages/mbolli/php-geobuf)

PHP library for the geobuf compact geospatial format.

This is essentially a PHP port of the great [pygeobuf](https://github.com/pygeobuf/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](https://github.com/mapbox/geobuf) or the [Python implementation](https://github.com/pygeobuf/pygeobuf).

**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
------------

[](#installation)

```
composer require mbolli/php-geobuf
```

Usage
-----

[](#usage)

The following methods are exposed:

### Encoder

[](#encoder)

- `encode()` reads a JSON string. Returns a geobuf-encoded string
    - string `$dataJson` a JSON string
- `encodeToFile()` reads a JSON string and writes to a file. Returns the filesize of the resulting file or false
    - string `$filePath` where to store the resulting geobuf file
    - string `$dataJson` a JSON string
- `encodeFileToBufFile()` reads from a JSON file and writes to a file. Returns the filesize of the resulting file or false
    - string `$jsonFile` path to the JSON file
    - string `$geobufFile` where to store the resulting geobuf file
- `encodeFileToBuf()` reads from a JSON file. Returns a geobuf-encoded string
    - string `$fileName` path to the JSON file

All encoding methods support the following two non-mandatory arguments:

- int `$precision` max number of digits after the decimal point in coordinates, 6 by default (10 cm).
- int `$dim` number of dimensions in coordinates, 2 by default.

### Decoder

[](#decoder)

- `decodeToArray()` returns a PHP array
    - string `$encodedInput` geobuf input
- `decodeFileToArray()` returns a PHP array
    - string `$fileName` path to the geobuf file
- `decodeToJson()` returns a JSON string
    - string `$encodedInput` geobuf input
- `decodeFileToJson()` returns a JSON string
    - string `$fileName` path to the geobuf file
- `decodeFileToJsonFile()` writes to a file and returns the filesize of the resulting JSON file or false
    - string `$geobufFile` path to the geobuf file
    - string `$jsonFile` where to store the resulting JSON file

### Example

[](#example)

```
