Skip to content

Commit

Permalink
cleanup vessels layer
Browse files Browse the repository at this point in the history
  • Loading branch information
j8seangel committed Apr 19, 2024
1 parent 2a39f00 commit 0915283
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 100 deletions.
25 changes: 19 additions & 6 deletions libs/deck-layers/src/layers/vessel/VesselLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type VesselLayerProps = BaseLayerProps &
VesselEventsLayerProps &
_VesselLayerProps

let warnLogged = false
export class VesselLayer extends CompositeLayer<VesselLayerProps & LayerProps> {
dataStatus: VesselDataStatus[] = []

Expand Down Expand Up @@ -68,6 +69,19 @@ export class VesselLayer extends CompositeLayer<VesselLayerProps & LayerProps> {
this.setState({ error })
}

_getTracksUrl({ start, end, trackUrl }: { start: string; end: string; trackUrl: string }) {
const trackUrlObject = new URL(trackUrl)
trackUrlObject.searchParams.append('start-date', start)
trackUrlObject.searchParams.append('end-date', end)
const format = trackUrlObject.searchParams.get('format') || 'DECKGL'
if (format !== 'DECKGL' && !warnLogged) {
console.warn(`only DECKGL format is supported, the current format (${format}) was replaced`)
warnLogged = true
}
trackUrlObject.searchParams.set('format', 'DECKGL')
return trackUrlObject.toString()
}

_getVesselTrackLayers() {
const { trackUrl, visible, startTime, endTime, color, highlightStartTime, highlightEndTime } =
this.props
Expand All @@ -76,17 +90,16 @@ export class VesselLayer extends CompositeLayer<VesselLayerProps & LayerProps> {
return []
}
const chunks = getVesselResourceChunks(startTime, endTime)
return chunks.map(({ start, end }) => {
return chunks.flatMap(({ start, end }) => {
if (!start || !end) {
return []
}
const chunkId = `${TRACK_LAYER_TYPE}-${start}-${end}`
const trackUrlObject = new URL(trackUrl as string)
trackUrlObject.searchParams.append('start-date', start as string)
trackUrlObject.searchParams.append('end-date', end as string)
trackUrlObject.searchParams.set('format', 'DECKGL')
return new VesselTrackLayer<any, { type: VesselDataType }>(
this.getSubLayerProps({
id: chunkId,
visible,
data: trackUrlObject.toString(),
data: this._getTracksUrl({ start, end, trackUrl }),
type: TRACK_LAYER_TYPE,
loaders: [VesselTrackLoader],
_pathType: 'open',
Expand Down
94 changes: 0 additions & 94 deletions libs/deck-loaders/src/vessels/lib/parse-tracks.ts
Original file line number Diff line number Diff line change
@@ -1,102 +1,8 @@
import { TrackField } from '@globalfishingwatch/api-types'
import { VesselTrackData } from './types'
import { DeckTrack } from './vessel-track-proto'

export const DEFAULT_NULL_VALUE = -Math.pow(2, 31)

const transformerByField: Partial<Record<TrackField, (number: number) => number>> = {
latitude: (value: number) => value / Math.pow(10, 6),
longitude: (value: number) => value / Math.pow(10, 6),
timestamp: (value: number) => value * Math.pow(10, 3),
}

type Point = Record<string, number | null>
export const trackValueArrayToSegments = (valueArray: number[], fields_: TrackField[]) => {
if (!fields_.length) {
throw new Error()
}

const fields = [...fields_]
if (fields.includes(TrackField.lonlat)) {
const llIndex = fields.indexOf('lonlat' as TrackField)
fields.splice(llIndex, 1, TrackField.longitude, TrackField.latitude)
}
const numFields = fields.length

let numSegments: number
const segmentIndices = [] as number[]
const segments = [] as Point[][]

let nullValue = DEFAULT_NULL_VALUE
let currentSegment = [] as Point[]
let currentPoint = {} as Point
let pointsFieldIndex = 0
let currentPointFieldIndex = 0
let currentSegmentIndex = 0
let currentSegPointIndex = 0
if (valueArray && valueArray?.length) {
valueArray.forEach((value, index) => {
if (index === 0) {
nullValue = value
} else if (index === 1) {
numSegments = value
} else if (index < 2 + numSegments) {
segmentIndices.push(value)
} else {
// a segment starts
if (segmentIndices.includes(pointsFieldIndex)) {
// close previous segment, only if needed (it's not the first segment)
if (currentSegmentIndex !== 0) {
currentSegment.push(currentPoint)
segments.push(currentSegment)
}
currentSegPointIndex = 0
currentSegmentIndex++
// create new segment
currentSegment = []
currentPoint = {}
}

// get what is the current field for current point
currentPointFieldIndex = pointsFieldIndex % numFields

// a point starts
if (currentPointFieldIndex === 0) {
// close previous point, only if needed (it's not the first point of this seg)
if (currentSegPointIndex !== 0) {
currentSegment.push(currentPoint)
}
currentSegPointIndex++
// create new point
currentPoint = {}
}

const field = fields[currentPointFieldIndex]
const transformer = transformerByField[field]

if (value === nullValue || transformer === undefined) {
currentPoint[field] = null
} else {
currentPoint[field] = transformer(value)
}

pointsFieldIndex++
}
})
}

// close last open point and open segment
if (Object.keys(currentPoint).length) {
currentSegment.push(currentPoint)
}

if (currentSegment.length) {
segments.push(currentSegment)
}

return segments
}

export const parseTrack = (arrayBuffer: ArrayBuffer): VesselTrackData => {
const track = DeckTrack.decode(new Uint8Array(arrayBuffer)) as any
return {
Expand Down

0 comments on commit 0915283

Please sign in to comment.