Skip to content

Commit

Permalink
Unify DEM tile handling
Browse files Browse the repository at this point in the history
  • Loading branch information
breki committed May 16, 2024
1 parent 552ad57 commit 853f2eb
Show file tree
Hide file tree
Showing 42 changed files with 642 additions and 706 deletions.
2 changes: 1 addition & 1 deletion Demeton.Console/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
open CommandLine.Common
open Demeton.Commands
open Demeton.Console
open Demeton.Srtm.Funcs
open Demeton.Dem.Funcs
open System

let runImportCommand parsedParameters =
Expand Down
8 changes: 4 additions & 4 deletions Demeton.Console/Wiring.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
module Demeton.Console.Wiring

open Demeton.Dem.Types
open Demeton.Dem.Funcs
open Demeton.Srtm.Downsampling
open Demeton.Srtm.Funcs
open Demeton.Srtm.Png
open Demeton.Srtm.Fetch

let readPngTile: SrtmPngTileReader =
let readPngTile: DemPngTileReader =
decodeSrtmTileFromPngFile FileSys.openFileToRead

let determineTileStatus srtmDir localCacheDir =
Expand All @@ -22,7 +22,7 @@ let readZippedHgtFile = readZippedHgtFile FileSys.readZipFile

let convertPngTile = convertZippedHgtTileToPng readZippedHgtFile writePngTile

let fetchSrtmTile srtmDir localCacheDir : SrtmTileReader =
let fetchSrtmTile srtmDir localCacheDir : DemTileReader =
fun tile ->

let constructHigherLevelTile = constructHigherLevelTileHeightsArray 3600
Expand Down Expand Up @@ -51,4 +51,4 @@ let fetchSrtmTile srtmDir localCacheDir : SrtmTileReader =
|> finalizeFetchSrtmTileProcessing

let fetchSrtmHeights srtmDir localCacheDir =
fetchSrtmHeights (fetchSrtmTile srtmDir localCacheDir)
fetchDemHeights (fetchSrtmTile srtmDir localCacheDir)
14 changes: 7 additions & 7 deletions Demeton.Tests/Aw3d/AW3D experiments.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@


open System
open Demeton.Aw3d.Types
open Demeton.Commands
open Demeton.Dem.Types
open Demeton.Dem.Funcs
open Demeton.Projections.PROJParsing
open Demeton.Shaders
open FsUnit
Expand All @@ -16,10 +16,10 @@ open BitMiracle.LibTiff.Classic
open Swensen.Unquote


// todo 0: transform into an AW3D tile loading function
// todo 3: transform into an AW3D tile loading function
let readAw3dHeights
cacheDir
(tileId: Aw3dTileId)
(tileId: DemTileId)
(fileName: string)
: DemHeight[] =
use tiff = Tiff.Open(fileName, "r")
Expand Down Expand Up @@ -75,7 +75,7 @@ let readAw3dHeights
[<Fact>]
let ``Load AW3D into a DemHeight`` () =
let fileName = @"samples\ALPSMLC30_N046E007_DSM.tif"
let tileId = { TileX = 46; TileY = -7 }
let tileId = demTileXYId 46 -7
let demHeight = readAw3dHeights "cache" tileId fileName

test <@ demHeight <> null @>
Expand Down Expand Up @@ -159,11 +159,11 @@ let tileSize = 3600
let fetchAw3dHeightsArray _ =
let fileName = @"Samples\ALPSMLC30_N046E007_DSM.tif"

let tileId = { TileX = 46; TileY = -7 }
let tileId = demTileXYId 46 -7
let demHeight = readAw3dHeights "cache" tileId fileName

let tileId = Demeton.Srtm.Funcs.parseTileName "N46E007"
let cellMinX, cellMinY = Demeton.Srtm.Funcs.tileMinCell tileSize tileId
let tileId = parseTileName "N46E007"
let cellMinX, cellMinY = tileMinCell tileSize tileId

let cellMinX = cellMinX
let cellMinY = cellMinY
Expand Down
52 changes: 28 additions & 24 deletions Demeton.Tests/Aw3d/Loading AW3D tiles.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
open System.IO

open Demeton.Geometry.Common
open Demeton.Dem.Funcs
open Demeton.Aw3d.Types
open Demeton.Aw3d.Funcs

