rules.tcl 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. ################################################################################
  2. #
  3. # FILE: rules.tcl
  4. #
  5. # PURPOSE: Permits interactive rule entry for r.reclass and r.recode
  6. #
  7. # AUTHOR: Michael Barton, Arizona State University
  8. # COPYRIGHT: (C) 2007 by the GRASS Development Team
  9. # This program is free software under the GNU General Public
  10. # License (>=v2). Read the file COPYING that comes with GRASS
  11. # for details.
  12. #
  13. #
  14. ################################################################################
  15. namespace eval GmRules {
  16. variable inmap
  17. variable outmap
  18. variable rules
  19. variable overwrite
  20. global env
  21. }
  22. # G_msg.tcl should be sourced first for internationalized strings.
  23. ###############################################################################
  24. # select input map
  25. proc GmRules::select_map { seltype } {
  26. set m [GSelect $seltype title [G_msg "Select input map"] parent "."]
  27. if { $m != "" } {
  28. set GmRules::inmap $m
  29. }
  30. }
  31. ###############################################################################
  32. #Create main panel for interactie rules entry
  33. proc GmRules::main { cmd } {
  34. variable inmap
  35. variable outmap
  36. variable rules
  37. variable overwrite
  38. global env
  39. global iconpath
  40. global bgcolor
  41. #initialize variables
  42. set inmap ""
  43. set outmap ""
  44. set rules ""
  45. set overwrite 0
  46. switch $cmd {
  47. "r.colors" {
  48. set label1 "Create new color table using color rules"
  49. set label2 "Raster map:"
  50. set label3 None
  51. set label4 "Enter color rules"
  52. set btn_icon "element-cell.gif"
  53. set seltype "cell"
  54. }
  55. "r.reclass" {
  56. set label1 "Reclassify raster map using rules"
  57. set label2 "Map to reclassify: \t"
  58. set label3 "Reclassified map: \t"
  59. set label4 "Enter reclassification rules"
  60. set btn_icon "element-cell.gif"
  61. set seltype "cell"
  62. }
  63. "r.recode" {
  64. set label1 "Recode raster map using rules"
  65. set label2 "Map to recode:"
  66. set label3 "Recoded map:"
  67. set label4 "Enter recoding rules"
  68. set btn_icon "element-cell.gif"
  69. set seltype "cell"
  70. }
  71. "v.reclass" {
  72. set label1 "Reclassify vector map using SQL rules"
  73. set label2 "Map to reclassify:"
  74. set label3 "Reclassified map:"
  75. set label4 "Enter reclassification rules"
  76. set btn_icon "element-vector.gif"
  77. set seltype "vector"
  78. }
  79. }
  80. # create rules input window
  81. set rules_win [toplevel .rulesPopup]
  82. wm title $rules_win [ G_msg "Interactive rules entry" ]
  83. # put it in the middle of the screen
  84. update idletasks
  85. set winWidth [winfo reqwidth $rules_win]
  86. set winHeight [winfo reqheight $rules_win]
  87. set scrnWidth [winfo screenwidth $rules_win]
  88. set scrnHeight [winfo screenheight $rules_win]
  89. set x [expr ($scrnWidth - $winWidth) / 2-250]
  90. set y [expr ($scrnHeight - $winHeight) / 2]
  91. wm geometry $rules_win +$x+$y
  92. wm deiconify $rules_win
  93. #create the form and buttons
  94. # Title
  95. set row [ frame $rules_win.heading ]
  96. Label $row.a -text [G_msg $label1] \
  97. -fg MediumBlue
  98. pack $row.a -side top -padx 5 -pady 3
  99. pack $row -side top -fill x -expand yes
  100. # input map
  101. set row [ frame $rules_win.input ]
  102. Label $row.a -text [G_msg "$label2"]
  103. Button $row.b -image [image create photo -file "$iconpath/$btn_icon"] \
  104. -highlightthickness 0 -takefocus 0 -relief raised -borderwidth 1 \
  105. -command "GmRules::select_map $seltype"
  106. Entry $row.c -width 30 -text "$inmap" \
  107. -textvariable GmRules::inmap
  108. pack $row.c $row.b $row.a -side right -padx 3 -anchor e
  109. pack $row -side top -fill x -expand no -padx 5
  110. if { $cmd != "r.colors" } {
  111. # set output file for everything exept r.colors
  112. set row [ frame $rules_win.output ]
  113. LabelEntry $row.a -label [G_msg "$label3"] \
  114. -textvariable GmRules::outmap -width 30
  115. pack $row.a -side right -anchor e -padx 3
  116. pack $row -side top -fill x -expand no -padx 5
  117. }
  118. set row [ frame $rules_win.help ]
  119. Button $row.a -text [G_msg "Help"] \
  120. -image [image create photo -file "$iconpath/gui-help.gif"] \
  121. -command "spawn g.manual --q $cmd" \
  122. -background $bgcolor \
  123. -helptext [G_msg "Help"]
  124. pack $row.a -side right -anchor e -padx 5
  125. if { $cmd != "r.colors" } {
  126. checkbutton $row.b -variable GmRules::overwrite \
  127. -text [G_msg "Overwrite existing file"]
  128. pack $row.b -side left -anchor w -padx 5
  129. }
  130. pack $row -side top -fill x -expand no
  131. # create text widget for rules entry
  132. set row [ frame $rules_win.rulestxt ]
  133. set rules_text [text $row.a \
  134. -wrap none -relief sunken \
  135. -exportselection true \
  136. -height 15 -width 50 \
  137. -yscrollcommand "$row.b set" \
  138. -xscrollcommand "$row.c set"]
  139. scrollbar $row.b -relief sunken -command "$rules_text yview"
  140. scrollbar $row.c -relief sunken -command "$rules_text xview" \
  141. -orient horizontal
  142. pack $row.c -side bottom -fill x -expand no
  143. pack $row.a -side left -fill both -expand yes
  144. pack $row.b -side left -fill y -expand no
  145. pack $row -side top -expand yes -fill both -pady 3 -padx 5
  146. set row [ frame $rules_win.buttons ]
  147. Button $row.a -text [G_msg "OK"] -width 8 -bd 1 \
  148. -command "GmRules::process_rules $cmd $rules_text 1"
  149. Button $row.b -text [G_msg "Cancel"] -width 8 -bd 1 \
  150. -command "destroy .rulesPopup"
  151. Button $row.c -text [G_msg "Apply"] -width 8 -bd 1 \
  152. -command "GmRules::process_rules $cmd $rules_text 0"
  153. pack $row.a $row.b $row.c -side right -padx 5
  154. pack $row -side bottom -pady 3 -padx 5 -expand 0 -fill none -anchor e
  155. bind Text <Control-c> {tk_textCopy %W}
  156. bind Text <Control-v> {tk_textPaste %W}
  157. }
  158. ###############################################################################
  159. # send rules to command
  160. proc GmRules::process_rules { cmd w quit } {
  161. variable inmap
  162. variable outmap
  163. variable overwrite
  164. global devnull
  165. if { $inmap == ""} {
  166. tk_messageBox -type ok -icon warning -parent $w \
  167. -message [G_msg "You must select an input map"] -title [G_msg "No input map selected"]
  168. return
  169. }
  170. if { $cmd != "r.colors" && $outmap == ""} {
  171. tk_messageBox -type ok -icon warning -parent $w \
  172. -message [G_msg "You must specify an output map"] -title [G_msg "No output map specified"]
  173. return
  174. }
  175. # make tempfile to store rules for input into command
  176. set rulespid [pid]
  177. if {[catch {set rulesfile [exec g.tempfile pid=$rulespid]} error]} {
  178. GmLib::errmsg $error [G_msg "Error creating tempfile"]
  179. }
  180. # get rules from text widget
  181. set rules [$w get 1.0 end]
  182. set rules [string trim $rules]
  183. if { $cmd == "r.recode" } {
  184. set rules "$rules\n"
  185. }
  186. # save rules to tempfile
  187. catch {set output [open $rulesfile w ]}
  188. puts $output $rules
  189. if {[catch {close $output} error]} {
  190. GmLib::errmsg $error [G_msg "Error creating rules file"]
  191. return
  192. }
  193. set options {}
  194. if { $cmd == "r.colors"} {
  195. lappend options "map=$inmap"
  196. } else {
  197. lappend options "input=$inmap"
  198. }
  199. lappend options "rules=$rulesfile"
  200. if { $cmd != "r.colors"} {
  201. lappend options "output=$outmap"
  202. }
  203. # set overwrite flage
  204. if { $overwrite == 1} {
  205. lappend options "--o"
  206. }
  207. if {[catch {eval [list exec -- $cmd] $options} error]} {
  208. tk_messageBox -type ok -icon error -message [G_msg $error]
  209. }
  210. # delete rules file and close popup window when finished
  211. if { $quit == 1 } {
  212. file delete $rulesfile
  213. destroy .rulesPopup
  214. }
  215. }