/qgis_scripts

A small collection of QGIS scripts to be used in the QGIS python console.

Primary LanguagePythonGNU General Public License v2.0GPL-2.0

QGIS Scripts

A small collection of QGIS scripts to be used in the QGIS python console.

custom_crs.py

The script defines functions that can be used in the QGIS python console to speed up the generation of custom projections for the region of interest, including:

  • Orthographic projection (i.e. view from space)
  • Satellite view (a.k.a. tilted perspective projection or general perspective)
  • Lambert azimuthal equal-area projection (LAEA)
  • Lambert conformal conic (LCC)
  • Albers equal-area conic projection
  • Robinson / Miller / Mollweide / Winkel Tripel or other projections centered on the Pacific or any custom longitude

Create custom CRS with functions

For example, the following line creates a custom CRS (and an autogenerated description of the CRS) that mimics the view of Peru from a satellite in a height of 5500 km above 25°S, 70°W, looking towards -20° from north direction with a tilt of 30° away from nadir (nice for small insets showing the area of interest):

crs, desc = crs_sat(-25, -70, h=5500000, azi=-20, tilt=30, setproject=True)

With setproject=True, the CRS of the project is set accordingly. This makes it very easy to adjust the values and to find the best angles. To save the CRS, pass the option savecrs=True while creating the custom crs or call the function save_crs(crs, desc) with an existing one.

Create custom CRS tailored for an Area of Interest

The class AreaOfInterest() provides an alternative approach. To define your area of interest, either choose a layer comprising only your area (the layer extend will be used) or select some features of a vector layer (the bounding box of selected features will be used). Now initiate an instance of the class:

aoi = AreaOfInterest()

And create a Lambert azimuthal equal-area projection centered of the center of the area of interest with:

crs = aoi.laea()

It is possible to override certain attributes and to round the values. For an Albers projection with standard parallels rounded to 2 digits and longitude centered on 0°:

aoi.albers(lon_0=0, round_digits=2)

You can save the CRS that was created last as user CRS to be used in other projects:

aoi.save_crs()

The default is to automatically set the project CRS to any newly created CRS. To change this behavior, set

aoi.setproject = False

Get started

Simply run or import the script in the console and start to play with the functions in the console. For details see the autogenerated documentation.

load_dem.py

Always download the SRTM tiles you need to the same folder and use load_dem.py to load all tiles for the current map canvas. If tiles are missing, you will get a polygon layer "Missing DEM tiles" with the coverage and names of the missing tiles. If all tiles exist (and with more than 1 tile), the script generates a mosaic (a file projectname_DEM.vrt in the folder of the project) and only adds the mosaic (instead of individual tiles) to the project.

To use the script, open the python console in QGIS, open the script in the tabbed editor (icon on top of the console). Change the variable dem_folder with the path to your SRTM tiles. Click on the "run" button. (To use the script without the tabbed editor you might need to change the code and help the script to find srtm_grid.gpkg.)

The file srtm_grid.gpkg used by the script is based on the coverage shapefiles of the SRTM data (public domain, USGS), but with a style (transparency, labels) saved in the geopackage.

animation_month.py

The script speeds up the creation of animations if you have a layer for each month, with the number of the month in the layername. It sets the time range of layers to a month taken from the layer name. It sets the min and max values of the raster shaders for all layers to the lowest min and greatest max of all layers.

If you want to change the color ramp, change it on one layer and use copy_paste_this_ramp(layers) to copy the ramp from the active layer to all layers.

The month is extracted with a regular expression and you'll need to adjust the re pattern. This one works for data from WorldClim, with layer names ending with 01.tif etc. (01 for January):

re_pattern = r'(\d*).tif$'

In the lines below re_pattern, you can adjust the layer selection (in case you have more layers in the project).