/usr/lib/grass64/etc/gm/profile.tcl is in grass-gui 6.4.3-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 | ##########################################################################
# profile.tcl - Dialog to create profile of raster surface for GRASS GIS Manager
# May 2006 Michael Barton, Arizona State University
# COPYRIGHT: (C) 1999 - 2007 by the GRASS Development Team
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#
##########################################################################
namespace eval GmProfile {
variable pcan
variable status ""
variable pmap ""
variable pmainframe
variable transect ""
variable tottransect ""
variable pcoords ""
variable pcoordslist ""
variable profilelist ""
variable x
variable y
variable first 1
variable firsteast 0.0
variable firstnorth 0.0
variable east1 0.0
variable north1 0.0
variable east2 0.0
variable north2 0.0
variable tlength 0.0
variable tottlength 0.0
variable linex1 0.0
variable liney1 0.0
variable linex2 0.0
variable liney2 0.0
variable array psegment len ;#
variable elevrange 0.0
variable elevmax 0.0
variable elevmin 0.0
variable elev 0.0
variable msg
}
###############################################################################
# select raster map to profile
# select base raster map from list and put name in layer tree node
proc GmProfile::select_rast { } {
variable pmap
variable status
set m [GSelect cell title [G_msg "Raster map for profile"] parent [winfo containing [winfo pointerx .] [winfo pointery .]]]
if { $m != "" } {
set pmap $m
}
set GmProfile::status [G_msg "Profile for $pmap"]
}
###############################################################################
# create canvas for displaying profile
proc GmProfile::create { mapcan } {
global gmpath
global iconpath
global env
global bgcolor
global mon
variable pmap
variable pcan
if { [winfo exists .profile] } {return}
set ptitle [G_msg "Profile Window"]
toplevel .profile
wm title .profile [G_msg $ptitle]
wm withdraw .profile
#wm overrideredirect $txt 1
# create canvas for profile
set profilemf [MainFrame .profile.mf \
-textvariable GmProfile::status]
set profile_frame [$profilemf getframe]
set pcan [canvas $profile_frame.can -bg white\
-borderwidth 0 -closeenough 1.0 \
-relief ridge -selectbackground #c4c4c4 \
-width 600 -height 300 ]
# setting geometry
place $pcan \
-in $profile_frame -x 0 -y 0 -anchor nw \
-bordermode ignore
# profile control buttons
set pcan_tb [$profilemf addtoolbar]
pack $pcan_tb -side left -expand yes -fill x
set pcanbb [ButtonBox $pcan_tb.bb -orient horizontal]
$pcanbb add -image [image create photo -file "$iconpath/element-cell.gif"] \
-command "GmProfile::select_rast" \
-highlightthickness 0 -takefocus 0 -relief link -borderwidth 1 \
-highlightbackground $bgcolor -activebackground $bgcolor\
-helptext [G_msg "Select raster map to profile.\nCurrently selected raster is default."] -highlightbackground $bgcolor
$pcanbb add -image [image create photo -file "$iconpath/gui-profiledefine.gif"] \
-command "GmProfile::profilebind $mapcan" \
-highlightthickness 0 -takefocus 0 -relief link -borderwidth 1 \
-highlightbackground $bgcolor -activebackground $bgcolor\
-helptext [G_msg "Draw profile transect in map display"] -highlightbackground $bgcolor
$pcanbb add -image [image create photo -file "$iconpath/gui-profiledraw.gif"] \
-command "GmProfile::pdraw" \
-highlightthickness 0 -takefocus 0 -relief link -borderwidth 1 \
-highlightbackground $bgcolor -activebackground $bgcolor\
-helptext [G_msg "Draw profile"] -highlightbackground $bgcolor
$pcanbb add -image [image create photo -file "$iconpath/gui-erase.gif"] \
-command "GmProfile::perase $mapcan" \
-highlightthickness 0 -takefocus 0 -relief link -borderwidth 1 \
-highlightbackground $bgcolor -activebackground $bgcolor\
-helptext [G_msg "Clear profile"] -highlightbackground $bgcolor
$pcanbb add -image [image create photo -file "$iconpath/file-save.gif"] \
-command "GmProfile::psave" \
-highlightthickness 0 -takefocus 0 -relief link -borderwidth 1 \
-highlightbackground $bgcolor -activebackground $bgcolor\
-helptext [G_msg "Save profile to EPS file"] -highlightbackground $bgcolor
set helpbtn [Button $pcan_tb.help -text [G_msg "Help"] \
-image [image create photo -file "$iconpath/gui-help.gif"] \
-command "spawn g.manual --q gm_profile" \
-background $bgcolor -borderwidth 1 \
-helptext [G_msg "Help"]]
# add a menu button to set preferences?
pack $profilemf -expand yes -fill both -padx 0 -pady 0
pack $pcan -fill both -expand yes
pack $helpbtn -side right -anchor e
pack $pcanbb -side left -anchor w
set GmProfile::status [G_msg "Profile for $pmap"]
$profilemf showstatusbar status
pack $profilemf -fill both -expand yes
BWidget::place .profile 0 0 at 500 100
wm deiconify .profile
# bindings for closing profile window
bind .profile <Destroy> "GmProfile::cleanup %W $mapcan"
}
###############################################################################
proc GmProfile::setelev { pmap } {
variable elevrange
variable elevmax
variable elevmin
global devnull
#calculate elevation range so all profiles with same window geometry with have
# same scale. Elevation range calcuated within currently displayed region.
#set input [exec r.describe -rdq map=$pmap]
#regexp -nocase {^([0-9]+) thru ([0-9]+)} $input trash elevmin elevmax
#set elevrange [expr $elevmax - $elevmin]
if {![catch {open "|r.univar --q -g map=$pmap 2> $devnull" r} input]} {
while {[gets $input line] >= 0} {
regexp -nocase {^([a-z]+)=(.*)$} $line trash key value
set elev($key) $value
}
if {[catch {close $input} error]} {
GmLib::errmsg $error [G_msg "r.univar error"]
}
set elevmax $elev(max)
set elevmin $elev(min)
set elevrange $elev(range)
}
#vwait elevrange
#set GmProfile::status [G_msg "Profile for $pmap"]
}
###############################################################################
# save profile to eps file
proc GmProfile::psave { } {
variable pcan
global env
set types {
{{EPS} {.eps}}
}
set path [tk_getSaveFile -initialdir $Gm::last_directory -filetypes $types \
-defaultextension ".eps" -parent .profile]
$pcan postscript -file "$path"
if { $path != "" } {
set Gm::last_directory [file dirname $path]
}
return
}
###############################################################################
# procedures for marking profile location
# bindings for drawing profile transect
proc GmProfile::profilebind { mapcan } {
variable measurement_annotation_handle
variable tlength
variable tottlength
variable linex1
variable liney1
variable linex2
variable liney2
variable first
variable msg
global mon
# Make the output for the measurement
set measurement_annotation_handle [monitor_annotation_start $mon "Measurement" {}]
if {[info exists linex1]} {unset linex1}
if {[info exists liney1]} {unset liney1}
if {[info exists linex2]} {unset linex2}
if {[info exists liney2]} {unset liney2}
bind $mapcan <1> "GmProfile::marktransect $mapcan %x %y"
bind $mapcan <B1-Motion> "GmProfile::drawtransect $mapcan %x %y"
bind $mapcan <ButtonRelease-1> "GmProfile::getcoords $mapcan"
set GmProfile::msg "Draw profile transect with mouse"
MapCanvas::setcursor $mon "pencil"
set tlength 0
set tottlength 0
}
# start profile transect
proc GmProfile::marktransect {mapcan x y} {
variable transect
variable tlength
variable tottlength
variable linex1
variable liney1
variable linex2
variable liney2
variable first
variable firsteast
variable firstnorth
variable pcoords
global mon
#start line
if { ![info exists linex1] } {
set linex1 [$mapcan canvasx $x]
set liney1 [$mapcan canvasy $y]
}
if { $first == 1 } {
set firsteast [MapCanvas::scrx2mape $mon $linex1]
set firstnorth [MapCanvas::scry2mapn $mon $liney1]
set pcoords "$firsteast,$firstnorth"
}
set first 0
#check for click with no drag
if { ![info exists linex2] } {
set linex2 $linex1
}
if { ![info exists liney2] } {
set liney2 $liney1
}
$mapcan delete transect
}
# draw profile transect
proc GmProfile::drawtransect { mapcan x y } {
variable measurement_annotation_handle
variable transect
variable tlength
variable tottlength
variable linex1
variable liney1
variable linex2
variable liney2
variable pcoords
global mon
set scrxmov $x
set scrymov $y
set eastcoord [eval MapCanvas::scrx2mape $mon $x]
set northcoord [eval MapCanvas::scry2mapn $mon $y]
set coords "$eastcoord,$northcoord"
set xc [$mapcan canvasx $x]
set yc [$mapcan canvasy $y]
# draw line segment
if {($linex1 != $xc) && ($liney1 != $yc)} {
$mapcan delete transect
$mapcan addtag transect withtag \
[$mapcan create line $linex1 $liney1 $xc $yc \
-fill red -arrow last -width 2]
set linex2 $xc
set liney2 $yc
}
}
# get coordinates for transect nodes and endpoints
proc GmProfile::getcoords { mapcan } {
variable measurement_annotation_handle
variable transect
variable tottransect
variable tlength
variable tottlength
variable linex1
variable liney1
variable linex2
variable liney2
variable pcoords
variable firsteast
variable east1
variable north1
variable east2
variable north2
variable pcoords
variable pcoordslist
global mon
# draw cumulative line
$mapcan addtag tottransect withtag \
[$mapcan create line $linex1 $liney1 $linex2 $liney2 \
-fill blue -arrow last -width 2]
# get line endpoints in map coordinates
set east1 [expr 1.0 * [MapCanvas::scrx2mape $mon $linex1]]
set north1 [expr 1.0 * [MapCanvas::scry2mapn $mon $liney1]]
set east2 [expr 1.0 * [MapCanvas::scrx2mape $mon $linex2]]
set north2 [expr 1.0 * [MapCanvas::scry2mapn $mon $liney2]]
# coordinates for use in r.profile
append pcoords "," $east2 "," $north2
# x distance off each transect segment
# First convert map coordinates to profile screen coordinates
# Then add the profile screen coordinates to the profile coordinates list.
# Only add east1 and north1 the first time;
# continue to add east2 and north2 until ready to draw profile
# calculate line segment length and total length
set tlength [expr {sqrt(pow(($east1 - $east2), 2) + pow(($north1 - $north2), 2))}]
set tottlength [expr {$tottlength + $tlength}]
lappend pcoordslist $tottlength
monitor_annotate $measurement_annotation_handle " --segment length\t= $tlength\n"
monitor_annotate $measurement_annotation_handle "cumulative length\t= $tottlength\n"
set linex1 $linex2
set liney1 $liney2
}
###############################################################################
# draw profile
proc GmProfile::pdraw { } {
variable pcan
variable pmap
variable tottlength
variable elevrange
variable elevmax
variable elevmin
variable firsteast
variable firstnorth
variable east1
variable north1
variable east2
variable north2
variable pcoords
variable pcoordslist
variable profilelist
variable status
global mon
global devnull
set cumdist 0.0
if {![catch {open "|g.proj -p" r} input]} {
set key ""
set value ""
while {[gets $input line] >= 0} {
if { [string equal "XY location (unprojected)" "$line"] } {
set mapunits "map units"
break
}
regexp -nocase {^(.*):(.*)$} $line trash key value
set key [string trim $key]
set value [string trim $value]
set prj($key) $value
}
if {[catch {close $input} error]} {
GmLib::errmsg $error [G_msg "g.proj or projection error"]
}
}
# r.profile always returns meters, but here profile length is calculated
# from the map canvas arrows and so is measured & plotted in map units.
# May already be set above if locn was XY.
if { ! [ info exist mapunits ] } {
set mapunits $prj(units)
}
if {$pmap == ""} {
# get currently selected raster map as default to profile if nothing else chosen
set tree($mon) $GmTree::tree($mon)
set sel [ lindex [$tree($mon) selection get] 0 ]
if { $sel != "" } {
set type [GmTree::node_type $sel]
if {$type == "raster" } {
set GmProfile::pmap [GmRaster::mapname $sel]
}
} else {
tk_messageBox -message [G_msg "You must select a raster to profile"] -type ok -icon warning \
-title [G_msg "No raster map selected"] -parent .profile
return
}
}
if {$pcoords == ""} {
tk_messageBox -message [G_msg "You must draw a transect to profile"] -type ok -icon warning \
-title [G_msg "No transect drawn"] -parent .profile
return
}
set GmProfile::status [G_msg "Please wait while profile elevations are calculated"]
update
# calculate elevation max, min, and range
GmProfile::setelev $pmap
if {$elevrange == 0} {
tk_messageBox -message [G_msg "Elevation range for selected raster is zero.\nNo profile can be created."]\
-type ok -icon warning -title [G_msg "Zero elevation range"] -parent .profile
return
}
# put map name back in status bar
set GmProfile::status [G_msg "Profile for $pmap"]
$pcan delete all
set profilelist ""
# calculate screen length and width
set w [winfo width $pcan]
set h [winfo height $pcan]
#calculate graph box and scale extents
set top [expr 0.20 * $h]
set bottom [expr 0.80 * $h]
set height [expr $bottom - $top]
set left [expr 0.2 * $w]
set right [expr 0.9 * $w]
set center [expr 0.55 * $w]
set width [expr $right - $left]
set yscaleright [expr $left - 10]
set xscaletop [expr $bottom + 10]
set titlebottom [expr 0.10 * $h]
set titleleft [expr $left + 15]
# create title
$pcan create text $center $titlebottom \
-text [G_msg "Profile for $pmap"] \
-font {-size 14 -weight bold} \
-anchor s \
-justify center
# create y axis (from 20%x20% to 20%x80% - top to bottom)
$pcan create line $left $top $left [expr $bottom + 5]
# format axis labels in a nice way, as a function of range
if { $elevrange >= 100 } {
set outfmt "%.0f"
} elseif { $elevrange > 0 } {
set outfmt "%.[expr {int(ceil(2 - log10($elevrange)))}]f"
} else {
# error: no range (nan?)
set outfmt "%g"
}
# add scale to y axis
$pcan create text $yscaleright $top \
-text "[format $outfmt $elevmax]" \
-anchor e \
-justify right
$pcan create text $yscaleright $bottom \
-text "[format $outfmt $elevmin]" \
-anchor e \
-justify right
# create x axis (from 20%x80% to 90%x80% - left to right)
$pcan create line [expr $left - 5] $bottom $right $bottom
# add scale to x axis
$pcan create text $left $xscaletop \
-text "0" \
-anchor n \
-justify center
# add tick marks
$pcan create line $right $bottom $right [expr $bottom + 5]
$pcan create line [expr $left - 5] $top $left $top
# Keep total points in profile graph to under 500 to prevent freezing in
# large, high resolution maps
set ns 0.0
set ew 0.0
set max_res 0.0
set max_dim 0.0
set transect_res 0
if {![catch {open [concat "|g.region" "-gp" "2> $devnull"] r} input]} {
while {[gets $input line] >= 0} {
set reglst [split $line "="]
set parts([lindex $reglst 0]) [lindex $reglst 1]
}
if {[catch {close $input} error]} {
GmLib::errmsg $error [G_msg "Error reading region values"]
}
set ns [expr abs($parts(n) - $parts(s))]
set ew [expr abs($parts(e) - $parts(w))]
set max_res [expr {$parts(nsres) > $parts(ewres) ? $parts(nsres):$parts(ewres)}]
set max_dim [expr {$ns > $ew ? $ns:$ew}]
if { [expr (2 * $max_dim) / $max_res] > 500} {
set transect_res [expr (2 * $max_dim / 500)]
} else {
set transect_res $max_res
}
}
# run r.profile first time to calculate total transect distance (needed for lat lon regions)
if {![catch {open "|r.profile input=$pmap profile=$pcoords res=$transect_res null=nan 2> $devnull" r} input]} {
while {[gets $input line] >= 0} {
if { [regexp -nocase {^([0-9].*) ([[.-.]0-9na].*)$} $line trash dist elev] } {
set cumdist $dist
}
}
if {[catch {close $input} error]} {
GmLib::errmsg $error [G_msg "r.profile error"]
}
}
set divisor "1.0"
if { [string equal "meters" "$mapunits"] } {
if { $tottlength > 2500 } {
set mapunits "km"
set divisor "1000.0"
}
} elseif { [string first "feet" "$mapunits"] >= 0 } {
# nano-bug: we match any "feet", but US Survey feet is really
# 5279.9894 per statute mile, or 1.06' per 100 miles. As >100
# miles the tick markers are rounded to the nearest 10th of a
# mile (528'), the difference in foot flavours is ignored.
if { $tottlength > 5280 } {
set mapunits "miles"
set divisor "5280.0"
}
} elseif { [string first "degree" "$mapunits"] >= 0 } {
if { $tottlength < 1 } {
set mapunits "minutes"
set divisor [expr 1/60.0]
}
}
# format axis labels in a nice way, as a function of range
if { [expr $tottlength/$divisor ] >= 500 } {
set outfmt "%.0f"
} elseif { [expr $tottlength/$divisor ] >= 100 } {
set outfmt "%.1f"
} elseif { [expr $tottlength/$divisor ] > 0 } {
set outfmt "%.[expr {int(ceil(2 - log10($tottlength/$divisor)))}]f"
} else {
# error: no range (nan?)
set outfmt "%g"
}
# add axis label
$pcan create text $center $xscaletop \
-text [G_msg "distance along transect ($mapunits)"] \
-anchor n \
-justify center
# add transect segment markers
foreach {x} $pcoordslist {
if { $tottlength > 0.0 } {
set segx [expr $left + (($x * $width) / $tottlength)]
$pcan create line $segx $bottom $segx $top -fill grey
$pcan create text $segx $top -text "[format $outfmt [expr $x/$divisor]]" \
-anchor s -justify center -fill grey
}
}
# add label for total transect distance
$pcan create text $right $xscaletop \
-text "[format $outfmt [expr $tottlength/$divisor]]" \
-anchor n \
-justify center
# run r.profile again to
# convert dist elev (stdout) to xy coordinates of profile line
if {![catch {open "|r.profile input=$pmap profile=$pcoords res=$transect_res null=nan 2> $devnull" r} input]} {
while {[gets $input line] >= 0} {
if { [regexp -nocase {^([0-9].*) ([[.-.]0-9na].*)$} $line trash dist elev] } {
if { [string equal "$elev" "nan" ] } {
# draw profile line
if { [ info exist profilelist ] } {
if { [llength $profilelist] >= 4 } {
$pcan create line $profilelist -fill blue
} elseif { [llength $profilelist] == 2 } {
# plot a dot
$pcan create oval [concat $profilelist $profilelist] \
-fill blue -outline blue -width 1
}
unset profilelist
}
} else {
set pelev [expr $bottom - ($height * ($elev - $elevmin) / $elevrange)]
set pdist [expr $left + (($dist * $width) / $cumdist)]
lappend profilelist $pdist $pelev
}
}
}
if {[catch {close $input} error]} {
GmLib::errmsg $error [G_msg "r.profile error"]
}
}
# draw profile line
if { [ info exist profilelist ] && [llength $profilelist] >= 4 } {
$pcan create line $profilelist -fill blue
}
}
###############################################################################
# erase profile and clear transects
proc GmProfile::perase { mapcan } {
variable pmap
variable pcan
variable transect
variable tottransect
variable tlength
variable tottlength
variable pcoords
variable pcoordslist
variable profilelist
variable first
variable linex1
variable liney1
variable elevrange
variable elevmax
variable elevmin
$pcan delete all
$mapcan delete transect
$mapcan delete tottransect
set tlength 0.0
set tottlength 0.0
set pcoords ""
set pcoordslist ""
set profilelist ""
set first 1
if { [info exists linex1] } {
unset linex1
}
if { [info exists liney1] } {
unset liney1
}
}
###############################################################################
proc GmProfile::cleanup { destroywin mapcan } {
# cleanup procedures on closing profile window
variable pcan
variable transect
variable tottransect
variable tlength
variable tottlength
variable pcoords
variable pcoordslist
variable profilelist
variable first
variable linex1
variable liney1
global mon
set tlength 0.0
set tottlength 0.0
set pcoords ""
set pcoordslist ""
set profilelist ""
set first 1
if { [info exists linex1] } {
unset linex1
}
if { [info exists liney1] } {
unset liney1
}
$mapcan delete transect
$mapcan delete tottransect
MapCanvas::restorecursor $mon
}
###############################################################################
|