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)
def run_script(iface):

	#Load DEM terrain data footprint for getting file refs
	#(made in createVectorBritishNationalGridRefLayer.py)
	footprint = QgsVectorLayer(
		'C:/Data/MapPolygons/Generated/NationalGrid5kmSquares_for_OSterrain/NationalGrid5kmSquares_for_OSterrain.shp','footprint','ogr')
	print(footprint.isValid())
	
	#load buffer created in QGIS
	buffshp = QgsVectorLayer('C:/Data/PointsOfInterest_OS/qgis/15kmSubBuffer2.shp','buffer','ogr')
	print(buffshp.isValid())

	#should only be the one feature...
	print('one?:' + str(buffshp.featureCount()))

	#get the one feature!
	buffr = buffshp.getFeatures().next()

	#First we need the list of filenames from the buffer intersect with the footprint file
	squares = [feature for feature in footprint.getFeatures() if feature.geometry().intersects(buffr.geometry())]

	print(len(squares))#68. Not much bigger than the bog standard 41 but we wouldn't want much larger.

	listOfFiles = [
				'C:/Data/Terrain5_OS_DEM_Scotland/Zips/allRasterFilesShared/' +
				(square.attributes()[4].encode('ascii','ignore') + 
				'.asc') 
				for square in squares]

	filename = ('C:/Data/WindFarmViewShed/ViewShedJava/SimpleViewShed/rasters/testbigraster2.vrt')

	processing.runalg('gdalogr:buildvirtualraster', listOfFiles, 0, False, False, filename)
		def writeVirtualRaster(filenametif):

			# First we need the list of filenames from the buffer intersect with the footprint file
			squares = [feature for feature in footprint.getFeatures() 
				if feature.geometry().intersects(bigBuff.geometry())
				and feature.attributes()[4] != NULL]

			#This is where the filename/grid ref is.
			print('Number of DEM squares in this buffer: ' + str(len(squares)))

			#Create virtual raster from the list of filenames
			#Note, this is unicode: print(type(squares[0].attributes()[4]))
			#convert unicode to string
			#http://stackoverflow.com/questions/1207457/convert-a-unicode-string-to-a-string-in-python-containing-extra-symbols
			listOfFiles = [
				'C:/Data/Terrain5_OS_DEM_Scotland/Zips/allRasterFilesShared/' +
				(square.attributes()[4].encode('ascii','ignore') + 
				'.asc') 
				for square in squares]

			#print "list of DEM files we're gonna attempt to get:"

			# for file in listOfFiles:
			# 	print file

			#Use turbine feature ID as reference
			#And add 
			# filename = ('ViewShedJava/SimpleViewShed/data/rasters/' +
			# str(buffr.id()) + 
			# '.vrt')

			#Oh good - can take lists too!
			#processing.runalg('gdalogr:buildvirtualraster', listOfFiles, 0, False, False, filename)
			#Output direct to merged tif
			processing.runalg('gdalogr:merge', listOfFiles, False, False, 5, filenametif)
Beispiel #4
0
def createVrt(vrt):
    """
    Creates a VRT file
    """
    count = 0
    size = len(vrt.keys())
    p = 0
    progress.setPercentage(p)    
    for key in vrt.keys():
        vrtfilename = os.path.join(Pasta, key, key+'.vrt')
        features = vrt[key]
        rasterList = []
        for feat in features:
            filename = feat['fileName']
            newfilename = copyFileSet(Pasta, key, filename)
            raster = QgsRasterLayer(newfilename, newfilename)
            rasterList.append(raster)
            ovr = newfilename+'.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, vrtfilename)
		def writeVirtualRaster():

			# First we need the list of filenames from the buffer intersect with the footprint file
			squares = [feature for feature in footprint.getFeatures() if feature.geometry().intersects(buffr)]

			#This is where the filename/grid ref is.
			#print(len(squares))#41! Just checked by eye, that's correct.

			#Create virtual raster from the list of filenames
			#Note, this is unicode: print(type(squares[0].attributes()[4]))
			#convert unicode to string
			#http://stackoverflow.com/questions/1207457/convert-a-unicode-string-to-a-string-in-python-containing-extra-symbols
			listOfFiles = [
				'C:/Data/Terrain5_OS_DEM_Scotland/Zips/allRasterFilesShared/' +
				(square.attributes()[4].encode('ascii','ignore') + 
				'.asc') 
				for square in squares]

			#Use turbine feature ID as reference
			filename = ('C:/Data/WindFarmViewShed/ViewshedPython/VirtualRasters/' +
			str(single.id()) + 
			'.vrt')

			#Oh good - can take lists too!
			processing.runalg('gdalogr:buildvirtualraster', listOfFiles, 0, False, False, filename)
    def addGrid(self):
	layer = self.iface.activeLayer()
	if layer is None:
		self.iface.messageBar().pushMessage("Error",
			"You must select a feature",
			level=QgsMessageBar.CRITICAL,
			duration=3)
		return
	
	if len(layer.selectedFeatures()) < 1:
		self.iface.messageBar().pushMessage("Error",
			"You must select a feature",
			level=QgsMessageBar.CRITICAL,
			duration=3)
	elif len(layer.selectedFeatures()) > 1:
		self.iface.messageBar().pushMessage("Error",
			"You must select only one feature",
			level=QgsMessageBar.CRITICAL,
			duration=3)
	else:
		feature = layer.selectedFeatures()[0]
		bbox = feature.geometry().boundingBox()
		extent = "%d,%d,%d,%d" % (bbox.xMinimum(), bbox.xMaximum(),
			bbox.yMinimum(), bbox.yMaximum())
		
		processing.runalg('qgis:creategrid', 1, extent,
			0.00025, 0.00025,'EPSG:4326','/tmp/tmp.shp')
    def dem_to_slopes(self):
        self.communications.show_message("info", "Transformation du DEM en Slope")

        self.slope = self.output_path + os.sep + "slope.tif"

        # Utilisation de l'algorithme de QGIS
        processing.runalg("gdalogr:slope", self.input_dem, 1, False, False, False, 1, self.slope)
        self.get_slopes_data()
    def dem_to_aspect(self):
        self.communications.show_message("info", "Calcul de l'orientation des pentes")

        self.aspect = self.output_path + os.sep + "aspect.tif"

        # Utilisation de l'algorithme de QGIS
        processing.runalg("gdalogr:aspect", self.input_dem, 1, False, False, False, False, self.aspect)
        self.get_aspect_data()
