0

I need to import data file to my database. I created a web interface in order to upload file but I have already an error

Warning: pg_query(): Query failed: ERROR: rt_raster_from_wkb: wkb size (0) < min size (61) LINE 1: INSERT INTO eau_param_hydro(rast,filename) VALUES('','testte... ^ in C:\wamp\www\test\test.php on line 32

My code is :

<form method="post" action="" enctype="multipart/form-data">
     <label for="fichier">Ajouter raster :</label><br />
     <input type="file" name="fichier"  />
     <input type="submit" name="submit" value="Envoyer" /> </form> 
       <?php $name = $_FILES['fichier']['name']; $db = pg_connect('host=localhost port=5432 dbname=gisdb user=postgres >password=******');           $output =shell_exec("raster2pgsql -a -s 32629 -I -C -F -M '".$_FILES["fichier"]["tmp_name"]."' eau_param_hydro | psql -U postgres -d gisdb -h localhost -p 5432 ");         $req = exec(" SELECT * FROM '".$output."' ");
    $result = pg_query($db,"INSERT INTO eau_param_hydro(rast,filename)  >VALUES('".$req."','".$name."');");
         if (!$result) { 
         $errormessage = pg_last_error(); 
         echo "Error with query: " . $errormessage; 
         exit(); 
        }  pg_close();  ?>

in PostGIS I have a table :

CREATE TABLE eau_param_hydro ( rid serial NOT NULL, rast raster,
filename text, CONSTRAINT eau_param_hydro_pkey PRIMARY KEY (rid ),
CONSTRAINT enforce_num_bands_rast CHECK (st_numbands(rast) = 1),
CONSTRAINT enforce_srid_rast CHECK (st_srid(rast) = 4326) ) WITH (
OIDS=FALSE ); ALTER TABLE eau_param_hydro OWNER TO postgres;

-- Index: eau_param_hydro_st_convexhull_idx

-- DROP INDEX eau_param_hydro_st_convexhull_idx;

CREATE INDEX eau_param_hydro_st_convexhull_idx ON eau_param_hydro
USING gist (st_convexhull(rast) );

  • Why are you trying to insert with raster2pgsql as well as database insert? Your $req is no good. The raster2pgsql is not going to return a raster, which it seems your code is assuming. It will insert directly into the database assuming it even works. Does that part work? – LR1234567 Sep 1 '16 at 14:11
  • i need to insert a raster and the name of raster to my data base postgres ( in table " eau_param_hydro ") – oustyle Sep 1 '16 at 14:21
0

Here is a way to do it with just PHP, not tested. Two useful links: Writing binary files to postgres - https://stackoverflow.com/questions/11162695/store-blob-like-data-in-postgresql

-- this is a postgis function that converts a regular raster into a postgis raster http://postgis.net/docs/manual-dev/RT_ST_FromGDALRaster.html

You first of all need to do this with a parameterized query. What you are doing will not work:

 <?php $fcontents = file_get_contents($_FILES["fichier"]["tmp_name"]);
   $db = pg_connect('host=localhost port=5432 dbname=gisdb user=postgres password=******');         
   $sql = 'INSERT INTO eau_param_hydro(filename,rast)
        VALUES($1, ST_FromGDALRaster( ($2)::bytea ) );';
   $params = array($_FILES["fichier"]["name"], pg_escape_bytea($fcontents) );

  $result = pg_query_params($db, $sql, $params);
  pg_close();  ?>
  • thanks , please what's $1 and $2 ? – oustyle Sep 2 '16 at 10:51
  • That's how you parameterize in postgresql php. with pg_query_params you provide an array of options (in this case $params). The item in position 1, is what gets put in the query whereever you have $1 (so in this case the name you want to store. $2 is the raster itself (since that is what I am putting in second element of $params array. – LR1234567 Sep 4 '16 at 5:15
  • i have error : " Warning: pg_query_params(): Query failed: ERROR: function st_fromgdalraster(bytea) does not exist LINE 2: VALUES($1, ST_FromGDALRaster( ($2)::bytea ) ) ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. in C:\wamp\www\test\test.php on line 28 " – oustyle Sep 5 '16 at 8:44
  • i need your help please – oustyle Sep 5 '16 at 13:28
  • Which version of PostGIS are you running? That function came in PostGIS 2.1.0, so if you are running an earlier PostGIS you wouldn't have it. Lots of speed improvements for raster came in 2.1.0 so I would suggest upgrading if you can. – LR1234567 Sep 7 '16 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.