Search code examples
phppostgresqlpostgisshapefile

Storing a shapefile into postgresql using PHP


I'm trying to develop a PHP script that lets users upload shapefiles to import to a postGIS database.

First of all, for the conversion part, AFAIK we can use shp2pgsql to convert the shapefile to a postgresql table; I was wondering if there is another way of doing the conversion, as I would prefer not to use the exec() command.

I would also appretiate any idea on storing the data in a way that does not require dozens of uniquenamed tables.


Solution

  • There seems to be no other way than using the postgresql's binary to convert the shapefile. Although it is not really a bad choice, I would rather not use exec() if there is a PHP native function, or apache module to do it! However, it sounds like exec is the only sane option available. So I'm going to use it. No hard feelings! :)

    About the last part, it's a different question and should be asked separately. Although, I'm afraid there is no other way of doing it.

    UPDATE example added

    $queries = shell_exec("shp2pgsql -s ".SRID." -c $shpfilpath $tblname")
                        or respond(false, "Error parsing the shapfile.");
    pg_query($queries) or respond(false, "Query failed!");
    
    • SRID is a constant containing the "SRID"!
    • $shpfilpath is a path to the desired ShapeFile
    • $tblname is the desired name for the table