NCSU GIS/MEA582:
Geospatial Modeling and Analysis

This is an unmaintained course material, please see current material at:

Data acquisition and integration

Resources: GRASS GIS overview and manual

Understanding GRASS GIS commands

You can run GRASS several ways using its graphical user interface (wxGUI, GUI) and command line interface (CLI):

  • RECOMMENDED: for display (d.*) commands use wxGUI, for other commands (r.*, g.*, v.*) type the command name into Command console (see tab on the bottom of Layer Manager) and Enter to open its GUI;
  • run everything through wxGUI: start the command using a relevant button or select it from a drop down menu (use "Search module" to find the command);
  • for the display (d.*) commands use wxGUI, for other commands type or paste (Paste or Shift Insert) command with all options into Command console and Enter (useful when you are running out of time, but you will learn less); you can use Up/Down arrows on your keyboard to retrieve previously run command, if you want to re-run it with different options;
  • when running on Linux or Mac, paste the command with the options into the shell (you can copy/paste multiple commands at once)

Screenshot-based tutorial demonstrates these options: Getting started with GRASS GIS using wxGUI

Instructions for both wxGUI and command line approach are provided in the introductory assignments, but the instructions will be mostly command line later on. Feel free to use the approach that suits you the best. For a quick reference, here are the wxGUI equivalents for the d.* commands:

In GRASS GIS Layer Manager toolbar

  • d.rast: Add raster map layer
  • d.vect: Add vector map layer
  • d.shade: Add various raster map layers > Add shaded relief map
In GRASS GIS Map toolbar The d.* commands throughout the assignments indicate which map layers should be included in the output map, d.out.file indicates which map should be saved for the report. You might already have the right map layers in your Layer Manager, so you don't have to load them again. The CLI instructions can also be used for running the assignments on Linux and Mac as scripts.

GRASS GIS location and mapset

Before starting with GRASS GIS it is important to understand that GRASS GIS uses the concept of LOCATION, MAPSET and computational region to support efficient analysis and modeling without the need to subset and resample data. LOCATION is a project space that has common coordinate system. Each LOCATION can have several MAPSETS, one is called PERMANENT to store baseline data, others are set by users. You can modify the data only in your current MAPSET, but you can view or copy data from other MAPSETS. See also structure of GRASS database.

DO NOT MOVE the files under LOCATION directory using non-GRASS tools.

Suggested workflow is to create a new mapset for each assignment and do all computations there.

Computational region is set by g.region module (command) and specifies the extent and resolution at which raster operation are performed.

Start GRASS GIS

Start GRASS - click on GRASS icon or on Linux type:

grass70

In startup pannel set GIS Data Directory to path to datasets, for example on MS Windows, C:\Users\myname\grassdata. For Project location select nc_spm_08_grass7 (North Carolina, State Plane, meters) and for Accessible mapset create a new mapset (called e.g. HW_data_acquisition) and click Start GRASS.

Display data in 2D

First set working region in GUI:
Settings > Region > Set Region > Set current region from named region > select swwake_30m > Run > Close or alternatively, paste in the command console:
g.region swwake_30m -p

Display elevation map:

In Layer Manager > Add raster layer button > select raster "elevation" > OK.
In Map Display > Zoom to computational region extent.
Make sure the Render button (lower right corner) for automatic rendering of maps is checked on. You can also use the first button Display map in Map Display to render maps.

Alternatively, you can add raster layer by pasting this command in the command console:

d.rast elevation

Display elevation as a colored shaded map:

In Layer Manager > Add various raster maps layers > Add shaded relief map layer > select "elevation_shade" as shaded relief map and "elevation" as color map.
Under Optional tab set Percent to brighten to 30 > OK.

Command line equivalent: paste into Command console and Enter:

d.shade shade=elevation_shade color=elevation brighten=30
Note: depending on your installed GRASS version, the names of the d.shade module can be different. Open the d.shade dialog to find the correct options.

Now check the range of elevation values of the entire map. Either by pasting the following command in the command console or right clicking on the elevation raster layer and selecting Metadata in the context menu.

