/[thuban]/trunk/thuban/Thuban/Model/layer.py
ViewVC logotype

Contents of /trunk/thuban/Thuban/Model/layer.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (show annotations)
Tue Sep 4 16:45:28 2001 UTC (23 years, 6 months ago) by bh
File MIME type: text/x-python
File size: 6053 byte(s)
Open the shapefile immediately. This will cause an exception in case the
file can't be opened and we can display an appropriate message.

1 # Copyright (c) 2001 by Intevation GmbH
2 # Authors:
3 # Bernhard Herzog <[email protected]>
4 #
5 # This program is free software under the GPL (>=v2)
6 # Read the file COPYING coming with Thuban for details.
7
8 __version__ = "$Revision$"
9
10 import shapelib
11
12 from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \
13 LAYER_VISIBILITY_CHANGED
14
15 from color import Color
16 # Some predefined colors for internal use
17 _black = Color(0, 0, 0)
18
19
20 from table import Table
21
22 from base import TitledObject, Modifiable
23
24 class Shape:
25
26 """Represent one shape"""
27
28 def __init__(self, points):
29 self.points = points
30 #self.compute_bbox()
31
32 def compute_bbox(self):
33 xs = []
34 ys = []
35 for x, y in self.points:
36 xs.append(x)
37 ys.append(y)
38 self.llx = min(xs)
39 self.lly = min(ys)
40 self.urx = max(xs)
41 self.ury = max(ys)
42
43 def Points(self):
44 return self.points
45
46
47
48 # Shape type constants
49 SHAPETYPE_POLYGON = "polygon"
50 SHAPETYPE_ARC = "arc"
51 SHAPETYPE_POINT = "point"
52
53 # mapping from shapelib shapetype constants to our constants
54 shapelib_shapetypes = {shapelib.SHPT_POLYGON: SHAPETYPE_POLYGON,
55 shapelib.SHPT_ARC: SHAPETYPE_ARC,
56 shapelib.SHPT_POINT: SHAPETYPE_POINT}
57
58 shapetype_names = {SHAPETYPE_POINT: "Point",
59 SHAPETYPE_ARC: "Arc",
60 SHAPETYPE_POLYGON: "Polygon"}
61
62 class BaseLayer(TitledObject, Modifiable):
63
64 """Base class for the layers."""
65
66 def __init__(self, title, visible = 1):
67 """Initialize the layer.
68
69 title -- the title
70 visible -- boolean. If true the layer is visible.
71 """
72 TitledObject.__init__(self, title)
73 Modifiable.__init__(self)
74 self.visible = visible
75
76 def Visible(self):
77 """Return true if layer is visible"""
78 return self.visible
79
80 def SetVisible(self, visible):
81 """Set the layer's visibility."""
82 self.visible = visible
83 self.issue(LAYER_VISIBILITY_CHANGED, self)
84
85
86 class Layer(BaseLayer):
87
88 """Represent the information of one geodata file (currently a shapefile)
89
90 All children of the layer have the same type.
91
92 A layer has fill and stroke colors. Colors should be instances of
93 Color. They can also be None, indicating no fill or no stroke.
94
95 The layer objects send the following events, all of which have the
96 layer object as parameter:
97
98 TITLE_CHANGED -- The title has changed.
99 LAYER_PROJECTION_CHANGED -- the projection has changed.
100 LAYER_LEGEND_CHANGED -- the fill or stroke attributes have changed
101
102 """
103
104 def __init__(self, title, filename, projection = None,
105 fill = None, stroke = _black, visible = 1):
106 """Initialize the layer.
107
108 title -- the title
109 filename -- the name of the shapefile
110 projection -- the projection object. Its Inverse method is
111 assumed to map the layer's coordinates to lat/long
112 coordinates
113 fill -- the fill color or None if the shapes are not filled
114 stroke -- the stroke color or None if the shapes are not stroked
115 visible -- boolean. If true the layer is visible.
116
117 colors are expected to be instances of Color class
118 """
119 BaseLayer.__init__(self, title, visible = visible)
120 self.filename = filename
121 self.projection = projection
122 self.fill = fill
123 self.stroke = stroke
124 self.shapefile = None
125 self.open_shapefile()
126 # shapetable is the table associated with the shapefile, while
127 # table is the default table used to look up attributes for
128 # display
129 self.shapetable = Table(filename)
130 self.table = self.shapetable
131
132 def open_shapefile(self):
133 if self.shapefile is None:
134 self.shapefile = shapelib.ShapeFile(self.filename)
135 numshapes, shapetype, mins, maxs = self.shapefile.info()
136 self.numshapes = numshapes
137 self.shapetype = shapelib_shapetypes[shapetype]
138 self.bbox = mins[:2] + maxs[:2]
139
140 def BoundingBox(self):
141 """Return the bounding box of the layer's shapes in their default
142 coordinate system"""
143 self.open_shapefile()
144 return self.bbox
145
146 def LatLongBoundingBox(self):
147 """Return the layer's bounding box in lat/long coordinates"""
148 llx, lly, urx, ury = self.BoundingBox()
149 if self.projection is not None:
150 llx, lly = self.projection.Inverse(llx, lly)
151 urx, ury = self.projection.Inverse(urx, ury)
152 return llx, lly, urx, ury
153
154 def NumShapes(self):
155 """Return the number of shapes in the layer"""
156 self.open_shapefile()
157 return self.numshapes
158
159 def ShapeType(self):
160 """Return the type of the shapes in the layer.
161 This is either SHAPETYPE_POINT, SHAPETYPE_ARC or SHAPETYPE_POLYGON.
162 """
163 self.open_shapefile()
164 return self.shapetype
165
166 def Shape(self, index):
167 """Return the shape with index index"""
168 self.open_shapefile()
169 shape = self.shapefile.read_object(index)
170 if self.shapetype == SHAPETYPE_POINT:
171 points = shape.vertices()
172 else:
173 #for poly in shape.vertices():
174 poly = shape.vertices()[0]
175 points = []
176 for x, y in poly:
177 points.append(x, y)
178 return Shape(points)
179
180 def SetProjection(self, projection):
181 """Set the layer's projection"""
182 self.projection = projection
183 self.changed(LAYER_PROJECTION_CHANGED, self)
184
185 def SetFill(self, fill):
186 """Set the layer's fill color. None means the shapes are not filled"""
187 self.fill = fill
188 self.changed(LAYER_LEGEND_CHANGED, self)
189
190 def SetStroke(self, stroke):
191 """Set the layer's stroke color. None means the shapes are not
192 stroked."""
193 self.stroke = stroke
194 self.changed(LAYER_LEGEND_CHANGED, self)

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26