Sunday, June 26, 2022

QGIS Select by attribute - specific character or text string

 Lets say you want to perfect attribute selection where the entry contains a specific word or letter, this can get complicated if the word is not alone in the table cell.

There are couple of ways to solve this kind of query. The most advance way is using regular expression, I have written about regular expression on the page titled: Working with Regular Expression in QGIS

An easier approach would be to use the LIKE operator together with the % wildcard symbol. For example, lets say you want to search for ward names that contain the letter "o", the expression will look like this:-

"ward_name" LIKE '%o%'

You just need to add the text string you want to search for in between %...% as seen below:-

"ward_name" LIKE '%Add Something To Search%'

That is it!

Saturday, June 25, 2022

Useful code snippets to Clip polygon with another polygon and Convert Vector file in Geopackage to Shapefile in QGIS

 Clip polygon with another polygon using processing algorithm in QGIS

# Clip polygon with another polygon using processing algorithm in QGIS

ovly = 'C:\\Users\\Yusuf_08039508010\\Desktop\\Working_Files\\...\\Name_Bouganville Lower - N.gpkg'

outfile = ovly.split('\\')[-1].replace('Name_', '').replace('.gpkg', '')
print('Processing...', outfile)

parameters = {'INPUT':'C:\\Users\\Yusuf_08039508010\\Desktop\\Working_Files\\...\\Results\\SHP\\Woody_02.shp',
'OVERLAY':ovly,
'OUTPUT':f'C:/Users/Yusuf_08039508010/Desktop/Working_Files/.../Results/SHP/Study Location Individual Files/SHP with Holes/{outfile}.shp'}

processing.run("native:clip", parameters)



# -----------------------------------------------
# Bulk Clip polygon with another polygon using processing algorithm in QGIS

import glob

overlay_file = glob.glob('C:\\Users\\Yusuf_08039508010\\Desktop\\Working_Files\\Fiverr\\2022\\06-June\\Masking None Vegetation Ground Covers\\Results\\SHP\\Study Location Individual Files\\*.gpkg')

failed = []
for ovly in overlay_file:
    try:
        outfile = ovly.split('\\')[-1].replace('Name_', '').replace('.gpkg', '')
        print('Processing...', outfile)

        parameters = {'INPUT':'C:\\Users\\Yusuf_08039508010\\Desktop\\Working_Files\\Fiverr\\2022\\06-June\\Masking None Vegetation Ground Covers\\Results\\SHP\\Woody_02.shp',
        'OVERLAY':ovly,
        'OUTPUT':f'C:/Users/Yusuf_08039508010/Desktop/Working_Files/Fiverr/2022/06-June/Masking None Vegetation Ground Covers/Results/SHP/Study Location Individual Files/SHP with Holes/{outfile}.shp'}

        processing.run("native:clip", parameters)
    except Exception:
        failed.append(ovly)

print('Done...')



Convert Vector file in Geopackage to Shapefile

# Convert Vector file in Geopackage to Shapefile


gpkg = 'C:\\Users\\Yusuf_08039508010\\Desktop\\Working_Files\\...\\Name_Bouganville Lower - N.gpkg'

# Read vector layer and test it is valid....
input_file = QgsVectorLayer(gpkg, "polygon", "ogr")
input_file.isValid()

# Construct output file name...
out_filename = gpkg.split('\\')[-1].replace('.gpkg', '')

# Write the file to disc...
out_folder = r"C:\Users\Yusuf_08039508010\Desktop\Working_Files\...\Results\SHP\Study Location Individual Files\Failed files"
QgsVectorFileWriter.writeAsVectorFormat(input_file, f"{out_folder}\\{out_filename}.shp", "UTF-8", input_file.crs(), "ESRI Shapefile")
print('Done...') # ----------------------------------------------------------- # Bulk Convert Vector file in Geopackage to Shapefile import glob gpkg_folder = r"C:\Users\Yusuf_08039508010\Desktop\Working_Files\Fiverr\2022\06-June\Masking None Vegetation Ground Covers\Results\Deliverables\Shp\gpk" gpkg_files = glob.glob(f'{gpkg_folder}\\*.gpkg') for gpkg in gpkg_files: # Read vector layer and test it is valid.... input_file = QgsVectorLayer(gpkg, "polygon", "ogr") input_file.isValid() # Construct output file name... out_filename = gpkg.split('\\')[-1].split('_')[-1].replace('.gpkg', '') # Write the file to disc... out_folder = r"C:\Users\Yusuf_08039508010\Desktop\Working_Files\Fiverr\2022\06-June\Masking None Vegetation Ground Covers\Results\Deliverables\Shp" QgsVectorFileWriter.writeAsVectorFormat(input_file, f"{out_folder}\\{out_filename}.shp", "UTF-8", input_file.crs(), "ESRI Shapefile") print('Done...')



Different ways to read map layers into pyqgis

#1 - Read active layer from the layer panel
layer = iface.activeLayer() # or
layer = qgis.utils.iface.activeLayer()

  
#2 - Read all layers listed on the layers panel...
layers_on_panel = QgsProject.instance().mapLayers()


#3 - Read layer by name from the layer panel...
layer = QgsProject.instance().mapLayersByName('Churches')


#4 - Read from file...
vector_file = r"C:\Users\Yusuf_08039508010\Desktop\...\NGA_adm2.shp"
layer = QgsVectorLayer(vector_file, 'DISPLAYNAME', 'ogr')



Save all shapefile layer listed on the layer panel into a new directory/folder

# Save layers on layer panel to new folder...
import shutil

newFolder = r'C:\Users\Yusuf_08039508010\Desktop\Map of StudyArea\BMC SHP'

# Read all layers listed on the layers panel...
layers_on_panel = QgsProject.instance().mapLayers()

# Loop over each file and copy it to new folder location...
for k, v in layers_on_panel.items():
    # Construct file name...
    fn = v.name() + '.shp'
    
    # Get file path...
    fp1 = v.source()
    fp2 = v.source().replace('.shp', '') + '.dbf'
    fp3 = v.source().replace('.shp', '') + '.shx'
    fp4 = v.source().replace('.shp', '') + '.prj'
    fp5 = v.source().replace('.shp', '') + '.prj'
    
    print('Copying....', fn)
    print('')
    
    # Copy the shapefiles to new folder location...
    shutil.copy( fp1, newFolder ) # shutil.copy( src, dst )
    shutil.copy( fp2, newFolder )
    shutil.copy( fp3, newFolder )
    shutil.copy( fp4, newFolder )
    shutil.copy( fp5, newFolder )
    
    # break

print('Done...')


Friday, June 24, 2022

Extracting 'Standardized Precipitation-Evapotranspiration Index (SPEI)' data from netCDF file into CSV spreadsheet

 In this post, we shall learn how to use python to extract netCDF data into CSV.


What is NetCDF?

NetCDF stands for "network Common Data Form" and it is a file format for storing multidimensional scientific data (variables) such as temperature, humidity, pressure, wind speed, precipitation and direction. There are several versions of netCDF and the version we are using here is version 4 (netCDF4).


The Dataset

The dataset am going to use is the Global 01-month 1901-2020 'Standardized Precipitation-Evapotranspiration Index (SPEI )' provided by Digital.csis



Read more details on this dataset on: SPEI Global Drought Monitor Database

How to open netCDF dataset

The netCDF data can be visualized using QGIS software. This will give us a quick overview as to what variables and attributes are contained in the file.


Some GIS packages allow reading netCDF data, such as QGIS, ArcGIS and IDRISI Taiga. There are other netCDF viewers, such as Panoply (developed in Java), ncBrowse, ncview, and nCDF_Browser.