How to Install GDAL for Any Operating System
| | |

How to Install GDAL for Any Operating System

GDAL, the Geospatial Data Abstraction Library, was designed to interact with raster and vector (geospatial) data. Over time it has evolved to include sophisticated data manipulation and analysis tools. The GDAL source code is C++, which makes it fast to run but difficult to install. Many software and applications use GDAL for handling and analyzing…

The Easiest Way to Install Python on Windows 11 (also works for Mac and Linux)

The Easiest Way to Install Python on Windows 11 (also works for Mac and Linux)

Having trouble finding an easy way to install Python that always works? You’re in the right place. After years of trying different Python installation methods, I’ve come up with a method that will get you installing packages and running code in no time! The hardest part of learning Python isn’t writing Python code. There are…

How to Clip a Raster to a Polygon or an Extent with Python (Extract by Mask)
| |

How to Clip a Raster to a Polygon or an Extent with Python (Extract by Mask)

Clipping, extracting to masks, and subsetting rasters are common GIS operations. Using the gdal package for Python it is very easy to automate these processes. This tutorial will demonstrate how to use the Warp() function from the gdal Python API to clip rasters to a specified extent and to clip rasters to a polygon layer. These operations are also often referred to as mask extraction or extract to mask.

8 Ways to Create (Initialize) Pandas Data Frames
| | |

8 Ways to Create (Initialize) Pandas Data Frames

One of the key tasks for data scientists and computer programmers is to read, write, organize, and manipulate data. Perhaps the most intuitive format in which these data are stored is the tabular format. This format organizes data into tables, which are also commonly referred to as data frames or spreadsheets. Because of data, programs…

How to Install GeoPandas and Check GeoPandas Version
| |

How to Install GeoPandas and Check GeoPandas Version

GeoPandas is a high-level Python package for reading, writing, analyzing, and manipulating vector (points, lines, and polygons) geographic data. As the name ‘GeoPandas’ implies, it brings pandas data frame functionality to geographic data so that you interact with the data much as you would an attribute table in ArcMap or QGIS or a spreadsheet in…

GDAL Python Tutorial: Reading and Writing Raster Datasets
| |

GDAL Python Tutorial: Reading and Writing Raster Datasets

The Geospatial Data Abstraction Library (GDAL) is the standard for managing spatial data formats. GDAL has been incorporated into many different enterprise and open source GIS projects. Because GDAL is open source, it can be used by all. Learning to use GDAL with Python can help you automate workflows and implement custom raster processing solutions….

Run Python and Start an Interactive Session from the Windows Command Prompt (Terminal)

Run Python and Start an Interactive Session from the Windows Command Prompt (Terminal)

Python is a very versatile and popular cross-platform programming language that is used for many computational tasks. One thing that makes Python so versatile is that scripts can easily be run from both integrated development environments (IDEs) and the command prompt or terminal (check out this tutorial for instructions). Additionally, you can type and run…

Use Python to Download Multiple Files (or URLs) in Parallel
|

Use Python to Download Multiple Files (or URLs) in Parallel

Automating file downloads can save a lot of time. There are several ways for automating file downloads in Python. The easiest way to download files is using a simple Python loop to iterate through a list of URLs to download. This serial approach can work well with a few small files, but if you are…

Jupyter Notebooks in Visual Studio Code

Jupyter Notebooks in Visual Studio Code

Over the past couple of years, Visual Studio Code (VSC) has become my favorite Python IDE and all-around code editor. It works well for almost any programming language, has an intuitive layout, can be customized and extended with extensions, and (most importantly) it’s completely free. One awesome Visual Studio Code feature is that it integrates…

The Correct Way to Generate Random Numbers in Python with NumPy
|

The Correct Way to Generate Random Numbers in Python with NumPy

Random number generation is a common programming task that is required for many different programs and applications. In Python, the most common way to generate random numbers is arguably the NumPy module. NumPy is fast, reliable, easy to install, and relied on by many programs. As of 2022 (numpy version 1.22) the proper way to…

