Skip to content

Instantly share code, notes, and snippets.

@sgibbes
Created August 20, 2018 20:28
Show Gist options
  • Save sgibbes/3dc7ae2f2897cc8c04a0f01056fb0cc6 to your computer and use it in GitHub Desktop.
Save sgibbes/3dc7ae2f2897cc8c04a0f01056fb0cc6 to your computer and use it in GitHub Desktop.
Clip Indonesia and Southeast Asia landcover tiles to 10x10 deg size and unset nodata
import gdal
import subprocess
def clip_using_input_coords(original):
file_to_clip = 'sea_land_cover_classified.tif'
# get coords of file to mock
src = gdal.Open(original)
ulx, xres, xskew, uly, yskew, yres = src.GetGeoTransform()
lrx = ulx + (src.RasterXSize * xres)
lry = uly + (src.RasterYSize * yres)
clipped = 'fixed/{}'.format(original)
# # clip big file to that one
cmd = ['gdalwarp', '-co', "TILED=YES", '-co', 'COMPRESS=DEFLATE', '-ot', 'Byte', '-tr', '.00025', '.00025',
'-tap', '-te', str(ulx), str(lry), str(lrx), str(uly), file_to_clip, clipped]
subprocess.check_call(cmd)
# unset nodata
cmd = ['gdal_edit.py', clipped, '-unsetnodata']
subprocess.check_call(cmd)
files_to_fix = ['4b6e12-sea_wgs84-0000065536-0000393216.tif', '3678c7-sea_wgs84-0000065536-0000458752.tif',
'a2ba68-sea_wgs84-0000000000-0000458752.tif', '8a785d-sea_wgs84-0000000000-0000262144.tif',
'7728f5-sea_wgs84-0000065536-0000327680.tif', '7dea80-sea_wgs84-0000000000-0000393216.tif',
'd27da7-sea_wgs84-0000000000-0000327680.tif']
for f in files_to_fix:
clip_using_input_coords(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment