2

I am attempting to loop over dozen of https S3-hosted COPC (LAZ) files in Python for QGIS to (1) render them, and (2) write out a simple thumbnail Jpeg. Using the script below I can successfully loop over downloaded local COPC files, but I can't figure out how to load https S3-hosted COPC (LAZ) files into QGIS.

At line 16, next, QgsPointCloudLayer call either faults or crashes QGIS using an https address. (again this works with local files).

pLayer = QgsPointCloudLayer(urlLOLA, nameCOPC, "pdal", pLayerOptions)

Here is the full code block, but really figuring out how to load a COPC/LAZ file from an https address using QgsPointCloudLayer should the key. All the image writing is just extra here.

EDIT: updated code below to replace "pdal" with "copc" and added pause function to allow time for the streamed file to load and render.

from qgis.core import *
import time

def pause(seconds):
    print("Paused for " + str(seconds) + " seconds to allow load")
    time_end = time.time() + seconds
    while time.time() < time_end:
        QgsApplication.processEvents()

#S3 URL (now works also!)
files=["https://astrogeo-ard.s3-us-west-2.amazonaws.com/moon/lro/lola/LolaRDR_15N30N_270E285E.copc.laz",
       "https://astrogeo-ard.s3-us-west-2.amazonaws.com/moon/lro/lola/LolaRDR_15N30N_285E300E.copc.laz"]

#local files (works)
#files=["/tmp/LolaRDR_15N30N_270E285E.copc.laz",
#      "/tmp/LolaRDR_15N30N_285E300E.copc.laz"]

canvas = iface.mapCanvas()
for urlLOLA in files:
    nameCOPC=urlLOLA.split("/")[-1]
    nameJPEG="/tmp/" + nameCOPC.replace(".copc.laz","_thumb.jpg")

    pLayer = QgsPointCloudLayer(urlLOLA, nameCOPC, "copc")

    if pLayer.isValid():
        print(nameCOPC + ": " + str(pLayer.pointCount()))
        QgsProject.instance().addMapLayer(pLayer)

        #Grab newly added layer and set to 70% transparent
        layer = iface.activeLayer()
        layer.setOpacity(0.7)

        #zoom to layer and refresh
        extent = layer.extent()
        canvas.setExtent(extent)
        canvas.refresh()
        #force 60-second pause to allow streamed COPC to load and render
        pause(60)
        canvas.waitWhileRendering()

        #export Jpeg
        canvas.saveAsImage(nameJPEG,None,"JPEG")
        print("created file: " + nameJPEG)            
        
        #remove layer
        QgsProject.instance().removeMapLayer( layer.id() )
        canvas.refreshAllLayers()
1
  • with the addition of the pause function, likely QgsApplication.processEvents(), there are some stability issues for QGIS. Not sure what is going on (memory leak?). In the end, I used downloaded local files, not streamed files.
    – Trent
    Sep 12 at 21:00

1 Answer 1

3

Change the provider from "pdal" to "copc"

With this code:

from qgis.core import *

#S3 URL
files=["https://astrogeo-ard.s3-us-west-2.amazonaws.com/moon/lro/lola/LolaRDR_15N30N_285E300E.copc.laz"]

canvas = iface.mapCanvas()
for urlLOLA in files:
    pLayer = QgsPointCloudLayer(urlLOLA, nameCOPC, "copc")

    if pLayer.isValid():
        QgsProject.instance().addMapLayer(pLayer)

Output:

enter image description here

enter image description here

1
  • Brilliant - sure enough the update from from "pdal" to "copc" worked. Now since the COPC files are streamed, I had to add a delay into the script to allow the file to fully load and render before saving an image. Using canvas.waitWhileRendering() wasn't enough since the file still wasn't fully loaded. I added a "pause" timer function to the code above to help with that issue.
    – Trent
    Sep 9 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.