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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 73 - (hide annotations)
Mon Feb 4 19:19:25 2002 UTC (23 years, 1 month ago) by bh
File MIME type: text/x-python
File size: 6283 byte(s)
	* Thuban/Model/layer.py (Layer.__init__): New parameter and
	instance variable stroke_width
	(Layer.SetStrokeWidth): Set the stroke_width.

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