file_option.tcl 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #=====================================================================================
  2. #
  3. # FILE: file_option.tcl
  4. #
  5. # DESCRIPTION: creates location from georeferenced file
  6. #
  7. # NOTES: ---
  8. # AUTHOR: Michael Barton
  9. # COMPANY: Arizona State University
  10. # COPYRIGHT: Copyright (C) 2007 Michael Barton and GRASS Development Team
  11. # VERSION: 1.2
  12. # CREATED: 23/04/2006
  13. # REVISION: ---
  14. # CHANGELOG: 1.0.1 08/12/2006 - Fixed directory choosing dialogs. Maris Nartiss.
  15. # : 1.2 - 6 Jan 2007 - Fixed file creation for windows and reformatted
  16. # dialog widgets (Michael Barton).
  17. # Added check for return status of g.proj to catch failed location
  18. # creation (by Maris Nartiss).
  19. #=====================================================================================
  20. #
  21. #
  22. #
  23. # This library is free software; you can redistribute it and/or
  24. # modify it under the terms of the GNU Library General Public
  25. # License as published by the Free Software Foundation; either
  26. # version 2 of the License, or (at your option) any later version.
  27. #
  28. # This library is distributed in the hope that it will be useful,
  29. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  31. # Library General Public License for more details.
  32. #
  33. # You should have received a copy of the GNU Library General Public
  34. # License along with this library; if not, write to the Free
  35. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  36. # USA
  37. #
  38. #1. Redistributions of source code must retain the above copyright
  39. # notice, this list of conditions and the following disclaimer.
  40. #2. Redistributions in binary form must reproduce the above copyright
  41. # notice, this list of conditions and the following disclaimer in the
  42. # documentation and/or other materials provided with the distribution.
  43. #
  44. #############################################################################
  45. namespace eval fileOpt {
  46. variable fileLocation ;#name of new location to be created
  47. variable filepath ;#path to georeferenced file
  48. global env
  49. global database
  50. global mingw ;#test to see if we are running a windows version in mingw
  51. global refresh
  52. }
  53. # G_msg.tcl should be sourced first for internationalized strings.
  54. # the frame used to set parameters
  55. proc fileOpt::fileLocCom args {
  56. #vars declaration
  57. variable filepath
  58. variable fileLocation
  59. global database
  60. global env
  61. set fileLocation "newLocation"
  62. set filepath ""
  63. set locpath $database
  64. set buttonstate "disabled"
  65. # creation of the parameter window
  66. set file_win [toplevel .fileloc]
  67. wm title $file_win [ G_msg "Define location using projection information in georeferenced file" ]
  68. # put it in the middle of the screen
  69. update idletasks
  70. set winWidth [winfo reqwidth $file_win]
  71. set winHeight [winfo reqheight $file_win]
  72. set scrnWidth [winfo screenwidth $file_win]
  73. set scrnHeight [winfo screenheight $file_win]
  74. set x [expr ($scrnWidth - $winWidth) / 2-250]
  75. set y [expr ($scrnHeight - $winHeight) / 2]
  76. wm geometry $file_win +$x+$y
  77. wm deiconify $file_win
  78. set row1 [frame $file_win.row1]
  79. set row2 [frame $file_win.row2]
  80. set row3 [frame $file_win.row3]
  81. set row4 [frame $file_win.row4]
  82. #create the form and buttons
  83. LabelEntry $row1.newloc -label [G_msg "Name of new location"] \
  84. -labeljustify right -labelanchor e -labelwidth 30 -wraplength 200 \
  85. -textvariable fileOpt::fileLocation -width 35 \
  86. -helptext [G_msg "Enter name of location to be created"]
  87. pack $row1.newloc -side left -expand 0 -fill x -padx 2
  88. LabelEntry $row2.filepath -label [G_msg "Path to georeferenced file"] \
  89. -labeljustify right -labelanchor e -labelwidth 30 -wraplength 200 \
  90. -textvariable fileOpt::filepath -width 35 \
  91. -helptext [G_msg "Path to georeferenced file (format must be readable by GDAL/OGR)"]
  92. #browse for georeferenced file
  93. Button $row2.browsefile -justify center -padx 10 -bd 1 -text [G_msg "Browse..."] \
  94. -helptext [G_msg "Browse to locate georeferenced file"] \
  95. -command "fileOpt::browse_file"
  96. pack $row2.filepath $row2.browsefile -side left -expand 0 -fill x -padx 2
  97. Button $row3.submit -justify center -padx 10 -text [G_msg "Define location"] \
  98. -command "fileOpt::def_loc" -bd 1
  99. Button $row3.cancel -justify center -padx 10 -text [G_msg "Cancel"] \
  100. -command {destroy .fileloc} -bd 1
  101. pack $row3.submit -side left -fill x -expand 0
  102. pack $row3.cancel -side right -fill x -expand 0
  103. pack $row1 $row2 $row3 -side top -fill both -expand 1 -padx 3 -pady 3
  104. }
  105. proc fileOpt::browse_file {} {
  106. global env
  107. variable filepath
  108. if { [info exists env(HOME)] } {
  109. set dir $env(HOME)
  110. set fileOpt::filepath [tk_getOpenFile -parent .fileloc -initialdir $dir \
  111. -title [ G_msg "Choose georeferenced file" ] -multiple false]
  112. } else {
  113. set fileOpt::filepath [tk_getOpenFile -parent .fileloc \
  114. -title [ G_msg "Choose georeferenced file" ] -multiple false]
  115. }
  116. }
  117. proc fileOpt::def_loc { } {
  118. # define new location using georeferenced file readable by GDAL/OGR
  119. #vars declaration
  120. variable filepath
  121. variable fileLocation
  122. global database
  123. global env
  124. if {$filepath==""} {return}
  125. if {$filepath==""} {
  126. tk_messageBox -type ok -icon error \
  127. -message [G_msg "WARNING: Please supply a\nvalid georeferenced file"]
  128. return
  129. }
  130. set fileLocation [ string trim $fileLocation ]
  131. if {[file exists $fileLocation ]== 1} {
  132. tk_messageBox -type ok -icon error \
  133. -message [G_msg "WARNING: The location '$fileLocation'\nalready exists, please try another name"]
  134. set fileLocation ""
  135. return
  136. }
  137. if {[file exists $fileLocation ]==0} {
  138. destroy .fileloc
  139. fileOpt::create_loc
  140. set refresh 1
  141. return
  142. }
  143. }
  144. proc fileOpt::create_loc { } {
  145. # Create a new location using g.proj
  146. # original bash code by M. Neteler
  147. variable filepath
  148. variable fileLocation
  149. global location
  150. global mapset
  151. set dtrans ""
  152. catch {set dtrans [exec g.proj --q -c location=$fileLocation georef=$filepath datumtrans=-1]} errMsg
  153. if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
  154. DialogGen .wrnDlg [G_msg "Error creating location!"] error \
  155. [format [G_msg "g.proj returned the following message:\n%s"] $errMsg] \
  156. 0 OK
  157. } elseif {$dtrans eq ""} {
  158. # if nothing written to stdout, there was no choice of
  159. # datum parameters and we need not do anything more
  160. if {$errMsg ne ""} {
  161. DialogGen .wrnDlg [G_msg "Informational output from g.proj"] info \
  162. [format [G_msg "g.proj returned the following informational message:\n%s"] $errMsg] \
  163. 0 OK
  164. }
  165. set location $fileLocation
  166. set mapset "PERMANENT"
  167. } else {
  168. # user selects datum transform
  169. #create dialog that lists datum transforms, asks user to enter a number and press OK
  170. set paramset [fileOpt::sel_dtrans $dtrans]
  171. # operation canceled
  172. if {$paramset == -9} {return}
  173. # create new location from georeferenced file
  174. catch {exec g.proj --q -c georef=$filepath location=$fileLocation datumtrans=$paramset} errMsg
  175. #catch any other errors
  176. if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
  177. DialogGen .wrnDlg [G_msg "Error creating location!"] warning \
  178. [format [G_msg "g.proj returned the following message:\n%s"] $errMsg] \
  179. 0 OK
  180. } else {
  181. if {$errMsg ne ""} {
  182. DialogGen .wrnDlg [G_msg "Informational output from g.proj"] info \
  183. [format [G_msg "g.proj returned the following informational message:\n%s"] $errMsg] \
  184. 0 OK
  185. }
  186. set location $fileLocation
  187. set mapset "PERMANENT"
  188. }
  189. }
  190. return
  191. }
  192. proc fileOpt::sel_dtrans {dtrans} {
  193. # Dialog for selecting optional datum transform parameters
  194. # Argument is stdout from g.proj
  195. # default is not to specify datum transformation
  196. set fileOpt::dtnum 0
  197. # Create a popup search dialog
  198. toplevel .dtrans_sel
  199. wm title .dtrans_sel [G_msg "Select datum transformation parameters:"]
  200. set row1 [frame .dtrans_sel.frame1]
  201. set row3 [frame .dtrans_sel.frame3]
  202. radiobutton $row1.0 -value 0 -variable fileOpt::dtnum -wraplength 640 -justify left -text [G_msg "Continue without specifying parameters - if used when creating a location, other GRASS modules will use the \"default\" (likely non-optimum) parameters for this datum if necessary in the future."]
  203. pack $row1.0 -anchor w
  204. set dtrans [split $dtrans "\n"]
  205. for {set i 0} { $i < [llength $dtrans] } {incr i} {
  206. set thisnum [lindex $dtrans $i]
  207. if {$thisnum == "---"} {
  208. continue
  209. }
  210. set thisdesc $thisnum.
  211. while { [incr i] < [llength $dtrans] && [lindex $dtrans $i] != "---"} {
  212. set thisdesc ${thisdesc}\n[lindex $dtrans $i]
  213. }
  214. radiobutton $row1.$thisnum -variable fileOpt::dtnum -value $thisnum -wraplength 640 -justify left -text $thisdesc
  215. pack $row1.$thisnum -anchor w
  216. }
  217. pack $row1
  218. Button $row3.ok -text [G_msg "OK"] -padx 10 -bd 1 \
  219. -command "destroy .dtrans_sel"
  220. pack $row3.ok -side left -padx 3
  221. button $row3.cancel -text [G_msg "Cancel"] -padx 10 -bd 1 \
  222. -command "set fileOpt::dtnum -9; destroy .dtrans_sel"
  223. pack $row3.cancel -side left -padx 3
  224. pack $row3 -anchor center -pady 3
  225. tkwait window .dtrans_sel
  226. return $fileOpt::dtnum
  227. }