Setup Anaconda (Python) to Work With Visual Studio Code on Windows

Setup Anaconda (Python) to Work With Visual Studio Code on Windows

Visual Studio Code and Anaconda are powerful tools for Python developers. However, if you are trying to use Anaconda and Visual Studio Code together there is a good chance you have run into some problems. As common as these tools are for programming with Python, it can be difficult to figure out how to get…

10 Ways to Initialize a Numpy Array (How to create numpy arrays)
|

10 Ways to Initialize a Numpy Array (How to create numpy arrays)

In Python, matrix-like data structures are most commonly represented with numpy arrays. The numpy Python package has been well-developed for efficient computation of matrices. The first step to using numpy arrays is to initialize, or create, an array. In Python, there are many ways to create a numpy array. In this article, I’ll demonstrate how to create numpy arrays in ten different ways.

5 Easy Ways to Iterate Through a Python List or Tuple

5 Easy Ways to Iterate Through a Python List or Tuple

With Python, there is a multitude of methods to iterate through lists and tuples. Some of these methods can be complicated. If you’re just learning about iteration in Python, you’ll be best served by learning the easier, more common interaction methods that can be applied to almost any situation. Here, I present 5 easy methods to iterate through Python lists and tuples.

Read and Plot NetCDF Data in Python with xarray and rioxarray
| |

Read and Plot NetCDF Data in Python with xarray and rioxarray

Recently, I’ve started using rioxarray to read NetCDF data into xarray format. If you’re not familiar with the xarray python package it’s basically a wrapper (for lack of a better term) around numpy arrays that allows metadata to be included with the arrays (more on this later with an example).

|

How to Use numpy.where() in Python with Examples

npwhere ¶ The where function from numpy is a powerful way to vectorize if/else statements across entire arrays. There are two primary ways to use numpy.where. First, numpy.where can be used to idenefity array indices where a condition is true (or false). Second, it can be used to index and change values where a condition…

How To Run Python Scripts From the Command Line (Terminal)

Creating Python scripts that can be run from the command line makes it much easier to abstract and share your code so that it can be reused and shared with others. Running scripts from the command line can also streamline your development and analysis workflows to make them more concise and make you more productive….

How to Check if NumPy is Installed and Find Your NumPy Version
|

How to Check if NumPy is Installed and Find Your NumPy Version

The numpy Python module is widely used for many different analyses and as a dependency for many other Python packages. In many instances, it is necessary to determine if numpy is installed and which numpy version is installed. This article will show you multiple ways to determine your numpy version and installation status. Check if…

How To Install GDAL for Python with Anaconda
|

How To Install GDAL for Python with Anaconda

The Geospatial Data Abstraction Library (GDAL) is a fundamental package for spatial analysis with Python. GDAL is a powerful package with a lot of functionality. However, it is notoriously difficult to install. There are two reliable ways to install the GDAL python package: from the conda-forge channel using the conda installer or using pip to…

Sort NumPy Arrays By Columns or Rows
|

Sort NumPy Arrays By Columns or Rows

NumPy is a fundamental module used in many Python programs and analyses because it conducts numerical computations very efficiently. However, for those new to NumPy, it can be difficult to grasp at first. Specifically, understanding array indexing and sorting quickly becomes complex. Fortunately, NumPy has some built-in functions that make performing basic sorting operations quite…

PyQGIS: Calculate Geometry and Field Values with the QGIS Python API
|

PyQGIS: Calculate Geometry and Field Values with the QGIS Python API

Using attribute fields in calculations is one of the most fundamental components of GIS analysis. Field calculations are what makes it possible to take data linked to spatial features and analyze features based on what they are and where they are. Manually performing field calculations in QGIS can be tedious, especially if you’re working with…

Vectorize Moving Window Grid Operations on NumPy Arrays
| |

Vectorize Moving Window Grid Operations on NumPy Arrays

