Plotting GRASS data in Python

GRASS GIS offers some useful but basic plotting options for raster data. However, for plotting of data in attribute tables and for more advanced graphs, we need to use other software tools. In this tutorial I explore some of the possibilities offered by Pandas plot() and how we can further tune plots using matplotlib / pyplot library.

map_municipals
Map of the municipals in Wake County, North Carolina, and for each municipal the distribution of distances to the nearest school (data source: North Carolina sample data set).

 

GRASS and Pandas – from attribute table to pandas dataframe

Introduction

In this post I show how to import an attribute table of a vector layer in a GRASS GIS database into a Pandas data frame. Pandas stands for Python Data Analysis Library which provides high-performance, easy-to-use data structures and data analysis tools for the Python programming language. For people familiar with R, the Pandas data frame is an object similar to the R data frame. They are a lot like the most common way in which spreadsheets are used, with the data presented in rectangular form with columns holding variables and rows holding observations. An important characteristic is that the data frame, like a spreadsheet, can hold different types of data in different columns: numbers, character data, dates and so on. Continue reading “GRASS and Pandas – from attribute table to pandas dataframe”

PyGRASS equivalent of piping results from one function to another

When using GRASS commands on the command line, one very neat little trick is to ‘pipe’ results from one function directly to another. An example I used in an earlier post is:

r.category My_map | r.category My_map rules=-

So, how does that works in python using pygrass?
Continue reading “PyGRASS equivalent of piping results from one function to another”

Recode your raster file in GRASS GIS using a csv file

The two easiest ways to reclassify a raster layer in GRASS GIS are using the r.reclass or r.recode functions. Although both are easy enough to use, sometimes it would be nice if you could just provide the input layer and a simple table with re-class values to create new raster maps. A fairly trivial task, so ideal to make another attempt at creating a python script.

I create a small add-on (r.recode.attr) that let you reclass/recode a raster layer based on values in a csv (comma separated text) file. The csv file should include one column with the raster values and one or more columns with the reclassification values. For each of these columns a new map will be created.  See the manual page for more information. Continue reading “Recode your raster file in GRASS GIS using a csv file”

Creating a raster layer with a weighted random sample of points (or, my first attempt to create a python script)

I needed to create a raster map layer with a weighted random sample of all raster cells, using the percentage of crop land as weight. I couldn’t find a function to create such a weighted sample, so I decided to create a script to do this for me. Continue reading “Creating a raster layer with a weighted random sample of points (or, my first attempt to create a python script)”