Exemplo n.º 1
0
def createVrt(inventario, vrt):
    #Camada de inventario
    layer = processing.getObject(Inventario)
    
    count = 0
    size = layer.featureCount()
    p = 0
    progress.setPercentage(p)    
    rasterList = []
    for feature in layer.getFeatures():
        filename = feature['fileName']
        
        raster = QgsRasterLayer(filename, filename)
        if Override_CRS:
            raster.setCrs( QgsCoordinateReferenceSystem(int(CRS.split(':')[-1]), QgsCoordinateReferenceSystem.EpsgCrsId) )
           
        rasterList.append(raster)
        ovr = filename+'.ovr'
        if not os.path.isfile(ovr):
            progress.setText('Fazendo Pirâmides...')
            #('gdalogr:overviews', input, levels=8, clean=False, resampling_method=0(nearest), format=1(Gtiff .ovr))
            processing.runalg('gdalogr:overviews', raster, '4 8 32 128', True, 0, 1)

        if int(float(count)/size*100) != p:
            p = int(float(count)/size*100)
            progress.setPercentage(p)    
        count += 1
    progress.setText('Fazendo raster virtual...')
    processing.runalg('gdalogr:buildvirtualraster', rasterList, 0, False, False, VRT)
Exemplo n.º 2
0
def load(fileName, name=None, crs=None, style=None, isRaster=False):
    """
    Loads a layer/table into the current project, given its file.

    .. deprecated:: 3.0
    Do not use, will be removed in QGIS 4.0
    """

    from warnings import warn
    warn("processing.load is deprecated and will be removed in QGIS 4.0", DeprecationWarning)

    if fileName is None:
        return
    prjSetting = None
    settings = QgsSettings()
    if crs is not None:
        prjSetting = settings.value('/Projections/defaultBehavior')
        settings.setValue('/Projections/defaultBehavior', '')
    if name is None:
        name = os.path.split(fileName)[1]

    if isRaster:
        qgslayer = QgsRasterLayer(fileName, name)
        if qgslayer.isValid():
            if crs is not None and qgslayer.crs() is None:
                qgslayer.setCrs(crs, False)
            if style is None:
                style = ProcessingConfig.getSetting(ProcessingConfig.RASTER_STYLE)
            qgslayer.loadNamedStyle(style)
            QgsProject.instance().addMapLayers([qgslayer])
        else:
            if prjSetting:
                settings.setValue('/Projections/defaultBehavior', prjSetting)
            raise RuntimeError(QCoreApplication.translate('dataobject',
                                                          'Could not load layer: {0}\nCheck the processing framework log to look for errors.').format(
                fileName))
    else:
        qgslayer = QgsVectorLayer(fileName, name, 'ogr')
        if qgslayer.isValid():
            if crs is not None and qgslayer.crs() is None:
                qgslayer.setCrs(crs, False)
            if style is None:
                if qgslayer.geometryType() == QgsWkbTypes.PointGeometry:
                    style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POINT_STYLE)
                elif qgslayer.geometryType() == QgsWkbTypes.LineGeometry:
                    style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_LINE_STYLE)
                else:
                    style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POLYGON_STYLE)
            qgslayer.loadNamedStyle(style)
            QgsProject.instance().addMapLayers([qgslayer])

    if prjSetting:
        settings.setValue('/Projections/defaultBehavior', prjSetting)

    return qgslayer
Exemplo n.º 3
0
def load(fileName, name=None, crs=None, style=None, isRaster=False):
    """Loads a layer/table into the current project, given its file.
    """

    if fileName is None:
        return
    prjSetting = None
    settings = QSettings()
    if crs is not None:
        prjSetting = settings.value('/Projections/defaultBehaviour')
        settings.setValue('/Projections/defaultBehaviour', '')
    if name is None:
        name = os.path.split(fileName)[1]

    if isRaster:
        qgslayer = QgsRasterLayer(fileName, name)
        if qgslayer.isValid():
            if crs is not None and qgslayer.crs() is None:
                qgslayer.setCrs(crs, False)
            if style is None:
                style = ProcessingConfig.getSetting(ProcessingConfig.RASTER_STYLE)
            qgslayer.loadNamedStyle(style)
            QgsMapLayerRegistry.instance().addMapLayers([qgslayer])
            iface.legendInterface().refreshLayerSymbology(qgslayer)
        else:
            if prjSetting:
                settings.setValue('/Projections/defaultBehaviour', prjSetting)
            raise RuntimeError('Could not load layer: ' + unicode(fileName)
                               + '\nCheck the processing framework log to look for errors')
    else:
        qgslayer = QgsVectorLayer(fileName, name, 'ogr')
        if qgslayer.isValid():
            if crs is not None and qgslayer.crs() is None:
                qgslayer.setCrs(crs, False)
            if style is None:
                if qgslayer.geometryType() == QGis.Point:
                    style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POINT_STYLE)
                elif qgslayer.geometryType() == QGis.Line:
                    style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_LINE_STYLE)
                else:
                    style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POLYGON_STYLE)
            qgslayer.loadNamedStyle(style)
            QgsMapLayerRegistry.instance().addMapLayers([qgslayer])

    if prjSetting:
        settings.setValue('/Projections/defaultBehaviour', prjSetting)

    return qgslayer
Exemplo n.º 4
0
def load(fileName, name=None, crs=None, style=None, isRaster=False):
    """Loads a layer/table into the current project, given its file.
    """

    if fileName is None:
        return
    prjSetting = None
    settings = QgsSettings()
    if crs is not None:
        prjSetting = settings.value('/Projections/defaultBehavior')
        settings.setValue('/Projections/defaultBehavior', '')
    if name is None:
        name = os.path.split(fileName)[1]

    if isRaster:
        qgslayer = QgsRasterLayer(fileName, name)
        if qgslayer.isValid():
            if crs is not None and qgslayer.crs() is None:
                qgslayer.setCrs(crs, False)
            if style is None:
                style = ProcessingConfig.getSetting(ProcessingConfig.RASTER_STYLE)
            qgslayer.loadNamedStyle(style)
            QgsProject.instance().addMapLayers([qgslayer])
        else:
            if prjSetting:
                settings.setValue('/Projections/defaultBehavior', prjSetting)
            raise RuntimeError('Could not load layer: ' + str(fileName) +
                               '\nCheck the processing framework log to look for errors')
    else:
        qgslayer = QgsVectorLayer(fileName, name, 'ogr')
        if qgslayer.isValid():
            if crs is not None and qgslayer.crs() is None:
                qgslayer.setCrs(crs, False)
            if style is None:
                if qgslayer.geometryType() == QgsWkbTypes.PointGeometry:
                    style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POINT_STYLE)
                elif qgslayer.geometryType() == QgsWkbTypes.LineGeometry:
                    style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_LINE_STYLE)
                else:
                    style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POLYGON_STYLE)
            qgslayer.loadNamedStyle(style)
            QgsProject.instance().addMapLayers([qgslayer])

    if prjSetting:
        settings.setValue('/Projections/defaultBehavior', prjSetting)

    return qgslayer