r.info -r elevation

Display vector line and point maps:

In Layer Manager: Add vector map layer button > select "streets_wake" > OK. Adjust the colors, line widths, symbols by clicking on relevant tabs. Add additional maps ("roadsmajor", "schools_wake").

These are the command line equivalents:

d.vect streets_wake
d.vect roadsmajor color=red width=2
d.vect schools_wake icon=basic/circle size=10 fill_color=blue

Save the displayed map:
In Map Display click Save display to graphic file.

d.out.file mymap

Display data in 3D

This is just a quick preview: We will explore 3D visualization in 3rd assignment.

In Layer Manager check off the elevation_shade layer, but keep the elevation raster on. In Map Display switch 2D view to 3D view. After the 3D mode is loaded, increase viewing height with slider, rotate puck to the south. Click Data tab and set Fine mode resolution to 1, to get the full resolution surface. Save the image by clicking on Save display to graphic file (tiff support is required) or take screenshot.

Data view and query

To get familiar with query and zoom tools, set the view back to 2D. Querying data means finding out the values in raster maps or attributes in vector maps at a location selected by mouse. In Layer Manager select the map layer(s) to query by clicking on it. In Map Display click button Query raster/vector map(s) and click on a map at locations where you want to know the values/attributes.

In Map Display explore the options to zoom: zoom-in, zoom-out, zoom to selected map, zoom to computational region. You can also zoom to specific map layer by right clicking on the layer and selecting Zoom to selected map(s).

Now display statewide DEM, county boundaries and climate stations:

Remove all map layers from Layer Manager.
In Layer Manager menu: Settings > Region > Set Region.
In g.region dialog: Set current region from named region > select "nc_500m" > Run > Close.
In Map Display: Zoom to computational region.
In Layer Manager: Add raster layer > select "elev_state_500m".
Add vector map layer "precip_30ynormals", set the symbol to basic/marker.
Add vector map layer boundary_county, to display only county boundary, under tab Selection switch off areas. Rearrange the layers by mouse if needed.

The command line equivalent is below, except for removing all map layers from the Layer Manager. When using GUI and its Command Console, you can either uncheck the map layers or completely remove them in the Layer Manager. When using the system command line and d.mon, you can use d.erase.

g.region nc_500m -p

d.rast elev_state_500m
d.vect precip_30ynormals icon=basic/marker
d.vect boundary_county type=boundary
d.out.file nc_precip_stations

Managing data

We will learn how to find more information about the data, copy and rename maps.

List raster, vector and region (saved extents) data in the provided database:
In GUI: File > Manage maps > List.

Or paste the following command into command console:

g.list -f type=raster,vector,region

List only vector maps starting with "s":
In GUI: File > Manage Maps > List.
In tab Required -> select type vector map(s) and in tab Pattern type s* for Map name search pattern.

Or paste the following command into command console:

g.list type=vector pattern="s*"

Find the working coordinate system and spatial extent:
In GUI: Settings > Map projections > Display map projection
In GUI: Settings > Region > Display Region

Or paste the following command into command console:

g.proj -p
g.region -pl

Find out how to copy, rename and remove maps:
GUI: File > Manage maps > Copy > select/type in names from,to
GUI: File > Manage maps > Rename > select/type in names from,to
GUI: File > Manage maps > Delete > select type and name and check Force removal.
Note that you can only remove maps from the current working mapset.

Or paste the following command into command console:

g.copy raster=geology_30m,mygeology
g.rename raster=mygeology,mygeology_30m
g.list raster
g.remove type=raster name=mygeology_30m -f

Find information about individual maps layers and their attributes:
In GUI: right click raster layer "elevation", select Metadata, right click vector layer "streets_wake", select Metadata, Show attribute data.

Or paste the following command into command console:

r.info elevation
v.info streets_wake
v.info -c streets_wake

All data (maps) are stored in your GRASS database directory. You can save your project into a GRASS workspace file *.gxw under File > Workspace > Save as.

Import and export for raster and vector data