Beispiel #9
0
    def onDialogAccepted(self):

        if self.shapeLayerBox.currentText() == '' or \
           self.immobIdBox.currentText() == '' or \
           self.mobIdBox.currentText() == '' or \
           self.folderEdit.text() == '':
            self.mainWindow.messageBar().pushMessage(self.name,
                                                'Insufficient number of arguments!',
                                                  QgsMessageBar.WARNING, 5.0)
            return

        # clip shapefile if necessary
        if self.clipperBox.isEnabled():
            clippedShapeFile = os.path.join(self.folderEdit.text(), DamageRasterExport.CLIPPED_INPUT_TMP)
            clipLayer = self.clipperBox.itemData(self.clipperBox.currentIndex())
            if not self.checkLayerPath(clipLayer):
                QtGui.QMessageBox.warning(self.mainWindow, self.name, 'The clipping layer path cannot be converted to ascii.\nClipping with this layer will fail!')
            processing.runalg('qgis:clip', clipLayer, self.shapeLayer, clippedShapeFile)
            self.shapeLayer = QgsVectorLayer(clippedShapeFile, 'input_layer', 'ogr')
            if not self.shapeLayer.isValid():
                QgsMessageLog.logMessage('Could not load temporary shape file %s' % clippedShapeFile,
                                       level=QgsMessageLog.WARNING)
                os.remove(clippedShapeFile)
                return
            QgsMessageLog.logMessage('Finished clipping damage shape file and saved result at %s' % clippedShapeFile,
                                     level=QgsMessageLog.INFO)

        # rasterize shape file
        if not self.rasterizeAndConvert('ecn_immob', 4):
            self.removeTemporaryFiles(True)
            return
        if not self.rasterizeAndConvert('ecn_mob', 4):
            self.removeTemporaryFiles(True)
            return

        if self.immobStockBox.isEnabled():
            if not self.rasterizeAndConvert('ecn_immob_stock'):
                self.removeTemporaryFiles(True)
                return
        if self.mobStockBox.isEnabled():
            if not self.rasterizeAndConvert('ecn_mob_stock'):
                self.removeTemporaryFiles(True)
                return
        if self.popCatBox.isEnabled():
            if not self.rasterizeAndConvert('pop_dam_category', 4):
                self.removeTemporaryFiles(True)
        if self.popDensBox.isEnabled():
            if not self.rasterizeAndConvert('pop_density'):
                self.removeTemporaryFiles(True)
                return

        # clean up
        self.removeTemporaryFiles()
        # and inform user
        self.mainWindow.messageBar().pushMessage(self.name, 'Export has finished successfully!', QgsMessageBar.INFO, 5.0)

        self.finished.emit()
Beispiel #10
0
	def run(self):
		"""Run method that performs all the real work"""
		# update and show the dialog
		self.dlg.__init__(iface)
		self.dlg.show()
		# Run the dialog event loop
		result = self.dlg.exec_()
		# See if OK was pressed
		if result:
			inputLayer = str(self.dlg.cbo1.currentText())
			overLayer = str(self.dlg.cbo2.currentText())
			#clipLower and clipUpper are used to iterate throught shapes
			clipLower = 0
			clipUpper = 1
			#baseFilename is also project dependant and should be input by the user if this script is fully implemented
			baseFilename = str(self.dlg.outShp.text())[:-4]
			outputFilename = None
			#finds two layers by name
			for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
					if lyr.name() == overLayer:
							overLayer = lyr
							break

			for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
					if lyr.name() == inputLayer:
							inputLayer = lyr
							break

			#creates lists of feature IDs for given layers
			iface.setActiveLayer(overLayer)
			it = overLayer.getFeatures( QgsFeatureRequest( ) )
			overLayerIds = [i.id() for i in it]
			it = inputLayer.getFeatures( QgsFeatureRequest() )
			inputLayerIds = [i.id() for i in it]

			#selects all of the input feature and one feature from the clip layer
			inputLayer.setSelectedFeatures( inputLayerIds )

			for i in overLayerIds:
				overLayer.setSelectedFeatures( overLayerIds[clipLower:clipUpper] )
				outputFilename = (baseFilename + `clipUpper` + ".shp")
				processing.runalg("qgis:clip", inputLayer, overLayer, outputFilename)
				print i
				clipUpper += 1
				clipLower += 1
				time.sleep(2)

			#Clears map canvas selection
			mc = iface.mapCanvas()
			for layer in mc.layers():
				if layer.type() == layer.VectorLayer:
					layer.removeSelection()
			mc.refresh()
			# Do something useful here - delete the line containing pass and
			# substitute with your code.
def run_script(iface):

	start = time.time()

	print(os.chdir('C:/Data/temp/QGIS'))
	#print(processing.alglist('line'))

	#Get all shapefile names
	matches = []

	#Get folder names with shapefiles in
	for root, dirnames, filenames in os.walk('linesOfSightShapefiles/singleSales'):
		for filename in fnmatch.filter(filenames, '*.shp'):
			matches.append(filename)

	print ("Total line shapefiles: " + str(len(matches)))

	#matches = matches[2450:2452]

	for match in matches:
		print match
		#print match.split(".")[0]

	######
	# GET THE TWO POLYGON LAYERS TO INTERSECT LINES OF SIGHT WITH
	mastermapGrid = QgsVectorLayer(
		'C:/Data/WindFarmViewShed/QGIS/ReportOutputs/BH_mastermap_grid.shp',
		'mastermapGrid','ogr')
	print(mastermapGrid.isValid())

	CEDA_convexHulls = QgsVectorLayer(
		'C:/Data/BuildingHeight_CEDA/ConvexHulls/dissolve_Intersect_w_GRID_convexHullsOverLayerID.shp',
		'mastermapGrid','ogr')
	print(CEDA_convexHulls.isValid())

	#cycle over line files
	for match in matches:

		before = time.time()

		#Get pre-formed line shapefile from R
		linez = QgsVectorLayer(
			('C:/Data/temp/QGIS/linesOfSightShapefiles/singleSales/' + match),
			'mastermapGrid','ogr')
		print(linez.isValid())

		#See if lines cross any areas where there could be building height data
		processing.runalg('saga:linepolygonintersection', linez, mastermapGrid, 1, ("C:/Data/temp/QGIS/linesOfSightIntersects_mastermap/singleSales/" + match.split(".")[0] + ".csv"))
		processing.runalg('saga:linepolygonintersection', linez, CEDA_convexHulls, 1, ("C:/Data/temp/QGIS/linesOfSightIntersects_CEDA/singleSales/" + match.split(".")[0] + ".csv"))

		print(('saved : ' + match + ", ") + str(time.time() - before) + " seconds. " + str((time.time() - start)/60) + " mins total.")


	print("Total time: " + str((time.time() - start)/60) + " mins")
Beispiel #12
0
def segmentation( segThresh, filteredPath, tempDir, plotCode ):
    
    progress.setInfo( '\nSegmenting image' )
    segName = '{0}_seg.tif'.format( plotCode)
    segPath = os.path.join( tempDir, segName)
    progress.setInfo( 'Writing to {0}'.format(segPath) )
    
    #runs watershed segmenation on image
    processing.runalg('saga:watershedsegmentation',  filteredPath, 1, 1, 1, segThresh, True, True, segPath, None, None)
    
    progress.setInfo( 'Image segmented' )
    
    return segPath
Beispiel #13
0
def exportLayers(iface, layers, folder, precision, optimize, popupField, json):
    srcCrs = iface.mapCanvas().mapSettings().destinationCrs()
    epsg4326 = QgsCoordinateReferenceSystem("EPSG:4326")
    layersFolder = os.path.join(folder, "layers")
    QDir().mkpath(layersFolder)
    reducePrecision = (
        re.compile(r"([0-9]+\.[0-9]{%s})([0-9]+)" % unicode(int(precision))))
    for layer, encode2json, popup in zip(layers, json, popupField):
        if (layer.type() == layer.VectorLayer and
                (layer.providerType() != "WFS" or encode2json)):
            layer = writeTmpLayer(layer, popup)

            tmpPath = os.path.join(layersFolder,
                                   safeName(layer.name()) + ".json")
            path = os.path.join(layersFolder, safeName(layer.name()) + ".js")
            QgsVectorFileWriter.writeAsVectorFormat(layer, tmpPath, "utf-8",
                                                    epsg4326, 'GeoJson')
            with open(path, "w") as f:
                f.write("var %s = " % ("geojson_" + safeName(layer.name())))
                with open(tmpPath, "r") as f2:
                    for line in f2:
                        line = reducePrecision.sub(r"\1", line)
                        if optimize:
                            line = line.strip("\n\t ")
                            line = removeSpaces(line)
                        f.write(line)
            os.remove(tmpPath)
        elif layer.type() == layer.RasterLayer:
            name_ts = safeName(layer.name()) + unicode(time.time())
            in_raster = unicode(layer.dataProvider().dataSourceUri())
            prov_raster = os.path.join(tempfile.gettempdir(),
                                       'json_' + name_ts + '_prov.tif')
            out_raster = os.path.join(layersFolder,
                                      safeName(layer.name()) + ".png")
            crsSrc = layer.crs()
            crsDest = QgsCoordinateReferenceSystem(3857)
            xform = QgsCoordinateTransform(crsSrc, crsDest)
            extentRep = xform.transform(layer.extent())
            extentRepNew = ','.join([unicode(extentRep.xMinimum()),
                                     unicode(extentRep.xMaximum()),
                                     unicode(extentRep.yMinimum()),
                                     unicode(extentRep.yMaximum())])
            processing.runalg("gdalogr:warpreproject", in_raster,
                              layer.crs().authid(), "EPSG:3857", "", 0, 1,
                              0, -1, 75, 6, 1, False, 0, False, "",
                              prov_raster)
            processing.runalg("gdalogr:translate", prov_raster, 100,
                              True, "", 0, "", extentRepNew, False, 0,
                              0, 75, 6, 1, False, 0, False, "",
                              out_raster)
