<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PyQGIS - GIS Course | TYC GIS Training</title>
	<atom:link href="https://www.giscourse.com/tag/pyqgis/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.giscourse.com</link>
	<description>Courses &#38; Training - ArcGIS 10, ArcGIS PRO, QGIS, Python, JS, Development</description>
	<lastBuildDate>Wed, 21 Oct 2020 08:10:58 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.2.6</generator>

<image>
	<url>https://www.giscourse.com/wp-content/uploads/2017/07/cropped-flavicon-01-32x32.png</url>
	<title>PyQGIS - GIS Course | TYC GIS Training</title>
	<link>https://www.giscourse.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Automatically merge raster files using PyQGIS</title>
		<link>https://www.giscourse.com/automatically-merge-raster-files-using-pyqgis/</link>
					<comments>https://www.giscourse.com/automatically-merge-raster-files-using-pyqgis/#comments</comments>
		
		<dc:creator><![CDATA[Arturo Mendes]]></dc:creator>
		<pubDate>Tue, 12 May 2020 10:49:36 +0000</pubDate>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[PyQGIS]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[QGIS]]></category>
		<guid isPermaLink="false">https://www.giscourse.com/?p=17267</guid>

					<description><![CDATA[In this post we are going to learn how to merge massive amounts of raster files automatically using PyQGIS. This way we will avoid all the crashes and troubles associated with the processing script found in the toolbox. And of course it saves us all the trouble of having to manually select the files. TL;  [...]]]></description>
										<content:encoded><![CDATA[<p style="text-align: justify;">In this post we are going to learn how to merge massive amounts of raster files automatically using PyQGIS. <span id="more-17267"></span>This way we will avoid all the crashes and troubles associated with the processing script found in the toolbox. And of course it saves us all the trouble of having to manually select the files.</p>
<h3 style="text-align: justify;">TL; DR, the solution</h3>
<p style="text-align: justify;">We will arrive to the solution after taking two simple steps. The first step will be creating a VRT file. A VRT file is a Virtual Dataset that contains all of the images merged. Second and final step will be just to have that file translated to the desired extension.</p>
<pre class="prettyprint lang-python" data-start-line="1" data-visibility="visible" data-highlight="5" data-caption="">vrt_path = os.path.join(BASE_PATH, 'prov_vrt.vrt')
vrt = gdal.BuildVRT(vrt_path, list)

result = os.path.join(BASE_PATH, 'merged.tif')
gdal.Translate(result, vrt, format='GTiff')</pre>
<p style="text-align: justify;">Here the variable list is a list composed of the path of each and every file we want to merge. This is important, don&#8217;t rush with the copypasting or you will get an error.</p>
<h3 style="text-align: justify;">Complete solution</h3>
<p style="text-align: justify;">If you are starting from scratch you will have to import the libraries <em>os</em> and <em>pathlib</em>. They will help us explore folders and files with ease.</p>
<p style="text-align: justify;">In this code <em>glob</em> will help us filter the files in the folder to only those with the extension TIF.</p>
<pre class="prettyprint lang-python" data-start-line="1" data-visibility="visible" data-highlight="15, 18" data-caption="">from pathlib import Path
from osgeo import gdal
import os

folder_path = r"C:\Users\Usuario\Desktop\input"
BASE_PATH = os.path.dirname(os.path.abspath(folder_path))
folder = Path(folder_path)

l = []

for f in folder.glob('**/*.tif'):
    f_path = f.as_posix()
    l.append(f_path)

vrt_path = os.path.join(BASE_PATH, 'prov_vrt.vrt')
vrt = gdal.BuildVRT(vrt_path, l)

result = os.path.join(BASE_PATH, 'merged.tif')
gdal.Translate(result, vrt, format='GTiff')</pre>
<p style="text-align: justify;">The processing script found in the toolbox as well as the use of <em>gdal_merge</em> imply that every image to be merged has to be loaded previously in memory. The code above bypasses this step, thus avoiding all the problems related to lack of memory.</p>
<p style="text-align: right;">Note: There is a rating embedded within this post, please visit this post to rate it.</p>
<h2 style="text-align: center;">Quality training taught by professionals</h2>
<p style="text-align: center;"><a href="https://www.giscourse.com/qgis-courses/qgis-course-beginner-to-advanced-level-online-gis-training/" target="_blank" rel="noopener noreferrer"><img decoding="async" class="alignnone wp-image-17065 size-medium" src="https://www.giscourse.com/wp-content/uploads/2020/03/specialist-qgis_cuadrada-300x300.jpg" alt="" width="300" height="300" srcset="https://www.giscourse.com/wp-content/uploads/2020/03/specialist-qgis_cuadrada-66x66.jpg 66w, https://www.giscourse.com/wp-content/uploads/2020/03/specialist-qgis_cuadrada-100x100.jpg 100w, https://www.giscourse.com/wp-content/uploads/2020/03/specialist-qgis_cuadrada-150x150.jpg 150w, https://www.giscourse.com/wp-content/uploads/2020/03/specialist-qgis_cuadrada-200x200.jpg 200w, https://www.giscourse.com/wp-content/uploads/2020/03/specialist-qgis_cuadrada-300x300.jpg 300w, https://www.giscourse.com/wp-content/uploads/2020/03/specialist-qgis_cuadrada-400x400.jpg 400w, https://www.giscourse.com/wp-content/uploads/2020/03/specialist-qgis_cuadrada-600x600.jpg 600w, https://www.giscourse.com/wp-content/uploads/2020/03/specialist-qgis_cuadrada.jpg 627w" sizes="(max-width: 300px) 100vw, 300px" /></a>   <a href="https://www.giscourse.com/qgis-courses/qgis-course-beginner-level-online-gis-training/" target="_blank" rel="noopener noreferrer"><img decoding="async" class="alignnone wp-image-15588 size-medium" src="https://www.giscourse.com/wp-content/uploads/2017/12/Online-Course-QGIS-Beginner-Level-300x300.jpg" alt="" width="300" height="300" srcset="https://www.giscourse.com/wp-content/uploads/2017/12/Online-Course-QGIS-Beginner-Level-66x66.jpg 66w, https://www.giscourse.com/wp-content/uploads/2017/12/Online-Course-QGIS-Beginner-Level-120x120.jpg 120w, https://www.giscourse.com/wp-content/uploads/2017/12/Online-Course-QGIS-Beginner-Level-150x150.jpg 150w, https://www.giscourse.com/wp-content/uploads/2017/12/Online-Course-QGIS-Beginner-Level-200x200.jpg 200w, https://www.giscourse.com/wp-content/uploads/2017/12/Online-Course-QGIS-Beginner-Level-300x300.jpg 300w, https://www.giscourse.com/wp-content/uploads/2017/12/Online-Course-QGIS-Beginner-Level-400x400.jpg 400w, https://www.giscourse.com/wp-content/uploads/2017/12/Online-Course-QGIS-Beginner-Level-500x500.jpg 500w, https://www.giscourse.com/wp-content/uploads/2017/12/Online-Course-QGIS-Beginner-Level-600x600.jpg 600w, https://www.giscourse.com/wp-content/uploads/2017/12/Online-Course-QGIS-Beginner-Level-768x769.jpg 768w, https://www.giscourse.com/wp-content/uploads/2017/12/Online-Course-QGIS-Beginner-Level-800x801.jpg 800w, https://www.giscourse.com/wp-content/uploads/2017/12/Online-Course-QGIS-Beginner-Level-1024x1024.jpg 1024w, https://www.giscourse.com/wp-content/uploads/2017/12/Online-Course-QGIS-Beginner-Level-1200x1201.jpg 1200w, https://www.giscourse.com/wp-content/uploads/2017/12/Online-Course-QGIS-Beginner-Level.jpg 1307w" sizes="(max-width: 300px) 100vw, 300px" /></a></p><p>The post <a href="https://www.giscourse.com/automatically-merge-raster-files-using-pyqgis/">Automatically merge raster files using PyQGIS</a> first appeared on <a href="https://www.giscourse.com">GIS Course  |  TYC GIS Training</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.giscourse.com/automatically-merge-raster-files-using-pyqgis/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
