Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ansonl/DEM2STL
Browse files Browse the repository at this point in the history
  • Loading branch information
ansonl committed Jul 28, 2023
2 parents e835cb4 + f56f86a commit 5ae06cf
Show file tree
Hide file tree
Showing 10 changed files with 456 additions and 26 deletions.
2 changes: 1 addition & 1 deletion dem-feature-generation/dem-feature-generation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ gdalbuildvrt -resolution highest -overwrite raiseLandAIfNotInHydroMaskBAndScaleA
gdal_translate -ot Int16 -co "TILED=YES" -co "COMPRESS=ZSTD" -co "PREDICTOR=2" -co "BIGTIFF=IF_SAFER" -co "NUM_THREADS=ALL_CPUS" raiseLandAIfNotInHydroMaskBAndScaleAt4m.vrt raiseLandAIfNotInHydroMaskBAndScaleAt4m-250m-raised-460m.tif --config GDAL_VRT_ENABLE_PYTHON YES

#create VRT for raiseLandAScaleAt4m
gdalbuildvrt -resolution highest -overwrite raiseLandAScaleAt4m.vrt raiseOverSeaLevelLandAIfInHydroMaskB.vrt ../hydrographic-masks/north_america_hydrographic_mask_merge_102004_250m.tif
#gdalbuildvrt -resolution highest -overwrite raiseLandAScaleAt4m.vrt raiseOverSeaLevelLandAIfInHydroMaskB.vrt ../hydrographic-masks/north_america_hydrographic_mask_merge_102004_250m.tif
gdalbuildvrt -resolution highest -overwrite raiseLandAScaleAt4m.vrt ../sources/north_america_gmted2010_srtm_merge_102004_1000m_avg_250m_cubicspline.tif ../hydrographic-masks/north_america_hydrographic_mask_merge_102004_250m.tif
gdal_translate -ot Int16 -co "TILED=YES" -co "COMPRESS=ZSTD" -co "PREDICTOR=2" -co "BIGTIFF=IF_SAFER" -co "NUM_THREADS=ALL_CPUS" raiseLandAScaleAt4m.vrt raiseLandAScaleAt4m-250m-raised-400m.tif --config GDAL_VRT_ENABLE_PYTHON YES

Expand Down
33 changes: 28 additions & 5 deletions dem-feature-generation/demFeatureProcessing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import numpy as np

import math

#Powershell envvar settings
"""
$env:GDAL_VRT_ENABLE_PYTHON = 'YES'
Expand Down Expand Up @@ -39,6 +41,30 @@ def raiseOverSeaLevelLandAIfInHydroMaskB(a, b):
return a
"""

# Global log scale. Add 50 (popover height) before log scale.Transparent version. Output subtrahend DEM
def globalLogScaleLandADeleteIfInHydroMaskB(a,b):
if b > 0:
return -32768
elif a > 0 :
return math.log(a+50)
elif a == 0:
return 0
elif a > -32768:
return -1 * math.log(abs(a))
else:
return a

# Global log scale. Output minuend DEM
def globalLogScaleLandA(a):
if a > 0:
return math.log(a)
elif a == 0:
return 0
elif a > -32768:
return -1 * math.log(abs(a))
else:
return a

# Input elevation DEM-A and hydro mask-B. Output subtrahend DEM.
def raiseLandAIfNotInHydroMaskBAndScaleAt4m(a, b):
if b > 0:
Expand All @@ -55,7 +81,7 @@ def raiseLandAIfNotInHydroMaskBAndScaleAt4m(a, b):
else:
return a

