masked_area_selection.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/bin/sh
  2. #%Module
  3. #%description: Select a circular or polygonal area
  4. #%End
  5. #%option
  6. #% key: raster
  7. #% type: string
  8. #% description: raster map to to analyse
  9. #% required: yes
  10. #%end
  11. #%option
  12. #% key: vector
  13. #% type: string
  14. #% description: vector to overlay
  15. #% required: no
  16. #%end
  17. #%option
  18. #% key: site
  19. #% type: string
  20. #% description: site to overlay
  21. #% required: no
  22. #%end
  23. #%option
  24. #% key: config
  25. #% type: string
  26. #% description: name of configuration file where insert areas
  27. #% required: yes
  28. #%end
  29. #%option
  30. #% key: north
  31. #% type: string
  32. #% description: nothern edge (use only with f flag)
  33. #% required: no
  34. #%end
  35. #%option
  36. #% key: south
  37. #% type: string
  38. #% description:south edge (use only with f flag)
  39. #% required: no
  40. #%end
  41. #%option
  42. #% key: east
  43. #% type: string
  44. #% description: east edge(use only with f flag)
  45. #% required: no
  46. #%end
  47. #%option
  48. #% key: west
  49. #% type: string
  50. #% description: west edge(use only with f flag)
  51. #% required: no
  52. #%end
  53. #%flag
  54. #% key: f
  55. #% description: sample frame yet selected
  56. #%end
  57. #%flag
  58. #% key: c
  59. #% description: take a circular area
  60. #%end
  61. # Where to find the others scripts
  62. f_path="$GISBASE/etc/r.li.setup"
  63. # Check if we have grass
  64. if test "$GISBASE" = ""; then
  65. echo "You must be in GRASS GIS to run this program." >&2
  66. exit 1
  67. fi
  68. if [ "$1" != "@ARGS_PARSED@" ] ; then
  69. exec g.parser "$0" "$@"
  70. fi
  71. #### set temporary files
  72. TMP="`g.tempfile pid=$$`"
  73. if [ $? -ne 0 ] || [ -z "$TMP" ] ; then
  74. echo "ERROR: unable to create temporary files" 1>&2
  75. exit 1
  76. fi
  77. #### environment variables
  78. g.gisenv LOCATION_NAME > $TMP.var
  79. read LOCATION < $TMP.var
  80. g.gisenv GISDBASE > $TMP.var
  81. read GISDBASE < $TMP.var
  82. g.gisenv MAPSET > $TMP.var
  83. read MAPSET < $TMP.var
  84. # show the sampling frame
  85. if [ $GIS_FLAG_f -eq 1 ] ; then
  86. g.region n=$GIS_OPT_north s=$GIS_OPT_south e=$GIS_OPT_east w=$GIS_OPT_west
  87. else
  88. g.region rast=$GIS_OPT_raster
  89. fi
  90. # open x1 monitor
  91. d.mon stop=x1
  92. d.mon start=x1
  93. d.rast -o map=$GIS_OPT_raster
  94. if [ -n "$GIS_OPT_vector" ] ; then
  95. d.vect map=$GIS_OPT_vector
  96. fi
  97. if [ -n "$GIS_OPT_site" ] ; then
  98. d.vect map=$GIS_OPT_site
  99. fi
  100. #let draw area
  101. if [ $GIS_FLAG_c -eq 1 ] ; then
  102. cp $f_path/circle.txt $$.txt
  103. echo "$$" >> $$.txt
  104. else
  105. cp $f_path/polygon.txt $$.txt
  106. echo "$$" >> $$.txt
  107. fi
  108. r.digit < $$.txt
  109. #show the selected area
  110. d.rast -o map=$$
  111. export name=$$.val
  112. $GRASS_WISH $f_path/area_query
  113. cat $name | cut -f1 -d ' ' > $name.var
  114. read ok < $name.var
  115. cat $name | cut -f2 -d' ' > $name.var
  116. r_name=""
  117. read r_name < $name.var
  118. if [ $ok -eq 1 ] ; then
  119. r.to.vect input="$$" output="v$$" feature=area
  120. g.region vect="v$$"
  121. v.to.rast input="v$$" output=$r_name value=1 use=val
  122. #write info in configuration file
  123. g.region -g| grep "n=" | cut -f2 -d'='> $name.var
  124. read north < $name.var
  125. g.region -g| grep "s=" | cut -f2 -d'=' > $name.var
  126. read south < $name.var
  127. g.region -g| grep "e=" | cut -f2 -d'=' > $name.var
  128. read east < $name.var
  129. g.region -g| grep "w=" | cut -f2 -d'=' > $name.var
  130. read west < $name.var
  131. echo "SAMPLEAREAMASKED $r_name $north|$south|$east|$west" >>\
  132. $GIS_OPT_conf
  133. #remove tmp raster and vector
  134. g.remove rast=$$
  135. g.remove vect=v$$
  136. #rm -f "$GISDBASE"/"$LOCATION"/"$MAPSET"/cats/"$$"
  137. #rm -f "$GISDBASE"/"$LOCATION"/"$MAPSET"/cell/"$$"
  138. #rm -f "$GISDBASE"/"$LOCATION"/"$MAPSET"/cellhd/"$$"
  139. #rm -fR "$GISDBASE"/"$LOCATION"/"$MAPSET"/cell_misc/"$$"
  140. #rm -f "$GISDBASE"/"$LOCATION"/"$MAPSET"/colr/"$$"
  141. #rm -f "$GISDBASE"/"$LOCATION"/"$MAPSET"/hist/"$$"
  142. #rm -fR "$GISDBASE"/"$LOCATION"/"$MAPSET"/vector/"v$$"
  143. echo DROP TABLE "v$$" | db.execute
  144. if [ $GIS_FLAG_f -eq 1 ] ; then
  145. g.region n=$GIS_OPT_north s=$GIS_OPT_south e=$GIS_OPT_east w=$GIS_OPT_west
  146. else
  147. g.region rast=$GIS_OPT_raster
  148. fi
  149. else
  150. echo 0 >> $GIS_OPT_conf
  151. fi
  152. d.mon stop=x1
  153. #clean tmp files
  154. rm -f $$*
  155. rm -f $TMP*