3

I'm developing a basic application out of QGIS and am having trouble getting my code to generate all the layers I have set. Currently it only generates two of the four layers: "self.basemap_layer", which is a TIFF raster layer, and "self.pointlayer", which is a point layer generated by a text-delimited CSV. It does not generate the two vector shapefiles left over.

Instead, I get two errors:

QEventLoop: Cannot be used without QApplication
QGraphicsScene::addItem: item has already been added to this scene

I can't figure out why it does connect to two layers, but does not connect to the other two...

My code is below:

def loadMap(self):

    cur_dir = os.path.dirname(os.path.realpath(__file__))

    #DOES LOAD
    filename = os.path.join(cur_dir, "data","NE1_50M_SR_W.tif") 
    self.basemap_layer = QgsRasterLayer(filename, "basemap")
    QgsMapLayerRegistry.instance().addMapLayer(
        self.basemap_layer)

    #DOESN'T LOAD
    filename = os.path.join(cur_dir, "data", "ne_10m_roads.shp")
    self.roads_layer = QgsVectorLayer(filename, "roads", "ogr")
    QgsMapLayerRegistry.instance().addMapLayer(self.roads_layer)

    #DOES LOAD       
    filename = "/home/me/Desktop/PyQGIS/appfiles/data/thiefsymbolcsv.csv?delimiter=%s&crs=epsg:3857&xField=%s&yField=%s" % (
    ",", "X", "Y")
    self.pointlayer = QgsVectorLayer(filename, "point", "delimitedtext")
    QgsMapLayerRegistry.instance().addMapLayer(self.pointlayer)  

    #DOESN'T LOAD
    filename = os.path.join(cur_dir, "data", "line_points.shp")
    self.loblayer = QgsVectorLayer(filename, "lobs", "ogr")
    QgsMapLayerRegistry.instance().addMapLayer(self.loblayer)

    self.showVisibleMapLayers()
    self.mapCanvas.setExtent(QgsRectangle(-127.7, 24.4, -79.3, 49.1))

It's not giving me errors related to my imported PyQT and PyQGIS libraries, and the application window works just as inteded, so I don't think I've set my environment variables incorrectly. But I did read elsewhere that these error might have to do with an incorrect wrapper script, so I'm not entirely sure.

Why are these errors occurring, and how do I get rid of them? Seeing as both the problem layers are .SHP files, and the ones that work are .TIF and .CSV, could their file type somehow be the issue?

My full code is here for reference

UPDATE: After closer examination, I've found that all layers do indeed load, just in the wrong order. My point feature is at the top, followed by the basemap, then the line and road layers on the bottom. Trying to figure out how to change the drawing order. I also moved my "watcher" portion, which was below my csvLobRefresh method, to the bottom of my __init__ method in my MapExplorer class, which got rid of the "QEventLoop" error. the "QGraphicsScene" error remains, however.

  • in your code you shoud use QgsApplication (derived from QApplication) otherwise init and exit are not used – Luigi Pirelli Sep 26 '15 at 18:56
  • I use QgsApplication later on in my code. You will see starting at line 181 in the code I linked at the bottom of my original post. Am I using it incorrectly? – Alex92 Sep 28 '15 at 13:45
  • This is not an answer to your question but since I cannot comment yet, i'll just ask here. I'm having the same problem. I trust you are following the tutorial in "Building Mapping Applications With QGIS". My layers seem to be invalid. I tested it using the isValid() function and counted the layers in the QgsMapLayerRegistry. Did you find a way to solve this? – William Ernesto Jun 20 '18 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.