# Input minuend DEM-A and hydro mask-B. Output minuend DEM.
# Input elevation DEM-A and hydro mask-B. Output minuend DEM.
def raiseLandAScaleAt4m(a, b):
if a > 40: # 40m -> 400m
return a + 360
Expand Down Expand Up @@ -89,10 +115,7 @@ def runOperation(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_y
#ctypes.windll.user32.MessageBoxW(0, "Your text", "Your title", 1)

#print(f'Requested {op}')
if op == 'raiseOverSeaLevelLandAIfInHydroMaskB':
vRaiseOverSeaLevelLandAIfInHydroMaskB = np.vectorize(raiseOverSeaLevelLandAIfInHydroMaskB)
np.round_(vRaiseOverSeaLevelLandAIfInHydroMaskB(in_ar[0], in_ar[1]), out=out_ar)
elif op == 'raiseLandAIfNotInHydroMaskBAndScaleAt4m':
if op == 'raiseLandAIfNotInHydroMaskBAndScaleAt4m':
vRaiseLandAIfNotInHydroMaskBAndScaleAt4m = np.vectorize(raiseLandAIfNotInHydroMaskBAndScaleAt4m)
np.round_(vRaiseLandAIfNotInHydroMaskBAndScaleAt4m(in_ar[0], in_ar[1]), out=out_ar)
elif op == 'raiseLandAScaleAt4m':
Expand Down
18 changes: 3 additions & 15 deletions hydrographic-masks/hydro1k.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
#buffer at 1k width
#rasterize 250x250m
#export in 102004

# Upsample rasterized source hydro1k to 1500x1500m resolution
# source resolution 25x25m
# output resolution 750x750m

# 25x25 > 1000x1000 > 750x750 > 500x500

#gdalwarp -overwrite -of GTiff -tr 1500.0 1500.0 ./sources/hydro1k_mask_25m_102004.tif ./hydrographic-masks/hydro1k_mask_1250m_102004.tif -r max -multi -co "TILED=YES" -co "COMPRESS=ZSTD" -co "PREDICTOR=2" -co "BIGTIFF=IF_SAFER" -co "NUM_THREADS=ALL_CPUS" -wo "NUM_THREADS=ALL_CPUS" -wo "USE_OPENCL=TRUE"

#gdalwarp -overwrite -of GTiff -tr 750.0 750.0 ./hydrographic-masks/hydro1k_mask_1250m_102004.tif ./hydrographic-masks/hydro1k_mask_750m_102004.tif -r max -multi -co "TILED=YES" -co "COMPRESS=ZSTD" -co "PREDICTOR=2" -co "BIGTIFF=IF_SAFER" -co "NUM_THREADS=ALL_CPUS" -wo "NUM_THREADS=ALL_CPUS" -wo "USE_OPENCL=TRUE"

#gdalwarp -overwrite -of GTiff -tr 250.0 250.0 ./hydrographic-masks/hydro1k_mask_750m_102004.tif ./hydrographic-masks/hydro1k_mask_250m_102004.tif -r bilinear -multi -co "TILED=YES" -co "COMPRESS=ZSTD" -co "PREDICTOR=2" -co "BIGTIFF=IF_SAFER" -co "NUM_THREADS=ALL_CPUS" -wo "NUM_THREADS=ALL_CPUS" -wo "USE_OPENCL=TRUE"
#buffer at 1000m width
#reproject to wgs84
#rasterize at source DEM 0.0002777777777777777775 degrees resolution
6 changes: 1 addition & 5 deletions hydrographic-masks/hydrographic-mask.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,4 @@ gdalwarp -overwrite -t_srs ESRI:102004 north_america_hydrographic_mask_merge.vrt

#downscale to 250x250m
gdalwarp -overwrite -tr 250.0 250.0 north_america_hydrographic_mask_merge_102004.vrt north_america_hydrographic_mask_merge_102004_250m.vrt -r max -multi -co "TILED=YES" -co "COMPRESS=ZSTD" -co "PREDICTOR=2" -co "BIGTIFF=IF_SAFER" -co "NUM_THREADS=ALL_CPUS" -wo "NUM_THREADS=ALL_CPUS" -wo "USE_OPENCL=TRUE"
gdal_translate -ot Int16 -co "TILED=YES" -co "COMPRESS=ZSTD" -co "PREDICTOR=2" -co "BIGTIFF=IF_SAFER" -co "NUM_THREADS=ALL_CPUS" north_america_hydrographic_mask_merge_102004_250m.vrt north_america_hydrographic_mask_merge_102004_250m.tif

#downscale to 250x250m test cubic?
gdalwarp -overwrite -tr 250.0 250.0 north_america_hydrographic_mask_merge_102004.vrt north_america_hydrographic_mask_merge_102004_250m_cubic.vrt -r cubic -multi -co "TILED=YES" -co "COMPRESS=ZSTD" -co "PREDICTOR=2" -co "BIGTIFF=IF_SAFER" -co "NUM_THREADS=ALL_CPUS" -wo "NUM_THREADS=ALL_CPUS" -wo "USE_OPENCL=TRUE"
gdal_translate -ot Int16 -co "TILED=YES" -co "COMPRESS=ZSTD" -co "PREDICTOR=2" -co "BIGTIFF=IF_SAFER" -co "NUM_THREADS=ALL_CPUS" north_america_hydrographic_mask_merge_102004_250m_cubic.vrt north_america_hydrographic_mask_merge_102004_250m_cubic.tif
gdal_translate -ot Int16 -co "TILED=YES" -co "COMPRESS=ZSTD" -co "PREDICTOR=2" -co "BIGTIFF=IF_SAFER" -co "NUM_THREADS=ALL_CPUS" north_america_hydrographic_mask_merge_102004_250m.vrt north_america_hydrographic_mask_merge_102004_250m.tif
62 changes: 62 additions & 0 deletions hydrographic-masks/lake-locations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
{
"name": "west coast pacific",
"extent": "-125.288780633,-116.880174030,32.284590810,49.269976148 [EPSG:4326]",
"start": "-123.69, 38.21",
"elevation": "0.5"
},
{
"name": "northeast atlantic ME-NJ",
"extent": "-75.741066225,-65.524609202,36.961878233,45.454570902 [EPSG:4326]",
"start": "-69.9,42.62",
"elevation": "0.5"
},
{
"name": "south atlantic DE-FL",
"extent": "-81.8,-75.293373615,24.167165057,40.774163098 [EPSG:4326]",
"start": "-78.19, 32.95",
"elevation": "0.5"
},
{
"name": "south gulf coast FL-TX",
"extent": "-98.353977224,-81.368591886,24.083078991,31 [EPSG:4326]",
"start": "-83.81, 29.13",
"elevation": "0.5"
},
{
"name": "LA Bon secour bay",
"extent": "-89.571805556,-89.105694444,30.288750000,30.531250000 [EPSG:4326]",
"start": "-89.30523, 30.3411",
"elevation": "0.5"
},
{
"name": "FL Saint Johns River N",
"extent": "-81.758750000,-81.494027778,29.654861111,30.089305556 [EPSG:4326]",
"start": "-81.5568, 29.7321",
"elevation": "3.5"
},
{
"name": "FL Saint Johns River S",
"extent": "-81.725147988,-81.587560973,29.388604094,29.659191890 [EPSG:4326]",
"start": "-81.621, 29.6323",
"elevation": "5.5"
},
{
"name": "FL Indian River N",
"extent": "-80.886527778,-80.497638889,28.283472222,28.835972222 [EPSG:4326]",
"start": "-80.8053, 28.6989",
"elevation": "0.5"
},
{
"name": "FL Banana River N",
"extent": "-80.886527778,-80.497638889,28.283472222,28.835972222 [EPSG:4326]",
"start": "-80.5988, 28.5107",
"elevation": "0.5"
},
{
"name": "FL Banana River S",
"extent": "-80.886527778,-80.497638889,28.283472222,28.835972222 [EPSG:4326]",
"start": "-80.64126, 28.3807",
"elevation": "0.5"
}
]
27 changes: 27 additions & 0 deletions workflow/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import re
def get_trailing_number(s):
m = re.search(r'\d+$', s)
return int(m.group()) if m else None

