## Zonal Statistics Algorithm with Python in 4 Steps

It is a common need to summarize information from a gridded dataset within an irregularly shaped area. While at first glance this may seem simple, reconciling differences between raster (gridded) and vector (polygon) datatypes can quickly become complicated. This article shows how to implement a zonal statistics algorithm in Python in 4 steps. Load raster…

## PyQGIS: Clip Vector Layers

It is very simple to clip vector layers with PyQGIS. This tutorial will demonstrate how to use Python in QGIS to clip a line layer with a polygon layer. First, import the processing module and set the paths to the line, polygon and output layers. The next step is to run the clip tool. This…

## PyQGIS: Create and Print a Map Layout with Python

With the PyQGIS API you are able to use Python in QGIS to automate the creation of map layouts. There are two reasons this is really awesome! First, once you’ve written the initial code to set up the map, you can change map symbology or map layers and exactly replicate layout. Second, once you have…

## PyQGIS: Create Raster

In a previous tutorial I showed you how to access raster values and data with the Geospatial Data Abstraction Library (GDAL). This tutorial will show you how to create a raster with GDAL. Start by importing the gdal and osr Python modules. The osr module is used for handling spatial references. Also import numpy. Now…

## PyQGIS: Get Raster Data with GDAL

PyQGIS has some methods for accessing single values in a raster, but the best way to access raster data is using the Geospatial Data Abstraction Library, or GDAL. GDAL comes pre-installed on the QGIS Python interpreter, so if you access the Python interpreter from QGIS (Plugins > Python Console) you won’t have to install any…

## PyQGIS: Delete Features

Deleting features is a common operation in QGIS. It is easy to automate this task with Python, allowing you to quickly delete features based on any conditions you set. First, select the layer from which you want to delete features. This line of code will select the first layer with the name ‘layer name’. You…

## PyQGIS: Get Feature Geometry

Geometry is what defines the shape and spatial location of a feature. Features can be points, lines, or polygons, can be anywhere on earth, and can be represented by coordinates in a number of different units (depending on the coordinate reference system, or projection). Points are represented by a single coordinate pair while lines and…

## PyQGIS: Create a Point Feature

The QGIS Python API (PyQGIS) makes it possible to automate creation of point features. This tutorial will show you how to add features to an existing shapefile. For this example we will assume the layer containing the point features is already opened in the QGIS table of contents. First, we save all layers with a…

## R: Save NetCDF as a CSV

Sometimes certain analysis may require data in a tabular format (as opposed to the gridded format of NetCDF). This tutorial will take you through how to convert a NetCDF (or other raster/gridded data) to an R data frame, which can then used for analysis or saved as a CSV file. Read the NetCDF file First,…

## numpy: Methods For Creating Arrays

It is tedious, and not practical, to manually type in values for array creation. In this tutorial we will go through methods for automating array creation and importing tabular data into numpy arrays. Creating empty arrays With numpy you don’t actually create an ‘empty’ array. But you can create an array without intializing specific values. This can be useful…

## numpy: Array shapes and reshaping arrays

We’ve gone through the basics to manually create arrays of different dimensions and shapes. There will be times that you will want to query array shapes, or automatically reshape arrays. This tutorial will show you how to use numpy.shape and numpy.reshape to query and alter array shapes for 1D, 2D, and 3D arrays. Different methods are required to find…

## numpy: Creating Arrays

Creating a simple array After numpy is installed, we can begin to create arrays. First, we’ll need to import numpy into our python project. Here I use the statement, import numpy as np, to limit my typing later. This code will allow me to use np in my script to represent instead of typing the full numpy everytime. Then, we can create a simpy…

## numpy: Install numpy

Check Installation First, check to see if you already have numpy installed. From the terminal, you can use pip to do this. If numpy is installed you will get output similar to this. If numpy is not installed no output will be shown. Install numpy numpy can be installed simply using pip. Check version You can check the numpy version using pip show as demonstrated above. Video tutorial This…

## R: Get data from a NetCDF

List the data attributes Accessing the var property of our netCDF varialbe will list the data attributes for the netCDF file. The output shows us this dataset has one data attribute with the name ‘data’. Which is contained within the names property of var. Calling the names property directly after the attributes() call gives the same result. We can get the name of specific…

## R: Open NetCDF and view metadata

Let’s get right into the code. First, make sure you have the ncdf4 package installed. This requires the netcdf4 and hdf5 software to be installed. netcdf4 and hdf5 are not r packages. There are various sources for installing these on your machine. I found this guide to work well for Ubuntu. Once all dependencies are installed we can load…