There’s a good chance you’ve done something today that used a sliding window (also known as a moving window) and you didn’t even know it. Have you done any photo editing? Many editing algorithms are based on moving windows. Do you do terrain analysis in GIS? Most topographic raster metrics (slope, aspect, hillshade, etc.) are…

Use Python to Convert Polygons to Raster with GDAL.RasterizeLayer
|

Use Python to Convert Polygons to Raster with GDAL.RasterizeLayer

When you work with spatial data it’s inevitable that you will need to implement information from both a vector and raster data source for the same location. This task can easily be accomplished manually, but it often becomes quite cumbersome when the process must be automated across a large number of features, time periods, and/or…

PyQGIS: Dissolve Vector Features with Python and QGIS
|

PyQGIS: Dissolve Vector Features with Python and QGIS

The QGIS Python API (PyQGIS) makes it really simple automate a feature dissolve. PyQGIS gives access to all the QGIS processing tools directly from the Python interface. This tutorial will demonstrate three different dissolve examples with Python in QGIS. Basic Dissolve with Python First, lets dissolve polygon features into a single polygon, this is the…

Zonal Statistics Algorithm with Python in 4 Steps
| | |

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…

Create NetCDF Files with Python
|

Create NetCDF Files with Python

Gridded, spatial data are commonly stored in NetCDF files. This is especially true for climate data. NetCDF files offer more flexibility and transparency than some traditional raster formats by supporting multiple variables and detailed metadata. Because of the metadata and file structure NetCDF files can be more difficult to access than traditional raster formats. This…

Unsupervised Land Cover Classification with Python
| | |

Unsupervised Land Cover Classification with Python

Aerial imagery is used for purposes ranging from military actions to checking out the backyard of a house you might buy. Our human brains can easily identify features in these photographs, but it’s not as simple for computers. Automated analysis of aerial imagery requires classification of each pixel into a land cover type. In other…

Parallel Programming in Python with Message Passing Interface (mpi4py)
| | | |

Parallel Programming in Python with Message Passing Interface (mpi4py)

Did you know you can write parallel Python code that will run on your laptop and on a super computer? You can, and it’s not as difficult as you might expect. If you already write code for asynchronous parallelization then you won’t even have to do much restructuring. High Performance Computing (HPC) distributes pieces of jobs across…

Asynchronous Parallel Programming in Python with Multiprocessing
| |

Asynchronous Parallel Programming in Python with Multiprocessing

A flexible method to speed up code on a personal computer Do you wish your Python scripts could run faster? Maybe they can. And you won’t (probably) have to buy a new computer, or use a super computer. Most modern computers contain multiple processing cores but, by default, python scripts only use a single core….

NetCDF with Python (netCDF4): Metadata, Dimensions, and Variables
|

NetCDF with Python (netCDF4): Metadata, Dimensions, and Variables

Important information describing the data contained in netCDF files is embedded directly in the netCDF file. Accessing and reading of this information (i.e. metadata) can help you automate tasks, debug code, and validate results easily. This tutorial covers how to access information describing the metadata, dimensions, and variables contained in a netCDF file. For the…

Python: Geographic Object-Based Image Analysis (GeOBIA) – Part 2: Image Classification
| | | |

Python: Geographic Object-Based Image Analysis (GeOBIA) – Part 2: Image Classification

Use the random forests algorithm to classify image segments into land cover categories. This post is a continuation of Geographic Object-Based Image Analysis (GeOBIA). Herein, we use data describing land cover types to train and test the accuracy of a random forests classifier. Land cover data were created in the previous post. Step-be-step video instructions…

Python: Geographic Object-Based Image Analysis (GeOBIA) – Part 1: Image Segmentation
| | | |

Python: Geographic Object-Based Image Analysis (GeOBIA) – Part 1: Image Segmentation

This tutorial will walk you through segmenting and classifying high resolution imagery using Python. Part 1 of this tutorial teaches how to segment images with Python. After you have completed Part 1, Part 2 will teach how to use machine learning methods to classify segments into land cover types with Python. YouTube videos give step-by-step…