-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathr.commons.py
More file actions
127 lines (110 loc) · 3.97 KB
/
r.commons.py
File metadata and controls
127 lines (110 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/python
#
############################################################################
#
# MODULE: r.commons.py
# AUTHOR(S): Isaac Ullah, Arizona State University
# PURPOSE: Define the areas that can be considered "the commons" for analysis of the "Tragedy of the Commons" in an area with multiple sites. Module requires r.walk.
# ACKNOWLEDGEMENTS: National Science Foundation Grant #BCS0410269
# COPYRIGHT: (C) 2009 by Isaac Ullah, Michael Barton, Arizona State University
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#
#############################################################################
#%Module
#% description: Define the areas that can be considered "the commons" for analysis of the "Tragedy of the Commons" in an area with multiple sites using the "commons equation". Module requires r.walk.
#%END
#%option
#% key: elev
#% type: string
#% gisprompt: old,cell,raster
#% description: Input elevation map (DEM)
#% required : yes
#%END
#%option
#% key: vect
#% type: string
#% gisprompt: old,vector,vector
#% description: Name of input vector points map containg the set of sites for this analysi.
#% required : yes
#%END
#%option
#% key: x_column
#% type: string
#% description: Column containing x values for site coordinates
#% required : yes
#%END
#%option
#% key: y_column
#% type: string
#% description: Column containing y values for site coordinates
#% required : yes
#%END
#%option
#% key: i_column
#% type: string
#% description: Column containing values of i ("importance index") for each site in input vector map
#% required : yes
#%END
#%option
#% key: name_column
#% type: string
#% description: Column with unique identifiers for each site in input vector map (CAT column can be used)
#% required : yes
#%END
#%option
#% key: cvmax
#% type: string
#% description: Cost surface value (seconds of walking time) encompassing the zone of 100% usage for the largest/most important site in site set.
#% required : yes
#%END
#%option
#% key: frict
#% type: string
#% gisprompt: old,cell,raster
#% description: Optional map of extra "friction" costs. If no map selected, "friction" = 1
#% answer:
#% required : no
#%END
#%flag
#% key: k
#% description: -k Use knight's move for calculating cost surface (slower but more accurate)
#%END
#%flag
#% key: c
#% description: -c Keep all the interim cost surface used to calculate the commons equation
#%END
import sys
import os
import subprocess
import tempfile
import random
grass_install_tree = os.getenv('GISBASE')
sys.path.append(grass_install_tree + os.sep + 'etc' + os.sep + 'python')
import grass.script as grass
#main block of code starts here
def main():
#bring in input variables
elev = os.getenv("GIS_OPT_elev")
vect = os.getenv("GIS_OPT_vect")
xcol = os.getenv("GIS_OPT_cfact_x_column")
ycol = os.getenv("GIS_OPT_y_column")
icol = os.getenv("GIS_OPT_i_column")
namecol = os.getenv("GIS_OPT_name_column")
#read in info from the table of the vector sites map, and parse it into a list of lists of info for each site
s1 = grass.read_command("v.db.select", quiet = "True", map = vect, columns = xcol + "," + ycol + "," + icol + "," + namecol, fs = ",", nv = "False").strip()
masterlist = []
for item in s1.split("\n"):
masterlist.append(item.strip("\n").split(","))
#the first row is the column names, so pop that out of our master list
indexlist = masterlist.pop(0)
#now, loop through the master list and run r.walk for each of the sites, and append the cost surfaces to a list (so we can work with them later)
for site in masterlist:
#DO SOME CODE ### STILL TO DO ###
# here is where the code in "main" actually gets executed. This way of programming is neccessary for the way g.parser needs to run.
if __name__ == "__main__":
if ( len(sys.argv) <= 1 or sys.argv[1] != "@ARGS_PARSED@" ):
os.execvp("g.parser", [sys.argv[0]] + sys.argv)
else:
main()