Exemplo n.º 5
0
def create_default_project(available_maps, visible_maps, project_template, authcfg=None):
    """Create a default project from a template and return it as a string"""
    layers = []
    for m in available_maps:
        connstring = u'type=xyz&url=%(url)s'
        if authcfg is not None:
            connstring = u'authcfg=%(authcfg)s&' + connstring
        layer = QgsRasterLayer(connstring % {
            'url': quote(m['endpoint']),
            'authcfg': authcfg,
        }, m['name'], 'wms')
        # I've no idea why the following is required even if the crs is specified 
        # in the layer definition
        layer.setCrs(QgsCoordinateReferenceSystem('EPSG:3857'))
        layers.append(layer)
    if len(layers):
        xml = QgsMapLayer.asLayerDefinition(layers)
        maplayers = "\n".join(xml.toString().split("\n")[3:-3])
        layer_tree_layer = ""
        custom_order = ""
        legend_layer = ""
        layer_coordinate_transform = ""
        for layer in layers:
            is_visible = layer.name() in visible_maps
            values = {'name': layer.name(), 'id': layer.id(), 'visible': ('1' if is_visible else '0'), 'checked': ('Qt::Checked' if is_visible else 'Qt::Unchecked')}
            custom_order += "<item>%s</item>" % layer.id()
            layer_tree_layer += """
            <layer-tree-layer expanded="1" checked="%(checked)s" id="%(id)s" name="%(name)s">
                <customproperties/>
            </layer-tree-layer>""" % values
            legend_layer += """
            <legendlayer drawingOrder="-1" open="true" checked="%(checked)s" name="%(name)s" showFeatureCount="0">
              <filegroup open="true" hidden="false">
                <legendlayerfile isInOverview="0" layerid="%(id)s" visible="%(visible)s"/>
              </filegroup>
            </legendlayer>""" % values
            layer_coordinate_transform += '<layer_coordinate_transform destAuthId="EPSG:3857" srcAuthId="EPSG:3857" srcDatumTransform="-1" destDatumTransform="-1" layerid="%s"/>' % layer.id()
        tpl = ""
        with open(project_template, 'rb') as f:
            tpl = f.read()
        for tag in ['custom_order', 'layer_tree_layer', 'legend_layer', 'layer_coordinate_transform', 'maplayers']:
            tpl = tpl.replace("#%s#" % tag.upper(), locals()[tag])
        return tpl
    else:
        return None
def createDefaultProject(available_maps, visible_maps, project_template, authcfg=None):
    """Create a default project from a template and return it as a string"""
    layers = []
    for m in available_maps:
        connstring = u'type=xyz&url={url}'
        if authcfg is not None:
            connstring = u'authcfg={authcfg}&' + connstring
        layer = QgsRasterLayer(connstring.format(url=urllib2.quote("{}?version={}".format(m['endpoint'], pluginSetting("apiVersion"))),
                                                 authcfg=authcfg), m['name'], "wms")
        # I've no idea why the following is required even if the crs is specified
        # in the layer definition
        layer.setCrs(QgsCoordinateReferenceSystem('EPSG:3857'))
        layers.append(layer)
    if len(layers):
        xml = QgsMapLayer.asLayerDefinition(layers)
        maplayers = "\n".join(xml.toString().split("\n")[3:-3])
        layer_tree_layer = ""
        custom_order = ""
        legend_layer = ""
        layer_coordinate_transform = ""
        for layer in layers:
            is_visible = layer.name() in visible_maps
            values = {'name': layer.name(), 'id': layer.id(), 'visible': ('1' if is_visible else '0'), 'checked': ('Qt::Checked' if is_visible else 'Qt::Unchecked')}
            custom_order += "<item>%s</item>" % layer.id()
            layer_tree_layer += """
            <layer-tree-layer expanded="1" checked="%(checked)s" id="%(id)s" name="%(name)s">
                <customproperties/>
            </layer-tree-layer>""" % values
            legend_layer += """
            <legendlayer drawingOrder="-1" open="true" checked="%(checked)s" name="%(name)s" showFeatureCount="0">
              <filegroup open="true" hidden="false">
                <legendlayerfile isInOverview="0" layerid="%(id)s" visible="%(visible)s"/>
              </filegroup>
            </legendlayer>""" % values
            layer_coordinate_transform += '<layer_coordinate_transform destAuthId="EPSG:3857" srcAuthId="EPSG:3857" srcDatumTransform="-1" destDatumTransform="-1" layerid="%s"/>' % layer.id()
        tpl = ""
        with open(project_template, 'rb') as f:
            tpl = f.read()
        for tag in ['custom_order', 'layer_tree_layer', 'legend_layer', 'layer_coordinate_transform', 'maplayers']:
            tpl = tpl.replace("#%s#" % tag.upper(), locals()[tag])
        return tpl
    else:
        return None
Exemplo n.º 7
0
    def testRenderWithPainterClipRegions(self):
        raster_layer = QgsRasterLayer(
            os.path.join(TEST_DATA_DIR, 'rgb256x256.png'))
        self.assertTrue(raster_layer.isValid())
        raster_layer.setCrs(QgsCoordinateReferenceSystem('EPSG:3857'))

        mapsettings = QgsMapSettings()
        mapsettings.setOutputSize(QSize(400, 400))
        mapsettings.setOutputDpi(96)
        mapsettings.setDestinationCrs(
            QgsCoordinateReferenceSystem('EPSG:4326'))
        mapsettings.setExtent(
            QgsRectangle(0.0001451, -0.0001291, 0.0021493, -0.0021306))
        mapsettings.setLayers([raster_layer])

        region = QgsMapClippingRegion(
            QgsGeometry.fromWkt(
                'Polygon ((0.00131242078273144 -0.00059281669806561, 0.00066744230712249 -0.00110186995774045, 0.00065145110524788 -0.00152830200772984, 0.00141369839460392 -0.00189076925022083, 0.00210931567614912 -0.00094195793899443, 0.00169354442740946 -0.00067810310806349, 0.00131242078273144 -0.00059281669806561))'
            ))
        region.setFeatureClip(
            QgsMapClippingRegion.FeatureClippingType.ClipPainterOnly)
        region2 = QgsMapClippingRegion(
            QgsGeometry.fromWkt(
                'Polygon ((0.00067010750743492 -0.0007740503193111, 0.00064612070462302 -0.00151764120648011, 0.00153629760897587 -0.00158693641460339, 0.0014909892036645 -0.00063812510337699, 0.00106722235398754 -0.00055816909400397, 0.00067010750743492 -0.0007740503193111))'
            ))
        region2.setFeatureClip(
            QgsMapClippingRegion.FeatureClippingType.ClipToIntersection)
        mapsettings.addClippingRegion(region)
        mapsettings.addClippingRegion(region2)

        renderchecker = QgsMultiRenderChecker()
        renderchecker.setMapSettings(mapsettings)
        renderchecker.setControlPathPrefix('rasterlayerrenderer')
        renderchecker.setControlName('expected_painterclip_region')
        result = renderchecker.runTest('expected_painterclip_region')
        self.report += renderchecker.report()
        self.assertTrue(result)
