Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database manager cannot import layer if any layer is None #41152

Closed
sickel opened this issue Jan 24, 2021 · 1 comment · Fixed by #41173
Closed

Database manager cannot import layer if any layer is None #41152

sickel opened this issue Jan 24, 2021 · 1 comment · Fixed by #41173
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! DB Manager Relating to the DB Manager core plugin Feedback Waiting on the submitter for answers

Comments

@sickel
Copy link
Contributor

sickel commented Jan 24, 2021

Describe the bug

Trying to use db manager to import a layer to a postgis database by pressing "Import layer/file" when a postgis database is connected, a python error is displayed and the available layers are not being listed. This happens if there is a defective layer in the project from the Open Layers plugin.

How to Reproduce

Make a connection to a postgis database, Install the open layer plugin. In a project with CRS EPSG:3857 Pseudo mercator add in e.g. Bing Roads from open layers. Save and close the project and reopen, the layer will not display. Open db manager, connect to the postgis database and press "Import layer/file". A python error will be displayed.

QGIS and OS versions
Last tested on qgis 3.16-3 on windows and linux (debian), but also seen on several previous versions.

Additional context

The bug is triggerd by a bug in open layers, but as far as I can see, the fix will not have any negative concequences and will generally make the import function more robust.

** Proposed fix **

The error message is (on my Windows 10 PC)
File "C:/PROGRA1/QGIS31.16/apps/qgis/./python/plugins\db_manager\dlg_import_vector.py", line 135, in populateLayers
if layer.type() == QgsMapLayerType.VectorLayer:
AttributeError: 'NoneType' object has no attribute 'type'

The present code is:
def populateLayers(self):
self.cboInputLayer.clear()
for nodeLayer in QgsProject.instance().layerTreeRoot().findLayers():
layer = nodeLayer.layer()
# TODO: add import raster support!
if layer.type() == QgsMapLayerType.VectorLayer:
self.cboInputLayer.addItem(layer.name(), layer.id())

this could be rewritten as:

def populateLayers(self):
    self.cboInputLayer.clear()
    for nodeLayer in QgsProject.instance().layerTreeRoot().findLayers():
        layer = nodeLayer.layer()
        # TODO: add import raster support!
        if layer != None:
            if layer.type() == QgsMapLayerType.VectorLayer:
                self.cboInputLayer.addItem(layer.name(), layer.id())

It could of course also be done by a try / expect or a check if layer is the right type of object.

@sickel sickel added the Bug Either a bug report, or a bug fix. Let's hope for the latter! label Jan 24, 2021
@gioman
Copy link
Contributor

gioman commented Jan 24, 2021

** Proposed fix **

The error message is (on my Windows 10 PC)
File "C:/PROGRA1/QGIS31.16/apps/qgis/./python/plugins\db_manager\dlg_import_vector.py", line 135, in populateLayers
if layer.type() == QgsMapLayerType.VectorLayer:
AttributeError: 'NoneType' object has no attribute 'type'

The present code is:
def populateLayers(self):
self.cboInputLayer.clear()
for nodeLayer in QgsProject.instance().layerTreeRoot().findLayers():
layer = nodeLayer.layer()

TODO: add import raster support!

if layer.type() == QgsMapLayerType.VectorLayer:
self.cboInputLayer.addItem(layer.name(), layer.id())

this could be rewritten as:

def populateLayers(self):
    self.cboInputLayer.clear()
    for nodeLayer in QgsProject.instance().layerTreeRoot().findLayers():
        layer = nodeLayer.layer()
        # TODO: add import raster support!
        if layer != None:
            if layer.type() == QgsMapLayerType.VectorLayer:
                self.cboInputLayer.addItem(layer.name(), layer.id())

It could of course also be done by a try / expect or a check if layer is the right type of object.

@sickel please make in the form of a pull request, thanks.

@gioman gioman added Feedback Waiting on the submitter for answers DB Manager Relating to the DB Manager core plugin labels Jan 24, 2021
sickel added a commit to sickel/QGIS that referenced this issue Jan 25, 2021
This change will make the db managers import function more robust towards (some types of) invalid layers. cf issue qgis#41152
sickel added a commit to sickel/QGIS that referenced this issue Jan 25, 2021
This change will make the db managers import function more robust towards (some types of) invalid layers. cf issue qgis#41152

Before trying to read the type of the layer when compling them for the drop down box, it checks if the layer value is of NoneType. This should not happen, but at least for some usage of the open layer plugin, a layer may end up as none type.

I have observed this issue in several earlier versions of qgis and the code has not been changed since at least 3.10 LTR, so it could, and probably should be backported to 3.10
nyalldawson pushed a commit that referenced this issue Jan 27, 2021
github-actions bot pushed a commit that referenced this issue Jan 27, 2021
nyalldawson pushed a commit that referenced this issue Jan 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! DB Manager Relating to the DB Manager core plugin Feedback Waiting on the submitter for answers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants