6

I am new to ogr2ogr. Does anyone know if it is possible to loop through all shapefiles in a directory, clip and output them into an identical directory with a suffix "_clipped"?

I saw this similar question about converting all files in a directory. Problem here is that using this method I overwrite all my files. Using ogr2ogr to convert all shapefiles in directory?

EDIT:

I want to make a .bat script to execute the code. This is how far I've come. It works if I output to the same folders as input, but I want it to output to some other folder. The user enters a new folder name, to where the clipped files will be saved.

Here is the returned errors:
Error 1: Failed to create file .shp file
Error 4: Failed to open shapefile 'correct path'

--
set parent=%~dp0
set ogr2ogrPath="C:\Program Files\QGIS 2.16.1\bin\ogr2ogr.exe"

set /p inFolder=Enter the name of the root folder containing the shapefiles to clip:
set /p outFolder=Enter a new name for the folder to save the clipped files:
set /p xMin=Enter xMinCoord:
set /p yMin=Enter yMinCoord:
set /p xMax=Enter xMaxCoord:
set /p xMax=Enter yMaxCoord:

for /R %parent%%inFolder% %%f in (*.shp) do %ogr2ogrPath% -skipfailures -clipsrc xMin yMin xMax xMax %parent%%outFolder%\%%~nf.shp %%f

9

Modifying the referenced answer, in the Windows command line:

for /R %f in (*.shp) do ogr2ogr "%~dpnf_clipped.shp" -clipsrc clipper.shp "%f"

You just add _clipped to the output. Note, this will also clip the clipping file, and work recursively, so it will also clip the shapefiles in sub-folders.

If you don't want it recursive:

for %f in (*.shp) do ogr2ogr "%~dpnf_clipped.shp" -clipsrc clipper.shp "%f"

To run it into a subfolder without changing names of the files, although it cannot create the folder, so an empty "clipped" folder needs to exist:

for %f in (*.shp) do ogr2ogr ".\clipped\%~nf.shp" -clipsrc clipper.shp "%f"

And finally for a different folder in the same path. So instead of "C:\data" to get it into "C:\clipped" you can do:

for %f in (*.shp) do ogr2ogr ".\..\clipped\%~nf.shp" -clipsrc clipper.shp "%f"

Like with the previous command, that folder needs to exist.

  • 2
    Note that Windows command line and batch file command differ: You have to double the percent %f to %%f. See ss64.com/nt/for.html for details. – AndreJ Aug 23 '16 at 15:53
  • Added that this was for the command line. – HeikkiVesanto Aug 23 '16 at 16:06
  • Yes, it will work, but I'd like it to output all files with file name unchanged, in an other folder with a different name. Do you think it is possible? – Adrian Tofting Aug 24 '16 at 15:55
  • Updated answer. – HeikkiVesanto Aug 29 '16 at 10:18
  • This is it! Thank you. All I was missing was adding the output folder first. – Adrian Tofting Aug 29 '16 at 13:19
1

Can you use Python? You could use some for cycle. I tried to write some concept here:

import os

wd = r"c:\your\working\dir"
os.chdir = wd
list_of_files = os.listdir(wd)

file_that_clips = "path_to_shapefile_that_clips_other"

for file in list_of_files:
    if file_to_clip[-3:] == "shp":                          # If file is a shapefile
        clipped_file = file_to_clip[:-4] + "_clipped.shp"   # New name for output
        os.system("ogr2ogr -clipsrclayer " + file_that_clips + " " + clipped_file + " " + file_to_clip)    # Execute cmd order

I'm not sure about the ogr2ogr Clip statement. But you could copy there yours.

  • Thanks, but I will try to keep it as simple as possible, so I'll look for a solution to be executed in shell first. – Adrian Tofting Aug 24 '16 at 15:52

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.