Exemplo n.º 8
0
def load(fileName, name=None, crs=None, style=None, isRaster=False):
    """
    Loads a layer/table into the current project, given its file.

    .. deprecated:: 3.0
    Do not use, will be removed in QGIS 4.0
    """

    from warnings import warn
    warn("processing.load is deprecated and will be removed in QGIS 4.0",
         DeprecationWarning)

    if fileName is None:
        return
    prjSetting = None
    settings = QgsSettings()
    if crs is not None:
        prjSetting = settings.value('/Projections/defaultBehavior')
        settings.setValue('/Projections/defaultBehavior', '')
    if name is None:
        name = os.path.split(fileName)[1]

    if isRaster:
        qgslayer = QgsRasterLayer(fileName, name)
        if qgslayer.isValid():
            if crs is not None and qgslayer.crs() is None:
                qgslayer.setCrs(crs, False)
            if style is None:
                style = ProcessingConfig.getSetting(
                    ProcessingConfig.RASTER_STYLE)
            qgslayer.loadNamedStyle(style)
            QgsProject.instance().addMapLayers([qgslayer])
        else:
            if prjSetting:
                settings.setValue('/Projections/defaultBehavior', prjSetting)
            raise RuntimeError(
                QCoreApplication.translate(
                    'dataobject',
                    'Could not load layer: {0}\nCheck the processing framework log to look for errors.'
                ).format(fileName))
    else:
        qgslayer = QgsVectorLayer(fileName, name, 'ogr')
        if qgslayer.isValid():
            if crs is not None and qgslayer.crs() is None:
                qgslayer.setCrs(crs, False)
            if style is None:
                if qgslayer.geometryType() == QgsWkbTypes.PointGeometry:
                    style = ProcessingConfig.getSetting(
                        ProcessingConfig.VECTOR_POINT_STYLE)
                elif qgslayer.geometryType() == QgsWkbTypes.LineGeometry:
                    style = ProcessingConfig.getSetting(
                        ProcessingConfig.VECTOR_LINE_STYLE)
                else:
                    style = ProcessingConfig.getSetting(
                        ProcessingConfig.VECTOR_POLYGON_STYLE)
            qgslayer.loadNamedStyle(style)
            QgsProject.instance().addMapLayers([qgslayer])

    if prjSetting:
        settings.setValue('/Projections/defaultBehavior', prjSetting)

    return qgslayer
Exemplo n.º 9
0
    def calculate_zensus(self, gemeinden, default_kk_index, base_kk):
        '''
        return the centroids of the zensus cells as points inside the
        given area
        '''
        self.log('Extrahiere Siedlungszellen aus Zensusdaten...')
        epsg = self.project.settings.EPSG

        bbox = get_bbox(gemeinden.table)

        zensus_file = os.path.join(self.project.basedata.base_path,
                                   self.project.settings.ZENSUS_500_FILE)

        clipped_raster, raster_epsg = clip_raster(zensus_file, bbox)

        raster_layer = QgsRasterLayer(clipped_raster)
        # workaround QGIS 3.10.2+: does not set crs correctly when reading
        # raster file
        crs = QgsCoordinateReferenceSystem()
        crs.createFromSrid(raster_epsg)
        raster_layer.setCrs(crs)

        parameters = {
            'INPUT_RASTER': raster_layer,
            'RASTER_BAND': 1,
            'FIELD_NAME': 'value',
            'OUTPUT': 'memory:'
        }
        point_layer = processing.run('native:pixelstopoints',
                                     parameters)['OUTPUT']
        point_layer.setSubsetString('value>0')

        overlay = create_layer(gemeinden,
                               'Polygon',
                               fields=['ags'],
                               name='overlay',
                               epsg=epsg,
                               target_epsg=raster_layer.crs().postgisSrid())

        #clip_tmp = tempfile.NamedTemporaryFile(suffix='.gpkg').name
        parameters = {
            'INPUT': point_layer,
            'OVERLAY': overlay,
            'OUTPUT': 'memory:'
        }
        clipped_layer = processing.run('native:clip', parameters)['OUTPUT']

        parameters = {
            'INPUT': clipped_layer,
            'OVERLAY': overlay,
            'OUTPUT': 'memory:'
        }
        clipped_w_ags = processing.run('native:intersection',
                                       parameters)['OUTPUT']

        self.log('Schreibe Siedlungszellen in Datenbank...')
        tr = QgsCoordinateTransform(
            clipped_w_ags.crs(), QgsCoordinateReferenceSystem(f'epsg:{epsg}'),
            QgsProject.instance())

        ags_list = [g.ags for g in gemeinden]
        gem_acc = dict([(a, [0, 0]) for a in ags_list])

        for feature in clipped_w_ags.getFeatures():
            ew = feature.attribute('VALUE')
            if ew <= 0:
                continue
            # for some reason all geometries are MultiPoint with length 1
            geom = feature.geometry()
            geom.transform(tr)
            point = geom.asMultiPoint()[0]
            ags = feature.attribute('ags')
            # take default kk_index only atm (there is a table (KK2015) with
            # indices in the basedata though)
            kk_index = default_kk_index
            kk = ew * base_kk * kk_index / 100
            self.cells.add(ew=ew,
                           kk_index=kk_index,
                           kk=kk,
                           id_teilflaeche=-1,
                           in_auswahl=True,
                           geom=point,
                           ags=ags)
            # accumulate einwohner and kaufkraft
            acc = gem_acc[ags]
            acc[0] += ew
            acc[1] += kk

        for ags, (ew, kk) in gem_acc.items():
            gem = self.centers.get(ags=ags)
            gem.ew = ew
            gem.kk = kk
            gem.save()
