zoran-cuckovic / QGIS-visibility-analysis

Quantum GIS plugin for visibility analysis
GNU General Public License v3.0
72 stars 18 forks source link

Help! Help! Help! #80

Closed Editor-1 closed 1 month ago

Editor-1 commented 1 month ago

I found the frequent creation of lookout points a bit tedious, so I wrote a python script to automate this, using the files generated by the lookout points as input for the next step, but the following error message was reported:

2024-08-08T11:02:39     WARNING    warning:C:\Users\xxx\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\ViewshedAnalysis\algorithms\modules\visibility.py:182: RuntimeWarning: divide by zero encountered in divide
              data /= distance_matrix #all one line = (data -z - mxcurv) /mx_dist

             traceback: File "C:\Users\xxx\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\location_selection\Location_Selection.py", line 195, in run
              result = self.dlg.exec_()
              File "C:\Users\xxx\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\location_selection\Location_Selection_dialog.py", line 141, in run_analysis
              result = processing.run("visibility:viewshed",
              File "F:\/apps/qgis/./python/plugins\processing\tools\general.py", line 109, in run
              return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
              File "F:\/apps/qgis/./python/plugins\processing\core\Processing.py", line 192, in runAlgorithm
              ret, results = execute(alg, parameters, context, feedback, catch_exceptions=False)
              File "F:\/apps/qgis/./python/plugins\processing\gui\AlgorithmExecutor.py", line 70, in execute
              results, ok = alg.run(parameters, context, feedback, {}, False)
              File "C:\Users\xxx\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\ViewshedAnalysis\algorithms\viewshed_raster.py", line 264, in processAlgorithm
              matrix_vis = ws.viewshed_raster (analysis_type, pt[id1], dem,
              File "C:\Users\xxx\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\ViewshedAnalysis\algorithms\modules\visibility.py", line 182, in viewshed_raster
              data /= distance_matrix #all one line = (data -z - mxcurv) /mx_dist
          Miraculously, I manually split it into step by step operations without this problem, can someone help me? Thank you very much
zoran-cuckovic commented 1 month ago

Hello, There seems to be a problem with your dem. What are you trying to do, exactly ? could you please provide your code?

Editor-1 commented 1 month ago

Thank you for your help. The following is my python script code. What I want to do is to combine the two operations of creating lookout point and view analysis together. I did not report any error when I used your plug-in step by step, but I reported an error when I combined them

import os
import datetime

project = QgsProject.instance()
projectLocation = project.fileName() 
fileStorage = os.path.dirname(os.path.dirname(projectLocation)) +'/storage' 
os.makedirs(fileStorage, exist_ok=True) 
pubulicName = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
demFile = r'C:\Users\xxx\Desktop\xxx\xxx\GIS_Study_Data\xxx\xxx_16bit_12.5m.tif'
shpFile = r'C:\Users\xxx\xxx\xxx\xxx\GIS_Study_Data\xxx\xxx.shp'
gpkgFile = fileStorage + '/'+ pubulicName+'-vp.gpkg'
obRadius =  5000
obHeight = 5
tarHeight = 0
viewtifFile = fileStorage + '/'+ pubulicName+'-view.tif'
resultFile = fileStorage + '/'+ pubulicName+'-result.tif'

processing.run("visibility:createviewpoints", 
               {
                'OBSERVER_POINTS':shpFile, 
                'DEM':demFile, 
                'OBSERVER_ID':'', 
                'RADIUS':obRadius, 
                'RADIUS_FIELD':'',
                'OBS_HEIGHT':obHeight, 
                'OBS_HEIGHT_FIELD':'', 
                'TARGET_HEIGHT':tarHeight,
                'TARGET_HEIGHT_FIELD':'', 
                'RADIUS_IN_FIELD':'', 
                'AZIM_1_FIELD':'',
                'AZIM_2_FIELD':'', 
                'ANGLE_UP_FIELD':'', 
                'ANGLE_DOWN_FIELD':'', 
                'OUTPUT':gpkgFile 
                })
processing.run("visibility:viewshed",
                {
                    'ANALYSIS_TYPE':0, 
                    'OBSERVER_POINTS':gpkgFile, 
                    'DEM':demFile, 
                    'USE_CURVATURE':False, 
                    'REFRACTION':0.13,
                    'OPERATOR':0, 
                    'OUTPUT':viewtifFile
                })
processing.run("native:reclassifybytable", 
               {
                'INPUT_RASTER':viewtifFile,
                'RASTER_BAND':1, 
                'TABLE':['0','0','0','1','255','1'], 
                'NO_DATA':-9999,
                'RANGE_BOUNDARIES':0,
                'NODATA_FOR_MISSING':False,
                'DATA_TYPE':1
                # 'OUTPUT':'TEMPORARY_OUTPUT'
                'OUTPUT':resultFile
                })

layer_tree = project.layerTreeRoot()
group_name = pubulicName + '-Group'
group = None
group = QgsLayerTreeGroup(group_name)
layer_tree.addChildNode(group)

viewtLayer = QgsRasterLayer(viewtifFile, pubulicName + 'view')
resultLayer = QgsRasterLayer(resultFile, pubulicName + 'resultview')
project.addMapLayer(viewtLayer, addToLegend=False)
group.addLayer(viewtLayer)
project.addMapLayer(resultLayer, addToLegend=False)
group.addLayer(resultLayer)
Editor-1 commented 1 month ago

My dem should be fine, because the above errors did not occur when I stepped through your plugin

zoran-cuckovic commented 1 month ago

Hello, this is quite complicated function. When I tried to run it, a coding error appeared on line 55 (missing comma). Once fixed, no problems appeared on my side (?)