Bug report #10763

QgsProject is unable to load a project with a raster

Added by David AMAR over 10 years ago. Updated over 5 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:PyQGIS Console
Affected QGIS version:2.4.0 Regression?:No
Operating System:Debian Easy fix?:No
Pull Request or Patch supplied:No Resolution:end of life
Crashes QGIS or corrupts data:No Copied to github as #:19146

Description

Steps to reproduce:

Create a new project with QGis Desktop 2.4
Save it into home directory as "myproject.qgs"
Add a raster TIF (img.tif in my case) located at the same place of this project
Save the project again.

Create a new project and open python console
Then execute these Python commands
---
p = QgsProject.instance()
qgsInfo = QFileInfo( "myproject.qgs" )
isLoaded = p.read( qgsInfo )
---

A window "Handle bad layers" is displayed to correct layer path. The incorrect path is "./img.tif". I have to correct it manually to "img.tif" to load the layer.

modifyqgs.py Magnifier (1.75 KB) David AMAR, 2014-07-01 03:43 AM

History

#1 Updated by Nathan Woodrow over 10 years ago

  • Status changed from Open to Closed
  • Resolution set to invalid
  • Tag set to python

You have saved the project with relative paths.

If you want to open a project using Python inside QGIS you can use:

iface.addProject(path)

If you want to use the method that you are using you need to set the working directory, which is reset when you create a new project:

http://qt-project.org/doc/qt-4.8/qdir.html#setCurrent

QDir.setCurrent(folderofproject)

#2 Updated by David AMAR over 10 years ago

Hello Nathan,

I think there are some points to deal with before closing:
- iface can't be used in a stand-alone python Script (see attached python script)
- This bug is a regression: QProject.read() worked correctly with QGIS 2.0.1 on Windows platform (In fact, attached script is working correctly with QGIS 2.0.1!)

Regards.

#3 Updated by Nathan Woodrow over 10 years ago

  • Status changed from Closed to Reopened
  • Resolution deleted (invalid)

- iface can't be used in a stand-alone python Script (see attached python script)

Of course that is why I said you have to do the current working folder thing.

- This bug is a regression: QProject.read() worked correctly with QGIS 2.0.1 on Windows platform (In fact, attached script is working correctly with QGIS 2.0.1!)

Ok reopened for more investigation.

#4 Updated by Giovanni Manghi over 7 years ago

  • Regression? set to No
  • Easy fix? set to No

#5 Updated by Giovanni Manghi over 5 years ago

  • Resolution set to end of life
  • Status changed from Reopened to Closed

Also available in: Atom PDF