Skip to content

Instantly share code, notes, and snippets.

@luisalucchese
Created April 15, 2021 15:52
Show Gist options
  • Select an option

  • Save luisalucchese/3346c6c86af2b385dc8f5d9cc75ff48f to your computer and use it in GitHub Desktop.

Select an option

Save luisalucchese/3346c6c86af2b385dc8f5d9cc75ff48f to your computer and use it in GitHub Desktop.
Add multiple raster layers to QGIS based on naming patterns, dates version
#
# add multiple raster layers to QGIS based on naming patterns, dates version
#
# created by Luisa Lucchese
# in April 2021
#
# run directly from QGIS
#
# MIT License
#Copyright (c) 2021 Luisa V. Lucchese
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
# load packages to work with dates in Python
from datetime import date
from datetime import datetime
# where are your files
caminho_base = "your path here"
# how many files are there to open
MAX_ARQ=10
# the starting date
startyear=1970
startmonth=1
startday=1
date_start=date.toordinal(date(startyear,startmonth,startday))
for i in range(1,MAX_ARQ+1):
# the number i in strings, with different leading zeros
num_1dig=str(i); # 1 digit
num_2dig=str(i).zfill(2); # 2 digit
num_3dig=str(i).zfill(3); # 3 digit
# dates in strings
date_loop=date_start+i
loop_datetime=date.fromordinal(date_loop)
year = loop_datetime.strftime("%Y")
month = loop_datetime.strftime("%m")
day = loop_datetime.strftime("%d")
# Suppose your files have a pattern in their names.
# Now is the time to implement it:
caminho_arquivo = caminho_base + year+ '-' + month + '-' + day + '/' + 'raster.tif'
# Other possibilities
# caminho_arquivo = caminho_base + year+ '_' + month + '_' + day + '/' + 'raster.tif'
# Check luisalucchese.com/post/batch-process-load-raster-layers-QGIS/ for more information.
#
# If your file path does not include dates, there is a gist for you
# called "abrir_layers_qgis.py"
# Your layers can also have unique names
nome_layer= 'camada_' + num_1dig
raster_camada = QgsRasterLayer(caminho_arquivo, nome_layer)
QgsProject.instance().addMapLayers([raster_camada])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment