Files
map-asset-gateway/demo/demo_import_and_slice.ps1
T
Harmon 73ad03fa59 Rewrite the gateway around health, retries, slicing and a Windows service
Scanner
- manifest lookup is now depth-bounded and prunes numeric zoom directories, so
  a scan no longer walks the whole tile payload
- rescans only report a version as updated when something actually changed
- overlapping runs are refused instead of interleaving their bookkeeping

Hot path
- token authorisation loads only that token grants and throttles the
  last_used_at write instead of touching it on every tile
- SQLite runs in WAL with a busy timeout and a real connection pool
- the catalog lookup used by /tiles no longer loads every basemap

Security
- the admin surface requires ADMIN_TOKEN (bearer) or ADMIN_USER/ADMIN_PASSWORD
  (basic), and the service refuses to bind off-host without credentials
- CORS is an explicit allow-list; newly minted tokens never reach a redirect URL
- client tokens are redacted from the access log

Operations
- resource health inspection flags a moved or deleted tile root before a 404
- pushes retry with backoff and can be re-queued by hand; the schedule lives in
  the database so a restart does not lose it
- access log plus /metrics counters

Image slicing
- tile_jobs drive the GDAL bundled with QGIS via scripts/gdal2tiles-qgis.bat
- layer_sources remembers what a layer is made of: submit one source to append
  a flight, or all of them to rebuild the layer as a VRT mosaic with no tile
  seams; the console exposes both, including a one-click rebuild
- slicing is available to integrations through a narrow slice token scope
  (submit, poll, idempotent by code, completion callback)

Console
- rebuilt around a fixed rail and single-view navigation with health and
  outstanding work leading, instead of one long scrolling page

Windows service
- native install/start/stop/status with crash recovery, auto-start and a
  rotating log file, so no NSSM wrapper is needed
- start-all.bat and stop-all.bat are removed: they killed the process by port
  with taskkill, which fought the service restart policy

Verified with 49 tests and live runs against real tile data and real GDAL.
2026-10-16 10:35:30 +08:00

191 lines
7.0 KiB
PowerShell

