Pythons scripts to use in the QGIS python console
Last active
September 27, 2021 14:55
-
-
Save Franc-Brs/08a46bfc975adf154553a41a7aca8a1a to your computer and use it in GitHub Desktop.
python script to run from inside the console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
canvas = qgis.utils.iface.mapCanvas() | |
allLayers = canvas.layers() | |
# Pah of the current directory in wihich you are qworking | |
pathToProject=os.getcwd() | |
# Create directory, 'Reprojected', to store the output | |
if not os.path.exists('Reprojected'): | |
os.makedirs('Reprojected') | |
# Declare output CRS | |
outputCRSSstr = 'EPSG:3003' | |
outputCRS = QgsCoordinateReferenceSystem(outputCRSStr) | |
for i in allLayers: | |
if i.type() != 0 : | |
# Print check | |
print(i.name() + " skipped as it is not a vector layer") | |
if ( (i.type() == 0) and i.crs() != outputCRS ): | |
# Print check | |
print (i.name()) | |
outputLayerPathReproj = os.path.join(pathToProject, "Reprojected", i.name()+".shp") | |
# if you want ot store the result in temporary layer | |
#parameter = {'INPUT': i, 'TARGET_CRS': 'EPSG:3003', 'OUTPUT': 'memory:Reprojected'} | |
#QgsProject.instance().addMapLayer(result['OUTPUT']) | |
# Store the result in a shp file in the same folder of the .qgs/.qgz project I'm working on | |
# The output crs is epsg 3003, in this case | |
parameter = {'INPUT': i, 'TARGET_CRS': outputCRSStr, 'OUTPUT': '{}'.format(outputLayerPathReproj)} | |
result = processing.run('native:reprojectlayer', parameter) | |
# Add the result to the .qgs/.qgz project | |
vlayer = QgsVectorLayer(outputLayerPathReproj, "reproj"+ i.name(), "ogr") | |
QgsProject.instance().addMapLayer(vlayer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
path=os.getcwd() | |
output_raster_path = os.path.join(path, "ideal_h.tif") | |
##if you want to take the raster froma different path uncomment the next line (and give the right path) and comment the one below | |
#rlayer = QgsRasterLayer("your/input/raster/path/fileName") | |
rlayer = QgsProject.instance().mapLayersByName('dtm_veneto')[0] | |
##choose the target crs | |
crs_target = QgsCoordinateReferenceSystem("EPSG:3003") | |
#check id the input raster is valid | |
if rlayer.isValid(): | |
print("The raster is valid") | |
else: | |
print("The raster is not valid") | |
##QgsRasterCalculatorEntry | |
entries = [] | |
ras = QgsRasterCalculatorEntry() | |
ras.ref = 'ras@1' | |
ras.raster = rlayer | |
ras.bandNumber = 1 | |
entries.append(ras) | |
##QgsRasterCalculator | |
expression = '\"ras@1\" >150' | |
calculation = QgsRasterCalculator( expression, output_raster_path,'GTiff', rlayer.extent(), crs_target,rlayer.width(), \ | |
rlayer.height(), entries, QgsCoordinateTransformContext() ) | |
calculation.processCalculation() | |
##add the output raster to the QGIS project | |
fin_res = iface.addRasterLayer(output_raster_path,"ideal_h","gdal") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment