Skip to content

Commit

Permalink
dem-with-water-bodies now checks whether the HGT file already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
breki committed Aug 8, 2024
1 parent 9c7fee9 commit 6951688
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 67 deletions.
10 changes: 5 additions & 5 deletions Demeton.Tests/Dem/Reading HGT files.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let ``Can read SRTM heights`` () =
1uy |]
)

let heights = Hgt.readFromStream 2 stream
let heights = Hgt.readHeightsFromStream 2 stream

heights |> should equal [| 2560s; 0s; 2561s; 1s |]

Expand All @@ -45,7 +45,7 @@ let ``Can read null SRTM heights`` () =
use stream =
new MemoryStream([| 0x80uy; 0uy; 10uy; 0uy; 10uy; 0uy; 0uy; 0uy |])

let heights = Hgt.readFromStream 1 stream
let heights = Hgt.readHeightsFromStream 1 stream

heights |> should equal [| DemHeightNone |]

Expand All @@ -56,7 +56,7 @@ let ``Can handle negative SRTM heights`` () =
[| 255uy; 0b10011100uy; 10uy; 0uy; 10uy; 0uy; 0uy; 0uy |]
)

let heights = Hgt.readFromStream 1 stream
let heights = Hgt.readHeightsFromStream 1 stream

heights |> should equal [| -100s |]

Expand Down Expand Up @@ -98,7 +98,7 @@ let ``Can create heights array from SRTM heights sequence`` () =

use stream = new MemoryStream(byteArray)

let tile = Hgt.createDemTileFromStream tileSize (demTileId 0 16 45) stream
let tile = Hgt.readHeightsArrayFromStream tileSize (demTileId 0 16 45) stream

test <@ tile.Width = tileSize @>
test <@ tile.Height = tileSize @>
Expand All @@ -117,6 +117,6 @@ let ``Can read HGT file`` () =

test <@ stream <> null @>

let heights = Hgt.createDemTileFromStream 3600 tileId stream
let heights = Hgt.readHeightsArrayFromStream 3600 tileId stream
test <@ heights.Width = 3600 @>
test <@ heights.Height = 3600 @>
2 changes: 1 addition & 1 deletion Demeton.Tests/Png/IDAT compression.fs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ let ``Determining the compression rate`` () =

printfn "Reading the heights array..."

let heightsArray = Hgt.createDemTileFromStream 3600 tileId hgtStream
let heightsArray = Hgt.readHeightsArrayFromStream 3600 tileId hgtStream

printfn
"%d Encoding heights into a raw image data..."
Expand Down
2 changes: 1 addition & 1 deletion Demeton.Tests/Srtm/Converting HGT to PNG.fs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ let ``Can convert a HGT file into PNG image`` () =

printfn "Reading the heights array..."

let heightsArray = Hgt.createDemTileFromStream 3600 tileId hgtStream
let heightsArray = Hgt.readHeightsArrayFromStream 3600 tileId hgtStream

printfn "%d Encoding heights into the PNG..." clock.ElapsedMilliseconds

Expand Down
126 changes: 70 additions & 56 deletions Demeton/Commands/DemWithWaterBodiesCommand.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open Raster
type Options =
{ TileId: DemTileId
HgtSize: int
LocalCacheDir: string}
LocalCacheDir: string }

[<Literal>]
let TileIdParameter = "tile-id"
Expand Down Expand Up @@ -219,63 +219,77 @@ let extendHeightsArrayWithAdditionalRowAndColumn (heightsArray: HeightsArray) =
HeightsArrayDirectImport extendedCells
)

// todo 3: if the HGT already exists, just read it