Exemplo n.º 10
0
    def runAlgorithm(self):
        start = timeit.default_timer()
        self._id = str(np.random.randint(1, 5000))
        logMessageFile = open(
            os.path.dirname(__file__) + "/logLCP" + self._id + ".txt", "w")

        logMessage = "LCPNetwork plugin init - loading points and base raster layers with id: " + self._id
        QgsMessageLog.logMessage(logMessage, tag="LCPNetwork", level=Qgis.Info)
        logMessageFile.write(logMessage + "\n")

        origins, destinations = self.loadPoints()
        baseRaster = self.loadBaseRaster()

        logMessage = "computing " + str(
            origins.featureCount()) + " origin points towards " + str(
                destinations.featureCount()) + " destinations"
        QgsMessageLog.logMessage(logMessage, tag="LCPNetwork", level=Qgis.Info)
        logMessageFile.write(logMessage + "\n")

        self.transform = baseRaster.GetGeoTransform()
        self.projection = baseRaster.GetProjection()

        nodata = baseRaster.GetRasterBand(1).GetNoDataValue()

        logMessage = "loading cost map with nodata value " + str(nodata)
        QgsMessageLog.logMessage(logMessage, tag="LCPNetwork", level=Qgis.Info)
        logMessageFile.write(logMessage + "\n")

        topLeft = QgsPointXY(self.transform[0], self.transform[3])

        pointsListO = []
        for point in origins.getFeatures():
            pointsListO.append(point.geometry().asPoint())

        pointsListD = []
        for point in destinations.getFeatures():
            pointsListD.append(point.geometry().asPoint())

        ## create the list of lcps
        lcps = []

        numThreads = os.cpu_count()
        logMessage = "creating " + str(numThreads) + " threads"
        QgsMessageLog.logMessage(logMessage, tag="LCPNetwork", level=Qgis.Info)
        logMessageFile.write(logMessage + "\n")

        pool = futures.ThreadPoolExecutor(numThreads)
        index = 0
        results = []
        for source in pointsListO:
            results.append(
                pool.submit(self.computeOnePath, source, index, start,
                            baseRaster, pointsListD, lcps, logMessageFile))
            index = index + 1

        for future in futures.as_completed(results):
            logMessageFile.write(future.result() + "\n")

        logMessage = "all lcps computed at time: " + str(
            "%.2f" % (timeit.default_timer() - start))
        QgsMessageLog.logMessage(logMessage, tag="LCPNetwork", level=Qgis.Info)
        logMessageFile.write(logMessage + "\n")

        # same CRS than base raster cost map
        crs = QgsCoordinateReferenceSystem()
        crs.createFromString(baseRaster.GetProjection())

        for i in range(index):
            outputName = os.path.dirname(
                __file__) + "/distances" + self._id + "_" + str(i) + ".tif"
            newRasterQGIS = QgsRasterLayer(
                outputName, "distances" + self._id + "_" + str(i))
            newRasterQGIS.setContrastEnhancement(
                QgsContrastEnhancement.StretchToMinimumMaximum)
            newRasterQGIS.setCrs(crs)
            QgsProject.instance().addMapLayer(newRasterQGIS)

        # add the list of lcps to the network layer
        network = self.iface.addVectorLayer("LineString?crs=" + crs.authid(),
                                            "least cost path network",
                                            "memory")
        network.dataProvider().addFeatures(lcps)

        logMessage = "LCPNetwork plugin finished! time (sec.): " + str(
            "%.2f" % (timeit.default_timer() - start))
        QgsMessageLog.logMessage(logMessage, tag="LCPNetwork", level=Qgis.Info)
        logMessageFile.write(logMessage + "\n")
        logMessageFile.close()
