On 06/08/2014 03:41 PM, Bleninger, Sara wrote:

Hi,

I am quite unfamiliar to GRASS GIS, so I hope, that the following question is not too elementary…

 

There are two datasets:

-          a shapefile consisting of geographical information to create a map of regions, the last column is a code for the regions

-          a file of results of R computations. The units are the regions that are coded in the shapefile. Here the code for regions is the first column. It is possible to store this file in several formats.

 

It is possible to upload the shapefile data in GRASS and make the map of regions. Now, I want to color the map according to the R results. How can I connect the R results information to the mapping information and color the map?

 


You could do this in a number of ways:

If you want to work in GRASS then v.db.join is the module you need. It would go something like:


# Make sure you are using the sqlite database backend first

db.connect -p

#Import the shape into GRASS

v.in.ogr dsn=regions.shp out=regions

# Should contain a column "code"

# Now import a csv of the R computation data

# Assume we have a column "r_code" different from the column name in the shapefile

db.in.ogr dsn=r.computations.csv out=r_comp

v.db.join map=regions column=code otable=r_comp ocolumn=r_code

# Verify that the new data exists:

v.info -c

# You can now export the joined vector data table back to a shapefile

v.out.ogr input=regions dsn=regions_merged.shp

# (Or you could create the map within GRASS...)



You might also prefer to do this straight in R, with the merge() function:

# load the rgdal library

library(rgdal)

# Import the shapefile

reg <- readOGR('regions.shp', 'regions')

# Check the structure, make sure you have the data column "code"

str(reg)

# Make the R computations a data frame (if it is not already)

r.comp.df <- as.data.frame(r.comp)

# Now merge

reg.merge <- merge(reg, r.comp.df, by.x="code", by.y="r_code")

str(reg.merge)

# If you need this as a shapefile, you can export

writeOGR(reg.merge, dsn="reg_merge.shp",layer="reg_merge", driver="ESRI Shapefile")

# or as GRASS vector. You probably will have to install the spgrass6 package first:

install.packages('spgrass6')

library(spgrass6)

# The following needs to be done when R is started *from with a GRASS session*

writeVECT6(reg.merge, "reg_merge")


Finally, most folks might prefer the simplest: to do the join right in QGIS by loading the shapefile, and using the built in JOIN capability - it's in the layer's Properties window, JOIN tab.

Now, once the join is done, in QGIS you can use Styling to color the region polygons using the joined attribute columns. In the layer's Properties window you select the Style tab, and choose "Categorized" instead of "Single Symbol", then select which column contains the categories, setup coloring, etc.


HTH,

Micha


Thanks for your help!

 

Sara

 



This mail was received via Mail-SeCure System.


_______________________________________________
grass-user mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/grass-user
This mail was received via Mail-SeCure System.



_______________________________________________
grass-user mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/grass-user

Reply via email to