TransWikia.com

PyQGIS doesn't load user libraries properly

Geographic Information Systems Asked by GlenS on January 25, 2021

I’m trying to put my PyQGIS functions in separate .py files and load them from my main script.

from qgis.core import QgsProject

def getVectorLayerGeo():
    
    project = QgsProject.instance()
    layers = project.mapLayersByName('Yalcowinna')
    layer = layers[0]
    features=layer.getFeatures()

    coords = list()
    
    for feature in features:
        geom = feature.geometry()
        geomSingleType = QgsWkbTypes.isSingleType(geom.wkbType())
        if geom.type() == QgsWkbTypes.PointGeometry:
            if geomSingleType:
                x = geom.asPoint()
                print("Point: ", x)
            else:
                x = geom.asMultiPoint()
                print("MultiPoint: ", x)
        elif geom.type() == QgsWkbTypes.PolygonGeometry:
            if geomSingleType:
                x = geom.asPolygon()
                print("Polygon: ", x, "Area: ", geom.area())
            else:
                x = geom.asMultiPolygon()
                print("MultiPolygon: ", x, "Area: ", geom.area())
        else:
            print("Unknown or invalid geometry")

    for coord in x[0][0]:
        coords.append([coord.x(),coord.y()])
        
    return coords
    
print(getVectorLayerGeo())

does exactly what it’s supposed to, which is print a list of coordinates (as lists).

Then I try to call getVectorLayerGeo from another script:

import ee
from ee_plugin import Map
from qgis.core import QgsProject
from get_layer_coords import getVectorLayerGeo

vizParams = {'bands' : ['B4', 'B3', 'B2'], 'min':0, 'max':10000}
geom = ee.Geometry.Polygon(getVectorLayerGeom())
S2 = ee.ImageCollection("COPERNICUS/S2_SR")
image = S2.filterBounds(geo).first()

Map.addLayer(image, vizParams, 'Sentinel-2')
Map.centerObject(image, 10)

and I get

Traceback (most recent call last):
  File "C:PROGRA~1QGIS 3.16appsPython37libcode.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 4, in <module>
  File "C:/PROGRA~1/QGIS 3.16/apps/qgis/./pythonqgisutils.py", line 792, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
  File "C:/Users/Glen Shennan/Google Drive/African Lovegrass/ShannFlatget_layer_coords.py", line 56, in <module>
    print(getVectorLayerGeo())
  File "C:/Users/Glen Shennan/Google Drive/African Lovegrass/ShannFlatget_layer_coords.py", line 18, in getVectorLayerGeo
    geomSingleType = QgsWkbTypes.isSingleType(geom.wkbType())
NameError: name 'QgsWkbTypes' is not defined

The thing is, when I copy getVectorLayerGeo into my main script it works as intended. Obviously I have the solution for this case but I’m intending to write a lot of functions and I don’t want to put them all in one giant script.

QgsWkbTypes is part of qgis.core, but when I import qgis.core at the start of get_layer_coords.py script it doesn’t seem to help. Am I doing something wrong or does QGIS not like calling functions from other .py files?

One Answer

You haven't imported QgsWkbTypes anywhere in the module. Try:

from qgis.core import QgsProject, QgsWkbTypes

Correct answer by user2856 on January 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP