[GIS] How to reproject all the vector files in a folder to a particular projection using ogr2ogr

ogr2ogrqgisvector

We have a folder with all sorts of vector files like tab,shp,dxf. Many of the files are in different projection. I would like to convert all the files to EPSG: 4326 and eventually the non-shapefiles to shapefiles.

I've used the following command

For %%f in ("*.shp",".tab",".dxf") do ogr2ogr -t_SRS EPSG:4326 "ESRI Shapefile" "%~dpnf.shp" "%%f"

It say's.."Failure COuldn't fetch requested layer".

And how to save the output files in a separate folder, not in the same folder itself

Edit:

I've attached the image of the error

enter image description here

Best Answer

For doing that is required a script; not a command. As I don't know which operative system you have, I am going to use a bash script in my Linux system (in Windows the equivalent is a bat file) and by using only shapefiles.

#!/bin/bash
for i in *.shp
do
ogr2ogr -t_srs EPSG:4326 out_data/$i $i
done 

The above script (named as convert and running with ./convert) worked well and it projected all the shapefiles in the current directory to ESPG:4326 and it saved them with the same name in the out_data directory (out_data must be created in the directory where is running the script).

The following image is the working directory with diferents shapefiles and raster (included the script and out_data):

enter image description here

Directory out_data after running the script (the shapefiles were loaded to QGIS with the fly projection activated for verification purpose). It only contain shapefiles with the desired projection.

enter image description here

Editing Note:

I didn't know the bat sintaxis but I found it. It's precisely in one line command. The correct sintaxis is:

FOR %%i IN (*.shp) DO ogr2ogr -t_srs EPSG:4326 "out_data/%%i" "%%i%"

The above bat file (named as convert.bat and running with convert) worked well and it projected all the shapefiles in the current directory to ESPG:4326 and it saved them with the same name in the out_data directory (out_data must be created in the directory where is running the bat file).

Successful execution of the bat file in my XP Windows System:

enter image description here