Gooong / LeastCostPath

The Least Cost Path Plugin for QGIS
https://plugins.qgis.org/plugins/leastcostpath/
GNU General Public License v3.0
24 stars 12 forks source link

Question: How to run the least cost path process from a Python script? #7

Closed diegogentilepassaro closed 3 years ago

diegogentilepassaro commented 3 years ago

I have run a process through the QGIS GUI and I got the answer I needed. Now I want to make it reproducible and I am trying to implement without success after a couple of hours. Could you please show me an example of code running any least cost path? I have tried the following without luck:

processing.run("Cost distance analysis:Least Cost Path", 
    {'INPUT_COST_RASTER':some_cost_raster,
    'INPUT_RASTER_BAND':1,
    'INPUT_START_LAYER':some_input_layer,
    'INPUT_END_LAYER':some_other_input_layer,
    'BOOLEAN_FIND_LEAST_PATH_TO_ALL_ENDS':True,'BOOLEAN_OUTPUT_LINEAR_REFERENCE':False,
    'OUTPUT':some_output_layer})
Gooong commented 3 years ago

Hi, this is an example from issue #3. Ensure the two layers have the same CRS.

vlayer = QgsVectorLayer("D:\\tst\\start.shp", "start", "ogr")
vlayer2 = QgsVectorLayer("D:\\tst\\end.shp", "end", "ogr")
test=processing.run("Cost distance analysis:Least Cost Path", { 'BOOLEAN_OUTPUT_LINEAR_REFERENCE' : False, 'BOOLEAN_FIND_LEAST_PATH_TO_ALL_ENDS':False, 'INPUT_COST_RASTER' : 'D:/tst/cost.tif', 'INPUT_END_LAYER' : vlayer2, 'INPUT_RASTER_BAND' : 1, 'INPUT_START_LAYER' : vlayer, 'OUTPUT' : 'memory:' })

If it still doesn't work, please attach the error log so that we can find the problem. Thanks!