GTIFF_creation_options = {
'TILES': 'YES',
'COMPRESS': 'ZSTD',
'PREDICTOR': '2',
'BIGTIFF': 'IF_SAFER',
'NUM_THREADS': 'ALL_CPUS'
}

GTIFF_write_options = {
'NUM_THREADS': 'ALL_CPUS'
#USE_OPENCL: 'TRUE'
}

configuration_keywords = {
'GDAL_VRT_ENABLE_PYTHON': 'YES'
}

GTIFF_creation_options_str = ' '.join([f'-co "{key}={value}"' for key, value in GTIFF_creation_options.items()])
GTIFF_write_options_str = ' '.join([f'-wo "{key}={value}"' for key, value in GTIFF_creation_options.items()])

# https://gdal.org/programs/raster_common_options.html#cmdoption-config
configuration_keywords_str = ' '.join([f'--config "{key} {value}"' for key, value in configuration_keywords.items()])
121 changes: 121 additions & 0 deletions workflow/hydrographicMask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import os
import json

from constants import *

def generateCoastlineDEMCommands(sourceDEMFilename):

lakeClippedMasksPath = 'coastline/'
lakeMaskFilename = 'coastline_hydrographic_mask_merge.vrt'
lakeMaskInt16Filename = os.path.splitext(lakeMaskFilename)[0] + "_int16" + ".vrt"

#load lakes data from lake-locations.json
input_file = open ('../hydrographic-masks/lake-locations.json')
lakes = json.load(input_file)

for location in lakes:

featureName = location['feature']
clippedDEMFilename = featureName.replace(' ', '-') + '.tif'

featureExtent = location['extent']
#re
extentMatches = re.findall('([-\d.]+),([-\d.]+),([-\d.]+),([-\d.]+) \[([A-Z:0-9]*)\]', featureExtent)
if len(extentMatches) == 5:
extents = extentMatches[0:4]
extentsCRS = extentMatches[4]
else:
print(f'# Could not parse feature extent for {featureName}. Got {len(extentMatches)}/5 matches.')
continue

featureStart = location['start']

featureElevation = location['elevation']

