1

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.

4
  • Can you be more specific about what a "shape" is? If it is a polygon having lines between a number of (x, y) nodes, then a table having (x, y, ord) columns will do the trick. The ord is to keep the coordinates in the right order, of course. Commented Jul 11, 2013 at 14:34
  • @halfer 'shapefile's are datasheets, containing a column with geometrical data. Commented Jul 11, 2013 at 14:36
  • 1
    What is the cause for needing "dozens of unique named coloumns"? I don't see anything in base file structure that would require this in the data output. Also, why do you want to avoid exec/shellcmd/etc? Running an external compiled application to convert the data will be far more efficient than attempting to process the data with something like PHP, and there is nothing particularly wrong with doing so. Commented Jul 11, 2013 at 14:44
  • @JacobS Sorry, my bad. I meant "tables"! Commented Jul 12, 2013 at 10:09

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

How exactly did you use it? Can you provide an example? Thanks
1

See this blog post about loading shapefiles using the PHP shapefile reader plugin from here. http://www.phpclasses.org/package/1741-PHP-Read-vectorial-data-from-geographic-shape-files.html. The blog post focuses on using PHP on the backend to load data for a Flash app, but you should be able to ignore the flash part and use the PHP portion for your needs.

Once you have the data loaded from the shapefile, you could convert the geometry to a WKT string and use ST_GeomFromText or other PostGIS functions to store in the database.

Regarding the unique columns for a shapefile, I've found that to be the most straightforward way to store ad-hoc shapefile attributes and then retrieve that data. However, you could use a "tuple" system, and convert the attributes to strings, then store them in arbitrarily named columns (col1, col2, col3, etc.) if you don't care about attribute names or types.

If you cared about names and types, you could go one step further and store them as a shapefile "schema" in another table.

Comments

0
  • Write your shp2pgsql and define its parameters using text editor ie sublime notepad etc.
  • Copy, paste and change shapefile name for each layer.
  • Save as a batch file .bat.
  • Pull up command window.
  • Pull up directory with where yu .bat file is saved.
  • Hit enter and itll run the code for all your shapefiles and they will be uploaded to the database you defined in writing your code.
  • Use qgis, go to postgis window and hit connect.

You are good to go your shapefiles are now ready to go and can be added as layers to your map. Make sure the spatial reference matches what they were prior to running it. Does that make sense? I hope that helped its the quickest way.

1 Comment

Unfortunately not. As I mentioned, it's an script that enables the users of the website to upload their own shapefiles. Welcome to SOF though :)
0

Adding this answer just for the benefit of anyone who is looking for the same as the OP and does not want to rely on eval() nor external tools.

As of August 2019, you could use PHP Shapefile, a free and open source PHP library I have been developing and maintaining for a few years that can read and write any ESRI Shapefile and convert it natively from/to WKT and GeoJSON, without any third party dependency.

Using my library, which provides WKT to use with PostGIS ST_GeomFromText() function and an array containing all the data to perform a simple INSERT, makes this task trivial, fast and secure, without the need of evil eval().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.