Exemplo n.º 11
0
    def process(self, data):

        # data = {upd/npd: {dating = [calendar years BP, ...], uncert = [calendar years, ...], coords = [[x, y], ...], accur = [accuracy, ...]}}

        UPD_t_ds = np.round(data["upd"]["dating"]).astype(
            int
        )  # mean datings of archaeological components (calendar years BP)
        UPD_uncert_ds = np.round(data["upd"]["uncert"]).astype(
            int)  # uncertainties of the datings (calendar years)
        UPD_As = np.round(data["upd"]["coords"]).astype(
            int)  # spatial coordinates of archaeological components (metres)
        UPD_accurs = np.round(data["upd"]["accur"]).astype(
            int
        )  # accuracies of spatial coordinates of archaeological components (+-metres)

        NPD_t_ds = np.round(data["npd"]["dating"]).astype(
            int
        )  # measured radiocarbon ages of archaeological components (radiocarbon years)
        NPD_uncert_ds = np.round(data["npd"]["uncert"]).astype(
            int
        )  # 1-sigma uncertainties of the measured radiocarbon ages (radiocarbon years)
        NPD_As = np.round(data["npd"]["coords"]).astype(
            int)  # spatial coordinates of archaeological components (metres)
        NPD_accurs = np.round(data["npd"]["accur"]).astype(
            int
        )  # accuracies of spatial coordinates of archaeological components (+-metres)

        if (not UPD_t_ds.size) and (not NPD_t_ds.size):
            return

        s_halflife = self.s_duration / 2  # expected half-life of a settlement in years
        s_radius = self.s_diameter / 2  # expected radius of a settlement in metres

        # temporal extent
        t_min = np.inf
        t_max = -np.inf
        if UPD_t_ds.size:
            t_min = min(t_min,
                        (UPD_t_ds - UPD_uncert_ds).min() - self.s_duration)
            t_max = max(t_max,
                        (UPD_t_ds + UPD_uncert_ds).max() + self.s_duration)
        if NPD_t_ds.size:
            t_min = min(t_min,
                        (NPD_t_ds - 2 * NPD_uncert_ds).min() - self.s_duration)
            t_max = max(t_max,
                        (NPD_t_ds + 2 * NPD_uncert_ds).max() + self.s_duration)
        t_min, t_max = [
            int(round(value / 10) * 10) for value in [t_min, t_max]
        ]

        if self.time_from is not None:
            t_max = min(t_max, self.time_from)
        if self.time_to is not None:
            t_min = max(t_min, self.time_to)

        ts_slices = np.arange(t_max, t_min - 2 * self.time_step,
                              -self.time_step).tolist()  # times of time slices

        # prepare lookup for probability distributions of 14C datings
        self.setLabelText("Calibrating radiocarbon dates")
        cal_curve = load_curve(
            os.path.join(os.path.dirname(__file__), "intcal13.14c")
        )  # [[CalBP, ConvBP, CalSigma], ...], sorted by CalBP

        # filter calibration curve to include only time-step dates
        cal_curve = cal_curve[(cal_curve[:, 0] >= t_min)
                              & (cal_curve[:, 0] <= t_max)][::-1]
        ts = cal_curve[:, 0]
        curve_conv_age = cal_curve[:, 1]
        curve_uncert = cal_curve[:, 2]
        if ts[-1] < ts_slices[-1]:
            ts_slices.append(ts[-1])

        # calculate probability distributions for all combinations of 14c age and uncertainty
        unique_dates = set()  # ((age, uncert), ...)
        for idx in range(NPD_t_ds.shape[0]):
            unique_dates.add((NPD_t_ds[idx], NPD_uncert_ds[idx]))
        lookup_14c = defaultdict(
            dict
        )  # {age: {uncert: D, ...}, ...}; D[ti] = p; where ti = index in ts, p = probability
        cmax = len(unique_dates)
        cnt = 0
        for age, uncert in unique_dates:
            QtWidgets.QApplication.processEvents()
            if not self.running:
                return
            self.setValue((cnt / cmax) * 100)
            cnt += 1
            lookup_14c[age][uncert] = calibrate(age, uncert, curve_conv_age,
                                                curve_uncert)

        # prepare lookup of spatial probability distribution around evidence points
        self.setLabelText("Calculating spatial probability distribution")
        self.setValue(0)
        accurs = set()
        accurs.update(UPD_accurs.tolist())
        accurs.update(NPD_accurs.tolist())
        lookup_f_s = {
        }  # {accur: M, ...}; M[n, n] = f_s(d, accur, s_radius); where center is point A and n is 2 * [maximum distance from A in raster units] + 1; where f_s > 0
        cnt = 0
        cmax = len(accurs)
        for accur in accurs:
            QtWidgets.QApplication.processEvents()
            if not self.running:
                return
            self.setValue((cnt / cmax) * 100)
            cnt += 1
            r = int(round((accur + 2 * s_radius) / self.cell_size))
            n = 2 * r + 1
            lookup_f_s[accur] = np.zeros((n, n), dtype=float)
            rcs = np.argwhere(np.ones((n, n), dtype=bool))
            mask = (rcs > r).all(axis=1)
            for row, col in rcs[mask]:
                d = (((row - r)**2 + (col - r)**2)**0.5) * self.cell_size
                if self.approximate:
                    p = f_s_approx(d, accur, s_radius)
                else:
                    p = f_S_lens(d, accur, s_radius) / f_S(accur, s_radius)
                if (p == np.inf) or np.isnan(p):
                    p = 0
                lookup_f_s[accur][row, col] = p
                lookup_f_s[accur][n - row, col] = p
                lookup_f_s[accur][row, n - col] = p
                lookup_f_s[accur][n - row, n - col] = p
            lookup_f_s[accur][0, 0] = lookup_f_s[accur][0, 1]

        # spatial extent
        row_min, col_min = np.inf, np.inf
        row_max, col_max = -np.inf, -np.inf
        for As, accurs in [[UPD_As, UPD_accurs], [NPD_As, NPD_accurs]]:
            for idx in range(As.shape[0]):
                A = As[idx]
                accur = accurs[idx]
                r = int(lookup_f_s[accur].shape[0] / 2)
                col, row = np.round(A / self.cell_size).astype(int)
                row_min = min(row_min, row - r - 1)
                col_min = min(col_min, col - r - 1)
                row_max = max(row_max, row + r)
                col_max = max(col_max, col + r)
        width, height = (col_max - col_min), (row_max - row_min)
        x0, y0 = col_min * self.cell_size, row_min * self.cell_size

        # calculate time-slices
        self.setLabelText("Generating time-slices")
        paths = []
        summed = []
        val_max = -np.inf
        grid_summed = np.zeros((height, width), dtype=float)
        t_slice_prev = ts_slices.pop(0)
        t_slice = ts_slices.pop(0)
        n_slice = 1
        for ti in range(ts.shape[0]):
            QtWidgets.QApplication.processEvents()
            if not self.running:
                return
            self.setValue((ti / ts.shape[0]) * 100)

            grid = np.ones((height, width), dtype=float)

            for idx in range(UPD_t_ds.shape[0]):
                t_d = UPD_t_ds[idx]
                uncert_d = UPD_uncert_ds[idx]
                A = UPD_As[idx]
                accur = UPD_accurs[idx]
                M = 1 - lookup_f_s[accur] * f_t_UPD(ts[ti], t_d, uncert_d,
                                                    s_halflife)
                r = int((M.shape[0] - 1) / 2)
                col0, row0 = np.round((A - [x0, y0]) / self.cell_size - r -
                                      1).astype(int)
                grid[row0:row0 + M.shape[0], col0:col0 + M.shape[0]] *= M

            for idx in range(NPD_t_ds.shape[0]):
                t_d = NPD_t_ds[idx]
                uncert_d = NPD_uncert_ds[idx]
                A = NPD_As[idx]
                accur = NPD_accurs[idx]
                M = 1 - lookup_f_s[accur] * f_t_NPD(
                    ts[ti], s_halflife, lookup_14c[t_d][uncert_d], ts)
                r = int((M.shape[0] - 1) / 2)
                col0, row0 = np.round((A - [x0, y0]) / self.cell_size - r -
                                      1).astype(int)
                grid[row0:row0 + M.shape[0], col0:col0 + M.shape[0]] *= M

            grid = 1 - grid
            grid[np.isnan(grid)] = 0
            grid[grid == np.inf] = 0

            summed.append(grid.sum())

            if ts[ti] <= t_slice:
                val_max = max(val_max, grid_summed.max())
                t_ce, cebce = bp_to_ce(t_slice_prev)
                t_ce2, cebce2 = bp_to_ce(t_slice)
                datestr = "%03d_%d_%s_-_%d_%s" % (n_slice, t_ce, cebce, t_ce2,
                                                  cebce2)
                paths.append([
                    datestr,
                    os.path.join(self.path_layers, "ede_%s.tif" % (datestr))
                ])
                self.save_raster(grid_summed, x0, y0, paths[-1][1])
                t_slice_prev = t_slice
                t_slice = ts_slices.pop(0)
                n_slice += 1
                grid_summed[:] = grid
            else:
                grid_summed += grid

        if self.path_summed:
            self.save_summed(ts, summed)

        project = QgsProject.instance()
        val_max = val_max * 0.9
        self.setLabelText("Rendering time-slices")
        cnt = 0
        cmax = len(paths)
        for datestr, path in paths:
            QtWidgets.QApplication.processEvents()
            if not self.running:
                return
            self.setValue((cnt / cmax) * 100)
            cnt += 1
            layer = QgsRasterLayer(path, "EDE_%s" % (datestr))
            layer.setCrs(self.crs)
            s = QgsRasterShader()
            c = QgsColorRampShader()
            c.setColorRampType(QgsColorRampShader.Interpolated)
            i = []
            i.append(QgsColorRampShader.ColorRampItem(0, self.colors[0]))
            i.append(
                QgsColorRampShader.ColorRampItem(val_max / 2, self.colors[1]))
            i.append(QgsColorRampShader.ColorRampItem(val_max, self.colors[2]))
            c.setColorRampItemList(i)
            s.setRasterShaderFunction(c)
            ps = QgsSingleBandPseudoColorRenderer(layer.dataProvider(), 1, s)
            ps.setClassificationMin(0)
            ps.setClassificationMax(val_max)
            layer.setRenderer(ps)

            self.save_rendered(
                layer, os.path.join(self.path_rendered, "%s.tif" % (datestr)))

            project.addMapLayer(layer)
Exemplo n.º 12
0
canvas.show()

vlayer = QgsVectorLayer(
    os.getenv("TEST_DATA", "testdata/Australia_Airports.geojson"),
    "Airports layer",
    "ogr",
)
if not vlayer.isValid():
    print("Vector layer failed to load!")

