-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add sentinel 2 script #335
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
UTF-8 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PROJCS["NZGD_2000_New_Zealand_Transverse_Mercator",GEOGCS["GCS_NZGD_2000",DATUM["D_NZGD_2000",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",1600000.0],PARAMETER["False_Northing",10000000.0],PARAMETER["Central_Meridian",173.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]] |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,167 @@ | ||||||||||||||
#!/usr/bin/env python3 | ||||||||||||||
|
||||||||||||||
""" | ||||||||||||||
This is a Python version of a bash script written by Rebecca Clarke https://gist.github.com/rebclarke/b9e55a3558be3ec76248c78379b67366 | ||||||||||||||
""" | ||||||||||||||
|
||||||||||||||
import os | ||||||||||||||
import glob | ||||||||||||||
import shutil | ||||||||||||||
from osgeo import gdal | ||||||||||||||
import subprocess | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
# user definated variables | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||
TILE_INDEX = "nz-150k-tile-index.shp" | ||||||||||||||
MIN = "800" | ||||||||||||||
MAX = "3000" | ||||||||||||||
Comment on lines
+16
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are passed to |
||||||||||||||
CWD = "C:/Temp/image-processing/test" | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "CWD" ("current working directory") has a specific meaning in operating systems, being the directory from which a program was executed (not the directory where the script lives). You can get this directory using |
||||||||||||||
SENTINEL_IMAGE_DIR = "R10m" | ||||||||||||||
GDAL_DIR = "C:/Program Files/QGIS 3.34.4/apps/Python39/Scripts" | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very brittle, because the directory path will be different for anyone using a different OS or version of QGIS. This can be made more flexible in a couple of ways:
|
||||||||||||||
|
||||||||||||||
|
||||||||||||||
def make_directories(dir_name): | ||||||||||||||
""" | ||||||||||||||
Create directors for the various processed images to be saved into. | ||||||||||||||
""" | ||||||||||||||
path = os.path.join(CWD, dir_name) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you |
||||||||||||||
if os.path.exists(path): | ||||||||||||||
shutil.rmtree(path) | ||||||||||||||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very risky! It would be pretty easy for a programming mistake to delete a bunch of files which shouldn't be deleted. I'd recommend either:
|
||||||||||||||
os.mkdir(path) | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
# make output directories | ||||||||||||||
make_directories("output") | ||||||||||||||
make_directories("output/merged") | ||||||||||||||
make_directories("output/rescaled") | ||||||||||||||
make_directories("output/nearblack") | ||||||||||||||
make_directories("output/reprojected") | ||||||||||||||
make_directories("output/tiled") | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
path = os.path.join(CWD, SENTINEL_IMAGE_DIR) | ||||||||||||||
rgb_files = glob.glob("R10m/*B0[2-4]_10m.jp2") | ||||||||||||||
|
||||||||||||||
blue = rgb_files[0] | ||||||||||||||
green = rgb_files[1] | ||||||||||||||
red = rgb_files[2] | ||||||||||||||
Comment on lines
+43
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
Suggested change
|
||||||||||||||
|
||||||||||||||
gdal_merge_path = os.path.join(GDAL_DIR, "gdal_merge.py") | ||||||||||||||
|
||||||||||||||
merged_output = os.path.join(CWD, "output/merged", "merged.tif") | ||||||||||||||
translate_output = os.path.join(CWD, "output/rescaled", "rescaled.tif") | ||||||||||||||
nearblack_output = os.path.join(CWD, "output/nearblack", "nearblack.tif") | ||||||||||||||
reprojected_output = os.path.join(CWD, "output/reprojected", "reprojected.tif") | ||||||||||||||
tiled_output_dir = os.path.join(CWD, "output/tiled") | ||||||||||||||
vrt_output = os.path.join(CWD, "output", "overall.vrt") | ||||||||||||||
|
||||||||||||||
# merge the three R,G,B images together to create a single RGB image | ||||||||||||||
print("Merging image tiles into one RGB image") | ||||||||||||||
subprocess.run( | ||||||||||||||
[ | ||||||||||||||
"python.exe", | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you run just |
||||||||||||||
gdal_merge_path, | ||||||||||||||
"-separate", | ||||||||||||||
"-co", | ||||||||||||||
"PHOTOMETRIC=RGB", | ||||||||||||||
"-o", | ||||||||||||||
merged_output, | ||||||||||||||
red, | ||||||||||||||
green, | ||||||||||||||
blue, | ||||||||||||||
] | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
# rescale the imagery to 8bit unsigned | ||||||||||||||
print("Rescaling to 8bit unsigned") | ||||||||||||||
subprocess.run( | ||||||||||||||
[ | ||||||||||||||
"gdal_translate", | ||||||||||||||
"-ot", | ||||||||||||||
"Byte", | ||||||||||||||
"-of", | ||||||||||||||
"GTiff", | ||||||||||||||
"-scale", | ||||||||||||||
MIN, | ||||||||||||||
MAX, | ||||||||||||||
"1", | ||||||||||||||
"255", | ||||||||||||||
Comment on lines
+87
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the documentation, "If omitted the output range is 0 to 255". Do we actually want to use the output range 1 to 255 instead? |
||||||||||||||
"-b", | ||||||||||||||
"1", | ||||||||||||||
"-b", | ||||||||||||||
"2", | ||||||||||||||
"-b", | ||||||||||||||
"3", | ||||||||||||||
"-co", | ||||||||||||||
"compress=lzw", | ||||||||||||||
merged_output, | ||||||||||||||
translate_output, | ||||||||||||||
] | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
# run nearblack | ||||||||||||||
print("Running nearblack and setting alpha channel") | ||||||||||||||
subprocess.run( | ||||||||||||||
[ | ||||||||||||||
"nearblack", | ||||||||||||||
"-of", | ||||||||||||||
"GTiff", | ||||||||||||||
"-setalpha", | ||||||||||||||
"-o", | ||||||||||||||
nearblack_output, | ||||||||||||||
translate_output, | ||||||||||||||
] | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
# reproject to NZTM 2000 | ||||||||||||||
print("Reprojecting to NZTM 2000") | ||||||||||||||
subprocess.run( | ||||||||||||||
[ | ||||||||||||||
"gdalwarp", | ||||||||||||||
"-t_srs", | ||||||||||||||
"EPSG:2193", | ||||||||||||||
"-r", | ||||||||||||||
"bilinear", | ||||||||||||||
"-tr", | ||||||||||||||
"10", | ||||||||||||||
"-10", | ||||||||||||||
"-co", | ||||||||||||||
"compress=lzw", | ||||||||||||||
nearblack_output, | ||||||||||||||
reprojected_output, | ||||||||||||||
] | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
# mosaic into a single image | ||||||||||||||
print("Building VRT") | ||||||||||||||
subprocess.run(["gdalbuildvrt", vrt_output, reprojected_output]) | ||||||||||||||
|
||||||||||||||
print("Tiling to Topo 50") | ||||||||||||||
ogr_info = subprocess.Popen( | ||||||||||||||
["ogrinfo", "-ro", "-al", TILE_INDEX], stdout=subprocess.PIPE | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
# tile into Topo50 sheet extents | ||||||||||||||
ogr_output = ogr_info.communicate()[0].decode("utf-8") | ||||||||||||||
Comment on lines
+140
to
+145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like you run this and then just wait for it to finish immediately. In that case I'd recommend |
||||||||||||||
for line in ogr_output.splitlines(): | ||||||||||||||
if "sheet_code" in line: | ||||||||||||||
sheet_code = line[len(line) - 4 : len(line) - 0] | ||||||||||||||
subprocess.run( | ||||||||||||||
[ | ||||||||||||||
"gdalwarp", | ||||||||||||||
vrt_output, | ||||||||||||||
f"{tiled_output_dir}/{sheet_code}.tif", | ||||||||||||||
"-cutline", | ||||||||||||||
TILE_INDEX, | ||||||||||||||
"-cwhere", | ||||||||||||||
f"sheet_code = '{sheet_code}'", | ||||||||||||||
"-tr", | ||||||||||||||
"10", | ||||||||||||||
"-10", | ||||||||||||||
"-co", | ||||||||||||||
"compress=lzw", | ||||||||||||||
"-t_srs", | ||||||||||||||
"EPSG:2193", | ||||||||||||||
"-crop_to_cutline", | ||||||||||||||
] | ||||||||||||||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't look like you're using this import anywhere. Did you mean to change the
subprocess
uses below to use this library? That would be preferable.