[go: up one dir, main page]

Get data for a pixel location in a ImageSource

Summary

I have not found a method to retrieve Pixel information from ImageSource.

My objective is to have a method equivalent to OpenLayers.ImageLayer.getData(pixel) You can found that method here https://openlayers.org/en/latest/apidoc/module-ol_layer_Tile-TileLayer.html#getData and an example here https://openlayers.org/en/latest/examples/getfeatureinfo-tile.html to change cursor when hoving on a WMS layer.

What i have done

I have come up with a "solution" that appears to work, i wonder if that seems realistic / stable for you ?

   const mouse_vector = instance.eventToCanvasCoords(evt, new Vector2())

   const map_pick_result: Array<MapPickResult> = this.map.pick(mouse_vector, pickOptions)
   if (map_pick_result.length > 0) {
      for (let index = 0; index < this.map.getLayers().length; index++) {
         const layer = this.map.getLayers()[index] as ColorLayer;
         let targets = Array.from(layer["_targets"].values()) as Array<Target>
        const target = targets.find((t: Target) => t.node.uuid == map_pick_result[0].object.uuid)
        const renderer = layer["_composer"].composer["_renderer"] as WebGLRenderer

        const node_top_right = map_pick_result[0].object.extent.topRight().toVector3()
        const node_bottom_left = map_pick_result[0].object.extent.bottomLeft().toVector3()
        node_bottom_left.project(instance.camera.camera3D)
        node_top_right.project(instance.camera.camera3D)

        const node_bottom_left_normalized = new Vector2()
        const node_top_right_normalized = new Vector2()

        instance.normalizedToCanvasCoords(new Vector2(node_bottom_left.x, node_bottom_left.y), node_bottom_left_normalized)
        instance.normalizedToCanvasCoords(new Vector2(node_top_right.x, node_top_right.y), node_top_right_normalized)


        let target_dimension = node_top_right_normalized.x - node_bottom_left_normalized.x
        let target_resolution = target_dimension / target.width
        const click_point_x = (mouse_vector.x - node_bottom_left_normalized.x)  / target_resolution
        const click_point_y = ((renderer.domElement.height - mouse_vector.y) - (renderer.domElement.height - node_bottom_left_normalized.y))   / target_resolution

        let pixels = new Uint8Array(10 * 10 * 4);

        renderer.readRenderTargetPixels(
          target["renderTarget"],
          click_point_x, click_point_y, 10, 10, pixels
        )
        if (pixels.reduce((sum, number) => sum + number) > 0){
           // Layer have data on this pixel
        }else{
         // Layer does not have data on this pixel
        }
      }
   }

Informations

  • Giro3D version: 0.37.0
  • Browser (Firefox, Chrome, Opera, Safari...) and version: Firefox
  • OS (Linux, MacOS, Windows...) : MacOS
Edited by Nelson Tayou