Python

Opening and visualizing Fmask in QGIS using a Python Console simple code

Today, I am going to present a gist that I developed that separates the masking information contained in a Fmask layer into different bands, to be visualized separately, in QGIS, using the Python Console of QGIS, also known as PyQGIS.

How to use GDAL to save raster files in Python

Solutions using rasterio work, we all know that. However, installing rasterio and GDAL in the same conda environment is many times a challenge… So I usually try not to install both of them together. I am showing to you today what I think is, in my experience, the easiest way to save raster data using GDAL.

Splitting many polygons into (almost) equal parts using a simple script in PyQGIS, the Python Console of QGIS

Whatever is your geo-thing, you may have come across a Polygon which needed to be split in half, three parts, five parts (a hundred parts?). Possible applications of the split polygons are performing zonal statistics and terrain parceling (agriculture).

The wedge tool: drawing circular quadrants, rings, and related geometries in QGIS

The input layer should be a point layer, in which there should be a point. The coordinates of this point should be the center of the circle (or wedge, or semi-circle). The default width of the wedge is 45°, or one-eight of a circle and the default azimuth of the wedge is 0°. The outer radius is what we are used to simply call a radius of a circle, and the inner radius variable will only be filled when we want to draw a ring or donut-like geometry.

Saving temporary scratch layers using PyQGIS and visualizing them in your QGIS Project

Since, I have been saving the generated files in scratch layers whenever they are intermediate products, of auxiliary nature to the process I am running. This way, they are automatically deleted, and I don’t have to worry about them after executing my code. So, I decided to summarize how I save PyQGIS tool outputs in scratch layers, in case this is useful to someone out there!

Quick workaround for PyQGIS error “file is not a directory” when saving files

This error is especially common when you are re-running a code you have already ran in the past. Sometimes, as I am coding, I run an incomplete version of my code, just to know if the compiler/interpreter accuses any errors. The codes frequently include open and saving items. I thought that the mentioned file would be overwritten every time I ran a file saving in the QGIS Python Console.

Solved: QGIS 3.20 Python Console shows an error when running Clip Vector by Extent tool

ERROR 1: Attempt to write non-multipoint (POINT) geometry to multipoint shapefile. ERROR 1: Unable to write feature 0 from layer points_layer. ERROR 1: Terminating translation prematurely after failed translation of layer points_layer (use -skipfailures to skip errors)

How to read grib2 raster data on Python

I use pygrib to open the files. In their documentation, they point to two ways to install pygrib, using pip or using conda. However, I could only make it work using conda. Run this in your Anaconda Prompt to install pygrib.

Solved: new version rasterio 1.0+ won’t let me use GDAL transforms

This week, I am discussing an error which used to be a warning on the old rasterio - and I ignored it. Lesson learned! I was doing my usual savings of GeoTIFF rasters in an updated python environment (with rasterio 1.2.6) when I saw this error:

How to open, edit and save raster files (.tif) using Python

Today, I am going to write about how to open, edit and save raster files using Python if you are familiarized with Python (mainly Anaconda). Maybe you need to run every pixel in a raster through a Python function, or you want to do custom operations with them that are not available on GUI software such as QGIS. It also can be used to do batch processing. We are going to use GDAL on Python, installed through Conda, to do this.