param(
[string]$ProjectCode = "default",
[string]$ResultCode = ("demo_result_" + (Get-Date -Format "yyyyMMdd_HHmmss")),
[string]$ResultType = "ndvi",
[string]$InputPath = "",
[string]$SourceSystemCode = "",
[string]$RequestedBy = "demo-user",
[string]$StyleCode = "demo_continuous_style",
[string]$StyleName = "Demo Continuous Style",
[int]$ZoomMin = 0,
[int]$ZoomMax = 3,
[bool]$WaitForJobs = $true,
[switch]$StartStack,
[switch]$KeepStack,
[int]$ApiPort = 28910,
[int]$TilePort = 28900
)
. (Join-Path $PSScriptRoot "common.ps1")
$stack = $null
$run = New-RsRunContext -Prefix "import_slice"
try {
Import-RsDemoEnvironment
if ($StartStack) {
$stack = Start-RsDemoStack -ApiPort $ApiPort -TilePort $TilePort -IncludeWorker -RunID $run.RunID
}
if (-not $InputPath) {
$InputPath = Join-Path $run.OutputDirectory ($ResultCode + ".tif")
& (Get-RsPythonExe) (Join-Path $PSScriptRoot "create_sample_raster.py") `
--output $InputPath `
--preset "ndvi" | Out-Null
if ($LASTEXITCODE -ne 0) {
throw "Failed to create sample raster"
}
}
$inspection = Invoke-RsApi -Method "POST" -Path "/api/v1/admin/raster-inspections" -Body @{
path = [System.IO.Path]::GetFullPath($InputPath)
}
$styleVersion = Ensure-RsStyleVersion -ProjectCode $ProjectCode -StyleCode $StyleCode -StyleName $StyleName
$assetVersionBody = @{
format = $inspection.format
storage_uri = $inspection.storage_uri
mime_type = $inspection.mime_type
size_bytes = $inspection.size_bytes
crs = $inspection.crs
width = $inspection.width
height = $inspection.height
band_count = $inspection.band_count
dtype = $inspection.dtype
bbox = $inspection.bbox
metadata = $inspection.metadata
}
if ($inspection.PSObject.Properties.Name -contains "nodata_value" -and $null -ne $inspection.nodata_value) {
$assetVersionBody.nodata_value = $inspection.nodata_value
}
if ($inspection.PSObject.Properties.Name -contains "resolution_x" -and $null -ne $inspection.resolution_x) {
$assetVersionBody.resolution_x = $inspection.resolution_x
}
if ($inspection.PSObject.Properties.Name -contains "resolution_y" -and $null -ne $inspection.resolution_y) {
$assetVersionBody.resolution_y = $inspection.resolution_y
}
$project = Encode-RsURIComponent $ProjectCode
$importBody = @{
requested_by = $RequestedBy
asset = @{
source_ref = $inspection.storage_uri
asset_type = "raster"
display_name = $ResultCode
description = "Imported by demo_import_and_slice.ps1"
metadata = @{
demo_run = $run.RunID
}
}
asset_version = $assetVersionBody
product = @{
code = $ResultCode
type = $ResultType
name = $ResultCode
description = "Demo managed result"
}
product_version = @{
value_type = "continuous"
unit = "index"
bbox = $inspection.bbox
status = "ready"
}
style_binding = @{
style_version_id = $styleVersion.id
is_default = $true
availability = "enabled"
}
}
if ($SourceSystemCode) {
$importBody.source_system_code = $SourceSystemCode
}
$import = Invoke-RsApi -Method "POST" -Path "/api/v1/projects/$project/imports/results" -Body $importBody
$slice = Invoke-RsApi -Method "POST" -Path ("/api/v1/results/{0}/slice" -f $import.product.product.id) -Body @{
requested_by = $RequestedBy
build_cog = $true
generate_preview = $true
generate_legend = $true
generate_tiles = $true
build_cog_input = @{
compression = "deflate"
block_size = 256
force_rebuild = $true
}
preview_input = @{
style_version_id = $styleVersion.id
width = 512
height = 512
format = "png"
force_rebuild = $true
}
legend_input = @{
style_version_id = $styleVersion.id
width = 320
height = 80
format = "png"
force_rebuild = $true
}
tiles_input = @{
style_version_id = $styleVersion.id
zoom_min = $ZoomMin
zoom_max = $ZoomMax
tile_size = 256
tile_format = "webp"
force_rebuild = $true
}
}
$jobStates = @()
if ($WaitForJobs) {
foreach ($job in @($slice.jobs)) {
$jobStates += Wait-RsJob -JobID $job.id
}
}
$result = Invoke-RsApi -Method "GET" -Path ("/api/v1/results/{0}" -f $import.product.product.id)
$delivery = $null
if ($WaitForJobs) {
$delivery = Invoke-RsApi -Method "GET" -Path ("/api/v1/projects/{0}/products/{1}/delivery?version=latest&style_version_id={2}" -f `
(Encode-RsURIComponent $ProjectCode), `
(Encode-RsURIComponent $ResultCode), `
(Encode-RsURIComponent $styleVersion.id))
}
[ordered]@{
run_id = $run.RunID
api_base_url = Get-RsApiBaseUrl
tile_base_url = Get-RsTileBaseUrl
input_path = [System.IO.Path]::GetFullPath($InputPath)
result_id = $import.product.product.id
result_code = $ResultCode
product_version_id = $import.product_version.id
style_version_id = $styleVersion.id
imported_asset_id = $import.asset.id
imported_version_id = $import.asset_version.id
slice_job_ids = @($slice.jobs | ForEach-Object { $_.id })
slice_job_statuses = @($jobStates | ForEach-Object {
[ordered]@{
id = $_.id
job_type = $_.job_type
status = $_.status
}
})
cog_url = if ($delivery) { $delivery.downloads.cog_url } else { $null }
preview_url = if ($delivery -and $delivery.preview) { $delivery.preview.url } else { $null }
legend_url = if ($delivery -and $delivery.legend) { $delivery.legend.url } else { $null }
tileset_url = if ($delivery -and $delivery.tileset) { $delivery.tileset.url_template } else { $null }
result_status = $result.status
run_directory = $run.RunDirectory
log_directory = if ($stack) { $stack.LogDirectory } else { $null }
} | ConvertTo-Json -Depth 16
}
finally {
if ($stack -and -not $KeepStack) {
Stop-RsBackgroundProcesses -Processes $stack.Processes
}
}