sample_area_vector.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/bin/sh -x
  2. #
  3. # This program is free software under the GPL (>=v2)
  4. # Read the COPYING file that comes with GRASS for details.
  5. #%Module
  6. #% description: Create sample area from a vector map
  7. #%End
  8. #%option
  9. #% key: raster
  10. #% type: string
  11. #% description: raster map to to analyse
  12. #% required: yes
  13. #%end
  14. #%option
  15. #% key: vector
  16. #% type: string
  17. #% description: vector map where areas are defined
  18. #% required: yes
  19. #%end
  20. #%option
  21. #% key: config
  22. #% type: string
  23. #% description: name of configuration file where insert areas
  24. #% required: yes
  25. #%end
  26. # Check if we have grass
  27. if test "$GISBASE" = ""; then
  28. echo "You must be in GRASS GIS to run this program." >&2
  29. exit 1
  30. fi
  31. if [ "$1" != "@ARGS_PARSED@" ] ; then
  32. exec g.parser "$0" "$@"
  33. fi
  34. #### set temporary files
  35. TMP="`g.tempfile pid=$$`"
  36. if [ $? -ne 0 ] || [ -z "$TMP" ] ; then
  37. echo "ERROR: unable to create temporary files" 1>&2
  38. exit 1
  39. fi
  40. #### environment variables
  41. g.gisenv LOCATION_NAME > $TMP.var
  42. read LOCATION < $TMP.var
  43. g.gisenv GISDBASE > $TMP.var
  44. read GISDBASE < $TMP.var
  45. g.gisenv MAPSET > $TMP.var
  46. read MAPSET < $TMP.var
  47. f_path="$GISBASE/etc/r.li.setup"
  48. # list categories
  49. v.build map=$GIS_OPT_vector option=cdump| grep '|' |cut -f1 -d '|' |\
  50. tr ' ' -d > $TMP.cat
  51. # number of categories
  52. N="`grep -c "" < $TMP.cat`"
  53. ((N= N -3))
  54. #scanning categories
  55. I=1
  56. while(($I<=$N)); do
  57. #extract vector[i]
  58. v.extract input=$GIS_OPT_vector output=$GIS_OPT_vector"part"$I \
  59. type=point,line,boundary,centroid,area,face new=-1 -d where="(CAT=$I)"
  60. #opening monitor x1
  61. d.mon stop=x1
  62. d.mon start=x1
  63. #setting region with raster resolution
  64. g.region vect=$GIS_OPT_vector"part"$I align=$GIS_OPT_raster
  65. d.rast -o $GIS_OPT_raster
  66. d.vect $GIS_OPT_vector"part"$I
  67. #ask the user if he wants to analyse this vector and a name for raster
  68. #in graphical mode using wish
  69. export name=$TMP.val # where find the answer
  70. $GRASS_WISH $f_path/area_query
  71. cat $name | cut -f1 -d ' ' > $name.var
  72. read ok < $name.var
  73. cat $name | cut -f2 -d' ' > $name.var
  74. r_name=""
  75. read r_name < $name.var
  76. echo $r_name
  77. if [ $ok -eq 1 ] ; then
  78. #area selected, create mask
  79. v.to.rast input=$GIS_OPT_vector"part"$I output=$r_name use=cat value=1 rows=4096
  80. #write info in configuration file
  81. g.region -g| grep "n=" | cut -f2 -d'='> $name.var
  82. read north < $name.var
  83. g.region -g| grep "s=" | cut -f2 -d'=' > $name.var
  84. read south < $name.var
  85. g.region -g| grep "e=" | cut -f2 -d'=' > $name.var
  86. read east < $name.var
  87. g.region -g| grep "w=" | cut -f2 -d'=' > $name.var
  88. read west < $name.var
  89. echo "MASKEDOVERLAYAREA $r_name|n=$north|s=$south|e=$east|w=$west" >> $GIS_OPT_conf
  90. fi
  91. #clear vector map part
  92. g.remove vect=$GIS_OPT_vector"part"$I
  93. #rm -fR "$GISDBASE"/"$LOCATION"/"$MAPSET"/vector/$GIS_OPT_vector"part"$I
  94. #echo DROP TABLE $GIS_OPT_vector"part"$I | db.execute
  95. ((I++))
  96. done
  97. d.mon stop=x1
  98. # clean tmp files
  99. rm -f $TMP*