16 Nov 2023

Workflow for Replacing DOM With DEM Values in a Zone Specified by Vector Layer

1. Create polygon file with same CRS as DOM and DEM and draw polygon mask (obv. polygons with holes don't work!) 

2. Use the "Rasterize (overwrite with fixed value)" algorithm, using the polygon mask as vector layer and the DOM as raster layer (set the DOM as reference layer!). Set the burn value to -9999. BEAWARE: The original file will be overwritten - if you don't want to alter the original file, make a copy of the DOM first!

3. Use the raster calculator to rewrite the values in the DEM with values from the DOM by using this formula: 

(DOM@1 = -9999) * DGM@1 + (DOM@1 != -9999) * DOM@1

4 Oct 2023

Windows Batch Script for Zipping Shapefile Components

Windows Batch File to zip ESRI shapefile-components and delete original files. Put the code to a text file and save with .bat extension. Save the file to the folder you want to run the commands.
@ECHO OFF
set /p $dum="Hit enter to zip Shapefiles in %~dp0 ..."
FOR %%F IN (*.shp) DO "C:\Program Files\7-Zip\7zG.exe" a "%%~nF.zip" "%%~nF.shp" "%%~nF.dbf" "%%~nF.prj" "%%~nF.shx"
for /f "delims=" %%F in ('dir /b /a-d ^| findstr /vile ".bat .zip"') do Echo "%%F"
set /p $dum="Hit enter to delete original files, listed above.."
for /f "delims=" %%F in ('dir /b /a-d ^| findstr /vile ".bat .zip"') do del "%%F"
Echo Done !!
set /p $dum="Hit [Enter] to exit..."

24 Aug 2023

QGIS3 Virtual Layer & SQL: Calculation of Intersection Areas from (Segmented) Line Buffers and Polygon

Usecase: For a set of lines in one layer you would like to know the areas of (segmented) line buffers, that intersect with polygons from another layer. I.e., (segmented = with different buffer width segments) trail buffers, that intersect with wood patches. 

An inner buffer segment of, say 10 meters (5 meters to each side of the middle line) and an oute buffer segment, inbetween 5 and 10 meters offset from the middle line. 

 In QGIS Virtual Layers SQL you can not call Buffer function with specification of the end ("square", "flat", "round") and Buffer() will make round ends by default - so you need the workaround with ST_union() of single sided buffers to get the correct flat ends. 

Then, for the final calculations, all the feature's buffer polygons were merged into one polygon with outer ST_Union() call. 

The ST_difference() call is used to make the outer buffer segment by clipping the narrower ("Breite_Forst") from the wider buffer ("Breite_Forst"+"Breite_F_bef"). 

The final output will be to poylgons, one with the inner, narrower buffer, and one with the outer wider buffer, and the calculated areas for each polygon, and merged into one table with the UNION ALL statement.
SELECT 
	T.geom as geometry,
	ST_area(T.geom) AS Fl_Rod
FROM (
	SELECT
		ST_union(
			ST_union( 
			  intersection(st_SingleSidedBuffer(t.geometry,t.Breite_Forst/2,0), w.geometry),
			  intersection(st_SingleSidedBuffer(t.geometry,t.Breite_Forst/2,1), w.geometry)
			) 
		) AS geom
	FROM trails AS t, wood AS w
	UNION ALL 
	SELECT 
		ST_difference(
			ST_union(
				ST_union( 
				  intersection(st_SingleSidedBuffer(t.geometry,t.Breite_Forst/2+Breite_F_bef,0), w.geometry),
				  intersection(st_SingleSidedBuffer(t.geometry,t.Breite_Forst/2+Breite_F_bef,1), w.geometry)
				) 
			),
			ST_union(
				ST_union( 
				  intersection(st_SingleSidedBuffer(t.geometry,t.Breite_Forst/2,0), w.geometry),
				  intersection(st_SingleSidedBuffer(t.geometry,t.Breite_Forst/2,1), w.geometry)
				) 
			)
		) AS geom 
	FROM trails AS t, wood AS w    
) as T

12 Apr 2023

QGIS3: Elevation Gain for LinestringZ Geometry

Use this Expression in the QGIS3 Field Calculator to get the eƶevation gain for a LinestringZ geometry!
array_sum(
	array_filter(
		array_foreach(generate_series(1,num_geometries(nodes_to_points($geometry))),
			z(geometry_n( nodes_to_points($geometry), @element+1))-
			z(geometry_n( nodes_to_points($geometry), @element))
		), @element>0
	)
)

9 Mar 2023

QGIS3: Neat trick to convert color name into color rgb string

color_mix_rgb('blue', 'white', 0)
Arguments to the function are color1: a color string, color2: a color string, ratio: the ratio at which the two colors will be mixed. The output is:
'0,0,255,255'
The usecase is converting a color name, stored in a field (field name is "color_name_code"), to a color string to be used for symbolizing layer features, like so:
color_mix_rgb("color_name_code", 'white', 0.2)
which will give me a bit of a pastel tone, by mixing white into the given "color_name_code" (blue, red, green, etc.) with a ratio of 0.2

27 Feb 2023

QGIS3: Find Minimum/Maximum/Mean Raster Value (Elevation) for Polygon Overlay

For finding raster value statistics (Min, Max, Mean) for a polygon overlay, I apply this expression in a virtual field by using the field calculator. The raster layer that I'm querying is named
'DGM_merged'
The functions is looking for the min. raster value at the polygon's nodes

array_min(
	array_foreach(
		generate_series(1, num_points($geometry), 1),
		raster_value('DGM_merged', 1, 
			point_n(nodes_to_points($geometry), @element)
		)
	)
)