PHP ShapeFile 2.2.0
Random access to specific record, handling of unexpected bytes in the files and Iterator interface
23 November 2016
I stumbled across some shapefiles which seemed to be corrupted, even if many GIS softwares were still perfectly capable of reading them.
The answer lies in the reason why all three shp, dbf and shx are required for a shapefile to be deemed valid: there can be some useless, apparently random, bytes between records in the main shp file!
The sequential read approach used by this library wasn’t obviously capable of handling them, so a few changes were required.
What’s new in Version 2.2.0
- SHX file is now required
- Capability to randomly access the shapefile records
- Implements the Iterator interface
SHX file is now required
Technically this is not a breaking change, hovewer it could potentially break your code if you are explicitly passing an array with the single filenames without specifying the shx one. Check the constructor syntax.
Capability to randomly access the shapefile
It goes without saying that having the shx file available, the library is now capable of access a specific record, without needing to sequentially reed all of them.
Implements the Iterator interface
The library now implements the Iterator interface, meaning you can use a foreach loop to iterate over the records if you like.
This new method provides the number of records in the shapefile. See getTotRecords.
You can manually set the pointer to a specific record with this method. See setCurrentRecord.
Gets the current record pointer. See getCurrentRecord.
Download and documentation
Go to the Lab page: PHP ShapeFile