histogram.tcl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. ##########################################################################
  2. # histogram.tcl - raster histogram display layer options file for GRASS GIS Manager
  3. # COPYRIGHT: (C) 1999 - 2006 by the GRASS Development Team
  4. #
  5. # This program is free software under the GNU General Public
  6. # License (>=v2). Read the file COPYING that comes with GRASS
  7. # for details.
  8. #
  9. ##########################################################################
  10. namespace eval GmHist {
  11. variable array opt # hist current options
  12. variable count 1
  13. variable array tree # mon
  14. variable array lfile # histogram
  15. variable array lfilemask # histogram
  16. variable optlist
  17. variable first
  18. variable array dup # layer
  19. }
  20. proc GmHist::create { tree parent } {
  21. variable opt
  22. variable count
  23. variable lfile
  24. variable lfilemask
  25. variable optlist
  26. variable first
  27. variable dup
  28. global mon
  29. global iconpath
  30. set node "histogram:$count"
  31. set frm [ frame .histicon$count]
  32. set check [checkbutton $frm.check \
  33. -variable GmHist::opt($count,1,_check) \
  34. -height 1 -padx 0 -width 0]
  35. image create photo hico -file "$iconpath/module-d.histogram.gif"
  36. set ico [label $frm.ico -image hico -bd 1 -relief raised]
  37. bind $ico <ButtonPress-1> "GmTree::selectn $tree $node"
  38. pack $check $ico -side left
  39. #insert new layer
  40. if {[$tree selection get] != "" } {
  41. set sellayer [$tree index [$tree selection get]]
  42. } else {
  43. set sellayer "end"
  44. }
  45. $tree insert $sellayer $parent $node \
  46. -text "histogram $count"\
  47. -window $frm \
  48. -drawcross auto
  49. set opt($count,1,_check) 1
  50. set dup($count) 0
  51. set opt($count,1,map) ""
  52. set opt($count,1,opacity) 1.0
  53. set opt($count,1,color) #000000
  54. set opt($count,1,bgcolor) #ffffff
  55. set opt($count,1,bgcolor_none) 0
  56. set opt($count,1,style) "bar"
  57. set opt($count,1,font) ""
  58. set opt($count,1,nsteps) 255
  59. set opt($count,1,nulls) 0
  60. set opt($count,1,mod) 1
  61. set first 1
  62. set optlist {_check map opacity color bgcolor style font nsteps nulls}
  63. foreach key $optlist {
  64. set opt($count,0,$key) $opt($count,1,$key)
  65. }
  66. # create files in tmp diretory for layer output
  67. set mappid [pid]
  68. if {[catch {set lfile($count) [exec g.tempfile pid=$mappid]} error]} {
  69. GmLib::errmsg $error [G_msg "Error creating tempfile"]
  70. }
  71. set lfilemask($count) $lfile($count)
  72. append lfile($count) ".ppm"
  73. append lfilemask($count) ".pgm"
  74. incr count
  75. return $node
  76. }
  77. proc GmHist::set_option { node key value } {
  78. variable opt
  79. set id [GmTree::node_id $node]
  80. set opt($id,1,$key) $value
  81. }
  82. ##########################################################################
  83. proc GmHist::select_map { id } {
  84. variable tree
  85. variable node
  86. global mon
  87. set m [GSelect cell title [G_msg "Raster map for histogram"] parent "."]
  88. if { $m != "" } {
  89. set GmHist::opt($id,1,map) $m
  90. GmTree::autonamel [format [G_msg "histogram of %s"] $m]
  91. }
  92. }
  93. ##########################################################################
  94. proc GmHist::set_font { id } {
  95. variable opt
  96. if {$GmHist::opt($id,1,font) != "" } {
  97. set Gm::dfont $GmHist::opt($id,1,font)
  98. }
  99. Gm::defaultfont dhist
  100. tkwait variable Gm::dfont
  101. set GmHist::opt($id,1,font) $Gm::dfont
  102. set Gm::dfont ""
  103. }
  104. ##########################################################################
  105. # display histogram options
  106. proc GmHist::options { id frm } {
  107. variable opt
  108. global bgcolor
  109. global iconpath
  110. # Panel heading
  111. set row [ frame $frm.heading ]
  112. Label $row.a -text [G_msg "Draw histogram of values from raster map or image"] \
  113. -fg MediumBlue
  114. pack $row.a -side left
  115. pack $row -side top -fill both -expand yes
  116. #opacity
  117. set row [ frame $frm.opc]
  118. Label $row.a -text [G_msg "Opaque "]
  119. scale $row.b -from 1.0 -to 0.0 -showvalue 1 \
  120. -orient horizontal -length 300 -resolution 0.01 -fg "#656565"\
  121. -variable GmHist::opt($id,1,opacity)
  122. Label $row.c -text [G_msg " Transparent"]
  123. pack $row.a $row.b $row.c -side left
  124. pack $row -side top -fill both -expand yes
  125. # raster name for histogram
  126. set row [ frame $frm.name ]
  127. Label $row.a -text [G_msg "Raster to histogram: "]
  128. Button $row.b -image [image create photo -file "$iconpath/element-cell.gif"] \
  129. -highlightthickness 0 -takefocus 0 -relief raised -borderwidth 1 \
  130. -command "GmHist::select_map $id"
  131. Entry $row.c -width 35 -text " $opt($id,1,map)" \
  132. -textvariable GmHist::opt($id,1,map)
  133. Label $row.d -text " "
  134. Button $row.e -text [G_msg "Help"] \
  135. -image [image create photo -file "$iconpath/gui-help.gif"] \
  136. -command "spawn g.manual --q d.histogram" \
  137. -background $bgcolor -helptext [G_msg "Help"]
  138. pack $row.a $row.b $row.c $row.d $row.e -side left
  139. pack $row -side top -fill both -expand yes
  140. # graph style
  141. set row [ frame $frm.style ]
  142. Label $row.a -text [G_msg "Graph style"]
  143. ComboBox $row.b -padx 2 -width 4 -textvariable GmHist::opt($id,1,style) \
  144. -values {"bar" "pie"}
  145. Label $row.c -text [G_msg "\ttext font "]
  146. Button $row.d -image [image create photo -file "$iconpath/gui-font.gif"] \
  147. -highlightthickness 0 -takefocus 0 -relief raised -borderwidth 1 \
  148. -helptext [G_msg "select font for text"] \
  149. -command "GmHist::set_font $id"
  150. pack $row.a $row.b $row.c $row.d -side left
  151. pack $row -side top -fill both -expand yes
  152. # color
  153. set row [ frame $frm.color ]
  154. Label $row.a -text [G_msg "Histogram color: text & frame"]
  155. SelectColor $row.b -type menubutton -variable GmHist::opt($id,1,color)
  156. Label $row.c -text [G_msg " background"]
  157. SelectColor $row.d -type menubutton -variable GmHist::opt($id,1,bgcolor)
  158. Label $row.e -text " "
  159. checkbutton $row.f -text [G_msg "transparent background"] \
  160. -variable GmHist::opt($id,1,bgcolor_none)
  161. pack $row.a $row.b $row.c $row.d $row.e $row.f -side left
  162. pack $row -side top -fill both -expand yes
  163. # steps for fp maps and nulls
  164. set row [ frame $frm.steps ]
  165. Label $row.a -text [G_msg "Steps/bins for values (fp maps only)"]
  166. SpinBox $row.b -range {2 255 1} -textvariable GmHist::opt($id,1,nsteps) \
  167. -width 4 -helptext "steps/bins"
  168. Label $row.c -text " "
  169. checkbutton $row.d -text [G_msg "include null values"] \
  170. -variable GmHist::opt($id,1,nulls)
  171. pack $row.a $row.b $row.c $row.d -side left
  172. pack $row -side top -fill both -expand yes
  173. }
  174. ##########################################################################
  175. proc GmHist::save { tree depth node } {
  176. variable opt
  177. variable optlist
  178. global mon
  179. set id [GmTree::node_id $node]
  180. foreach key $optlist {
  181. GmTree::rc_write $depth "$key $opt($id,1,$key)"
  182. }
  183. }
  184. ##########################################################################
  185. proc GmHist::display { node mod } {
  186. global mon
  187. global env
  188. variable optlist
  189. variable lfile
  190. variable lfilemask
  191. variable opt
  192. variable tree
  193. variable dup
  194. variable count
  195. variable first
  196. set rasttype ""
  197. set tree($mon) $GmTree::tree($mon)
  198. set id [GmTree::node_id $node]
  199. # If we are told dirty (for zoom) force dirty
  200. # Don't remove a dirty from a previous unrendered zoom
  201. if {$mod} {set opt($id,1,mod) 1}
  202. if { $opt($id,1,map) == "" } { return }
  203. set color [GmLib::color $opt($id,1,color)]
  204. set bgcolor [GmLib::color $opt($id,1,bgcolor)]
  205. # transparent background color
  206. if { $opt($id,1,bgcolor_none) == 1 } {
  207. set bgcolor "none"
  208. }
  209. set cmd "d.histogram --q map=$opt($id,1,map) style=$opt($id,1,style) \
  210. color=$color bgcolor=$bgcolor"
  211. # include nulls
  212. if { $opt($id,1,nulls) } {
  213. append cmd " -n"
  214. }
  215. # set steps
  216. if { $opt($id,1,nsteps) != "" } {
  217. catch {set rt [open "|r.info map=$opt($id,1,map) -t" r]}
  218. set rasttype [read $rt]
  219. if {[catch {close $rt} error]} {
  220. GmLib::errmsg $error [G_msg "r.info error"]
  221. }
  222. if {[regexp -nocase ".=FCELL" $rasttype] || [regexp -nocase ".=DCELL" $rasttype]} {
  223. append cmd " nsteps=$opt($id,1,nsteps)"
  224. }
  225. }
  226. # check value of GRASS_FONT variable prior to display
  227. if {![catch {set env(GRASS_FONT)}]} {
  228. set currfont $env(GRASS_FONT)
  229. } else {
  230. set currfont "romans"
  231. }
  232. # set grass font environmental variable to user selection"
  233. if { $GmHist::opt($id,1,font) != ""} { set env(GRASS_FONT) $GmHist::opt($id,1,font) }
  234. # Decide whether to run, run command, and copy files to temp
  235. GmCommonLayer::display_command [namespace current] $id $cmd
  236. # set grass font environmental variable to whatever it was when we started
  237. # this lets different text layers have different fonts
  238. set env(GRASS_FONT) $currfont
  239. }
  240. ##########################################################################
  241. proc GmHist::mapname { node } {
  242. variable opt
  243. variable tree
  244. global mon
  245. global mapname
  246. set tree($mon) $GmTree::tree($mon)
  247. set id [GmTree::node_id $node]
  248. if { ! ( $opt($id,1,_check) ) } { return }
  249. if { $opt($id,1,map) == "" } { return }
  250. set mapname $opt($id,1,map)
  251. return $mapname
  252. }
  253. ##########################################################################
  254. proc GmHist::duplicate { tree parent node id } {
  255. variable optlist
  256. variable lfile
  257. variable lfilemask
  258. variable opt
  259. variable count
  260. variable dup
  261. variable first
  262. global iconpath
  263. set node "hist:$count"
  264. set dup($count) 1
  265. set first 1
  266. set frm [ frame .histicon$count]
  267. set check [checkbutton $frm.check \
  268. -variable GmHist::opt($count,1,_check) \
  269. -height 1 -padx 0 -width 0]
  270. image create photo hico -file "$iconpath/module-d.histogram.gif"
  271. set ico [label $frm.ico -image hico -bd 1 -relief raised]
  272. bind $ico <ButtonPress-1> "GmTree::selectn $tree $node"
  273. pack $check $ico -side left
  274. #insert new layer
  275. if {[$tree selection get] != "" } {
  276. set sellayer [$tree index [$tree selection get]]
  277. } else {
  278. set sellayer "end"
  279. }
  280. if { $opt($id,1,map) == ""} {
  281. $tree insert $sellayer $parent $node \
  282. -text "histogram $count" \
  283. -window $frm \
  284. -drawcross auto
  285. } else {
  286. $tree insert $sellayer $parent $node \
  287. -text "histogram for $opt($id,1,map)" \
  288. -window $frm \
  289. -drawcross auto
  290. }
  291. set opt($count,1,opacity) $opt($id,1,opacity)
  292. set optlist {_check map color style nsteps nulls}
  293. foreach key $optlist {
  294. set opt($count,1,$key) $opt($id,1,$key)
  295. set opt($count,0,$key) $opt($count,1,$key)
  296. }
  297. set id $count
  298. # create files in tmp directory for layer output
  299. set mappid [pid]
  300. if {[catch {set lfile($count) [exec g.tempfile pid=$mappid]} error]} {
  301. GmLib::errmsg $error [G_msg "Error creating tempfile"]
  302. }
  303. set lfilemask($count) $lfile($count)
  304. append lfile($count) ".ppm"
  305. append lfilemask($count) ".pgm"
  306. incr count
  307. return $node
  308. }