phpmyadmin / shapefile

ESRI ShapeFile library for PHP
https://packagist.org/packages/phpmyadmin/shapefile
GNU General Public License v2.0
28 stars 8 forks source link

Should not fail is there is no dbf file #36

Open JoolsMcFly opened 1 month ago

JoolsMcFly commented 1 month ago

Hi.

This lib handles shape files with no dbf files if dbase is not installed on the server. If dbase is installed and no dbf is found then lib bails out and doesn't process the shape file.

dbase is installed on our server but some of our clients do not include a dbf file so we cannot process their Shape file. Bit of a bummer!

It would be better, IMHO, to just proceed without dbf just like what happens without dbase.

So it basically boilds down to changing the following code

$dbfName = $this->getFilename('.dbf');
if (! is_readable($dbfName)) {
    $this->setError(sprintf('It wasn\'t possible to find the DBase file "%s"', $dbfName));

    return false;
}

so that it returns true;

$dbfName = $this->getFilename('.dbf');
if (! is_readable($dbfName)) {
    $this->setError(sprintf('It wasn\'t possible to find the DBase file "%s"', $dbfName));

    return true;
}

What do you think?

JoolsMcFly commented 4 weeks ago

Hi.

Just checking if you have any thoughts on this?

JoolsMcFly commented 2 weeks ago

bump after editing my original post.

Thoughts @MauricioFauth ?

MauricioFauth commented 2 weeks ago

Changing that return type is a BC break. Maybe adding an option to allow a missing file is better.

JoolsMcFly commented 2 weeks ago

Indeed.

I'm thinking either a second param (boolean) to public function loadFromFile(string $fileName): bool which would be passed to openDBFFile OR a method to set allowNoDbf to true.

Something like

$shp = new ShapeFile(0);
$shp->setAllowNoDbf(true);
private setAllowNoDbf(bool $allowNoDbf): void
{
    $this->allowNoDbf = $allowNoDbf;
}

private function openDBFFile(): bool
{
    if (! self::supportsDbase()) {
        $this->dbfFile = null;

        return true;
    }

    $dbfName = $this->getFilename('.dbf');
    if (! is_readable($dbfName)) {
        // changes start here
        if ($this->allowNoDbf) {
            return true;
        }
        // and end here

        $this->setError(sprintf('It wasn\'t possible to find the DBase file "%s"', $dbfName));

        return false;
    }

What do you think?