ecw_file = os.getenv("TEST_ECW", "testdata/64002.ecw")
ecw_layer = QgsRasterLayer(ecw_file, "Canberra 100K Map", "gdal")

if ecw_layer.isValid():
    ecw_layer.setCrs(
        QgsCoordinateReferenceSystem("EPSG:28355")
    )
    QgsProject.instance().addMapLayer(ecw_layer)
else:
    print("ECW layer failed to load!")

urlWithParams = "type=xyz&url=https://a.tile.openstreetmap.org/%7Bz%7D/%7Bx%7D/%7By%7D.png&zmax=19&zmin=0&crs=EPSG3857"
rlayer = QgsRasterLayer(urlWithParams, "OpenStreetMap", "wms")

if rlayer.isValid():
    QgsProject.instance().addMapLayer(rlayer)
else:
    print("XYZ layer failed to load!")

# set extent to the extent of our layer
canvas.setExtent(vlayer.extent())
Exemplo n.º 13
0
    try:
        dataOut = tempfile.mktemp('.tif')
    except Exception, e:
        raise Exception(
            'Problem creating temporary file to store raster data: ' + str(e))
    # TODO: Work out if the response is an XML error
    update.emit({'progress': 10, 'message': 'Downloading file...'})

    urllib.urlretrieve(bigURL, dataOut)

    # Load data as QgsRasterLayer and then re-save it, ensuring it has the correct projection info
    a = QgsRasterLayer(dataOut, "temporary raster layer")
    # Double-confirm projection info (sometimes WCS has it in the meta but not the file)
    crs = a.crs()
    crs.createFromId(int(srs.split(':')[1]))
    a.setCrs(crs)
    update.emit({'progress': 90, 'message': 'Saving file...'})
    writer = QgsRasterFileWriter(output_file)

    pipe = QgsRasterPipe()
    width, height = a.width(), a.height()
    extent = a.extent()
    provider = a.dataProvider()
    pipe.set(provider.clone())
    writer.writeRaster(pipe, width, height, extent, a.crs())
    a = None
    pipe = None
    provider = None
    # Delete temp file
    os.remove(dataOut)
    return output_file
Exemplo n.º 14
0
    def select_HDF5_file(self):
        """Run method that performs all the real work"""
        # show the dialog
        #self.dlg.show()
        # Run the dialog event loop
        result = self.dlg.exec_()
        # close the dialog
        self.dlg.close()
        
        # See if OK was pressed
        if result:
            
        #### to open windows browser and search for HDF5 files only:
            myfileNames = QFileDialog.getOpenFileNames(self.dlg,
                                                       self.tr("HDF5 File Selector"), 
                                                       "", 
                                                       self.tr("HDF5 (*.hdf5 *.h5)"))
        
            #### to loop through all selected HDF5 files, just handling one for now
            for i in myfileNames:
                #### bind the HDF5 file
                
                myfile = QgsRasterLayer(i)
                
                #### to deal with multiple datasets within file
                if (len(myfile.subLayers()) > 1):
                    #### to open dataset with desired data and name it Reflectance
                    #mydset = QgsRasterLayer(myfile.subLayers()[0], 'Reflectance')
                    
                    fileName = myfile.subLayers()[0]
                    fileInfo = QFileInfo(fileName)
                    baseName = fileInfo.baseName()
                    mydset = QgsRasterLayer(fileName, baseName)
                    
                    ##################################### Trials of setting proper extent begin here
                    
                    print 'Extent before: '
                    print mydset.extent().toString()
                    
                    ########################## test almost worked
                    ### loaded layer in right place but without data
                    #rect = QgsRectangle(326380.0,4103390.0-2853,326380.0+1332,4103390.0)
                    #mydset.setExtent(rect)
                    ####################################
                    
                    ######################## test almost worked
                    ### loaded layer in right place but without data
                    #size = QSizeF(1,1)
                    #point = QPointF(326380.0, 4103390.0)
                    #rect = QRectF(point, size)
                    #rect = QgsRectangle(rect)
                    #mydset.setExtent(rect)
                    ###################################
                    
                    ########################## did not change extent
                    #rect = QgsRectangle(326380.0,4103390.0-2853,326380.0+1332,4103390.0)
                    #context = QgsRenderContext()
                    #context.setExtent(rect)
                    #mydset.draw(context)
                    ####################################
                    
                    #################################### did not change extent
                    #size = QSizeF(1,1)
                    #point = QPointF(326380.0, 4103390.0)
                    #rect = QRectF(point, size)
                    #rect = QgsRectangle(rect)
                    #context = QgsRenderContext()
                    #context.setExtent(rect)
                    #mydset.draw(context)
                    #####################################
                    
                    ###################################### did not change extent
                    #rect = QgsRectangle(326380.0,4103390.0-2853,326380.0+1332,4103390.0)
                    #context = QgsRenderContext()
                    #context.setExtent(rect)
                    #print 'Context extent: '
                    #print context.extent().toString() #this printed correct extent
                    #mydset.createMapRenderer(context)
                    #######################################
                    
                    ######################## test almost worked
                    ### loaded layer in right place but without data
                    #rect = QgsRectangle(326380.0,4103390.0-2853,326380.0+1332,4103390.0)
                    #provider = mydset.dataProvider().clone()
                    #mydset.setExtent(rect)
                    #provider.reloadData()
                    ######################################
                    
                    ########################################
                    # did not change extent, loaded data
                    #rect = QgsRectangle(326380.0,4103390.0-2853,326380.0+1332,4103390.0)
                    #context = QgsRenderContext()
                    #context.setExtent(rect)
                    #renderer = mydset.createMapRenderer(context)
                    #renderer.render()
                    #########################################
                    
                    ###########################################
                    # did not change extent, loaded data
                    #rect = QgsRectangle(326380.0,4103390.0-2853,326380.0+1332,4103390.0)
                    #mydset.dataProvider().block(52, rect, 1332, 2853)
                    ##########################################
                    
                    #############################################
                    # did not change extent, loaded data
                    #rect = QgsRectangle(326380.0,4103390.0-2853,326380.0+1332,4103390.0)
                    #mydset.dataProvider().block(426, rect, 1332, 2853)
                    ###############################################
                    
                    #################################################
                    # changed extent but overwrote to no data
                    #rect = QgsRectangle(326380.0,4103390.0-2853,326380.0+1332,4103390.0)
                    #mydset.setExtent(rect)
                    #mydset.dataProvider().reload()
                    ####################################################
                    
                    ####################################################
                    # changed extent but overwrote to no data
                    #rect = QgsRectangle(326380.0,4103390.0-2853,326380.0+1332,4103390.0)
                    #mydset.setExtent(rect)
                    #mydset.dataProvider().block(52, rect, 1332, 2853)
                    #mydset.dataProvider().reload()
                    #####################################################
                    
                    #############################################
                    # not setting new cloned data provider correctly
                    # method wants QString object but gets meaningless string
                    #rect = QgsRectangle(326380.0,4103390.0-2853,326380.0+1332,4103390.0)
                    #provider = mydset.dataProvider()
                    #print 'Provider: '
                    #print provider
                    #clone = provider.clone()
                    #print 'Provider clone: '
                    #print clone
                    #clone.block(1, rect, 1332, 2853)
                    #mydset.setDataProvider(str(clone))
                    #print 'New provider: '
                    #print mydset.dataProvider()
                    ##############################################
                    
                    ######## printing result of extent changes
                    print 'Extent after: '
                    print mydset.extent().toString()
                    ###################################
                    
                    ####################################### Trials of setting proper extent end here
                    
                    #### to set proper Coordinate Reference System (crs) info
                    crs = mydset.crs()
                    crs.createFromId(32611)
                    mydset.setCrs(crs)
                    
                    ### this was josh's recommendation
                    #mycrs = QgsCoordinateReferenceSystem(32611)
                    #self.iface.mapCanvas().mapRenderer().setDestinationCrs(mycrs)

                    #### to set raster bands to load as RGB
                    mydset.renderer().setGreenBand(34)
                    mydset.renderer().setRedBand(52)
                    mydset.renderer().setBlueBand(18)
                    
                    ####### Tristan changes
                    
                    #myrenderer = mydset.renderer()
                    #print 'Renderer Type: '
                    #print mydset.renderer().type()
                    
                   # print 'Renderer block before: '
                    #print myrenderer.block(426, rect, 1, 1)
                    #myrenderer.block(426, rect, 1, 1)
                    #print 'Renderer block after: '
                    #print myrenderer.block() not enough arguments
                    
                    #if hasattr(mydset, "setCacheImage"):
                        #mydset.setCacheImage(None)
                        #mydset.triggerRepaint()
                        
                    #mydset.dataProvider().reloadData()
                    #mydset.triggerRepaint()
                    
                    #self.iface.legendInterface().refreshLayerSymbology(mydset)
                    
                    #mydset.reload()
                    #mydset.reloadData()
                    #mydset.triggerRepaint()
                    #mydset.draw()
                    #print 'Extent: '
                    #print mydset.extent().toString()
                    
                   # ident = rlayer.dataProvider().identify(QgsPoint(15.30, 40.98), \
                   # QgsRaster.IdentifyFormatValue)
                  #  if ident.isValid():
                 #   print ident.results()
                    
                    ####### End Tristan changes
                    
                    #### to add selected dataset/raster to map canvas
                    QgsMapLayerRegistry.instance().addMapLayer(mydset)
                    #canvas=QgsMapCanvas()
                    #canvas.show()
                    #canvas.setExtent(mydset.extent())
                    #canvas.setLayerSet([QgsMapCanvasLayer(mydset)])
                    
                    ####################################### Script for getting all raster values
                    # takes 11 minutes to run on (426,1332,2583)
                    #list = []
                    #for x in range(mydset.width()):
                        #x_coord = x
                        
                        #print 'Doing ' +str(x) + 'x right now...'
                        #for y in range(mydset.height()):
                            #y_coord = y
                            #ident = mydset.dataProvider().identify(QgsPoint(x_coord, y_coord), \
                                                           #QgsRaster.IdentifyFormatValue)
                            #list.append(ident.results().values())
                            
                    
                    #print 'Length of list is: '
                    #print len(list)
                    #print 'Length of list[0] is: '
                    #print len(list[0])
                    
                    ############################################    
                        
                        
                    #if ident.isValid():
                        #print ident.results()
                    
                        