clipRasterCmd = ' '.join((
'gdalwarp',
f'-te {extents[0]} {extents[1]} {extents[2]} {extents[3]}',
f'-te_srs {extentsCRS}',
f'{GTIFF_write_options_str}',
'-r cubicspline',
'-multi',
'-of GTiff',
f'{GTIFF_creation_options_str}',
'-overwrite',
f'{sourceDEMFilename}',
f'{clippedDEMFilename}'
))

print(f'# Clip lake feature {featureName} - source DEM {sourceDEMFilename}{clippedDEMFilename}')
print(clipRasterCmd)
print(f'# Run grass.lake with:\nStart: {featureStart}\nElevation: {featureElevation}\nLake raster location: {lakeClippedMasksPath}{clippedDEMFilename}')
print()

print(f'# Merge all lake rasters in {lakeClippedMasksPath}* → {lakeMaskFilename}')
print(f'gdalbuildvrt -resolution highest -overwrite {lakeMaskFilename} {lakeClippedMasksPath}/*.tif')
print()

print(f'# Set merged lake VRT data type from Float32 to Int16 to save space - {lakeMaskFilename}{lakeMaskInt16Filename}')
print(f'gdal_translate -ot Int16 {lakeMaskFilename} {lakeMaskInt16Filename}')
print()

return lakeMaskInt16Filename

# all inputs must be WGS84 CRS
def generateHydrographicMaskFinalCommands(coastlinesFilename, streamsFilename, lakesFilename, targetSRS, modelResolution):

hydrographicMask_merge_filename = "north_america_hydrographic_mask_merge.vrt"

print(f'# Merge coastline, hydro1k, hydrolakes masks - {coastlinesFilename} {streamsFilename} {lakesFilename}{hydrographicMask_merge_filename}')
print(f'gdalbuildvrt -resolution highest -overwrite {hydrographicMask_merge_filename} {coastlinesFilename} {streamsFilename} {lakesFilename}')
print()

hydrographicMask_reproject_filename = os.path.splitext(hydrographicMask_merge_filename)[0] + "_102004" + ".vrt"
hydrographicMask_reproject_resampling_method = "near"

hydrographicMask_reproject_cmd = ' '.join((
'gdalwarp',
f'-t_srs {targetSRS}', #ESRI:102004
f'-r {hydrographicMask_reproject_resampling_method}',
'-multi',
'-overwrite',
f'{hydrographicMask_merge_filename}',
f'{hydrographicMask_reproject_filename}'
))

print(f'# Reproject hydrographic mask merge to {targetSRS} CRS - {hydrographicMask_merge_filename}{hydrographicMask_reproject_filename}')
print(hydrographicMask_reproject_cmd)
print()

hydrographicMask_reproject_250m_VRT_filename = os.path.splitext(hydrographicMask_reproject_filename)[0] + "_250m" + ".vrt"
hydrographicMask_reproject_250m_TIF_filename = os.path.splitext(hydrographicMask_reproject_250m_VRT_filename)[0] + ".tif"
hydrographicMask_reproject_250m_target_resolution = 250
hydrographicMask_reproject_250m_resampling_method = "max"

hydrographicMask_reproject_250m_buildVRT_cmd = ' '.join((
'gdalwarp',
f'-tr {hydrographicMask_reproject_250m_target_resolution} {hydrographicMask_reproject_250m_target_resolution}',
f'-r {hydrographicMask_reproject_250m_resampling_method}',
'-multi',
'-overwrite',
f'{hydrographicMask_reproject_filename}',
f'{hydrographicMask_reproject_250m_VRT_filename}'
))

hydrographicMask_reproject_250m_translate_cmd = ' '.join((
'gdal_translate',
f'-ot Int16',
f'{GTIFF_creation_options_str}',
f'{hydrographicMask_reproject_250m_VRT_filename}',
f'{hydrographicMask_reproject_250m_TIF_filename}'
))

print(f'# Downscale hydrographic mask reproject to {hydrographicMask_reproject_250m_target_resolution}x{hydrographicMask_reproject_250m_target_resolution}m resolution. Create VRT first for previewing. - {hydrographicMask_reproject_filename}{hydrographicMask_reproject_250m_VRT_filename}')
print(hydrographicMask_reproject_250m_buildVRT_cmd)
print()

print(f'# Create {hydrographicMask_reproject_250m_target_resolution}x{hydrographicMask_reproject_250m_target_resolution}m resolution TIF from VRT - {hydrographicMask_reproject_250m_VRT_filename}{hydrographicMask_reproject_250m_TIF_filename}')
print(hydrographicMask_reproject_250m_translate_cmd)
print()

return hydrographicMask_reproject_250m_TIF_filename
Loading

0 comments on commit 5ae06cf

Please sign in to comment.