Summarizing my problem, I'm a beginner and I'm developing a plugin that will be able to do several spatial joins. For this, it will have to separate the spatial from the non-spatial layers and do the spatial join between them. I've a list of layer names from this code:
layerList = QgsProject.instance().layerTreeRoot().findLayers()
nlist = []
for layer in layerList:
nlist.append(layer.name())
hence I do the geometry test:
layer = iface.activeLayer()
features = layer.getFeatures()
for feature in features:
# the geometry type can be of single or multi type
geom = feature.geometry()
if geom.type() == QgsWkbTypes.PointGeometry:
print("point")
elif geom.type() == QgsWkbTypes.LineGeometry:
print("line")
elif geom.type() == QgsWkbTypes.PolygonGeometry:
print("area")
elif geom.type() == QgsWkbTypes.UnknownGeometry:
print("table")
and finally I do the spatial join:
root = QgsProject.instance().layerTreeRoot()
ids = root.findLayerIds()
target = QgsProject.instance().mapLayer(ids[1])
print('target: ', target.id())
layerToJoin = QgsProject.instance().mapLayer(ids[0])
print('layerToJoin: ', layerToJoin.id())
fieldToJoin = QgsProject.instance()
symb = QgsVectorLayerJoinInfo()
symb.setJoinFieldName('id_feature')
symb.setTargetFieldName('id')
symb.setJoinLayerId(layerToJoin.id())
symb.setUsingMemoryCache(True)
symb.setEditable(True)
symb.setDynamicFormEnabled(True)
symb.setUpsertOnEdit(True)
symb.setPrefix('')
symb.setJoinFieldNamesSubset('')
symb.setJoinLayer(layerToJoin)
target.addJoin(symb)
Separately, each code block works fine, but I need to put all together and I doesn't figure how. First issue the QgsProject doesn't have a geometry method, so I can't iterate in the layer list and test the geometry, that way I can't direct a vector layer and postgresql table to do the spatial join. If anyone has an idea or a better way to do this please help!!!