/[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 6 - (show annotations)
Tue Aug 28 15:41:52 2001 UTC (23 years, 6 months ago) by bh
File MIME type: text/x-python
File size: 6023 byte(s)
import all the source files

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 # shapetable is the table associated with the shapefile, while
126 # table is the default table used to look up attributes for
127 # display
128 self.shapetable = Table(filename)
129 self.table = self.shapetable
130
131 def open_shapefile(self):
132 if self.shapefile is None:
133 self.shapefile = shapelib.ShapeFile(self.filename)
134 numshapes, shapetype, mins, maxs = self.shapefile.info()
135 self.numshapes = numshapes
136 self.shapetype = shapelib_shapetypes[shapetype]
137 self.bbox = mins[:2] + maxs[:2]
138
139 def BoundingBox(self):
140 """Return the bounding box of the layer's shapes in their default
141 coordinate system"""
142 self.open_shapefile()
143 return self.bbox
144
145 def LatLongBoundingBox(self):
146 """Return the layer's bounding box in lat/long coordinates"""
147 llx, lly, urx, ury = self.BoundingBox()
148 if self.projection is not None:
149 llx, lly = self.projection.Inverse(llx, lly)
150 urx, ury = self.projection.Inverse(urx, ury)
151 return llx, lly, urx, ury
152
153 def NumShapes(self):
154 """Return the number of shapes in the layer"""
155 self.open_shapefile()
156 return self.numshapes
157
158 def ShapeType(self):
159 """Return the type of the shapes in the layer.
160 This is either SHAPETYPE_POINT, SHAPETYPE_ARC or SHAPETYPE_POLYGON.
161 """
162 self.open_shapefile()
163 return self.shapetype
164
165 def Shape(self, index):
166 """Return the shape with index index"""
167 self.open_shapefile()
168 shape = self.shapefile.read_object(index)
169 if self.shapetype == SHAPETYPE_POINT:
170 points = shape.vertices()
171 else:
172 #for poly in shape.vertices():
173 poly = shape.vertices()[0]
174 points = []
175 for x, y in poly:
176 points.append(x, y)
177 return Shape(points)
178
179 def SetProjection(self, projection):
180 """Set the layer's projection"""
181 self.projection = projection
182 self.changed(LAYER_PROJECTION_CHANGED, self)
183
184 def SetFill(self, fill):
185 """Set the layer's fill color. None means the shapes are not filled"""
186 self.fill = fill
187 self.changed(LAYER_LEGEND_CHANGED, self)
188
189 def SetStroke(self, stroke):
190 """Set the layer's stroke color. None means the shapes are not
191 stroked."""
192 self.stroke = stroke
193 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