File size: 9,722 Bytes
0914710
 
 
9bca4cd
0914710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bca4cd
0914710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bca4cd
 
 
 
 
 
 
 
 
0914710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import L, { icon, Evented as LEvented, type LatLng, Map as LMap } from 'leaflet'
import 'leaflet-providers'
import {
  responseMessageLisaRef,
  responseMessageRef,
  waitingString,
  durationRef,
  numberOfPolygonsRef,
  numberOfPredictedMasksRef
} from './constants'
import {
  ExcludeIncludeLabelPrompt as excludeIncludeLabelPrompt,
  type ArrayNumber,
  type BboxLatLng,
  type ExcludeIncludeLabelPrompt,
  type IBodyLatLngPoints,
  type IPointPrompt,
  type IRectanglePrompt, type IRectangleTable, type IPointTable, type IBodyLatLngWithStringPoints
} from './types.d'
import type { Ref } from 'vue'


export const getQueryParams = () => {
  const urlSearchParams = new URLSearchParams(window.location.search);
  const params = Object.fromEntries(urlSearchParams.entries());
  const {source,...options} = params
  return {source, options}
}


export const applyFnToObjectWithinArray = (array: Array<IPointPrompt | IRectanglePrompt>): Array<IPointTable | IRectangleTable> => {
  let newArray = []
  for (const el of array) {
    newArray.push(el.type === 'rectangle' ? getUpdatedRectangle(el) : getUpdatedPoint(el))
  }
  return newArray
}

const getUpdatedPoint = (obj: IPointPrompt): IPointTable => {
  return {
    id: obj.id,
    data: obj.data,
    label: obj.label
  }
}

const getUpdatedRectangle = (obj: IRectanglePrompt): IRectangleTable => {
  return {
    id: obj.id,
    data_ne: obj.data.ne,
    data_sw: obj.data.sw,
  }
}

/** get a custom icon given a PNG path with its anchor/size values  */
const getCustomIconMarker = (
  iconUrlNoExt: string,
  shadowUrl = '/marker-shadow.png',
  iconSize: ArrayNumber = [25, 41],
  iconAnchor: ArrayNumber = [12, 41],
  popupAnchor: ArrayNumber = [1, -34],
  tooltipAnchor: ArrayNumber = [5, -25],
  shadowSize: ArrayNumber = [41, 41]
): icon => {
  return icon({
    iconUrl: `${iconUrlNoExt}.png`,
    iconRetinaUrl: `${iconUrlNoExt}-2x.png`,
    shadowUrl,
    iconSize,
    iconAnchor,
    popupAnchor,
    shadowSize,
    tooltipAnchor
  })
}

/** get an  the leaflet editor geoman.io toolbar with the custom actions to draw/edit/move point and rectangle layers */
const getCustomGeomanActionsObject = (
  actionName: string, descriptionAction: string, arrayActions: Array<object>, customClassName: string
) => {
  return {
    name: actionName,
    block: 'custom',
    className: customClassName,
    title: descriptionAction,
    actions: arrayActions
  }
}

/** prepare the leaflet editor geoman.io toolbar with the custom actions to draw/edit/move point and rectangle layers */
export function setGeomanControls(localMap: LMap) {
  // leaflet geoman toolbar
  localMap.pm.addControls({
    position: 'topleft',
    drawControls: false,
    rotateMode: false,
    cutPolygon: false,
    customControls: true
  })

  const actionArray = [{
      onClick(actionEvent: LEvented) {
        console.log('actionEvent:', typeof actionEvent, '|', actionEvent, '')
      },
      name: 'actionName'
  }]
  const includeMarkerControl = localMap.pm.Toolbar.copyDrawControl('Marker',
    getCustomGeomanActionsObject(
      'IncludeMarkerPrompt',
      'Marker point that add recognition regions from SAM prompt requests',
      actionArray,
      'control-icon leaflet-pm-icon-marker-include'
    )
  )
  // custom marker icon on map
  includeMarkerControl.drawInstance.setOptions({
    markerStyle: { icon: getCustomIconMarker('/marker-icon-include') }
  })
  const excludeMarkerControl = localMap.pm.Toolbar.copyDrawControl('Marker',
    getCustomGeomanActionsObject(
      'ExcludeMarkerPrompt',
      'Marker point that remove recognition regions from SAM prompt requests',
      actionArray,
      'control-icon leaflet-pm-icon-marker-exclude'
    )
  )
  excludeMarkerControl.drawInstance.setOptions({
    markerStyle: { icon: getCustomIconMarker('/marker-icon-exclude') }
  })
  localMap.pm.Toolbar.copyDrawControl('Rectangle', {
    actions: actionArray,
    block: 'custom',
    name: 'RectanglePrompt',
    title: 'Rectangular recognition regions for SAM prompt requests'
  })
  localMap.pm.setPathOptions({
    color: "green",
    fillColor: "green",
    fillOpacity: 0.15,
  })
}

/** get the selected rectangle layer bounding box coordinate */
export const getSelectedRectangleCoordinatesBBox = (leafletEvent: LEvented): BboxLatLng => {
  const { _northEast, _southWest } = leafletEvent.layer._bounds
  return {
    ne: new L.LatLng(_northEast.lat, _northEast.lng),
    sw: new L.LatLng(_southWest.lat, _southWest.lng)
  }
}

/** get the current selected point coordinate */
export const getSelectedPointCoordinate = (leafletEvent: LEvented): LatLng => {
  return leafletEvent.layer._latlng
}

