teamzac / laravel-shapefiles
A wrapper for gasparesganga/php-shapefile with a Laravel-ish collections API
Requires
- php: ^7.1|^8.0
- gasparesganga/php-shapefile: ^3.4
- illuminate/support: ^7.0|^8.0
- proj4php/proj4php: ^2.0
Requires (Dev)
- orchestra/testbench: ^5.0|^6.0
- phpunit/phpunit: ^8.0|^9.0
README
The gasparesganga/php-shapefile package is extremely handy for working directly with Shapefiles, but we work mostly in Laravel and prefer the collection-style APIs found throughout. This package simply wraps the original ShapefileReader class in a LazyCollection to make iterating on it a bit more familiar.
Installation
You can install the package via composer:
composer require teamzac/laravel-shapefiles
Usage
$reader = new TeamZac\LaravelShapefiles\Reader('file_or_directory_path_here'); $reader->count(); // total number of records $reader->each(function($geometry) { // do something });
You can pass a reference to the .shp file, or the directory containing the .shp file, when instantiating the Reader.
When iterating in the original package, you'd receive an instance of Shapefile\Geometry\Geometry
. We also wrap this in a class that adds a couple of additional methods:
... $reader->each(function($geometry) { $geometry->asGeoJson(); // passes through to the getGeoJSON() method with the "as" verbiage commonly used in Laravel $geometry->asJson(); // a convenience method that runs the GeoJSON through json_decode first $geometry->getData('ID'); // allows retreival of a specific key in the data array $geometry->ID; // you can also access the data array as properties on the Geometry class $geometry->getDataArray(); // methods are passed through to the underlying Shapefile\Geometry\Geometry class $geometry->getRaw(); // you can retrieve the underlying Shapefile\Geometry\Geometry class with the getRaw() method }); ...
To-do
We don't currently implement a wrapper around the ShapefileWriter class, since we don't use it. That might be added one day if we need it.
Testing
composer test
The package comes with a small shapefile containing two polygons. Note, we've only tested this with polygons but there's no reason why it shouldn't work with any other kind of feature.
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email chad@zactax.com instead of using the issue tracker.
Credits
- Gaspare Sganga - Author of the underlying package
- Chad Janicek
- Laravel Package Boilerplate
License
The MIT License (MIT). Please see License File for more information.