Expand All @@ -16,42 +17,42 @@ open TestHelp
[<Fact>]
let ``Correctly calculates the AW3D tiles needed for a given boundary`` () =
let bounds =
{ MinLon = 46.1
MinLat = -8.1
MaxLon = 47.9
MaxLat = -6.9 }
{ MinLon = 6.9
MinLat = 46.1
MaxLon = 8.1
MaxLat = 47.9 }

test
<@
boundsToAw3dTiles bounds |> Seq.toList = [ { TileX = 46; TileY = 6 }
{ TileX = 47; TileY = 6 }
{ TileX = 46; TileY = 7 }
{ TileX = 47; TileY = 7 }
{ TileX = 46; TileY = 8 }
{ TileX = 47; TileY = 8 } ]
@>
let expectedTiles =
[ demTileXYId 6 -47
demTileXYId 6 -48
demTileXYId 7 -47
demTileXYId 7 -48
demTileXYId 8 -47
demTileXYId 8 -48 ]
|> Set.ofList

test <@ (boundsToAw3dTiles bounds |> Set.ofSeq) = expectedTiles @>


[<Theory>]
[<InlineData(46,
6,
[<InlineData(6,
-46,
"https://www.eorc.jaxa.jp/ALOS/aw3d30/data/release_v2303/N045E005/N046E006.zip")>]
[<InlineData(-36,
-120,
[<InlineData(-120,
36,
"https://www.eorc.jaxa.jp/ALOS/aw3d30/data/release_v2303/S035W120/S036W120.zip")>]
let ``For a given AW3D tile, construct its download URL``
tileX
tileY
expectedUrl
=
test
<@ aw3dTileDownloadUrl { TileX = tileX; TileY = tileY } = expectedUrl @>
test <@ aw3dTileDownloadUrl (demTileXYId tileX tileY) = expectedUrl @>


[<Fact>]
let ``Do not download tile if TIFF already in cache`` () =
let cacheDir = "cache"
let sampleTileId = { TileX = 46; TileY = 6 }
let sampleTileId = demTileXYId 6 -46

let sampleCachedTifFileName =
aw3dTileCachedTifFileName cacheDir sampleTileId
Expand Down Expand Up @@ -88,7 +89,7 @@ let ``Do not download tile if TIFF already in cache`` () =
[<Fact>]
let ``Download tile ZIP file if TIFF not in cache`` () =
let cacheDir = "cache"
let sampleTileId = { TileX = 46; TileY = 6 }
let sampleTileId = demTileXYId 6 -46

let expectedCachedZipFileName =
Path.Combine(cacheDir, Aw3dDirName, $"N046E006.zip")
Expand Down Expand Up @@ -131,7 +132,7 @@ let ``Download tile ZIP file if TIFF not in cache`` () =
[<Fact>]
let ``Extract tile TIFF to the cache`` () =
let cacheDir = "cache"
let sampleTileId = { TileX = 46; TileY = 6 }
let sampleTileId = demTileXYId 6 -46

let expectedCachedTifFileName =
Path.Combine("cache", Aw3dDirName, "N046E006.tif")
Expand Down Expand Up @@ -175,7 +176,7 @@ let ``Extract tile TIFF to the cache`` () =
[<Fact>]
let ``Delete downloaded ZIP file after extraction`` () =
let cacheDir = "cache"
let sampleTileId = { TileX = 46; TileY = 6 }
let sampleTileId = demTileXYId 6 -46

let expectedCachedTifFileName =
Path.Combine("cache", Aw3dDirName, "N046E006.tif")
Expand All @@ -198,7 +199,10 @@ let ``Delete downloaded ZIP file after extraction`` () =
zipFileDeleted := true
Ok fileName
else
fail "Unexpected ZIP file name"
fail (
"Unexpected ZIP file name, "
+ $"expected %s{expectedCachedZipFileName}, got %s{fileName}"
)

let x = zipFileDeleted

Expand Down
6 changes: 3 additions & 3 deletions Demeton.Tests/Commands tests/ImportSrtmTilesCommand tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

open CommandLine.Common
open Demeton.Commands
open Demeton.Srtm.Funcs
open Demeton.Srtm.Fetch
open Demeton.Dem.Types
open Demeton.Dem.Funcs
open Demeton.Srtm.Fetch

open Xunit
open Swensen.Unquote
Expand Down Expand Up @@ -168,7 +168,7 @@ let ``Parses the local cache dir parameter`` () =

[<Fact>]
let ``Imports all tiles within the specified boundaries`` () =
let tiles = [| srtmTileId 0 15 45; srtmTileId 0 16 46 |]
let tiles = [| demTileId 0 15 45; demTileId 0 16 46 |]

// we use a lock because the import function employs parallelization
let threadsLock = "some lock"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Tests.``Commands tests``.ShadeCommand.``Generating shaded tile``

open Demeton.Dem.Types
open Demeton.Dem.Funcs
open Raster
open Demeton.Commands
open Demeton.Shaders
open Demeton.Srtm.Funcs

open Xunit
open Swensen.Unquote
Expand Down Expand Up @@ -34,10 +34,10 @@ let ``Tile generator correctly calculates which SRTM tiles it needs`` () =

test
<@
tilesArray = [| srtmTileId 4 0 -4
srtmTileId 4 1 -4
srtmTileId 4 0 -3
srtmTileId 4 1 -3 |]
tilesArray = [| demTileId 4 0 -4
demTileId 4 1 -4
demTileId 4 0 -3
demTileId 4 1 -3 |]
@>

heights |> Some |> Ok
Expand Down
40 changes: 19 additions & 21 deletions Demeton.Tests/Png/IDAT compression.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Demeton.Tests.``IDAT compression``

open Png.Chunks
open Demeton.Dem.Funcs
open Demeton.Srtm.Funcs
open Demeton.Srtm.Png

Expand All @@ -17,8 +18,9 @@ open Xunit
[<Property>]
[<Trait("Category", "integration")>]
let ``Inflating a deflated data returns the original data``
(originalData: byte[]) =

(originalData: byte[])
=

use compressedOutputStream = new MemoryStream()
compress originalData compressedOutputStream
let compressedData = compressedOutputStream.ToArray()
Expand All @@ -28,7 +30,7 @@ let ``Inflating a deflated data returns the original data``

let decompressedData = decompressedOutputStream.ToArray()
decompressedData = originalData


/// <summary>
/// Calculates the compression rate of the image data encoded from the sample
Expand All @@ -37,7 +39,7 @@ let ``Inflating a deflated data returns the original data``
/// </summary>
[<Fact>]
[<Trait("Category", "slow")>]
let ``Determining the compression rate``() =
let ``Determining the compression rate`` () =
let srtmTileId = "N46E015"
let hgtFileNameOnly = srtmTileId + ".hgt"
let tileId = parseTileName hgtFileNameOnly.[0..6]
Expand All @@ -48,26 +50,22 @@ let ``Determining the compression rate``() =
clock.Start()

printfn ("Reading the heights array...")

let heightsArray = createSrtmTileFromStream 3600 tileId hgtStream

printfn
"%d Encoding heights into a raw image data..." clock.ElapsedMilliseconds
let imageData =
heightsArrayToImageData demHeightToUInt16Value heightsArray
printfn
"%d Encoding heights into a raw image data..."
clock.ElapsedMilliseconds

let imageData = heightsArrayToImageData demHeightToUInt16Value heightsArray

printfn
"%d Filtering and compressing the image data into PNG IDAT chunk..."
printfn
"%d Filtering and compressing the image data into PNG IDAT chunk..."
clock.ElapsedMilliseconds
let compressed =
serializeIdatChunkData
3600
3600
16
imageData

printfn
"%d Compression rate is %d%%..."

let compressed = serializeIdatChunkData 3600 3600 16 imageData

printfn
"%d Compression rate is %d%%..."
clock.ElapsedMilliseconds
(compressed.Length * 100 / (3600 * 3600 * 2))

2 changes: 1 addition & 1 deletion Demeton.Tests/Shaders/ShadingSampleGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ open Demeton.Projections.Common
open Demeton.Projections.Factory
open Demeton.Projections.PROJParsing
open Demeton.Projections.MinLonLatDelta
open Demeton.Srtm.Funcs
open Demeton.Dem.Funcs
open Xunit
open Swensen.Unquote
open TestHelp
Expand Down
Loading

0 comments on commit 853f2eb

Please sign in to comment.