Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

iharrison
Copy link

Python script and Topo50 index sheets shapefile for processing Sentinel 2 imagery.

Python script and Topo50 index sheets shapefile for processing Sentinel 2 imagery.
@iharrison iharrison marked this pull request as ready for review May 2, 2024 21:40
@iharrison iharrison requested a review from l0b0 May 2, 2024 21:40
@iharrison iharrison added the enhancement New feature or request label May 2, 2024
Comment on lines +43 to +47
rgb_files = glob.glob("R10m/*B0[2-4]_10m.jp2")

blue = rgb_files[0]
green = rgb_files[1]
red = rgb_files[2]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If rgb_files is supposed to have exactly three files you can unpack all of them in one go:

Suggested change
rgb_files = glob.glob("R10m/*B0[2-4]_10m.jp2")
blue = rgb_files[0]
green = rgb_files[1]
red = rgb_files[2]
blue, green, red = glob.glob("R10m/*B0[2-4]_10m.jp2")

import subprocess


# user definated variables
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These aren't really user defined anymore if they're in the script. If you want to pass them as arguments, I'd recommend looking into argparse (example).

TILE_INDEX = "nz-150k-tile-index.shp"
MIN = "800"
MAX = "3000"
CWD = "C:/Temp/image-processing/test"
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 os.getcwd().

MAX = "3000"
CWD = "C:/Temp/image-processing/test"
SENTINEL_IMAGE_DIR = "R10m"
GDAL_DIR = "C:/Program Files/QGIS 3.34.4/apps/Python39/Scripts"
Copy link
Collaborator

Choose a reason for hiding this comment

The 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:

  • Pass the directory as an option, like --gdal-dir="C:/Program Files/QGIS 3.34.4/apps/Python39/Scripts".
  • If files from that directory are discoverable by the surrounding shell, that is, if you can run for example gdal_merge.py without specifying its directory, then you can use shutil.which("gdal_merge.py") to get its absolute path.

Comment on lines +28 to +29
if os.path.exists(path):
shutil.rmtree(path)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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:

  • raising an exception telling the user what to do, or
  • using tempfile.mkdtemp to create a new random directory every run and tell the user which directory to look for their data in.

"""
Create directors for the various processed images to be saved into.
"""
path = os.path.join(CWD, dir_name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you from os.path import join you can shorten these expressions. But this is a stylistic choice, so I'll leave it to you whether you want to do that.

print("Merging image tiles into one RGB image")
subprocess.run(
[
"python.exe",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you run just "python" instead, it should work on other platforms without changes. But this line shouldn't even be necessary, since gdal_merge.py should be executable.

Comment on lines +140 to +145
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")
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 ogr_output = subprocess.run([…], capture_output=True, check=True, encoding="utf-8").output.

Comment on lines +16 to +17
MIN = "800"
MAX = "3000"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are passed to gdal_translate -scale; based on its documentation, would it be useful to name them something like MIN_SCALE and MAX_SCALE?

Comment on lines +87 to +88
"1",
"255",
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

import os
import glob
import shutil
from osgeo import gdal
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

2 participants