[Gfoss] qgis: layer not valid

giuliano su Tiscali giulianc a tiscali.it
Mar 17 Apr 2012 23:56:13 CEST


On Tue, 17 Apr 2012 23:00:21 +0200
Giuseppe Sucameli <sucameli a faunalia.it> wrote:

> Ciao Giuliano,

ciao (e grazie) Giuseppe :-)

> 
> On Monday, April 16, 2012, giuliano su Tiscali <giulianc a tiscali.it> wrote:
> > non supera il controllo di validità
> [snip]
> >    if not layer.isValid():
> >      print "oops: the layer is not valid"
> >      return ++++++++ il codice si ferma qui ++++++++++++++
> 
> i layer sono non validi perché probabilmente l'applicazione
> non riesce a caricare le librerie contenenti i provider.
> 
> Purtoppo le linee che hai postato non sono utili in questo
> caso. Dovresti postare le linee nelle quali fai il setup
> dell'applicazione standalone.
> 
> Probabilmente basterà cambiare il path che hai passato alla
> QgsApplication.setPrefix() per risolvere il problema, ma senza
> ulteriori informazioni sull'OS e sulla tua installazione di QGis è
> parecchio complicato.
> Se sei su linux prova /usr o /usr/local.

sì, scusa, ma sai che è facile perdersi in un bicchiere d'acqua ....
ho la 1.9.90alpha su linuz debian wheezy;

alla tua ipotesi circa il path ci avevo pensato; qgis è installato
in /usr/share/qgis e /usr/lib/qgis; ho provato con entrambi
(qgis_prefix = '/usr/share/qgis/' e qgis_prefix = '/usr/lib/qgis/')
senza risultato, però potrebbe non essere questo il parametro
determinante; ma non so cos'altro provare dopo aver letto Martin D. in
originale e tradotto;

metto sotto l'__init__(), l'addLayer() e il main() per adesso (non
vorrei disturbare gli altri con un centinaio di linee):


> Saluti.
> 
> Giuseppe Sucameli

grazie ancora, ciao, 
giuliano


	def __init__(self):
		QMainWindow.__init__(self)
		self.setWindowTitle("QGIS Demo App")

#		Required by Qt4 to initialize the UI
		self.setupUi(self)

#		Create the map canvas
		self.canvas = QgsMapCanvas()
		self.canvas.setCanvasColor(QColor(230,230,255))
		self.canvas.enableAntiAliasing(True)
		self.canvas.useImageToRender(False)
		self.canvas.show()

#		Lay our widgets out in the main window using a
#		vertical box layout
		self.layout = QVBoxLayout(self.frame)
		self.layout.addWidget(self.canvas)

#		Create the actions for our tools and connect each to the appropriate method
		actionQuit = QAction(QIcon(qgis_icons_prefix + "actionExit.png"), "Quit", self)

...... seguono altre actions ........

#	Add an OGR layer to the map
	def addLayer(self):
		file = QFileDialog.getOpenFileName(self, "Open Shapefile", ".", "Shapefiles (*.shp)")
		fileInfo = QFileInfo(file)

#		Add the layer
		layer = QgsVectorLayer(file, fileInfo.fileName(), "ogr")

		if not layer.isValid():
			print "oops: the layer is not valid"
			return

*************** non passa mai questo test *******************

#		Change the color of the layer to gray
		symbols = layer.renderer().symbols()
		symbol = symbols[0]
		symbol.setFillColor(QColor.fromRgb(192,192,192))

#		Add layer to the registry
		QgsMapLayerRegistry.instance().addMapLayer(layer); # usare addMapLayers(layer_list)
#		QgsMapLayerRegistry.addMapLayer(layer);

#		Set extent to the extent of our layer
		self.canvas.setExtent(layer.extent())

#		Set up the map canvas layer set
		cl = QgsMapCanvasLayer(layer)
		layers = [cl]
		self.canvas.setLayerSet(layers)

 def main(argv):
#	create Qt application
	app = QApplication(argv)

#	Initialize qgis libraries
	QgsApplication.setPrefixPath(qgis_prefix, True)
	QgsApplication.initQgis()

#	create main window
	wnd = MainWindow()
#	Move the app window to upper left
	wnd.move(100,100)
	wnd.show()

#	--- per vedere le librerie usate di default -------
#	---- quelle di QT --------------------------------
#	for tmp in app.instance().libraryPaths():
#		print tmp
#	---- restituisce: -----------------------------
#		/usr/lib/qt4/plugins
#		/usr/bin

#	---- quelle di qGis: -----------------------------
#	for tmp in QgsApplication.instance().libraryPaths():
#		print tmp
#	---- restituisce le stesse: ----------------------
#		/usr/lib/qt4/plugins
#		/usr/bin

#	run!
	retval = app.exec_()

#	exit
	QgsApplication.exitQgis()
	sys.exit(retval)

if __name__ == "__main__":
	main(sys.argv)


Maggiori informazioni sulla lista Gfoss