source: trunk/gui/scripts/map.tcl @ 4292

Last change on this file since 4292 was 4292, checked in by ldelgass, 10 years ago

layers method returns keys/ids not labels/titles

File size: 6.8 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2
3# ----------------------------------------------------------------------
4#  COMPONENT: map - extracts data from an XML description of a field
5#
6#  This object represents a map of data in an XML description of
7#  simulator output.  A map is similar to a field, but a field is
8#  a quantity versus position in device.  A map is any quantity
9#  versus any other quantity.  This class simplifies the process of
10#  extracting data vectors that represent the map.
11# ======================================================================
12#  AUTHOR:  Michael McLennan, Purdue University
13#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
14#
15#  See the file "license.terms" for information on usage and
16#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
17# ======================================================================
18package require Itcl
19package require BLT
20
21namespace eval Rappture {
22    # forward declaration
23}
24
25itcl::class Rappture::Map {
26    private variable _tree "";         # Tree of information about the map.
27    private variable _isValid 0;
28    private variable _nextLayer 0;     # Counter used to generate unique
29                                       # layer names.
30    private common _layerTypes
31    private common _mapTypes
32    array set _layerTypes {
33        "raster"        0
34        "elevation"     1
35        "polygon"       2
36        "points"        3
37        "circle"        4
38        "line"          5
39    }
40    array set _mapTypes {
41        "geocentric"    0
42        "projected"     1
43    }
44    protected method Parse { xmlobj path }
45
46    constructor {xmlobj path} {
47        # defined below
48    }
49    destructor {
50        # defined below
51    }
52
53    public method isGeocentric {}
54    public method layers {}
55    public method layer { layerName }
56    public method hints { args }
57    public method isvalid {} {
58        return $_isValid;
59    }
60    public method type { layerName }
61}
62
63# ----------------------------------------------------------------------
64# CONSTRUCTOR
65# ----------------------------------------------------------------------
66itcl::body Rappture::Map::constructor {xmlobj path} {
67    if {![Rappture::library isvalid $xmlobj]} {
68        error "bad value \"$xmlobj\": should be LibraryObj"
69    }
70    Parse $xmlobj $path
71}
72
73# ----------------------------------------------------------------------
74# DESTRUCTOR
75# ----------------------------------------------------------------------
76itcl::body Rappture::Map::destructor {} {
77    if { $_tree != "" } {
78        blt::tree destroy $_tree
79    }
80}
81
82#
83# hints --
84#
85itcl::body Rappture::Map::hints { args } {
86    switch -- [llength $args] {
87        0 {
88            return [$_tree get root]
89        }
90        1 {
91            set field [lindex $args 0]
92            return [$_tree get root $field ""]
93        }
94        default {
95            error "wrong # args: should \"hints ?name?\""
96        }
97    }
98}
99
100#
101# Parse --
102#
103#       Parses the map description in the XML object.
104#
105itcl::body Rappture::Map::Parse { xmlobj path } {
106
107    set map [$xmlobj element -as object $path]
108
109    if { $_tree != "" } {
110        blt::tree destroy $_tree
111    }
112    set _tree [blt::tree create]
113    set parent [$_tree insert root -label "layers"]
114    set layers [$map element -as object "layers"]
115    foreach layer [$layers children -type layer] {
116        # Unique identifier for layer.
117        set name "layer[incr _nextLayer]"
118        set child [$_tree insert $parent -label $name]
119        $_tree set $child "title" [$layers get $layer.label]
120        set layerType [$layers get $layer.type]
121        if { ![info exists _layerTypes($layerType)] } {
122            error "invalid layer type \"$layerType\": should be one of [array names _layerTypes]"
123        }
124        $_tree set $child "type" $layerType
125        foreach key { label description url } {
126            $_tree set $child $key [$layers get $layer.$key]
127        }
128        set file [$layers get $layer.file]
129        if { $file != "" } {
130            # FIXME: Add test for valid file path
131            $_tree set $child "url" $file
132        }
133    }
134    $_tree set root "label"       [$map get "about.label"]
135    $_tree set root "style"       [$map get "style"]
136 
137    set projection [$map get "projection"]
138    set extents    [$map get "extents"]
139    if { $projection  == "" } {
140        if {$extents != ""} {
141            error "cannot specify extents without a projection"
142        }
143        set projection "global-mercator"; # Default projection.
144    }
145    # FIXME: Verify projection is valid.
146    $_tree set root "projection" $projection
147    $_tree set root "extents"    $extents
148
149    set mapType [$map get "type"]
150    if { $mapType == "" } {
151        set mapType "projected";           # Default type is "projected".
152    }
153    if { ![info exists _mapTypes($mapType)] } {
154        error "unknown map type \"$mapType\": should be one of [array names _mapTypes]"
155    }
156    $_tree set root "type" $mapType
157
158    foreach {key path} {
159        toolId          tool.id
160        toolName        tool.name
161        toolCommand     tool.execute
162        toolTitle       tool.title
163        toolRevision    tool.version.application.revision
164    } {
165        set str [$xmlobj get $path]
166        if { "" != $str } {
167            $_tree set root $key $str
168        }
169    }
170    set _isValid 1
171}
172
173# ----------------------------------------------------------------------
174# USAGE: layers
175#
176# Returns a list of IDs for the layers in the map
177# ----------------------------------------------------------------------
178itcl::body Rappture::Map::layers {} {
179    set list {}
180    foreach node [$_tree children root->"layers"] {
181        lappend list [$_tree label $node]
182    }
183    return $list
184}
185
186# ----------------------------------------------------------------------
187# USAGE: layer <layerName>
188#
189# Returns an array of settings for the named layer
190# ----------------------------------------------------------------------
191itcl::body Rappture::Map::layer { layerName } {
192    set id [$_tree findchild root->"layers" $layerName]
193    if { $id < 0 } {
194        error "unknown layer \"$layerName\""
195    }
196    return [$_tree get $id]
197}
198
199# ----------------------------------------------------------------------
200# USAGE: type <layerName>
201#
202# Returns the type of the named layer
203# ----------------------------------------------------------------------
204itcl::body Rappture::Map::type { layerName } {
205    set id [$_tree findchild root->"layers" $layerName]
206    if { $id < 0 } {
207        error "unknown layer \"$layerName\""
208    }
209    return [$_tree get $id "type" ""]
210}
211
212# ----------------------------------------------------------------------
213# USAGE: isGeocentric
214#
215# Returns if the map is geocentric (1) or projected (0)
216# ----------------------------------------------------------------------
217itcl::body Rappture::Map::isGeocentric {} {
218    return [expr {[hints "type"] eq "geocentric"}]
219}
Note: See TracBrowser for help on using the repository browser.