Splitting vector maps in QGIS and GRASS GIS

I am currently involved in a project that is developing a vegetation map for eastern Africa. When ready we want to make the map available to the user, as GIS data layers, but also as a web-based map. I am currently trying out OpenLayers to create such a map.

One of the features I want to include is the option for the user to hide/show different vegetation types. You can see an example here (unfortunately it doesn’t work in Internet Explorer for the moment due to some bug I haven’t been able to solve yet). That means I need to create one layer per vegetation type, i.e., I need to split up the the vegetation map according to the vegetation type.

In Quantum GIS you can do this with the ‘Split Vector Layer’ tool, which you can find under the menu Vector → Data Management Tool → Split Vector. Just give the vector to split (can be any vector layer which you can open in QGIS), the ID field to determine how to split the polygon (e.g., column with vegetation types) and the output directory (the created vector maps will be saved as shapefiles).

But I want to do this in GRASS GIS, so I can make it part of a script. I might have to look better, but for now I can’t find a similar function as ‘Split Vector Layer’ in GRAS GIS though (if I missed it, please let me know). There is the function v.extract, which allows you to selects vector objects from an existing vector map using a SQL statement and creates a new map containing only the selected objects. I created a small R script which uses the v.extract function to automate the creation of new maps based on unique entries in one of the fields in the attribute table. As usual, you need to run R within GRAS GIS (open GRASS GIS and run R on the command line).

———————————————

# User defined variables
inputmap <- "Map to split"
columnvar <- "feature to be used to split the vector"
outputdir <- "Output directory"

# Get table with column value to split up the vector map 
namelist1 <- execGRASS("v.db.select",
 parameters=list(map=inputmap,columns=c(columnvar,columncol)), flags=c("c"),intern =TRUE)
namelist1 <- unique(namelist1) # remove duplicate names

# Extract vector with values to split up vector map 
namelist2 <- sapply(namelist1,function(x){ unlist(strsplit(x,"\\|"))[1]})

# Create and execute GRASS code to extract polygons. 
for (i in 1:length(namelist1){
  i=1
  param <- list(input=inputmap, output=namelist2[i], where=paste(columnvar,"='",namelist1[i],"'",sep=""))
  flags <- c("d")
  execGRASS("v.extract", parameters=param, flags=flags)
}
---------------------------------------------
About these ads

One thought on “Splitting vector maps in QGIS and GRASS GIS

  1. Pingback: A vegetation map in OpenLayers « Ecostudies

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s