0

I have a GeoJSON file that has bounding boxes (squares), and I also have many TIFF images. I want to crop the TIFF images exactly to bounding boxes provided by the GeoJSON file. While doing this, I want to preserve the coordinates of the TIFF images that is shown in QGIS. I searched a lot, but I could not find useful information about it.

4
  • 2
    Use gdalwarp with crop_to_cutline gdal.org/programs/gdalwarp.html. – user30184 Jul 29 '20 at 20:42
  • Thank you very much! I do not know how to use gdalwarp and crop_to_cutline. Could you please provide me with a link to a similar problem? – Rohullah Najibi Jul 29 '20 at 21:32
  • Did you read the docs that @user30184 linked to? It has examples. – user2856 Jul 29 '20 at 22:18
  • Yes, I saw them. However, they did not help much. – Rohullah Najibi Jul 29 '20 at 23:12
2

As @user30184 has mentioned, gdalwarp can perform exactly what you are looking for and can be called from the command line with the appropriate operations flags.

If you are working from python (as has been tagged) and all your data is in the proper coordinate systems, you might find the rasterio and fiona command line tools a bit simpler to handle. The operation that you are looking for is called clip

After installing both of those libraries, I would follow closely the example that the rasterio documentation shows. Something along the lines of...

rio clip input.tif output.tif --bounds $(fio info my_geojson.json --bounds)

...for each input.tif (using a loop or manually entering each if you aren't comfortable with shell scripting) would give you the desired outcome.

3
  • Thanks for the answer. However, this is what I get after running the code: ERROR 4: Unable to open EPSG support file gcs.csv. Try setting the GDAL_DATA environment variable to point to the directory containing EPSG csv files. ERROR:fiona._env:Unable to open EPSG support file gcs.csv. Try setting the GDAL_DATA environment variable to point to the directory containing EPSG csv files. Usage: rio clip [OPTIONS] INPUT OUTPUT Error: Invalid value for "--bounds": '907499.9999450892' is not a valid bounding box representation – Rohullah Najibi Jul 30 '20 at 18:34
  • This looks like a GDAL issue. Is your GDAL_DATA environment variable set properly? – Trevor J. Smith Jul 30 '20 at 18:45
  • Hi Trevor, I do not really know. – Rohullah Najibi Jul 30 '20 at 18:52

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.