def makeVecAddAbundDict(setupObject, layerList, idFieldName, convFactor):
    addAbundDict = {}
    addFeatIDSet = set()
    decPrec = setupObject.decimalPlaces

    puLayer = QgsVectorLayer(setupObject.puPath, "Planning units", "ogr")

    layerNumber = 1
    for aLayer in layerList:
        layerGeomType = aLayer.geometryType()
        qgis.utils.iface.mainWindow().statusBar().showMessage("Intersecting layer " + str(layerNumber) + "...")

        dirPath = os.path.dirname(setupObject.puPath)
        intersectShapeFileNameNumber = cluz_setup.returnLowestUnusedFileNameNumber(dirPath, "temp_int", ".shp")
        tempIntersectLayer = dirPath + os.sep + "temp_int" + str(intersectShapeFileNameNumber) + ".shp"
        overlayAnalyzer = QgsOverlayAnalyzer()
        overlayAnalyzer.intersection(puLayer, aLayer, tempIntersectLayer)
        outputLayer = QgsVectorLayer(tempIntersectLayer, "Temp", "ogr")
        outputFeatures = outputLayer.getFeatures()

        outputIDField = outputLayer.fieldNameIndex('Unit_ID')
        outputFeatIDField = outputLayer.fieldNameIndex(idFieldName)

        if layerGeomType == 1:
            lineBool = True
            polyBool = False
        elif layerGeomType == 2:
            lineBool = False
            polyBool = True

        qgis.utils.iface.mainWindow().statusBar().showMessage("Calculating data from layer " + str(layerNumber) + "...")
        for outputFeature in outputFeatures:
            outputAttributes = outputFeature.attributes()
            unitID = outputAttributes[outputIDField]
            featID = outputAttributes[outputFeatIDField]
            addFeatIDSet.add(featID)

            #Produce intersect of lines
            if lineBool:
                finalShapeAmount = calcFeatLineLengthInPU(outputFeature, convFactor, decPrec)

            #Produce intersect of polygons
            if polyBool:
                finalShapeAmount = calcFeatPolygonAreaInPU(outputFeature, convFactor, decPrec)

            if finalShapeAmount > 0:
                try:
                    puAddAbundDict = addAbundDict[unitID]
                except KeyError:
                    puAddAbundDict = {}
                try:
                    addAmount = puAddAbundDict[featID]
                except KeyError:
                    addAmount = 0
                addAmount += finalShapeAmount
                puAddAbundDict[featID] = addAmount
                addAbundDict[unitID] = puAddAbundDict
        layerNumber += 1

    qgis.utils.iface.mainWindow().statusBar().showMessage("")
    addFeatIDList = list(addFeatIDSet)
    addFeatIDList.sort()
    return addAbundDict, addFeatIDList
Exemple #2
0
 def setInitialShapeFilePath(self, setupObject):
     dirPath = os.path.dirname(setupObject.puPath)
     distShapeFileNameNumber = cluz_setup.returnLowestUnusedFileNameNumber(dirPath, "cluz_dist", ".shp")
     distShapeFileFullPath = dirPath + os.sep + "cluz_dist" + str(distShapeFileNameNumber) + ".shp"
     self.filePathlineEdit.setText(distShapeFileFullPath)