let ensureHgtFile cacheDir hgtSize tileId : HeightsArray option =
fetchAw3dTile tileId cacheDir
|> Result.map (fun aw3dTile ->
let availableWorldCoverTiles =
ensureGeoJsonFile cacheDir FileSys.fileExists FileSys.downloadFile
|> listAllAvailableFiles FileSys.openFileToRead
|> Set.ofSeq

let downsampledAw3dTile = aw3dTile |> downsampleAw3dTile hgtSize

loadWaterBodiesTileFromCache cacheDir availableWorldCoverTiles tileId
|> makeNoneFileIfNeeded cacheDir
|> extractWaterBodiesTileFromWorldCoverTileIfNeeded cacheDir
|> function
| CachedTileLoaded tileHeightsArray ->
// todo 20: simplify water bodies

tileHeightsArray
|> Option.map (fun tileHeightsArray ->
let downsampledWaterBodiesTile =
tileHeightsArray
|> downsampleWaterBodiesHeightsArray hgtSize

let demWaterBodiesTile =
downsampledAw3dTile
|> encodeWaterBodiesInfoIntoDem
downsampledWaterBodiesTile
// extend the heights array with one additional
// row and column so it is compatible with the original
// SRTM HGT files
|> extendHeightsArrayWithAdditionalRowAndColumn

demWaterBodiesTile
|> Hgt.writeHeightsArrayToFile (
Path.Combine
(cacheDir,
"AW3D-WaterBodies",
hgtSize.ToString(),
toTileName tileId + ".hgt"))
|> ignore

demWaterBodiesTile
)
| TileNeedsToBeDownloaded _ ->
invalidOp "Bug: this should never happen"
| TileDoesNotExistInWorldCover _ ->
NotImplementedException(
"The case when there is no WorldCover tile"
)
|> raise
let hgtFileName =
Path.Combine(
cacheDir,
"AW3D-WaterBodies",
hgtSize.ToString(),
toTileName tileId + ".hgt"
)
|> function
| Ok heightsArray -> heightsArray
| Error errorMessage ->
invalidOp errorMessage

if FileSys.fileExists hgtFileName then
Log.info "The HGT file already exists at '%s'." hgtFileName

FileSys.openFileToRead hgtFileName
|> function
| Ok stream ->
use stream = stream
Some(Hgt.readHeightsArrayFromStream hgtSize tileId stream)
| Error error -> raise error.Exception
else
fetchAw3dTile tileId cacheDir
|> Result.map (fun aw3dTile ->
let availableWorldCoverTiles =
ensureGeoJsonFile
cacheDir
FileSys.fileExists
FileSys.downloadFile
|> listAllAvailableFiles FileSys.openFileToRead
|> Set.ofSeq

let downsampledAw3dTile = aw3dTile |> downsampleAw3dTile hgtSize

loadWaterBodiesTileFromCache
cacheDir
availableWorldCoverTiles
tileId
|> makeNoneFileIfNeeded cacheDir
|> extractWaterBodiesTileFromWorldCoverTileIfNeeded cacheDir
|> function
| CachedTileLoaded tileHeightsArray ->
// todo 20: simplify water bodies

tileHeightsArray
|> Option.map (fun tileHeightsArray ->
let downsampledWaterBodiesTile =
tileHeightsArray
|> downsampleWaterBodiesHeightsArray hgtSize

let demWaterBodiesTile =
downsampledAw3dTile
|> encodeWaterBodiesInfoIntoDem
downsampledWaterBodiesTile
// extend the heights array with one additional
// row and column so it is compatible with the original
// SRTM HGT files
|> extendHeightsArrayWithAdditionalRowAndColumn

demWaterBodiesTile
|> Hgt.writeHeightsArrayToFile hgtFileName
|> ignore

demWaterBodiesTile)
| TileNeedsToBeDownloaded _ ->
invalidOp "Bug: this should never happen"
| TileDoesNotExistInWorldCover _ ->
NotImplementedException(
"The case when there is no WorldCover tile"
)
|> raise)
|> function
| Ok heightsArray -> heightsArray
| Error errorMessage -> invalidOp errorMessage


let run (options: Options) : Result<unit, string> =
Expand Down
6 changes: 3 additions & 3 deletions Demeton/Dem/Hgt.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ open FileSys
/// <summary>
/// Reads HeightsArray data from a HGT-encoded stream.
/// </summary>
let readFromStream tileSize (stream: Stream) : DemHeight[] =
let readHeightsFromStream tileSize (stream: Stream) : DemHeight[] =
let inline readNextHeightFromStream (streamReader: FunctionalStreamReader) =
let firstByte = streamReader.currentByte ()

Expand Down Expand Up @@ -69,8 +69,8 @@ let readFromStream tileSize (stream: Stream) : DemHeight[] =
/// Reads the <see cref="HeightsArray"/> of a SRTM tile from
/// a HGT file stream.
/// </summary>
let createDemTileFromStream tileSize tileId stream =
let srtmHeights = readFromStream tileSize stream
let readHeightsArrayFromStream tileSize tileId stream =
let srtmHeights = readHeightsFromStream tileSize stream

let cellMinX, cellMinY = tileMinCell tileSize tileId

Expand Down
2 changes: 1 addition & 1 deletion Demeton/Srtm/Png.fs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ let convertZippedHgtTileToPng

memoryStream.Seek(0L, SeekOrigin.Begin) |> ignore

Hgt.createDemTileFromStream SrtmTileSize tileId memoryStream |> Ok
Hgt.readHeightsArrayFromStream SrtmTileSize tileId memoryStream |> Ok

match
readHgtFileFromStream tileId zippedHgtFileName readHgtFileStream
Expand Down

0 comments on commit 6951688

Please sign in to comment.