def removeDir(path):
    result = ""
    if not QFile(path).exists():
        result = QCoreApplication.translate("QgsPluginInstaller", "Nothing to remove! Plugin directory doesn't exist:") + "\n" + path
    elif QFile(path).remove():  # if it is only link, just remove it without resolving.
        pass
    else:
        fltr = QDir.Dirs | QDir.Files | QDir.Hidden
        iterator = QDirIterator(path, fltr, QDirIterator.Subdirectories)
        while iterator.hasNext():
            item = iterator.next()
            if QFile(item).remove():
                pass
        fltr = QDir.Dirs | QDir.Hidden
        iterator = QDirIterator(path, fltr, QDirIterator.Subdirectories)
        while iterator.hasNext():
            item = iterator.next()
            if QDir().rmpath(item):
                pass
    if QFile(path).exists():
        result = QCoreApplication.translate("QgsPluginInstaller", "Failed to remove the directory:") + "\n" + path + "\n" + QCoreApplication.translate("QgsPluginInstaller", "Check permissions or remove it manually")
    # restore plugin directory if removed by QDir().rmpath()
    pluginDir = qgis.utils.home_plugin_path
    if not QDir(pluginDir).exists():
        QDir().mkpath(pluginDir)
    return result
Exemple #2
0
def removeDir(path):
    result = ""
    if not QFile(path).exists():
        result = QCoreApplication.translate(
            "QgsPluginInstaller",
            "Nothing to remove! Plugin directory doesn't exist:") + "\n" + path
    elif QFile(path).remove(
    ):  # if it is only link, just remove it without resolving.
        pass
    else:
        fltr = QDir.Dirs | QDir.Files | QDir.Hidden
        iterator = QDirIterator(path, fltr, QDirIterator.Subdirectories)
        while iterator.hasNext():
            item = iterator.next()
            if QFile(item).remove():
                pass
        fltr = QDir.Dirs | QDir.Hidden
        iterator = QDirIterator(path, fltr, QDirIterator.Subdirectories)
        while iterator.hasNext():
            item = iterator.next()
            if QDir().rmpath(item):
                pass
    if QFile(path).exists():
        result = QCoreApplication.translate(
            "QgsPluginInstaller", "Failed to remove the directory:"
        ) + "\n" + path + "\n" + QCoreApplication.translate(
            "QgsPluginInstaller", "Check permissions or remove it manually")
    # restore plugin directory if removed by QDir().rmpath()
    pluginDir = qgis.utils.home_plugin_path
    if not QDir(pluginDir).exists():
        QDir().mkpath(pluginDir)
    return result
Exemple #3
0
    def setInfoImages(self,
                      pathRoot,
                      hasSubdirectories,
                      filterNames=None,
                      reverse=False):
        def addValidImage(pathfile):
            def getSizesBoundingBox(ds):
                def getBBox(sizePixel, sizeRaster, rotation):
                    xMin, yMax = coefs[0], coefs[3]
                    xMax = xMin + (sizeRaster['pixelsX'] *
                                   sizePixel['resolutionX']) + (
                                       sizeRaster['pixelsY'] * rotation['x'])
                    yMin = yMax - (
                        (sizeRaster['pixelsY'] * sizePixel['resolutionY']) +
                        (sizeRaster['pixelsX'] * rotation['y']))
                    rect = QgsRectangle(xMin, yMin, xMax, yMax)
                    return rect.asWktPolygon()

                # coefs: 0 = ulX, 1 = resolutionX, 2 = rotationX, 3 = ulY, 4 = rotationY, 5 = resolutionY(Negative)
                coefs = ds.GetGeoTransform()
                sizes = {
                    'pixel': {
                        'resolutionX': coefs[1],
                        'resolutionY': -1 * coefs[5]
                    },
                    'raster': {
                        'pixelsX': ds.RasterXSize,
                        'pixelsY': ds.RasterYSize
                    }
                }
                rotation = {'x': coefs[2], 'y': coefs[4]}
                bbox = getBBox(sizes['pixel'], sizes['raster'], rotation)
                return sizes, bbox

            def getBands(ds):
                total = ds.RasterCount
                bands = []
                for idx in range(ds.RasterCount):
                    band = ds.GetRasterBand(idx + 1)
                    info = "B{}({})".format(
                        idx + 1, gdal.GetDataTypeName(band.DataType))
                    bands.append(info)
                return {'total': total, 'types': ",".join(bands)}

            # File is image
            try:
                ds = gdal.Open(pathfile, gdal.GA_ReadOnly)
            except RuntimeError:
                return
            # Has Projection and Bands
            wkt = ds.GetProjectionRef()
            if len(wkt) == 0 or ds.RasterCount == 0:
                return
            # Add image
            sizes, bbox = getSizesBoundingBox(ds)
            item = {
                'pathfile': pathfile,
                'name': QFileInfo(pathfile).completeBaseName(),
                'crs': self.getAuthority(wkt),
                'bands': getBands(ds),
                'sizes': sizes,
                'bbox_wkt': bbox,
                'drive': ds.GetDriver().GetDescription(),
                'nodata': ds.GetRasterBand(1).GetNoDataValue()
            }
            self.infoImages.append(item)
            ds = None  #close

        del self.infoImages[:]
        filters = QDir.Files
        flags = QDirIterator.Subdirectories if hasSubdirectories else QDirIterator.NoIteratorFlags

        iter = QDirIterator(pathRoot, [], filters, flags)
        if reverse and not filterNames is None:
            while iter.hasNext():
                pathfile = iter.next()
                pathfileUpper = QFileInfo(pathfile).fileName().upper()
                if any(name.upper() in pathfileUpper for name in filterNames):
                    continue
                addValidImage(pathfile)
                if self.feedback.isCanceled():
                    break
        else:
            if not filterNames is None:
                fn = ["*{}*".format(n) for n in filterNames]
                iter = QDirIterator(pathRoot, fn, filters, flags)
            while iter.hasNext():
                pathfile = iter.next()
                addValidImage(pathfile)
                if self.feedback.isCanceled():
                    break