Hi,
I'm using a script (see below) in grass57 to batch process 457 raster files into polygon shapefiles via v.out.ogr. After a some of the files (~80) have been processed successfully, I get the following error message when v.out.ogr tries to write the shapefile.
ERROR 4: Failed to open Shapefile `./Shapefiles/Vireo_flavifrons_9460.shp'.
Too many open files: /usr/local/grass57/driver/db/
ERROR: Cannot open driver pg
The script continues to run but no further shapefiles are exported. I guess I must need to include a command to close connections to the driver within the for loop - any suggestions?
Thanks,
David
#!/bin/bash
cd /Users/dorme/Work/ADHoC/America\ Breeding\ Atlas/
# requires a PostgreSQL dtabase called NoAmAlbers
if /usr/local/pgsql/bin/psql -l | grep -q "NoAmAlbers"
then
v.database driver=pg database=NoAmAlbers
#check for output folder
if ! ls | grep -q Shapefiles
then
mkdir ./Shapefiles
fi
for species in `g.mlist -r rast pattern='_[0-9]'`
do
# get vector file of areas where density >=0.5
r.mapcalc "tmp=if($species>=0.5,1,null())"
r.to.vect tmp output=$species feature=area
# restructure table to shapefile format
# clunky - substitute $species into .sql file using sed?
echo "ALTER TABLE $species rename cat to id;" | db.execute
echo "ALTER TABLE $species drop value;" | db.execute
echo "ALTER TABLE $species drop label;" | db.execute
echo "ALTER TABLE $species add sm_name varchar(50);" | db.execute
echo "ALTER TABLE $species add sm_number int;" | db.execute
echo "ALTER TABLE $species add date_ date;" | db.execute
echo "ALTER TABLE $species add presence int;" | db.execute
echo "ALTER TABLE $species add origin int;" | db.execute
echo "ALTER TABLE $species add by_ varchar(4);" | db.execute
echo "ALTER TABLE $species add source varchar(50);" | db.execute
echo "ALTER TABLE $species add src_name varchar(50);" | db.execute
echo "ALTER TABLE $species add Area double precision;" | db.execute
echo "ALTER TABLE $species add Rest_rng integer;" | db.execute
echo "ALTER TABLE $species add IUCN_thr varchar(5);" | db.execute
echo "ALTER TABLE $species add Data_qual int;" | db.execute
echo "ALTER TABLE $species add Data_type int;" | db.execute
echo "ALTER TABLE $species add Island_ID varchar(50);" | db.execute
echo "ALTER TABLE $species add Tax_cmnt varchar(50);" | db.execute
echo "ALTER TABLE $species add Dist_cmnt varchar(50);" | db.execute
# fill in table
echo "UPDATE $species SET sm_name = '$species', sm_number = 1, date_ ='today', presence=1, origin=1, by_ = 'DO',source = 'xyz (2003) #16', src_name = '$species';" | db.execute
# reconnect the vector data to the modified table
v.db.connect -o map=$species driver=pg database=NoAmAlbers table=$species key=id
# dump the shapefile version
v.out.ogr -c $species dsn=./Shapefiles layer=$species type=area
# delete the temporary raste
g.remove rast="tmp"
done
else
echo "PostgreSQL database NoAmAlbers does not exist:"
echo " - create using createdb NoAmAlbers"
fi