Exemplo n.º 15
0
def layerSubsetSave(rasterLayer: QgsRasterLayer, domainLayer: QgsVectorLayer,
                    subsetName: str) -> None:
    """Description: Processes a raster image into a vector polygon ocean/land mask.
		Make sure to save the shapefile, as it will be deleted otherwise! 
		Input:  QgsRasterLayer rasterLayer - layer that contains the raster image to process
				QgsVectorLayer domainLayer - layer that contains a polygon specifying the bounds of the raster image to process
				string name - output file name.
		Output: QgsRasterLayer, QgsVectorLayer - objects referencing the new mask layers
	"""

    # Get basic file name information on geotiff, raster image, masked raster subset image, and masked vector subset shp file
    fileSource = rasterLayer.source()
    fileInfo = QFileInfo(fileSource)
    fileName = fileInfo.baseName()
    savePaths = getSavePaths(fileSource, domainLayer, 'tif')
    subsetPath = savePaths + '/' + subsetName + '.tif'

    # Load geotiff and get domain layer/bounding box of area to mask
    geotiff = gdal.Open(fileSource)
    feature = domainLayer.getFeature(0)
    domain = feature.geometry().boundingBox()
    prj = geotiff.GetProjection()
    srs = osr.SpatialReference(wkt=prj)
    if srs.GetAttrValue("PROJCS|AUTHORITY", 1) is not None:
        epsgCode = srs.GetAttrValue("PROJCS|AUTHORITY", 1)
    elif srs.GetAttrValue("AUTHORITY", 1) is not None:
        epsgCode = srs.GetAttrValue("AUTHORITY", 1)
    else:
        epsgCode = str(32621)
    rasterCRS = "EPSG:" + epsgCode

    crs = rasterLayer.crs()
    crs.createFromId(int(epsgCode))
    rasterLayer.setCrs(crs)
    rasterLayer.triggerRepaint()

    #rasterCRS = rasterLayer.crs().authid()
    domainCRS = domainLayer.crs().authid()
    bounds = geotiffWorldToPixelCoords(geotiff, domain, rasterCRS, domainCRS)

    band = geotiff.GetRasterBand(1)
    img_full = band.ReadAsArray(0, 0, geotiff.RasterXSize, geotiff.RasterYSize)
    img = img_full[int(round(bounds.yMinimum())):int(round(bounds.yMaximum())),
                   int(round(bounds.xMinimum())):int(round(bounds.xMaximum()))]
    print('bounds', bounds.yMinimum(), bounds.yMaximum(), bounds.xMinimum(),
          bounds.xMaximum())
    print('img.shape', img.shape, 'img_full.shape', img_full.shape)
    print("img min/max/mean:", img.min(), img.max(), np.mean(img, axis=(0, 1)))
    img = (img.astype(np.float32) / img.max() * 65535).astype(np.uint16)
    print("after img min/max/mean:", img.min(), img.max(),
          np.mean(img, axis=(0, 1)))

    # print('Save subset:', subsetPath, resolve('landsat_raw/' + domainLayer.name() + '/' + subsetName + '.png'))
    if not DRY_RUN:
        arrayToRaster(img, geotiff, bounds, subsetPath)
        imsave(
            resolve('landsat_raw/' + domainLayer.name() + '/' + subsetName +
                    '.png'), img)
        # imsave(resolve('small/' + domainLayer.name() + '/' + subsetName + '.png'), img)
        # imsave(os.path.join(r'D:\Daniel\Documents\Github\CALFIN Repo\reprocessing\images_1024', domainLayer.name(), subsetName + '.png'), img)
    try:
        #Gather BQA info
        fileSourceBQA = fileSource[:-7] + '_BQA.TIF'
        #Save BQA subset
        geotiffBQA = gdal.Open(fileSourceBQA)
        imgBQA = geotiffBQA.GetRasterBand(1)
        imgBQA = imgBQA.ReadAsArray(0, 0, geotiffBQA.RasterXSize,
                                    geotiffBQA.RasterYSize).astype(np.uint16)
        imgBQA = imgBQA[
            int(round(bounds.yMinimum())):int(round(bounds.yMaximum())),
            int(round(bounds.xMinimum())):int(round(bounds.xMaximum()))]
        # print('Save BQA subset:', subsetPathBQA, resolve('landsat_raw/' + domainLayer.name() + '/' + subsetName + '_bqa.png'))
        if not DRY_RUN:
            # arrayToRaster(imgBQA, geotiffBQA, bounds, subsetPathBQA)
            # print(fileSourceBQA, geotiffBQA.RasterXSize, geotiffBQA.RasterYSize)
            # print(int(round(bounds.yMinimum())), int(round(bounds.yMaximum())), int(round(bounds.xMinimum())), int(round(bounds.xMaximum())))
            # imsave(resolve('landsat_raw/' + domainLayer.name() + '/' + subsetName + '_bqa.png'), imgBQA)
            pass

        #Gather MTL info
        fileSourceMTL = fileSource[:-7] + '_MTL.txt'
        #Save MTL subset
        if not DRY_RUN:
            image_feats = [''] * 6
            with open(fileSourceMTL, 'r') as image_feats_source_file:
                lines = image_feats_source_file.readlines()
                for line in lines:
                    if 'SUN_AZIMUTH =' in line:
                        image_feats[0] = line.strip()
                    elif 'SUN_ELEVATION =' in line:
                        image_feats[1] = line.strip()
                    elif 'CLOUD_COVER ' in line:
                        image_feats[2] = line.strip()
                    elif 'CLOUD_COVER_LAND ' in line:
                        image_feats[3] = line.strip()
                    elif 'DATE_ACQUIRED =' in line:
                        image_feats[4] = line.strip()
                    elif 'GRID_CELL_SIZE_REFLECTIVE =' in line:
                        image_feats[5] = line.strip()
            savePath = resolve('landsat_raw/' + domainLayer.name() + '/' +
                               subsetName + '_mtl.txt')
            with open(savePath, 'w') as image_feats_dest_file:
                for line in image_feats:
                    image_feats_dest_file.write(str(line) + '\n')
    except:
        print('No BQA/MTL found for:', subsetName)

    return img.shape
