https://github.com/mbolli/php-geobuf
PHP library for the geobuf compact geospatial format.
https://github.com/mbolli/php-geobuf
geobuf geojson json php protobuf topojson
Last synced: 3 months ago
JSON representation
PHP library for the geobuf compact geospatial format.
- Host: GitHub
- URL: https://github.com/mbolli/php-geobuf
- Owner: mbolli
- License: isc
- Created: 2021-11-06T22:06:30.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2026-03-26T06:44:28.000Z (3 months ago)
- Last Synced: 2026-03-27T02:08:20.030Z (3 months ago)
- Topics: geobuf, geojson, json, php, protobuf, topojson
- Language: PHP
- Homepage:
- Size: 251 KB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# php-geobuf
[](https://github.com/mbolli/php-geobuf/actions/workflows/php-test.yml) [](https://packagist.org/packages/mbolli/php-geobuf) [](https://packagist.org/packages/mbolli/php-geobuf) [](https://packagist.org/packages/mbolli/php-geobuf) [](https://packagist.org/packages/mbolli/php-geobuf) [](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
```bash
composer require mbolli/php-geobuf
```
## Usage
The following methods are exposed:
### 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
- `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
```php