Beispiel #14
0
def juntoshapes(busca,salida):
    files=glob.glob(busca)
    out=os.path.join(carpeta,salida+".shp")
    entrada=";".join(files)
    if len(files)>100:
        lista1=files[:len(files)/2]
        lista2=files[len(files)/2:]
        out=os.path.join(carpeta,salida+"1.shp")
        entrada=";".join(lista1)
        processing.runalg('saga:mergelayers',entrada,True,True,out)
        out=os.path.join(carpeta,salida+"2.shp")
        entrada=";".join(lista2)
        processing.runalg('saga:mergelayers',entrada,True,True,out)
    elif len(files) >1 and len(files) <=100:
        processing.runalg('saga:mergelayers',entrada,True,True,out)
    elif len(files) ==1:
        layer2=QgsVectorLayer(files[0],"entrada","ogr")
        QgsMapLayerRegistry.instance().addMapLayers([layer2])
        selection = layer2.getFeatures(QgsFeatureRequest().setFilterExpression(u'"DN" > 0'))
        selecionado = layer2.setSelectedFeatures([s.id() for s in selection])
        processing.runalg("qgis:saveselectedfeatures",files[0],out)
    else:
        pass
    del(out)
    del(entrada)
    del(files)
Beispiel #15
0
def closeFilter( notopoLayer, tempDir, plotCode ):
    
    #generate filtered file path
    #and process closing filter
    
    progress.setInfo( '\nPerforming closing filter' )
    
    filteredName = '{0}_Filtered.tif'.format( plotCode)
    filteredPath = os.path.join( tempDir, filteredName)
    progress.setInfo( 'Writing to {0}'.format(filteredPath) )
    
    processing.runalg('saga:morphologicalfilter', notopoLayer, 1,  2, 3, filteredPath)
    
    return filteredPath    
Beispiel #16
0
    def importFileOrLayer(self, source, schema, tablename, overwrite, singleGeom = False):

        if overwrite:
            if isinstance(source, basestring):
                layerName = QtCore.QFileInfo(source).completeBaseName()
            else:
                layerName = source.name()

            if tablename is None:
                tablename = layerName

            if isinstance(source, basestring):
                layer = QgsVectorLayer(source, layerName, "ogr")
                if not layer.isValid() or layer.type() != QgsMapLayer.VectorLayer:
                    layer.deleteLater()
                    raise WrongLayerFileError("Error reading file {} or it is not a valid vector layer file".format(source))
            else:
                layer = source
                if not layer.isValid() or layer.type() != QgsMapLayer.VectorLayer:
                    raise WrongLayerFileError("Layer '%s' is not valid or is not a vector layer".format(layer.name()))

            extent = '{},{},{},{}'.format(
                    layer.extent().xMinimum(), layer.extent().xMaximum(),
                    layer.extent().yMinimum(), layer.extent().yMaximum())
            geomtypes = {QGis.WKBPoint: 3,
                         QGis.WKBLineString: 4,
                         QGis.WKBPolygon: 5,
                         QGis.WKBMultiPoint: 7,
                         QGis.WKBMultiLineString: 9,
                         QGis.WKBMultiPolygon: 8}
            geomtype = geomtypes.get(layer.wkbType(), 0)
            import processing
            from processing.algs.gdal.ogr2ogrtopostgis import Ogr2OgrToPostGis as ogr2ogr
            params = {ogr2ogr.INPUT_LAYER: layer,
                        ogr2ogr.DBNAME: self.geodb.dbname,
                        ogr2ogr.PORT : self.geodb.port,
                        ogr2ogr.HOST : self.geodb.host,
                        ogr2ogr.USER : self.geodb.user,
                        ogr2ogr.PASSWORD: self.geodb.passwd,
                        ogr2ogr.SCHEMA: schema,
                        ogr2ogr.GTYPE: geomtype,
                        ogr2ogr.TABLE: tablename,
                        ogr2ogr.S_SRS: layer.crs().authid(),
                        ogr2ogr.T_SRS: layer.crs().authid(),
                        ogr2ogr.OVERWRITE: overwrite,
                        ogr2ogr.APPEND: not overwrite,
                        ogr2ogr.SPAT: extent
                        }
            processing.runalg("gdalogr:importvectorintopostgisdatabasenewconnection", params)
Beispiel #17
0
def reproject( cleanPath, tempDir, plotCode ):
    
    #reproject to a metered coordinate system
    #OR north state plane
    targetCRS = 'ESPG:3645'
    rpBase = '{0}_reprojected.shp'.format(plotCode)
    rpPath = os.path.join(tempDir, rpBase)
    remSHP( rpPath )
    
    progress.setInfo( '\nReprojecting to {0}'.format( targetCRS ) )
    progress.setInfo( 'Writing to {0}'.format( rpPath ) )
    
    processing.runalg( 'qgis:reprojectlayer', cleanPath, targetCRS, rpPath )
    
    return rpPath
