Add map area selection extraction
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { datasetsApi } from '../services/api'
|
||||
import { formatError } from '../lib/formatError'
|
||||
import type { DatasetCreateResponse, VectorSelectionBBox, VectorSelectionResponse } from '../types'
|
||||
|
||||
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)
|
||||
|
||||
useEffect(() => {
|
||||
setMapSelectionBbox(null)
|
||||
setMapSelectionResult(null)
|
||||
setMapSelectionError(null)
|
||||
}, [selectedProjectId, selectedDataset?.id])
|
||||
|
||||
const runMapSelectionExtract = async (bbox: VectorSelectionBBox) => {
|
||||
if (!selectedProjectId || !selectedDataset) {
|
||||
setMapSelectionError('Open a vector dataset before extracting a map area.')
|
||||
return
|
||||
}
|
||||
if (!isVectorDatasetType(selectedDataset.dataset_type)) {
|
||||
setMapSelectionError('Area extraction requires an active vector dataset.')
|
||||
return
|
||||
}
|
||||
|
||||
setMapSelectionLoading(true)
|
||||
setMapSelectionError(null)
|
||||
setMapSelectionBbox(bbox)
|
||||
try {
|
||||
const response = await datasetsApi.selectVectorFeatures(selectedProjectId, selectedDataset.id, {
|
||||
bbox: { ...bbox, crs: 'EPSG:4326' },
|
||||
limit: 250,
|
||||
})
|
||||
setMapSelectionResult(response)
|
||||
} catch (error) {
|
||||
setMapSelectionResult(null)
|
||||
setMapSelectionError(formatError(error, 'Area extraction failed'))
|
||||
} finally {
|
||||
setMapSelectionLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const resetMapSelectionExtract = () => {
|
||||
setMapSelectionBbox(null)
|
||||
setMapSelectionResult(null)
|
||||
setMapSelectionError(null)
|
||||
}
|
||||
|
||||
return {
|
||||
mapSelectionBbox,
|
||||
mapSelectionResult,
|
||||
mapSelectionLoading,
|
||||
mapSelectionError,
|
||||
runMapSelectionExtract,
|
||||
resetMapSelectionExtract,
|
||||
setMapSelectionBbox,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user