In this section we will cover importing and exporting of raster and vector data which are in the same projection as the GRASS location we work in. Cases where the projection differs are covered in a separate guide.

Import and export Shapefiles

Download Shapefile with geodetic points: geod_pts_spm.zip. Save and unzip it on Desktop or a directory where you have read/write permissions.

To import file in GUI use:

  • File > Import vector data > Common import formats.
  • In the dialog, select Source type > Directory.
  • Use Browse button to find the path to the directory "geod_pts_spm" to fill the Source name field.
  • Then click on Import button.
In case the imported point data was not displayed automatically, use Add vector to display the data and zoom to the vector layer.
Compare the imported vector with "streets_wake" vector to see how they are distributed.

Export the roadsmajor map as a shape file:

  • File > Export > Common formats
  • Select roadsmajor as Name of input vector map
  • Type C:\mydirectory\roadsmajor path as OGR output datasource name (path should be to a directory where you have write permission)
  • click Run
You can then display the roadsmajor in ArcGIS.

Import and export of vectors when scripting

For scripting workflows a command line import/export can be used.
It requires properly set path to external files. In this case, "gdc.shp" would be replaced, e.g. by "C:\path_to_my_file\gdc.shp" on MS Windows and by "/path_to_my_file/gdc.shp" on unix-like systems. And the full path, e.g. "C:\path_to_my_file\roadsmajor.shp", would be used instead of "roadsmajor.shp".

v.in.ogr input=gdc.shp output=geodetic_points
v.out.ogr input=roadsmajor output=roadsmajor.shp
On MS Windows it is often easier to do the import through GUI to get the correct path to your data.
On Mac, Linux and other systems you can use current working directory and/or auto-complete in the system command line to avoid typing the full path; you can of course use GUI if you prefer.

Import raster file provided in GeoTIFF format

Download a landuse raster in GeoTIFF format (referenced TIFF image): lc96ras_cut.tif. Save it on Desktop or in a directory of your choice.

To import file in GUI use: File > Import raster data > Common import formats. Keep the default GeoTIFF as the Format and use Browse button to find the path to downloaded "lc96ras_cut.tif" file.
Make sure the file is checked on in the List of GDAL layers. Click Import.

On MS Windows, the import is often easier to do through GUI because of getting the correct path to your data. In general, you can use the command line alternative below.
Note that it requires you to provide a full path to your input data, so you would have to replace "lc96ras_cut.tif" by something like "C:\path_to_my_file\lc96ras_cut.tif" or "/path_to_my_file/lc96ras_cut.tif" depending on the location of data and platform you are using.

r.in.gdal input=lc96ras_cut.tif output=landuse96_subset

Exporting raster files as ASCII grid and GeoTIFF

Now, export raster files as ASCII grid and GeoTIFF.

Use File > Export raster map > Common export formats.
Do not forget to specify the full path to your output files otherwise you might have hard time finding them especially on MS Windows.
Alternatively, you can use command line but again, don't forget to specify full path to output file or set your working directory to desired location.

r.out.gdal input=elev_ned_30m output=elev_ned_30m.ascii format=AAIGrid
r.out.gdal input=basin_50K output=basin_50K.tif

Optional: Add WMS layer

View and download orthophoto through web mapping service. Requires good internet connection. If it doesn't work, report the problem including any error message in the report.
First change region to the small rural area and display the provided orthophoto.
g.region rural_1m -p
d.rast ortho_2001_t792_1m

In Layer Manager toolbar Add web service layer.
Paste the link to the service into server field and press connect:

http://raster.nationalmap.gov/arcgis/services/Orthoimagery/USGS_EROS_Ortho_1Foot/ImageServer/WMSServer?
Wait until GUI changes. Be patient, it can take up to one minute.
In list of layers, there should be "Orthoimagery_All", click on that.
Press Add layer button and wait.
The dialog can be closed afterwards.

To save the layer:
Right click on the web service layer in Layer Manager - choose Save web service layer. In dialog, set the name of the layer ortho_new and press Save layer.