NCSU GIS/MEA582:
Geospatial Modeling and Analysis

Geospatial Data Introduction

Resources: GRASS GIS overview and manual

Understanding GRASS GIS commands

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

  • RECOMMENDED: for display (d.*) commands use GUI, for other commands (r.*, g.*, v.*) type the command name into the command console in the Console tab (see the tabs at the bottom of the Layer Manager) and then press Enter to open command (module) GUI;
  • run everything through GUI: start the command using a relevant button or select it from a drop down menu (use the Modules to find the command);
  • for the display (d.*) commands use GUI, for other commands type or paste (Paste or Shift Insert) command with all options into 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)

Tutorials:

Instructions for both GUI 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 GUI 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

If you don't already have GRASS GIS spatial database nc_spm_08_grass7 downloaded, you can find it on Course logistics page.

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 GIS as any other program on your computer or in command line type:

grass74

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

Change working directory:
Settings > GRASS working environment > Change working directory > select/create any directory
or type cd (stands for change directory) into the GUI Console and hit Enter:

cd

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 in the Console tab:
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 into the Console tab:

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 the Console and press Enter:

d.shade shade=elevation_shade color=elevation brighten=30

Now check the range of elevation values of the entire map. Either by pasting the following command into the 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 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.erase
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 existing data

Browse raster and vector data in the provided database using the Data tab in the Layer Manager. The tree shows locations and mapsets in your GRASS GIS database.

You can also paste the following command into the Console to list the raster and vector maps:

g.list -f type=raster,vector
You can also use left mouse click in the Data tab to add maps into Layers and display them this way.

Now list only vector maps which start with "s": In the Data tab enter to the Search box the following v:^s which limits the search to vectors using v: which start (^) with the letter s.

Or use g.list in the Console tab:

g.list type=vector pattern="s*"
Note that g.list by default uses different pattern matching syntax than the Console tab. If you use the g.list dialog in GUI, in tab Required select type vector map(s) and in tab Pattern enter s* for the Map name search pattern field.

Now list saved computational regions (which are extents and resolutions bookmarked in the database): In the File menu or in the Modules tab: Find Manage Maps > List to get the g.list dialog. In the dialog tab Required select type region definition(s). Then hit Enter or press Run.

Or just paste the following command into the Console tab:

g.list -p type=region
Alternatively, you can type g.list into the Console and hit Enter to get the module dialog that way.

Copy and remove data

You can copy and remove maps using the Data tab in the Layer Manager.

First find the raster map named geology_30m in the PERMANENT mapset and use right click to Copy it. Then find again the current mapset and use right click and Paste to create a copy the geology_30m in the current mapset. You will be prompted for the new name, change it to mygeology.

Than let's say we want to rename it. Use right click again on the new copy, select Rename and type mygeology_30m. Finally, let's delete this copy using right click and Delete.

The above instructions for copying, renaming, and deleting can be done in the Console using the modules g.copy, g.rename, and g.remove as follows:

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

You can find these modules in the GUI in the File menu or in the Modules tab under Manage Maps.

Note that you can only remove maps from the current mapset and also note that the Data tab asks for confirming the removal and the g.remove requires you to use the -f flag.

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 the 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 (or browse) C:\mydirectory\roadsmajor.shp path as OGR output datasource name (path should be to a directory where you have write permission)
  • Select ESRI_Shapefile in Data format to write
  • click Run
You can then display the roadsmajor in ArcGIS.

Coordinate system, spatial extent, and resolution

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 the Console tab:

g.proj -p
g.region -pl

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.import 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

Creating new GRASS Location

We will create new Locations based on EPSG code and based on georeferenced file.

First we create a new Location with Spatial Reference System (SRS) given by EPSG code 3404. Which SRS has EPSG 3404 and what are its characteristics?

Launch Location Wizard from Layer Manager menu > Settings > GRASS working environment > Create new location
or from GRASS Startup window > Select GRASS Location > New.
In Project Location field type "nc_spf" > Next
Select method Select EPSG code of spatial reference system > Next
Type EPSG code 3404 > Next > OK > Finish
Set default region extents? > No
In this new location review the projection information:

g.proj -p

Another way to set up a GRASS Location uses SRS information from a georeferenced file. Again launch the Location Wizard:

In Project Location field type "nc_spm" > Next
Select method Read projection and datum terms from a georeferenced data file > Next
Browse to downloaded file lc96ras_cut.tif > Next > Finish
Import data? > Yes > OK
Review again the projection information:

g.proj -p

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:

https://services.nationalmap.gov/arcgis/services/USGSNAIPPlus/MapServer/WMSServer?
Wait until GUI changes. Be patient, it can take up to one minute.
In list of layers, there should be "USGSNAIPImagery", 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.