Beispiel #18
0
 def test_gdalogrsieve(self):
     outputs = processing.runalg('gdalogr:sieve', raster(), 2, 0, None)
     output = outputs['dst_filename']
     self.assertTrue(os.path.isfile(output))
     dataset = gdal.Open(output, GA_ReadOnly)
     strhash = hash(str(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash, -1353696889)
def calculate_azimuth(layerSource):
    '''explose les lignes sources en bi-point, ajoute un champ 'temp' et le remplis avec l'azimuth de la ligne'''
    #exploser les lignes en bi-point et recuperer le layer resultat
    layer = processing.runalg("qgis:explodelines", layerSource.source(), None)
    print layer
    layer = QgsVectorLayer(layer.get('OUTPUT'),
                           layerSource.name() + '00', 'ogr')
    print layer
    layer.startEditing()
    #creer un champ temp pour y stocker l'azimuth et recuperer son id
    azimuth_field = QgsField('temp', QVariant.Int)
    exp = QgsExpression(
        '(atan((xat(-1)-xat(0))/(yat(-1)-yat(0)))) * 180/3.14159 + (180 *(((yat(-1)-yat(0)) < 0) + (((xat(-1)-xat(0)) < 0 AND (yat(-1) - yat(0)) >0)*2)))'
    )
    layer.dataProvider().addAttributes([azimuth_field])
    layer.commitChanges()
    layer.startEditing()
    idx_layer = layer.fieldNameIndex('temp')
    #calculer l'azimuth de chaque ligne et rempli le champ temp avec cette valeur
    for f in layer.getFeatures():
        xy = f.geometry().asPolyline()
        azimuth = int(xy[0].azimuth(xy[1]))
        print azimuth
        layer.changeAttributeValue(f.id(), idx_layer, azimuth)
    layer.commitChanges()
    QgsMapLayerRegistry.instance().addMapLayer(layer)
    return layer
Beispiel #20
0
 def runPolygonize(self, cl, areaLyr, lineLyr):
     """
     runs polygonize to generate coverage areas.
     store the polygonize return in the memory area layer with the following attributes:
     cl - original area class
     """
     QgsMapLayerRegistry.instance().addMapLayer(lineLyr)
     ret = processing.runalg('qgis:polygonize', lineLyr, False, True, None)
     if not ret:
         raise Exception(
             self.
             tr('Problem executing qgis:polygonize. Check your installed libs.\n'
                ))
     #updating original layer
     outputLayer = processing.getObject(ret['OUTPUT'])
     addList = []
     for feat in outputLayer.getFeatures():
         newFeat = QgsFeature(areaLyr.pendingFields())
         newFeat['cl'] = cl
         area = feat.geometry()
         area.convertToMultiType()
         newFeat.setGeometry(area)
         addList.append(newFeat)
     areaLyr.dataProvider().addFeatures(addList)
     #removing from registry
     QgsMapLayerRegistry.instance().removeMapLayer(lineLyr.id())
Beispiel #21
0
    def runProcessinAlg(self, layer):
        """
        Runs the actual grass process
        """
        alg = 'grass7:v.clean.advanced'

        #setting tools
        tools = 'rmsa,break,rmdupl,rmdangle'
        threshold = -1

        #getting table extent (bounding box)
        extent = layer.extent()
        (xmin, xmax, ymin, ymax) = extent.xMinimum(), extent.xMaximum(
        ), extent.yMinimum(), extent.yMaximum()
        extent = '{0},{1},{2},{3}'.format(xmin, xmax, ymin, ymax)

        snap = self.parameters['Snap']
        minArea = self.parameters['MinArea']

        ret = processing.runalg(alg, layer, tools, threshold, extent, snap,
                                minArea, None, None)
        if not ret:
            raise Exception(
                self.
                tr('Problem executing grass7:v.clean.advanced. Check your installed libs.\n'
                   ))

        #updating original layer
        outputLayer = processing.getObject(ret['output'])
        #self.updateOriginalLayerV2(layer, outputLayer)

        #getting error flags
        errorLayer = processing.getObject(ret['error'])
        return self.getProcessingErrors(errorLayer), outputLayer
Beispiel #22
0
    def test_SagaVectorAlgorithWithUnsupportedInputAndOutputFormat(self):
        """This tests both the exporting to shp and then the format
        change in the output layer.
        """

        layer = processing.getObject(polygonsGeoJson())
        feature = layer.getFeatures().next()
        selected = [feature.id()]
        layer.setSelectedFeatures(selected)
        outputs = processing.runalg('saga:polygoncentroids',
                                    polygonsGeoJson(), True,
                                    getTempFilename('geojson'))
        layer.setSelectedFeatures([])
        output = outputs['CENTROIDS']
        layer = dataobjects.getObjectFromUri(output, True)
        fields = layer.pendingFields()
        expectednames = ['ID', 'POLY_NUM_A', 'POLY_ST_A']
        expectedtypes = ['Real', 'Real', 'String']
        names = [unicode(f.name()) for f in fields]
        types = [unicode(f.typeName()) for f in fields]
        self.assertEqual(expectednames, names)
        self.assertEqual(expectedtypes, types)
        features = processing.features(layer)
        self.assertEqual(1, len(features))
        feature = features.next()
        attrs = feature.attributes()
        expectedvalues = ['0', '1.1', 'string a']
        values = [unicode(attr) for attr in attrs]
        self.assertEqual(expectedvalues, values)
        wkt = 'POINT(270787.49991451 4458955.46775295)'
        self.assertEqual(wkt, unicode(feature.geometry().exportToWkt()))
Beispiel #23
0
 def test_modelernotinorder(self):
     outputs = processing.runalg('modeler:notinorder', raster(), None)
     output = outputs['CAREA_ALG0']
     self.assertTrue(os.path.isfile(output))
     dataset = gdal.Open(output, GA_ReadOnly)
     strhash = hash(str(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash, -1557050506)
Beispiel #24
0
 def test_gdalogrmerge(self):
     outputs=processing.runalg("gdalogr:merge",raster(),False,False,None)
     output=outputs['OUTPUT']
     self.assertTrue(os.path.isfile(output))
     dataset=gdal.Open(output, GA_ReadOnly)
     strhash=hash(str(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash,-1353696889)
Beispiel #25
0
 def test_sagametricconversions(self):
     outputs=processing.runalg("saga:metricconversions",raster(),0,None)
     output=outputs['CONV']
     self.assertTrue(os.path.isfile(output))
     dataset=gdal.Open(output, GA_ReadOnly)
     strhash=hash(str(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash,-2137931723)
Beispiel #26
0
 def _get_joined_layer(self, layer, vertexLayer):
     """Returns joined layer.
     
     Args:
         layer (QgsVectorLayer): A reference to the active layer.
         vertexLayer (QgsVectorLayer): A reference to the vertex layer.
     
     Returns:
         QgsVectorLayer: A reference to the joined layer, None when layer
             or vertex layer has no feature.
     
     """
     
     selectedFeatureCount = layer.selectedFeatureCount()
     vertexLayerFeatureCount = vertexLayer.featureCount()
     
     parVertexLayer = None
     
     if selectedFeatureCount != 0 and vertexLayerFeatureCount != 0:
         parVertexLayerFilePath = processing.runalg(
             'qgis:joinattributesbylocation',
             layer, vertexLayer, u'touches', 0, 1, u'max', 1, None)['OUTPUT']
         
         vertexLayerCode = vertexLayer.name().split('|')[1]
         
         parVertexLayerName = layer.name() + u'-join-' + vertexLayerCode
         
         parVertexLayer = QgsVectorLayer(
             parVertexLayerFilePath, parVertexLayerName, 'ogr')
     
     return parVertexLayer
Beispiel #27
0
 def test_gdalogrwarpreproject(self):
     outputs=processing.runalg("gdalogr:warpreproject",raster(),"EPSG:23030","EPSG:4326",0,0,"",None)
     output=outputs['OUTPUT']
     self.assertTrue(os.path.isfile(output))
     dataset=gdal.Open(output, GA_ReadOnly)
     strhash=hash(str(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash,-2021328784)
Beispiel #28
0
 def test_sagasortgrid(self):
     outputs = processing.runalg('saga:sortgrid', raster(), True, None)
     output = outputs['OUTPUT']
     self.assertTrue(os.path.isfile(output))
     dataset = gdal.Open(output, GA_ReadOnly)
     strhash = hash(unicode(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash, 1320073153)
Beispiel #29
0
    def checkSagaIsInstalled(ignoreRegistrySettings=False):
        if ProcessingUtils.isWindows():
            path = SagaUtils.sagaPath()
            if path == "":
                return "SAGA folder is not configured.\nPlease configure it before running SAGA algorithms."
            cmdpath = os.path.join(path, "saga_cmd.exe")
            if not os.path.exists(cmdpath):
                return (
                    "The specified SAGA folder does not contain a valid SAGA executable.\n"
                    +
                    "Please, go to the processing settings dialog, and check that the SAGA\n"
                    + "folder is correctly configured")

        settings = QSettings()
        SAGA_INSTALLED = "/ProcessingQGIS/SagaInstalled"
        if not ignoreRegistrySettings:
            if settings.contains(SAGA_INSTALLED):
                return

        try:
            from processing import runalg
            result = runalg("saga:polygoncentroids", points(), False, None)
            if result is None or not os.path.exists(result['CENTROIDS']):
                return "It seems that SAGA is not correctly installed in your system.\nPlease install it before running SAGA algorithms."
        except:
            s = traceback.format_exc()
            return "Error while checking SAGA installation. SAGA might not be correctly configured.\n" + s

        settings.setValue(SAGA_INSTALLED, True)
Beispiel #30
0
def clMask(img, clPath, baseName, wod):
    # find path to cloudmask
    year = baseName.split("_")[2][0:4]
    tile = baseName.split("_")[5]
    mainDir = os.path.join(clPath, tile, year, baseName + ".SAFE")
    msPath = wod + "cloudmask.tif"
    if not os.path.exists(mainDir):
        print "problem finding cloud mask folder"
# look for cloudmask in remote folder
    for r, d, files in os.walk(mainDir):
        for f in files:
            if f.endswith("B00.gml"):
                clMpath = os.path.join(r, f)
    cpv = wod + "cloudVec.gml"
    shutil.copy2(clMpath, cpv)
    lay = QgsVectorLayer(cpv)
    #stopGo(401,baseName)

    ms = p.runalg("grass7:r.mask.vect", cpv, img, "", "", True, extImg, 0, -1,
                  0.0001, msPath)
    if not QgsRasterLayer(msPath).isValid():
        print "problem masking image %s function 8 clMask" % baseName
        msPath = img
        #stopGo(395, baseName)
    print "output of function 8 clMask is:\n %s" % msPath
    return msPath
Beispiel #31
0
    def checkGrassIsInstalled(ignoreRegistrySettings=False):
        if ProcessingUtils.isWindows():
            path = GrassUtils.grassPath()
            if path == "":
                return "GRASS folder is not configured.\nPlease configure it before running SAGA algorithms."
            cmdpath = os.path.join(path, "bin", "r.out.gdal.exe")
            if not os.path.exists(cmdpath):
                return (
                    "The specified GRASS folder does not contain a valid set of GRASS modules.\n"
                    +
                    "Please, go to the processing settings dialog, and check that the GRASS\n"
                    + "folder is correctly configured")

        settings = QSettings()
        GRASS_INSTALLED = "/ProcessingQGIS/GrassInstalled"
        if not ignoreRegistrySettings:
            if settings.contains(GRASS_INSTALLED):
                return
        try:
            from processing import runalg
            result = runalg(
                "grass:v.voronoi", points(), False, False,
                "270778.60198,270855.745301,4458921.97814,4458983.8488", -1,
                0.0001, 0, None)
            if not os.path.exists(result['output']):
                return "It seems that GRASS is not correctly installed and configured in your system.\nPlease install it before running GRASS algorithms."
        except:
            s = traceback.format_exc()
            return "Error while checking GRASS installation. GRASS might not be correctly configured.\n" + s

        settings.setValue(GRASS_INSTALLED, True)
Beispiel #32
0
 def test_modelersimplemodel(self):
     outputs = processing.runalg('modeler:simplemodel', raster(), None)
     output = outputs['SLOPE_ALG0']
     self.assertTrue(os.path.isfile(output))
     dataset = gdal.Open(output, GA_ReadOnly)
     strhash = hash(str(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash, 1891122097)
Beispiel #33
0
 def test_gdalogrsieveWithUnsupportedOutputFormat(self):
     outputs=processing.runalg("gdalogr:sieve",raster(),2,0, ProcessingUtils.getTempFilename("img"))
     output=outputs['dst_filename']
     self.assertTrue(os.path.isfile(output))
     dataset=gdal.Open(output, GA_ReadOnly)
     strhash=hash(str(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash,-1353696889)
Beispiel #34
0
 def test_SagaVectorAlgorithmWithSelection(self):
     layer = processing.getObject(polygons2())
     feature = layer.getFeatures().next()
     selected = [feature.id()]
     layer.setSelectedFeatures(selected)
     outputs = processing.runalg('saga:polygoncentroids', polygons2(),
                                 True, None)
     layer.setSelectedFeatures([])
     output = outputs['CENTROIDS']
     layer = dataobjects.getObjectFromUri(output, True)
     fields = layer.pendingFields()
     expectednames = ['ID', 'POLY_NUM_B', 'POLY_ST_B']
     expectedtypes = ['Real', 'Real', 'String']
     names = [unicode(f.name()) for f in fields]
     types = [unicode(f.typeName()) for f in fields]
     self.assertEqual(expectednames, names)
     self.assertEqual(expectedtypes, types)
     features = processing.features(layer)
     self.assertEqual(1, len(features))
     feature = features.next()
     attrs = feature.attributes()
     expectedvalues = ['2', '1', 'string a']
     values = [unicode(attr) for attr in attrs]
     self.assertEqual(expectedvalues, values)
     wkt = 'POINT(270806.69221918 4458924.97720492)'
     self.assertEqual(wkt, unicode(feature.geometry().exportToWkt()))
 def runProcessinAlg(self, layer):
     """
     Runs the actual grass process
     """
     alg = 'qgis:dissolve'
     uri = QgsDataSourceURI(layer.dataProvider().dataSourceUri())
     keyColumn = uri.keyColumn()
     #field.type() != 6 stands for virtual columns such as area_otf
     auxLayer = self.createUnifiedLayer(
         [layer],
         attributeTupple=True,
         attributeBlackList=self.
         parameters['AttributeBlackList (comma separated)'])
     if self.parameters['MaxDissolveArea'] > 0:
         auxLayer = self.addDissolveField(
             auxLayer, self.parameters['MaxDissolveArea'])
     ret = processing.runalg(alg, auxLayer, False, 'tupple', None)
     if not ret:
         raise Exception(
             self.
             tr('Problem executing qgis:dissolve. Check your installed libs.\n'
                ))
     #updating original layer
     outputLayer = processing.getObject(ret['OUTPUT'])
     QgsMapLayerRegistry.instance().removeMapLayer(auxLayer.id())
     self.splitUnifiedLayer(outputLayer, [layer])
     return outputLayer
Beispiel #36
0
 def test_SagaRasterAlgorithmWithUnsupportedOutputFormat(self):
     outputs=processing.runalg("saga:convergenceindex",raster(),0,0,ProcessingUtils.getTempFilename("img"))
     output=outputs['RESULT']
     self.assertTrue(os.path.isfile(output))
     dataset=gdal.Open(output, GA_ReadOnly)
     strhash=hash(str(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash, 485390137)
Beispiel #37
0
def DoMapCalc(maps, out_file, formula):
    layer = dataobjects.getObjectFromUri(maps[0], True)
    extent = str(layer.extent().xMinimum()) + "," + str(
        layer.extent().xMaximum()) + "," + str(
            layer.extent().yMinimum()) + "," + str(layer.extent().yMaximum())
    param = {
        'amap': maps[0],
        'bmap': maps[1],
        'cmap': maps[2],
        'dmap': maps[3],
        'formula': formula,
        'GRASS_REGION_PARAMETER': extent,
        'GRASS_REGION_CELLSIZE_PARAMETER': 0,
        'outfile': out_file
    }
    processing.runalg("grass:r.mapcalculator", param)
Beispiel #38
0
    def checkSagaIsInstalled(ignorePreviousState=False):
        if isWindows():
            path = SagaUtils.sagaPath()
            if path == '':
                return 'SAGA folder is not configured.\nPlease configure it \
                        before running SAGA algorithms.'

            cmdpath = os.path.join(path, 'saga_cmd.exe')
            if not os.path.exists(cmdpath):
                return 'The specified SAGA folder does not contain a valid \
                    SAGA executable.\n' \
                    + 'Please, go to the processing settings dialog, and \
                    check that the SAGA\n' \
                    + 'folder is correctly configured'

        if not ignorePreviousState:
            if SagaUtils.isSagaInstalled:
                return

        try:
            from processing import runalg
            result = runalg('saga:polygoncentroids', polygons(), 0, None)
            if result is None or not os.path.exists(result['CENTROIDS']):
                return 'It seems that SAGA is not correctly installed in \
                        your system.\nPlease install it before running SAGA \
                        algorithms.'

        except:
            s = traceback.format_exc()
            return 'Error while checking SAGA installation. SAGA might not \
                    be correctly configured.\n' + s

        SagaUtils.isSagaInstalled = True
Beispiel #39
0
 def test_modelernotinorder(self):
     outputs = processing.runalg("modeler:notinorder", raster(), None)
     output = outputs["CAREA_ALG0"]
     self.assertTrue(os.path.isfile(output))
     dataset = gdal.Open(output, GA_ReadOnly)
     strhash = hash(str(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash, -1557050506)
Beispiel #40
0
    def checkSagaIsInstalled(ignorePreviousState=False):
        if isWindows():
            path = SagaUtils.sagaPath()
            if path == '':
                return 'SAGA folder is not configured.\nPlease configure it \
                        before running SAGA algorithms.'
            cmdpath = os.path.join(path, 'saga_cmd.exe')
            if not os.path.exists(cmdpath):
                return 'The specified SAGA folder does not contain a valid \
                    SAGA executable.\n' \
                    + 'Please, go to the processing settings dialog, and \
                    check that the SAGA\n' \
                    + 'folder is correctly configured'

        if not ignorePreviousState:
            if SagaUtils.isSagaInstalled:
                return

        try:
            from processing import runalg
            result = runalg('saga:polygoncentroids', polygons(), 0, None)
            if result is None or not os.path.exists(result['CENTROIDS']):
                return 'It seems that SAGA is not correctly installed in \
                        your system.\nPlease install it before running SAGA \
                        algorithms.'
        except:
            s = traceback.format_exc()
            return 'Error while checking SAGA installation. SAGA might not \
                    be correctly configured.\n' + s

        SagaUtils.isSagaInstalled = True
Beispiel #41
0
 def test_modelerfieldautoextent(self):
     outputs = processing.runalg("modeler:fieldautoextent", polygons(), "POLY_NUM_A", None)
     output = outputs["USER_GRID_ALG0"]
     self.assertTrue(os.path.isfile(output))
     dataset = gdal.Open(output, GA_ReadOnly)
     strhash = hash(str(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash, 2026100494)
Beispiel #42
0
def doSingleTile(path):
  
  try:
      # MWShapeID_10.shp_id_25.shp  
    #splits = basename(path).replace('.shp', '').split('_')
    #newname = newname = splits[1] + splits[3] + '.shp'
    newname = basename(path)
    dump2output = join(dumppath2, newname) + '.shp'
    #print(dump2output)
    
    vlayer = QgsVectorLayer(path, newname, "ogr") 
    vlayer.setCrs(crs)
    
    extent = vlayer.extent()
    extentstr = '%.2f, %.2f, %.2f, %.2f' % (extent.xMinimum(), extent.xMaximum(), extent.yMinimum(), extent.yMaximum())
    tile_width = 1.5
    tile_height = 1.5

    t1 = join(dumppath2, newname) + '_GRID.shp'
    
    result = processing.runalg('qgis:vectorgrid', extentstr, tile_width, tile_height, 0, dump2output)

    # result['OUTPUT']
   # result = processing.runalg('gdalogr:clipvectorsbypolygon', t1, path, None, tempoutput)
    
  except Exception as ex:
    print ex + ' ' + path
Beispiel #43
0
    def checkGrassIsInstalled(ignorePreviousState=False):
        if isWindows():
            path = GrassUtils.grassPath()
            if path == "":
                return GrassUtils.tr(
                    "GRASS folder is not configured.\nPlease configure " "it before running GRASS algorithms."
                )
            cmdpath = os.path.join(path, "bin", "r.out.gdal.exe")
            if not os.path.exists(cmdpath):
                return GrassUtils.tr(
                    'The specified GRASS folder "{}" does not contain a valid '
                    "set of GRASS modules. Please, go to the Processing "
                    "settings dialog, and check that the GRASS folder is "
                    "correctly configured".format(os.path.join(path, "bin"))
                )

        if not ignorePreviousState:
            if GrassUtils.isGrassInstalled:
                return
        try:
            from processing import runalg

            result = runalg("grass:v.voronoi", points(), False, False, None, -1, 0.0001, 0, None)
            if not os.path.exists(result["output"]):
                return GrassUtils.tr(
                    "It seems that GRASS is not correctly installed and "
                    "configured in your system.\nPlease install it before "
                    "running GRASS algorithms."
                )
        except:
            return GrassUtils.tr(
                "Error while checking GRASS installation. GRASS might not " "be correctly configured.\n"
            )

        GrassUtils.isGrassInstalled = True
 def test_modeleremptystring(self):
     outputs = processing.runalg("modeler:emptystring", union(), None)
     output = outputs['OUTPUT_LAYER_ALG0']
     layer = QGisLayers.getObjectFromUri(output, True)
     fields = layer.pendingFields()
     expectednames = [
         'ID', 'POLY_NUM_A', 'POLY_ST_A', 'ID_2', 'POLY_NUM_B', 'POLY_ST_B',
         'NewField'
     ]
     expectedtypes = [
         'Integer', 'Real', 'String', 'Integer', 'Real', 'String', 'Integer'
     ]
     names = [str(f.name()) for f in fields]
     types = [str(f.typeName()) for f in fields]
     self.assertEqual(expectednames, names)
     self.assertEqual(expectedtypes, types)
     features = processing.getfeatures(layer)
     self.assertEqual(8, len(features))
     feature = features.next()
     attrs = feature.attributes()
     expectedvalues = ["1", "1.1", "string a", "2", "1", "string a", "10"]
     values = [str(attr) for attr in attrs]
     self.assertEqual(expectedvalues, values)
     wkt = 'POLYGON((270807.08580285 4458940.1594565,270798.42294527 4458914.62661676,270780.81854858 4458914.21983449,270763.52289518 4458920.715993,270760.3449542 4458926.6570575,270763.78234766 4458958.22561242,270794.30290024 4458942.16424502,270807.08580285 4458940.1594565))'
     self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
Beispiel #45
0
 def test_sagasortgrid(self):
     outputs=processing.runalg("saga:sortgrid",raster(),True,None)
     output=outputs['OUTPUT']
     self.assertTrue(os.path.isfile(output))
     dataset=gdal.Open(output, GA_ReadOnly)
     strhash=hash(str(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash,1320073153)
Beispiel #46
0
 def test_SagaVectorAlgorithmWithSelection(self):
     layer = processing.getObject(polygons2());
     feature = layer.getFeatures().next()
     selected = [feature.id()]
     layer.setSelectedFeatures(selected)
     outputs=processing.runalg("saga:polygoncentroids",polygons2(),True,None)
     layer.setSelectedFeatures([])
     output=outputs['CENTROIDS']
     layer=dataobjects.getObjectFromUri(output, True)
     fields=layer.pendingFields()
     expectednames=['ID','POLY_NUM_B','POLY_ST_B']
     expectedtypes=['Real','Real','String']
     names=[str(f.name()) for f in fields]
     types=[str(f.typeName()) for f in fields]
     self.assertEqual(expectednames, names)
     self.assertEqual(expectedtypes, types)
     features=processing.features(layer)
     self.assertEqual(1, len(features))
     feature=features.next()
     attrs=feature.attributes()
     expectedvalues=["2","1","string a"]
     values=[str(attr) for attr in attrs]
     self.assertEqual(expectedvalues, values)
     wkt='POINT(270806.69221918 4458924.97720492)'
     self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
Beispiel #47
0
 def test_sagametricconversions(self):
     outputs=processing.runalg("saga:metricconversions",raster(),0,None)
     output=outputs['CONV']
     self.assertTrue(os.path.isfile(output))
     dataset=gdal.Open(output, GA_ReadOnly)
     strhash=hash(str(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash,-2137931723)
Beispiel #48
0
 def test_SagaRasterAlgorithmWithUnsupportedOutputFormat(self):
     outputs=processing.runalg("saga:convergenceindex",raster(),0,0,getTempFilename("img"))
     output=outputs['RESULT']
     self.assertTrue(os.path.isfile(output))
     dataset=gdal.Open(output, GA_ReadOnly)
     strhash=hash(str(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash, 485390137)
Beispiel #49
0
    def checkSagaIsInstalled(ignoreRegistrySettings=False):
        if ProcessingUtils.isWindows():
            path = SagaUtils.sagaPath()
            if path == "":
                return "SAGA folder is not configured.\nPlease configure it before running SAGA algorithms."
            cmdpath = os.path.join(path, "saga_cmd.exe")
            if not os.path.exists(cmdpath):
                return (
                    "The specified SAGA folder does not contain a valid SAGA executable.\n"
                    + "Please, go to the processing settings dialog, and check that the SAGA\n"
                    + "folder is correctly configured"
                )

        settings = QSettings()
        SAGA_INSTALLED = "/ProcessingQGIS/SagaInstalled"
        if not ignoreRegistrySettings:
            if settings.contains(SAGA_INSTALLED):
                return

        try:
            from processing import runalg

            result = runalg("saga:thiessenpolygons", points(), None)
            if not os.path.exists(result["POLYGONS"]):
                return "It seems that SAGA is not correctly installed in your system.\nPlease install it before running SAGA algorithms."
        except:
            s = traceback.format_exc()
            return "Error while checking SAGA installation. SAGA might not be correctly configured.\n" + s

        settings.setValue(SAGA_INSTALLED, True)
Beispiel #50
0
 def test_gdalogrsieve(self):
     outputs = processing.runalg('gdalogr:sieve', raster(), 2, 0, None)
     output = outputs['dst_filename']
     self.assertTrue(os.path.isfile(output))
     dataset = gdal.Open(output, GA_ReadOnly)
     strhash = hash(str(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash, -1353696889)
Beispiel #51
0
 def test_modelersimplemodel(self):
     outputs = processing.runalg("modeler:simplemodel", raster(), None)
     output = outputs["SLOPE_ALG0"]
     self.assertTrue(os.path.isfile(output))
     dataset = gdal.Open(output, GA_ReadOnly)
     strhash = hash(str(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash, 1891122097)
def gdal2GeoTiff_ECMWF_WGS84(Filename, progress):
    progress.setConsoleInfo("Translating to GeoTIFF...")
    tiff_filename_base = os.path.split(Filename)[0] + os.sep
    tiff_filelist = []

    d = datetime(1970, 1, 1)

    # Read raster bands from file
    data = gdal.Open(Filename, GA_ReadOnly)
    number_of_bands = data.RasterCount

    # Get extent
    extent = dataobjects.extent([Filename])

    for i in range(1, number_of_bands + 1):
        # data = gdal.Open(Filename, GA_ReadOnly)
        band = data.GetRasterBand(i)
        htime = band.GetMetadata()["GRIB_REF_TIME"]
        userange = len(htime) - 7
        UTCtime_delta = int(band.GetMetadata()["GRIB_REF_TIME"][0:userange])
        # data = None
        tiff_filename = (
            tiff_filename_base + str(
                (d + timedelta(seconds=UTCtime_delta)).year) +
            str((d + timedelta(seconds=UTCtime_delta)).month).zfill(2) + str(
                (d + timedelta(seconds=UTCtime_delta)).day).zfill(2) + "_" +
            str((d + timedelta(seconds=UTCtime_delta)).hour).zfill(2) +
            "ECMWF.tif")

        # Convert to GeoTIFF using processing GDAL
        param = {
            "INPUT": Filename,
            "OUTSIZE": 100,
            "OUTSIZE_PERC": True,
            "NO_DATA": "",
            "EXPAND": 0,
            "SRS": "EPSG:4326",
            "PROJWIN": extent,
            "SDS": False,
            "EXTRA": "-b " + str(i),
            "OUTPUT": tiff_filename,
        }
        processing.runalg("gdalogr:translate", param)
        tiff_filelist.append(tiff_filename)

    data = None
    return tiff_filelist
def gdal2GeoTiff_GFS_WGS84(FileList, log_file, progress):
    """Translates from GDAL compatible data source to GeoTIFF files.

    OBS: Files must be WGS84"""
    tiff_FileList = []
    iteration = 0

    for f in FileList:
        iteration += 1
        # Append new filename to list
        tiff_filename = os.path.split(f)[0] + os.sep + os.path.split(f)[1].split('.grb')[0] + '.tif'
        tiff_FileList.append(tiff_filename)

        # Convert to GeoTIFF using processing GDAL
        layer = dataobjects.getObjectFromUri(f)
        extent = str(layer.extent().xMinimum())+","+str(layer.extent().xMaximum())+","+str(layer.extent().yMinimum())+","+str(layer.extent().yMaximum())
        #param = {'INPUT':f, 'OUTSIZE':100, 'OUTSIZE_PERC':True, 'NO_DATA':"none", 'EXPAND':0, 'SRS':'', 'PROJWIN':extent, 'SDS':False, 'EXTRA':'', 'OUTPUT':tiff_filename}
        #processing.runalg("gdalogr:translate",param)
        processing.runalg("gdalogr:translate",f,100,True,"",0,"",extent,False,5,4,75,6,1,False,0,False,"",tiff_filename)

        # Update geo-reference as GFS files longtitude of 55 degree is referenced as 360 + 55 = 415 degree
        data = gdal.Open(tiff_filename, GA_Update)
        geotransform = data.GetGeoTransform()
        # geotransform[0]: top left x
        # geotransform[1]: w-e pixel resolution
        # geotransform[2]: rotation, 0 if image is "north up"
        # geotransform[3]: top left y
        # geotransform[4]: rotation, 0 if image is "north up"
        # geotransform[5]: n-s pixel resolution
        if (geotransform[0] + geotransform[1] * data.RasterXSize) > 360:
            # Set left longitude
            geotransform = [geotransform[0]-360, geotransform[1], geotransform[2], geotransform[3], geotransform[4], geotransform[5]]
            data.SetGeoTransform(geotransform)

        # Set projection to WGS84
        srs = osr.SpatialReference()
        srs.SetWellKnownGeogCS( 'WGS84' )
        data.SetProjection( srs.ExportToWkt() )
        srs = None

        # Closing dataset
        data = None

        # Show progress
        progress.setPercentage(iteration/float(len(FileList)) * 100)

    return tiff_FileList
 def saveSelection(self):
     layer = self.setLayer()
     selectedLines = processing.runalg('qgis:saveselectedfeatures', layer,
                                       None)
     filename = os.path.basename(selectedLines['OUTPUT_LAYER'])
     location = os.path.abspath(selectedLines['OUTPUT_LAYER'])
     result_layer = self.iface.addVectorLayer(location, filename, "ogr")
     result_layer.setLayerName("memory:Desire Lines")
def uniqueFields(inputURI, field):
    # obtain dictionary object that contains unique values from specified field
    unique = processing.runalg('qgis:listuniquevalues', inputURI, field, None)
    # get the list of unique values from the dictionary's UNIQUE_VALUES key
    # unique values are in a semicolan delimeted string, so split them at them
    # at the same time to a Python array
    values = unique['UNIQUE_VALUES'].split(';')
    return values
Beispiel #56
0
 def test_modelerfieldautoextent(self):
     outputs = processing.runalg('modeler:fieldautoextent', polygons(),
                                 'POLY_NUM_A', None)
     output = outputs['USER_GRID_ALG0']
     self.assertTrue(os.path.isfile(output))
     dataset = gdal.Open(output, GA_ReadOnly)
     strhash = hash(unicode(dataset.ReadAsArray(0).tolist()))
     self.assertEqual(strhash, 2026100494)
Beispiel #57
0
def ras2dPreviewMesh(rgis):
    """Loads the mesh points to the canvas and builds Voronoi polygons"""
    areas = None
    u1 = QgsDataSourceURI()
    u1.setConnection(rgis.host, rgis.port, rgis.database, rgis.user,
                     rgis.passwd)
    u1.setDataSource(rgis.schema, 'MeshPoints2d', 'geom')
    mesh_pts = QgsVectorLayer(u1.uri(), 'MeshPoints2d', 'postgres')
    voronoi = processing.runalg('qgis:voronoipolygons', mesh_pts, 3, None)
    # QgsMapLayerRegistry.instance().addMapLayers([mesh_pts])

    # try to load the 2D Area polygon and clip the Voronoi diagram
    try:
        u2 = QgsDataSourceURI()
        u2.setConnection(rgis.host, rgis.port, rgis.database, rgis.user,
                         rgis.passwd)
        u2.setDataSource(rgis.schema, 'FlowAreas2d', 'geom')
        areas = QgsVectorLayer(u2.uri(), 'FlowAreas2d', 'postgres')
        if rgis.DEBUG:
            rgis.addInfo(
                'Voronoi layer: \n{0}\nFlow Areas 2d layer ok? {1}'.format(
                    voronoi['OUTPUT'], areas.isValid()))
        # TODO: construct voronoi polygons separately for each 2d mesh area
        voronoiClip = processing.runalg('qgis:clip', voronoi['OUTPUT'], areas,
                                        None)
        if rgis.DEBUG:
            rgis.addInfo('Cutted Voronoi polygons:\n{0}'.format(
                voronoiClip['OUTPUT']))
        sleep(1)
        voronoiClipLayer = QgsVectorLayer(voronoiClip['OUTPUT'],
                                          'Mesh preview', 'ogr')
        QgsMapLayerRegistry.instance().addMapLayers([voronoiClipLayer])

    except:
        voronoiLayer = QgsVectorLayer(voronoi['OUTPUT'], 'Mesh preview', 'ogr')
        QgsMapLayerRegistry.instance().addMapLayers([voronoiLayer])

    # change layers' style
    root = QgsProject.instance().layerTreeRoot()
    legItems = root.findLayers()
    for item in legItems:
        if item.layerName() == 'Mesh preview':
            stylePath = join(rgis.rivergisPath, 'styles/Mesh2d.qml')
            item.layer().loadNamedStyle(stylePath)
            rgis.iface.legendInterface().refreshLayerSymbology(item.layer())
            rgis.iface.mapCanvas().refresh()
Beispiel #58
0
def exportRasterLayer(i, safeLayerName, dataPath):
    print "Raster type: " + unicode(i.rasterType())
    name_ts = safeLayerName + unicode(time.time())
    # pipelayer = i
    # pipeextent = pipelayer.extent()
    # pipewidth, pipeheight = (pipelayer.width(), pipelayer.height())
    # piperenderer = pipelayer.renderer()
    # pipeprovider = pipelayer.dataProvider()
    # crs = pipelayer.crs().toWkt()
    # pipe = QgsRasterPipe()
    # pipe.set(pipeprovider.clone())
    # pipe.set(piperenderer.clone())
    # pipedFile = os.path.join(tempfile.gettempdir(), name_ts + '_pipe.tif')
    # print "pipedFile: " + pipedFile
    # file_writer = QgsRasterFileWriter(pipedFile)
    # file_writer.writeRaster(pipe, pipewidth, pipeheight, pipeextent,
    #                         pipelayer.crs())

    # in_raster = pipedFile
    in_raster = unicode(i.dataProvider().dataSourceUri())
    print "in_raster: " + in_raster
    prov_raster = os.path.join(tempfile.gettempdir(),
                               'json_' + name_ts + '_prov.tif')
    print "prov_raster: " + prov_raster
    out_raster = dataPath + '.png'
    print "out_raster: " + out_raster
    crsSrc = i.crs()
    crsDest = QgsCoordinateReferenceSystem(3857)
    xform = QgsCoordinateTransform(crsSrc, crsDest)
    extentRep = xform.transform(i.extent())
    extentRepNew = ','.join([
        unicode(extentRep.xMinimum()),
        unicode(extentRep.xMaximum()),
        unicode(extentRep.yMinimum()),
        unicode(extentRep.yMaximum())
    ])
    processing.runalg("gdalogr:warpreproject", in_raster,
                      i.crs().authid(), "EPSG:3857", "", 0, 1, 5, 2, 75, 6, 1,
                      False, 0, False, "", prov_raster)
    del in_raster
    # del pipedFile
    # os.remove(os.path.join(tempfile.gettempdir(), name_ts + '_pipe.tif'))
    processing.runalg("gdalogr:translate", prov_raster, 100, True, "", 0, "",
                      extentRepNew, False, 0, 0, 75, 6, 1, False, 0, False, "",
                      out_raster)
    del prov_raster
Beispiel #59
0
def check_validity(filename1,
                   filename2,
                   layer,
                   alg="qgis:checkvalidity",
                   save=False):
    global OutputFile
    save_name1 = OutputFile + filename1 + ".shp"
    save_name2 = OutputFile + filename2 + ".shp"
    if save == False:
        remove = processing.runalg(alg, layer, 0, None, None, None)
        return processing.getObject(remove['VALID_OUTPUT'])
    else:
        processing.runalg(alg, layer, 0, save_name1, save_name2, None)
        data_source = save_name1
        layer_name = filename1
        provider_name = "ogr"
        return iface.addVectorLayer(data_source, layer_name, provider_name)
Beispiel #60
-1
 def test_SagaVectorAlgorithWithUnsupportedInputAndOutputFormat(self):
     '''this tests both the exporting to shp and then the format change in the output layer'''
     layer = processing.getObject(polygonsGeoJson());
     feature = layer.getFeatures().next()
     selected = [feature.id()]
     layer.setSelectedFeatures(selected)
     outputs=processing.runalg("saga:polygoncentroids",polygonsGeoJson(),True, getTempFilename("geojson"))
     layer.setSelectedFeatures([])
     output=outputs['CENTROIDS']
     layer=dataobjects.getObjectFromUri(output, True)
     fields=layer.pendingFields()
     expectednames=['ID','POLY_NUM_A','POLY_ST_A']
     expectedtypes=['Real','Real','String']
     names=[str(f.name()) for f in fields]
     types=[str(f.typeName()) for f in fields]
     self.assertEqual(expectednames, names)
     self.assertEqual(expectedtypes, types)
     features=processing.features(layer)
     self.assertEqual(1, len(features))
     feature=features.next()
     attrs=feature.attributes()
     expectedvalues=["0","1.1","string a"]
     values=[str(attr) for attr in attrs]
     self.assertEqual(expectedvalues, values)
     wkt='POINT(270787.49991451 4458955.46775295)'
     self.assertEqual(wkt, str(feature.geometry().exportToWkt()))