Friday, 12 October 2018

batch - Using ogr2ogr to reproject and rename into shapefile?


I'm trying to reproject, move from tab to shp and rename all the .tab contained in a folder tree using ogr2ogr.



I'm working with Windows 7, QGIS 1.8 installed with advanced OsGeo4w and gdal 1.9.2. I have no experience in batch.


Here's the code :


for /R %%f IN ('D:\3.BATCH\FAUNE\*.tab') do C:\Users\Samy\Desktop\SIGMA\15.webmapping\ms4w\tools\gdal-ogr\ogr2ogr.exe -s_srs EPSG:3727 -t_srs EPSG:2975 %%f "ESRI Shapefile" %%f_2975.shp %%f
pause

By calling this .bat into the cmd line, nothing append. When I simplify the code, I have in the best case "Unable to open" as Nik posted in the 3rd ressource below.


I wrote this code with the following questions:




  1. Using ogr2ogr to convert all shapefiles in directory?





  2. Is there any way to reproject multiple layers in QGIS?




  3. How to reproject shapefile using ogr2ogr




According to resource 1, I translate the -f "MapInfo File" "%~dpnf.tab" "%f" into %%f "ESRI Shapefile" %%f_2975.shp %%f because as it is shape, I can't use dpnf.


According to resource 2, I wrote -s_srs EPSG:3727 -t_srs EPSG:2975 to reproject.



Finally I checked that the "Hide extensions for known file types" was off, according to ressource 3.


I don't know what to do now. There seems to be so many different ways to write this one line of code.



Answer



There are a few %%f too much in your command line. This one worked for me (with another conversion target):


for /R %%F IN ('D:\3.BATCH\FAUNE\*.tab') do C:\Users\Samy\Desktop\SIGMA\15.webmapping\ms4w\tools\gdal-ogr\ogr2ogr.exe -s_srs EPSG:3727 -t_srs EPSG:2975 -f "ESRI Shapefile" %%~dpnF_2975.shp %%F

Main obstacle is that %%F already contains the file extension, so you have to skip that with ~dpnF_2975.shp.


Note that f and F are considered as different by the Operating System.


You skipped the ~dpnf, but that is the essential part of clipping the file name to the desired part without extension. You can replace dp with full drive and path name.


No comments:

Post a Comment