Improved error and help messages for the ".archive" command and "-A" option
[sqlite.git] / ext / rtree / viewrtree.tcl
blob794677f5a371c772fdbc00c0134c4e931a6ae488
2 load ./libsqlite3.dylib
3 #package require sqlite3
4 source [file join [file dirname $argv0] rtree_util.tcl]
6 wm title . "SQLite r-tree viewer"
8 if {[llength $argv]!=1} {
9 puts stderr "Usage: $argv0 <database-file>"
10 puts stderr ""
11 exit
13 sqlite3 db [lindex $argv 0]
15 canvas .c -background white -width 400 -height 300 -highlightthickness 0
17 button .b -text "Parent Node" -command {
18 set sql "SELECT parentnode FROM $::O(zTab)_parent WHERE nodeno = $::O(iNode)"
19 set ::O(iNode) [db one $sql]
20 if {$::O(iNode) eq ""} {set ::O(iNode) 1}
21 view_node
24 set O(iNode) 1
25 set O(zTab) ""
26 set O(listbox_captions) [list]
27 set O(listbox_itemmap) [list]
28 set O(listbox_highlight) -1
30 listbox .l -listvariable ::O(listbox_captions) -yscrollcommand {.ls set}
31 scrollbar .ls -command {.l yview}
32 label .status -font courier -anchor w
33 label .title -anchor w -text "Node 1:" -background white -borderwidth 0
36 set rtree_tables [list]
37 db eval {
38 SELECT name
39 FROM sqlite_master
40 WHERE type='table' AND sql LIKE '%virtual%table%using%rtree%'
41 } {
42 set nCol [expr [llength [db eval "pragma table_info($name)"]]/6]
43 if {$nCol != 5} {
44 puts stderr "Not viewing $name - is not 2-dimensional"
45 } else {
46 lappend rtree_tables [list Table $name]
49 if {$rtree_tables eq ""} {
50 puts stderr "Cannot find an r-tree table in database [lindex $argv 0]"
51 puts stderr ""
52 exit
54 eval tk_optionMenu .select option_var $rtree_tables
55 trace add variable option_var write set_option_var
56 proc set_option_var {args} {
57 set ::O(zTab) [lindex $::option_var 1]
58 set ::O(iNode) 1
59 view_node
61 set ::O(zTab) [lindex $::rtree_tables 0 1]
63 bind .l <1> {listbox_click [.l nearest %y]}
64 bind .l <Motion> {listbox_mouseover [.l nearest %y]}
65 bind .l <Leave> {listbox_mouseover -1}
67 proc listbox_click {sel} {
68 if {$sel ne ""} {
69 set ::O(iNode) [lindex $::O(listbox_captions) $sel 1]
70 view_node
73 proc listbox_mouseover {i} {
74 set oldid [lindex $::O(listbox_itemmap) $::O(listbox_highlight)]
75 .c itemconfigure $oldid -fill ""
77 .l selection clear 0 end
78 .status configure -text ""
79 if {$i>=0} {
80 set id [lindex $::O(listbox_itemmap) $i]
81 .c itemconfigure $id -fill grey
82 .c lower $id
83 set ::O(listbox_highlight) $i
84 .l selection set $i
85 .status configure -text [cell_report db $::O(zTab) $::O(iNode) $i]
89 grid configure .select -row 0 -column 0 -columnspan 2 -sticky nsew
90 grid configure .b -row 1 -column 0 -columnspan 2 -sticky nsew
91 grid configure .l -row 2 -column 0 -sticky nsew
92 grid configure .status -row 3 -column 0 -columnspan 3 -sticky nsew
94 grid configure .title -row 0 -column 2 -sticky nsew
95 grid configure .c -row 1 -column 2 -rowspan 2 -sticky nsew
96 grid configure .ls -row 2 -column 1 -sticky nsew
98 grid columnconfigure . 2 -weight 1
99 grid rowconfigure . 2 -weight 1
101 proc node_bbox {data} {
102 set xmin 0
103 set xmax 0
104 set ymin 0
105 set ymax 0
106 foreach {rowid xmin xmax ymin ymax} [lindex $data 0] break
107 foreach cell [lrange $data 1 end] {
108 foreach {rowid x1 x2 y1 y2} $cell break
109 if {$x1 < $xmin} {set xmin $x1}
110 if {$x2 > $xmax} {set xmax $x2}
111 if {$y1 < $ymin} {set ymin $y1}
112 if {$y2 > $ymax} {set ymax $y2}
114 list $xmin $xmax $ymin $ymax
117 proc view_node {} {
118 set iNode $::O(iNode)
119 set zTab $::O(zTab)
121 set data [rtree_node db $zTab $iNode 12]
122 set depth [rtree_nodedepth db $zTab $iNode]
124 .c delete all
125 set ::O(listbox_captions) [list]
126 set ::O(listbox_itemmap) [list]
127 set $::O(listbox_highlight) -1
129 .b configure -state normal
130 if {$iNode == 1} {.b configure -state disabled}
131 .title configure -text "Node $iNode: [cell_report db $zTab $iNode -1]"
133 foreach {xmin xmax ymin ymax} [node_bbox $data] break
134 set total_area 0.0
136 set xscale [expr {double([winfo width .c]-20)/($xmax-$xmin)}]
137 set yscale [expr {double([winfo height .c]-20)/($ymax-$ymin)}]
139 set xoff [expr {10.0 - $xmin*$xscale}]
140 set yoff [expr {10.0 - $ymin*$yscale}]
142 foreach cell $data {
143 foreach {rowid x1 x2 y1 y2} $cell break
144 set total_area [expr {$total_area + ($x2-$x1)*($y2-$y1)}]
145 set x1 [expr {$x1*$xscale + $xoff}]
146 set x2 [expr {$x2*$xscale + $xoff}]
147 set y1 [expr {$y1*$yscale + $yoff}]
148 set y2 [expr {$y2*$yscale + $yoff}]
150 set id [.c create rectangle $x1 $y1 $x2 $y2]
151 if {$depth>0} {
152 lappend ::O(listbox_captions) "Node $rowid"
153 lappend ::O(listbox_itemmap) $id
158 proc cell_report {db zTab iParent iCell} {
159 set data [rtree_node db $zTab $iParent 12]
160 set cell [lindex $data $iCell]
162 foreach {xmin xmax ymin ymax} [node_bbox $data] break
163 set total_area [expr ($xmax-$xmin)*($ymax-$ymin)]
165 if {$cell eq ""} {
166 set cell_area 0.0
167 foreach cell $data {
168 foreach {rowid x1 x2 y1 y2} $cell break
169 set cell_area [expr $cell_area+($x2-$x1)*($y2-$y1)]
171 set cell_area [expr $cell_area/[llength $data]]
172 set zReport [format "Size = %.1f x %.1f Average child area = %.1f%%" \
173 [expr $xmax-$xmin] [expr $ymax-$ymin] [expr 100.0*$cell_area/$total_area]\
175 append zReport " Sub-tree height: [rtree_nodedepth db $zTab $iParent]"
176 } else {
177 foreach {rowid x1 x2 y1 y2} $cell break
178 set cell_area [expr ($x2-$x1)*($y2-$y1)]
179 set zReport [format "Size = %.1f x %.1f Area = %.1f%%" \
180 [expr $x2-$x1] [expr $y2-$y1] [expr 100.0*$cell_area/$total_area]
184 return $zReport
187 view_node
188 bind .c <Configure> view_node