3

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

  • 2
    Why don't you split the script in 3? One for shp, one for tab and one for dxf? I'm not sure that you can convert them all in a new EPSG, if all of them don't have an associated projection system (like dxf I suppose). You have to set the original EPSG for all of them. – Sergio Mar 13 '15 at 13:44
  • I would guess that first failure happens here because the -f switch for the format is missing: ogr2ogr -t_SRS EPSG:4326 "ESRI Shapefile" – user30184 Mar 13 '15 at 14:50
  • I've tried the command with -f & also the batch as suggested by xunilk in the comment section. It gives the same error. I've edited the question & attached an image of it – joseph_k Mar 16 '15 at 4:08
3

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

  • I've edited the question and attached an image. COuld you have a look ? Thanks for your reply – joseph_k Mar 16 '15 at 4:07
  • 1
    Did you use my bat file exactly? I saw that you are employing spaces in shapefiles' names. This is not a very good practice. Please, create a new folder and put in it my bat file and three shapefiles (*.dbf, *.prj, *.shp, *.shx) without spaces in their names. Don't forget to create the out_data directory. Run my bat file. – xunilk Mar 16 '15 at 6:25
  • OK. The problem was with the spaces in the names. But we can't do much about it. Those files are from the client. The folder will have tab files, dxf files, shp files & all sorts of vector files. Another question. Instead of creating a folder manually, how to create it during run time ? Anyway, Thanks for the quick response – joseph_k Mar 16 '15 at 6:59

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.