def addToDefaultProject(maps, visibleMaps, authcfg=None):
    """Add basemaps to the existing default project"""
    layers = []
    for m in maps:
        connstring = u'type=xyz&url={url}'
        if authcfg is not None:
            connstring = u'authcfg={authcfg}&' + connstring
        layer = QgsRasterLayer(connstring.format(url=urllib2.quote("{}?version={}".format(m['endpoint'], pluginSetting("apiVersion"))),
                                                 authcfg=authcfg), m['name'], 'wms')
        # I've no idea why the following is required even if the crs is specified
        # in the layer definition
        layer.setCrs(QgsCoordinateReferenceSystem('EPSG:3857'))
        layers.append(layer)

    if os.path.isfile(defaultProjectPath()):
        backup = defaultProjectPath().replace(
            '.qgs', '-%s.qgs' % datetime.now().strftime('%Y-%m-%d-%H_%M_%S'))
        shutil.copy2(defaultProjectPath(), backup)

   # open default project
    with open(defaultProjectPath()) as f:
        content = f.read()

    doc = QDomDocument()
    setOk, errorString, errorLine, errorColumn = doc.setContent(content)
    if not setOk:
        return False

    root = doc.documentElement()

    for layer in layers:
        is_visible = layer.name() in visibleMaps
        xml = QgsMapLayer.asLayerDefinition([layer])
        r = xml.documentElement()
        mapLayerElement = r.firstChildElement("maplayers").firstChildElement("maplayer")

        layerTreeLayerElement = doc.createElement("layer-tree-layer")
        layerTreeLayerElement.setAttribute("expanded", "1")
        layerTreeLayerElement.setAttribute("checked", "Qt::Checked" if is_visible else "Qt::Unchecked")
        layerTreeLayerElement.setAttribute("id", layer.id())
        layerTreeLayerElement.setAttribute("name", layer.name())

        customPropertiesElement = doc.createElement("customproperties")
        layerTreeLayerElement.appendChild(customPropertiesElement)

        legendLayerElement = doc.createElement("legendlayer")
        legendLayerElement.setAttribute("drawingOrder", "-1")
        legendLayerElement.setAttribute("open", "true")
        legendLayerElement.setAttribute("checked", "Qt::Checked" if is_visible else "Qt::Unchecked")
        legendLayerElement.setAttribute("name", layer.name())
        legendLayerElement.setAttribute("showFeatureCount", "0")

        filegroupElement = doc.createElement("filegroup")
        filegroupElement.setAttribute("open", "true")
        filegroupElement.setAttribute("hidden", "false")

        legendlayerfileElement = doc.createElement("legendlayerfile")
        legendlayerfileElement.setAttribute("isInOverview", "0")
        legendlayerfileElement.setAttribute("layerid", layer.id())
        legendlayerfileElement.setAttribute("visible", "1" if is_visible else "0")

        filegroupElement.appendChild(legendlayerfileElement)
        legendLayerElement.appendChild(filegroupElement)

        crsElement = doc.createElement("layer_coordinate_transform")
        crsElement.setAttribute("destAuthId", "EPSG:3857")
        crsElement.setAttribute("srcAuthId", "EPSG:3857")
        crsElement.setAttribute("srcDatumTransform", "-1")
        crsElement.setAttribute("destDatumTransform", "-1")
        crsElement.setAttribute("layerid", layer.id())

        itemElement = doc.createElement("item")
        text = doc.createTextNode(layer.id())
        itemElement.appendChild(text)

        e = root.firstChildElement("layer-tree-group")
        e.appendChild(layerTreeLayerElement)

        e = root.firstChildElement("mapcanvas").firstChildElement("layer_coordinate_transform_info")
        e.appendChild(crsElement)

        e = root.firstChildElement("layer-tree-canvas").firstChildElement("custom-order")
        e.appendChild(itemElement)

        e = root.firstChildElement("legend")
        e.appendChild(legendLayerElement)

        e = root.firstChildElement("projectlayers")
        e.appendChild(mapLayerElement)

    with open(defaultProjectPath(), "wb+") as f:
        f.write(doc.toString(2))

    settings = QSettings()
    settings.setValue('/qgis/newProjectDefault', True)
    return True