Skip to content

Commit 5dae0c8

Browse files
committed
temp fix to raster clip with a shapefile
1 parent 4b2275b commit 5dae0c8

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed
0 Bytes
Binary file not shown.

dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
FROM python:3.8
22
RUN apt-get -y update
33
RUN apt-get -y install libgdal-dev gdal-bin
4+
RUN pip install rasterio geopandas
45

56
COPY main.py .
67

main.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import glob
77
import json
88
from shutil import rmtree
9+
import rasterio
10+
import geopandas as gpd
911

1012
import string
1113
import random
@@ -382,15 +384,27 @@ def get_crs_of_data(file, vector=False):
382384

383385
else:
384386
print('clipping with bounding box of vector data')
385-
command_output = subprocess.run(["gdalwarp", "-cutline", clip_file[0], join(data_path, input_dir, data_to_clip_dir, input_file),
386-
join(data_path, output_dir, output_file_name_set)])
387+
388+
print(join(data_path, input_dir, data_to_clip_dir, input_file))
389+
print(join(data_path, output_dir, output_file_name_set)
390+
)
391+
# this should work but does not for some reason....
392+
#command_output = subprocess.run(["gdalwarp", "-cutline", clip_file[0], join(data_path, input_dir, data_to_clip_dir, input_file), join(data_path, output_dir, output_file_name_set)])
393+
# so instead using this....
394+
395+
# read in shapefile
396+
t = gpd.read_file(clip_file[0])
397+
# get bounding box for shapefile
398+
bounds = t.geometry.total_bounds
399+
# run clip
400+
subprocess.run(["gdalwarp", "-te", str(bounds[0]), str(bounds[1]), str(bounds[2]), str(bounds[3]), join(data_path, input_dir, data_to_clip_dir, input_file), join(data_path, output_dir, output_file_name_set)])
387401

388402
# check the code returned from GDAL to see if an error occurred or not
389-
if command_output.returncode == 1:
390-
print('Error! Clip did not run. Please check for common errors such as missing projection information.')
391-
logger.info('Error! Clip did not run for %s. Please check for common errors such as missing projection information.' % input_file)
392-
elif command_output.returncode == 0:
393-
logger.info('Clip process ran without an error being returned (%s)' % input_file)
403+
#if command_output.returncode == 1:
404+
# print('Error! Clip did not run. Please check for common errors such as missing projection information.')
405+
# logger.info('Error! Clip did not run for %s. Please check for common errors such as missing projection information.' % input_file)
406+
#elif command_output.returncode == 0:
407+
# logger.info('Clip process ran without an error being returned (%s)' % input_file)
394408

395409
# add check to see if file written to directory as expected
396410
if isfile(join(data_path, output_dir, output_file_name_set)):

0 commit comments

Comments
 (0)