/** get the current map bounding box coordinates */
export const getExtentCurrentViewMapBBox = (leafletMap: LMap): BboxLatLng => {
  const boundaries = leafletMap.getBounds()
  return { ne: boundaries.getNorthEast(), sw: boundaries.getSouthWest() }
}

/** send the ML request to the backend API through the cloudflare proxy function */
export const getGeoJSONRequest = async (
  requestBody: IBodyLatLngPoints | IBodyLatLngWithStringPoints,
  urlApi: string
) => {
  responseMessageRef.value = waitingString
  responseMessageLisaRef.value = waitingString
  console.log(`getGeoJSONRequest urlApi: ${urlApi} ...`)
  const data = await fetch(urlApi, {
    method: 'POST',
    body: JSON.stringify(requestBody),
    headers: {
      'Content-type': 'application/json'
    }
  })
  try {
    console.log(`getGeoJSONRequest data.status: ${data.status} ...`)
    if (data.status === 200) {
      const output: Object = await data.json()
      try {
        const parsed = JSON.parse(output.body)
        const { geojson, n_predictions, n_shapes_geojson } = parsed.output

        try {
          console.log("getGeoJSONRequest parsed.output output_string:", parsed.output.output_string, "#")
          const { output_string } = parsed.output
          responseMessageLisaRef.value = output_string
        } catch(e) {
          console.log("getGeoJSONRequest parsed.output e:", e, "#")
          responseMessageLisaRef.value = 'error: check the logs'
        }

        const parsedGeojson = JSON.parse(geojson)
      durationRef.value = parsed.duration_run
        numberOfPolygonsRef.value = n_shapes_geojson
        numberOfPredictedMasksRef.value = n_predictions
      responseMessageRef.value = ''
        return parsedGeojson
      } catch (errParseOutput1) {
        console.error("errParseOutput1::", errParseOutput1)
        return String(errParseOutput1)
      }
    } else {
      const outputText = await data.text()
      console.error('getGeoJSONRequest => status not 200, outputText', outputText, '#')
      responseMessageRef.value = `error message response: ${outputText}...`
    }
  } catch (errorOtherData) {
    const statusText = data.statusText || 'no response or uncaught exception!'
    console.error(
      'getGeoJSONRequest => data',
      data,
      'statusText',
      statusText,
      'errorOtherData',
      errorOtherData,
      '#'
    )
    responseMessageRef.value = `error status response: ${statusText}...`
  }
}

/** populate a single point ML request prompt, by type (exclude or include), see type ExcludeIncludeLabelPrompt */
export const getPointPromptElement = (e: LEvented, elementType: ExcludeIncludeLabelPrompt): IPointPrompt|IRectanglePrompt => {
  const currentPointLayer: LatLng = getSelectedPointCoordinate(e)
  return {
    id: e.layer._leaflet_id,
    type: 'point',
    data: currentPointLayer,
    label: elementType
  }
}

/** populate a single rectangle ML request prompt */
export const getRectanglePromptElement = (e: LEvented) => {
  return {
    id: e.layer._leaflet_id,
    type: 'rectangle',
    data: getSelectedRectangleCoordinatesBBox(e)
  }
}

/** handle different event/layer types (rectangle, point: IncludeMarkerPrompt, ExcludeMarkerPrompt) */
const updateLayerOnCreateOrEditEvent = (
  event: LEvented,
  getPopupContentPointFn: (arg0: LEvented, arg1: number) => HTMLDivElement,
  promptsArrayRef: Ref) => {
  responseMessageRef.value = '-'
  if (event.shape === 'IncludeMarkerPrompt' || event.shape === 'ExcludeMarkerPrompt') {
    const labelPoint = Number(excludeIncludeLabelPrompt[event.shape])
    const div = getPopupContentPointFn(event, labelPoint)
    event.layer.bindPopup(div).openPopup()
    promptsArrayRef.value.push(getPointPromptElement(event, labelPoint))
  }
  if (event.shape === 'RectanglePrompt') {
    event.layer.bindPopup(`id:${event.layer._leaflet_id}.`).openPopup()
    promptsArrayRef.value.push(getRectanglePromptElement(event))
  }
}

/** listen on the leaflet editor geoman.io events and update its layer properties within the promptsArrayRef vue ref */
export const updateMapData = (
  localMap: LMap,
  getPopupContentPointFn: (arg0: LEvented, arg1: number) => HTMLDivElement,
  promptsArrayRef: Ref
) => {
  localMap.on('pm:create', (e: LEvented) => {
    updateLayerOnCreateOrEditEvent(e, getPopupContentPointFn, promptsArrayRef)

    // listen to changes on the new layer and update its object within promptsArrayRef
    e.layer.on('pm:edit', function(newEvent: LEvented) {
      promptsArrayRef.value = removeEventFromArrayByIndex(promptsArrayRef.value, newEvent)
      updateLayerOnCreateOrEditEvent(e, getPopupContentPointFn, promptsArrayRef)
    });
  })
  localMap.on('pm:remove', (e: LEvented) => {
    responseMessageRef.value = '-'
    promptsArrayRef.value = removeEventFromArrayByIndex(promptsArrayRef.value, e)
  })
}

/** remove the selected layer from the ML request array prompt */
const removeEventFromArrayByIndex = (arr: Array<LEvented>, e: LEvented) => {
  return arr.filter((el: LEvented) => {
    return el.id != e.layer._leaflet_id
  })
}