/[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 147 - (show annotations)
Tue May 7 16:39:52 2002 UTC (22 years, 10 months ago) by bh
File MIME type: text/x-python
File size: 6824 byte(s)
	* Thuban/Model/layer.py (Layer.ShapesInRegion): Remove some debug
	prints.

1 # Copyright (c) 2001, 2002 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, shptree
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, stroke_width = 1, 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.stroke_width = stroke_width
125 self.shapefile = None
126 self.shapetree = None
127 self.open_shapefile()
128 # shapetable is the table associated with the shapefile, while
129 # table is the default table used to look up attributes for
130 # display
131 self.shapetable = Table(filename)
132 self.table = self.shapetable
133
134 def open_shapefile(self):
135 if self.shapefile is None:
136 self.shapefile = shapelib.ShapeFile(self.filename)
137 numshapes, shapetype, mins, maxs = self.shapefile.info()
138 self.numshapes = numshapes
139 self.shapetype = shapelib_shapetypes[shapetype]
140 self.bbox = mins[:2] + maxs[:2]
141 #print "building tree for", self.filename, "..."
142 self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, 0)
143 #print "done"
144
145 def BoundingBox(self):
146 """Return the bounding box of the layer's shapes in their default
147 coordinate system"""
148 self.open_shapefile()
149 return self.bbox
150
151 def LatLongBoundingBox(self):
152 """Return the layer's bounding box in lat/long coordinates"""
153 llx, lly, urx, ury = self.BoundingBox()
154 if self.projection is not None:
155 llx, lly = self.projection.Inverse(llx, lly)
156 urx, ury = self.projection.Inverse(urx, ury)
157 return llx, lly, urx, ury
158
159 def NumShapes(self):
160 """Return the number of shapes in the layer"""
161 self.open_shapefile()
162 return self.numshapes
163
164 def ShapeType(self):
165 """Return the type of the shapes in the layer.
166 This is either SHAPETYPE_POINT, SHAPETYPE_ARC or SHAPETYPE_POLYGON.
167 """
168 self.open_shapefile()
169 return self.shapetype
170
171 def Shape(self, index):
172 """Return the shape with index index"""
173 self.open_shapefile()
174 shape = self.shapefile.read_object(index)
175 if self.shapetype == SHAPETYPE_POINT:
176 points = shape.vertices()
177 else:
178 #for poly in shape.vertices():
179 poly = shape.vertices()[0]
180 points = []
181 for x, y in poly:
182 points.append((x, y))
183 return Shape(points)
184
185 def ShapesInRegion(self, box):
186 """Return the ids of the shapes that overlap the box.
187
188 Box is a tuple (left, bottom, right, top) in the coordinate
189 system used by the layer's shapefile.
190 """
191 left, bottom, right, top = box
192 return self.shapetree.find_shapes((left, bottom), (right, top))
193
194 def SetProjection(self, projection):
195 """Set the layer's projection"""
196 self.projection = projection
197 self.changed(LAYER_PROJECTION_CHANGED, self)
198
199 def SetFill(self, fill):
200 """Set the layer's fill color. None means the shapes are not filled"""
201 self.fill = fill
202 self.changed(LAYER_LEGEND_CHANGED, self)
203
204 def SetStroke(self, stroke):
205 """Set the layer's stroke color. None means the shapes are not
206 stroked."""
207 self.stroke = stroke
208 self.changed(LAYER_LEGEND_CHANGED, self)
209
210 def SetStrokeWidth(self, width):
211 """Set the layer's stroke width."""
212 self.stroke_width = width
213 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