1

Why isn't my heat layer valid and I don't get a .tif in this location (this location exists 100%)? Code below:

output = "D:/QGIS_Project_New/heat_map_last.tif"
heat = QgsRasterLayer(output, "Heatmap")
QgsMapLayerRegistry.instance().addMapLayers([heat])
if heat.isValid() is True:
print "Layer was loaded successfully!"
else:
print "Unable to read basename and file path - Your string is invalid"

I get the "Unable.." print, why is that?

EDIT:

So my code for loading layers is working:

highway_layer = QgsVectorLayer("D:/Tomas/QGIS_Project_New/points_highway.shp","Highway","ogr")

if highway_layer.isValid() is True:
    print " HIGHWAY Layer was loaded successfully!"
else:
    print " HIGHWAY Unable to read basename and file path - Your string is invalid"

But when I try to create and save a layer on this location it is not working. Also I'm using Python Console in QGIS.

Noura
  • 3,429
  • 3
  • 20
  • 41
John Doe
  • 19
  • 2
  • Welcome to GIS:SE @JohnDoe! How are you running your code, from a script or from the python console? Can you confirm that the file is indeed in the directory you specified? – Joseph Apr 18 '19 at 14:51
  • I've read that post, my loading is correct and I use this in my code before this code:

    highway_layer = QgsVectorLayer("D:/Tomas/QGIS_Project_New/points_highway.shp","Highway","ogr") if highway_layer.isValid() is True: print " HIGHWAY Layer was loaded successfully!" else: print " HIGHWAY Unable to read basename and file path - Your string is invalid"

    And I get " HIGHWAY Layer was loaded successfully!"

    I'm running the code from the qgis python console.

    – John Doe Apr 18 '19 at 14:58
  • Please add any additional clarifications and code to your question by editing it. – TomazicM Apr 18 '19 at 15:05
  • Did it just now. I'm trying to SAVE in my first example, loading in my second example is working. – John Doe Apr 18 '19 at 15:06
  • Does "D:/QGIS_Project_New/heat_map_last.tif" exist? it looks like you're trying to add an existing tif to a map, and the file isn't found. – SMiller Apr 18 '19 at 16:14
  • No it doesn't? I want to create it in the end.

    Check: Source of code(Page 272): http://www.green-forums.info/greenlib/geolibrary/Lawhead%20J/QGIS%20Python%20Programming%20Cookbook.%2020%20%2852%29/QGIS%20Python%20Programming%20Cookboo%20-%20Lawhead%20J.pdf

    – John Doe Apr 18 '19 at 17:03
  • @smiller Hi, yes I understand now, I thought I was creating heatmap in this code but actually I just have to import my heatmap from the process beforehand.

    Please help with my further issue:

    https://gis.stackexchange.com/questions/319254/kernel-density-estimation-weighting-factor?noredirect=1#comment519881_319254

    – John Doe Apr 18 '19 at 17:10

0 Answers0