115 lines
4.4 KiB
TypeScript
115 lines
4.4 KiB
TypeScript
import { useEffect, useRef, useState } from 'react'
|
|
import { datasetsApi } from '../services/api'
|
|
import { formatError } from '../lib/formatError'
|
|
import type { DatasetCreateResponse, VectorSelectionBBox, VectorSelectionResponse } from '../types'
|
|
import { terrainSelectionToMapSelection } from '../lib/terrainSelection'
|
|
import { floodHazardSelectionToMapSelection } from '../lib/floodHazardSelection'
|
|
import { thematicRasterSelectionToMapSelection } from '../lib/thematicRaster'
|
|
|
|
interface MapSelectionExtractOptions {
|
|
selectedProjectId: string | null
|
|
selectedDataset: DatasetCreateResponse | null
|
|
isVectorDatasetType: (datasetType: string) => boolean
|
|
}
|
|
|
|
export function useMapSelectionExtract({
|
|
selectedProjectId,
|
|
selectedDataset,
|
|
isVectorDatasetType,
|
|
}: MapSelectionExtractOptions) {
|
|
const [mapSelectionBbox, setMapSelectionBbox] = useState<VectorSelectionBBox | null>(null)
|
|
const [mapSelectionResult, setMapSelectionResult] = useState<VectorSelectionResponse | null>(null)
|
|
const [mapSelectionLoading, setMapSelectionLoading] = useState(false)
|
|
const [mapSelectionError, setMapSelectionError] = useState<string | null>(null)
|
|
const requestSequence = useRef(0)
|
|
|
|
useEffect(() => {
|
|
requestSequence.current += 1
|
|
setMapSelectionBbox(null)
|
|
setMapSelectionLoading(false)
|
|
}, [selectedProjectId])
|
|
|
|
useEffect(() => {
|
|
requestSequence.current += 1
|
|
setMapSelectionResult(null)
|
|
setMapSelectionError(null)
|
|
setMapSelectionLoading(false)
|
|
}, [selectedProjectId, selectedDataset?.id])
|
|
|
|
const runMapSelectionExtract = async (bbox: VectorSelectionBBox, areaId?: string) => {
|
|
if (!selectedProjectId || !selectedDataset) {
|
|
setMapSelectionError('Open a vector dataset before extracting a map area.')
|
|
return null
|
|
}
|
|
const terrainDataset = selectedDataset.dataset_type === 'raster' && selectedDataset.source_name === 'digitaal_vlaanderen_dhmv'
|
|
const floodHazardDataset = selectedDataset.dataset_type === 'raster' && selectedDataset.source_name === 'vmm_flood_hazard'
|
|
const thematicRasterDataset = selectedDataset.dataset_type === 'raster' && selectedDataset.source_name === 'department_omgeving_thematic_raster'
|
|
if (!isVectorDatasetType(selectedDataset.dataset_type) && !terrainDataset && !floodHazardDataset && !thematicRasterDataset) {
|
|
setMapSelectionError('Gebiedsanalyse ondersteunt een vectorlaag of een beheerd thematisch raster.')
|
|
return null
|
|
}
|
|
|
|
const sequence = requestSequence.current + 1
|
|
requestSequence.current = sequence
|
|
setMapSelectionLoading(true)
|
|
setMapSelectionError(null)
|
|
setMapSelectionBbox(bbox)
|
|
try {
|
|
const response = terrainDataset
|
|
? terrainSelectionToMapSelection(await datasetsApi.selectTerrain(selectedProjectId, selectedDataset.id, {
|
|
bbox: { ...bbox, crs: 'EPSG:4326' },
|
|
area_id: areaId,
|
|
}))
|
|
: floodHazardDataset
|
|
? floodHazardSelectionToMapSelection(await datasetsApi.selectFloodHazard(selectedProjectId, selectedDataset.id, {
|
|
bbox: { ...bbox, crs: 'EPSG:4326' },
|
|
area_id: areaId,
|
|
}))
|
|
: thematicRasterDataset
|
|
? thematicRasterSelectionToMapSelection(await datasetsApi.selectThematicRaster(selectedProjectId, selectedDataset.id, {
|
|
bbox: { ...bbox, crs: 'EPSG:4326' },
|
|
area_id: areaId,
|
|
}))
|
|
: await datasetsApi.selectVectorFeatures(selectedProjectId, selectedDataset.id, {
|
|
bbox: { ...bbox, crs: 'EPSG:4326' },
|
|
area_id: areaId,
|
|
limit: 1000,
|
|
})
|
|
if (requestSequence.current !== sequence) {
|
|
return null
|
|
}
|
|
setMapSelectionResult(response)
|
|
return response
|
|
} catch (error) {
|
|
if (requestSequence.current !== sequence) {
|
|
return null
|
|
}
|
|
setMapSelectionResult(null)
|
|
setMapSelectionError(formatError(error, 'Area extraction failed'))
|
|
return null
|
|
} finally {
|
|
if (requestSequence.current === sequence) {
|
|
setMapSelectionLoading(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
const resetMapSelectionExtract = () => {
|
|
requestSequence.current += 1
|
|
setMapSelectionBbox(null)
|
|
setMapSelectionResult(null)
|
|
setMapSelectionError(null)
|
|
setMapSelectionLoading(false)
|
|
}
|
|
|
|
return {
|
|
mapSelectionBbox,
|
|
mapSelectionResult,
|
|
mapSelectionLoading,
|
|
mapSelectionError,
|
|
runMapSelectionExtract,
|
|
resetMapSelectionExtract,
|
|
setMapSelectionBbox,
|
|
}
|
|
}
|