| 1 | '''Procedure to batch load shapefiles on window pc'''[[BR]] |
| 2 | |
| 3 | I have often many shapefiles to load on postgres/postgis.[[BR]] |
| 4 | Even 50, 100 shapefile every time.[[BR]] |
| 5 | So I will need a procedure to load all simply on a windows 7 machine. |
| 6 | [[BR]] |
| 7 | This batch file is my simply but very useful solution. |
| 8 | [[BR]] |
| 9 | Create a file named "load_shapefiles.bat" and put this code: |
| 10 | |
| 11 | ---- |
| 12 | |
| 13 | {{{ |
| 14 | REM ******************************************************************* |
| 15 | REM ** Simple batch procedure for Windows shell-dos (Win7 compatible)** |
| 16 | REM ** for batch load of a list of shapefiles on a Postgres/Postgis ** |
| 17 | REM ** database. ** |
| 18 | REM ** ** |
| 19 | REM ** usage: ** |
| 20 | REM ** Create a directory for the shapefiles ** |
| 21 | REM ** Create a directory for the resulting sql-files ** |
| 22 | REM ** Set the variable PATH_SHP with the path forward the shapefiles** |
| 23 | REM ** Set the variable PATH_PSQL forward the psql.exe ** |
| 24 | REM ** Set the variable PATH_SHP2SQL forward the sh2pgsql.exe ** |
| 25 | REM ** Set the variable PATH_SQL with the path forward the directory ** |
| 26 | REM ** where will be output the sql files ** |
| 27 | REM ** Set the pghost variale with the address of the server Postgres** |
| 28 | REM ** Set the pgport variable with the port of the istance postgres ** |
| 29 | REM ** Set the pgdb variable with the database target ** |
| 30 | REM ** Set the pggeom variable with the name to usage for the ** |
| 31 | REM ** "geometry" field will be create ** |
| 32 | REM ** Set the pgencoding variable with the Encoding to use ** |
| 33 | REM ** Set the pgschema variable with the target schema for tables ** |
| 34 | REM ** Set the pgtable variale with av optionally prefix to apply ** |
| 35 | REM ** to all the tables will be create ** |
| 36 | REM ** Set the pguser variable with the user account ** |
| 37 | REM ** Set the pgpassword with the password for the account ** |
| 38 | REM ** ** |
| 39 | REM ** After all these setting (whew....) ** |
| 40 | REM ** Fortunately all these settings will remain for all other loads** |
| 41 | REM ** you will need to do :) ** |
| 42 | REM ** open a shell dos and launch ** |
| 43 | REM ** load_shapefiles.bat ** |
| 44 | REM ** and wait for the result .... :) ** |
| 45 | REM ** ** |
| 46 | REM ******************************************************************* |
| 47 | |
| 48 | set PATH_SHP=D:\dbtopo\tools_postgres\procedura_caricamento_shapefiles\shapefiles |
| 49 | set PATH_PSQL=C:\Program Files (x86)\PostgreSQL\9.0\bin\psql.exe |
| 50 | set PATH_SHP2SQL=C:\Program Files (x86)\PostgreSQL\9.0\bin\shp2pgsql.exe |
| 51 | set PATH_SQL=D:\dbtopo\tools_postgres\procedura_caricamento_shapefiles\sql |
| 52 | |
| 53 | set pghost=localhost |
| 54 | set pgport=5432 |
| 55 | set pgdb=dblotto2 |
| 56 | set pgsrid=3003 |
| 57 | set pggeom=geom |
| 58 | set pgencoding="UTF-8" |
| 59 | |
| 60 | set pgschema=public |
| 61 | set pgtable_prefix="_____" |
| 62 | |
| 63 | set pguser=dbtopowrite |
| 64 | set pgpassword=dbtopowrite |
| 65 | |
| 66 | |
| 67 | REM "Scan shapefile and create the SQL file" |
| 68 | for %%f in (%PATH_SHP%\*.shp) do "%PATH_SHP2SQL%" -s %pgsrid% -d -g %pggeom% -D -i -I -W %pgencoding% %%f %pgschema%.%pgtable_prefix%_%%~nf > %PATH_SQL%\%%~nf.sql |
| 69 | |
| 70 | REM "Scan the SQL file and load them in the DB Postgres" |
| 71 | for %%f in (%PATH_SQL%\*.sql) do "%PATH_PSQL%" -h %pghost% -p %pgport% -d %pgdb% -L %%~nxf.log -U %pguser% -f %%f |
| 72 | |
| 73 | REM "Scan the SQL files and remove all them (to return to the original configuration in the file-system" |
| 74 | for %%f in (%PATH_SQL%\*.sql) do del %%f |
| 75 | |
| 76 | |
| 77 | }}} |
| 78 | |
| 79 | ---- |
| 80 | |
| 81 | Regards to all, |
| 82 | |
| 83 | Andrea Peri. |