140 |
"""Draw the shape layer layer onto the map. |
"""Draw the shape layer layer onto the map. |
141 |
|
|
142 |
Automatically called by render_map. Iterate through all shapes |
Automatically called by render_map. Iterate through all shapes |
143 |
as indicated by self.layer_ids() and draw them, using low-level |
as indicated by self.layer_shapes() and draw them, using |
144 |
renderers returned by self.low_level_renderer(). |
low-level renderers returned by self.low_level_renderer(). |
145 |
""" |
""" |
146 |
scale = self.scale |
scale = self.scale |
147 |
offx, offy = self.offset |
offx, offy = self.offset |
160 |
table = layer.ShapeStore().Table() |
table = layer.ShapeStore().Table() |
161 |
|
|
162 |
# Determine which render function to use. |
# Determine which render function to use. |
163 |
draw_func, draw_func_param = self.low_level_renderer(layer) |
useraw, draw_func, draw_func_param = self.low_level_renderer(layer) |
164 |
|
|
165 |
# Iterate through all shapes that have to be drawn. |
# Iterate through all shapes that have to be drawn. |
166 |
for i in self.layer_ids(layer): |
for shape in self.layer_shapes(layer): |
167 |
|
|
168 |
if field is None: |
if field is None: |
169 |
group = defaultGroup |
group = defaultGroup |
170 |
else: |
else: |
171 |
record = table.ReadRowAsDict(i) |
record = table.ReadRowAsDict(shape.ShapeID()) |
172 |
assert record is not None |
assert record is not None |
173 |
group = lc.FindGroup(record[field]) |
group = lc.FindGroup(record[field]) |
174 |
|
|
184 |
if prop != old_prop: |
if prop != old_prop: |
185 |
pen, brush = self.tools_for_property(prop) |
pen, brush = self.tools_for_property(prop) |
186 |
|
|
187 |
draw_func(draw_func_param, i, pen, brush) |
if useraw: |
188 |
|
data = shape.RawData() |
189 |
|
else: |
190 |
|
data = shape.Points() |
191 |
|
draw_func(draw_func_param, data, pen, brush) |
192 |
|
|
193 |
def layer_ids(self, layer): |
def layer_shapes(self, layer): |
194 |
"""Return the shape ids of the given layer that have to be drawn. |
"""Return an iterable over the shapes to be drawn from the given layer. |
195 |
|
|
196 |
The default implementation simply returns all ids in the layer. |
The default implementation simply returns all ids in the layer. |
197 |
Override in derived classes to be more precise. |
Override in derived classes to be more precise. |
198 |
""" |
""" |
199 |
return range(layer.NumShapes()) |
return layer.ShapeStore().AllShapes() |
200 |
|
|
201 |
def low_level_renderer(self, layer): |
def low_level_renderer(self, layer): |
202 |
"""Return the low-level renderer for the layer for draw_shape_layer |
"""Return the low-level renderer for the layer for draw_shape_layer |
203 |
|
|
204 |
The low level renderer to be returned by this method is a tuple |
The low level renderer to be returned by this method is a tuple |
205 |
(func, param) where func is a callable object and param is |
(useraw, func, param) where useraw is a boolean indicating |
206 |
passed as the first parameter to func. The draw_shape_layer |
whether the function uses the raw shape data, func is a callable |
207 |
method will call func like this: |
object and param is passed as the first parameter to func. The |
208 |
|
draw_shape_layer method will call func like this: |
209 |
func(param, rawshape, pen, brush) |
|
210 |
|
func(param, shapedata, pen, brush) |
211 |
where rawshape is currently the shapeid. pen and brush are the |
|
212 |
pen and brush to use to draw the shape on the dc. |
where shapedata is the return value of the RawData method of the |
213 |
|
shape object if useraw is true or the return value of the Points |
214 |
|
method if it's false. pen and brush are the pen and brush to use |
215 |
|
to draw the shape on the dc. |
216 |
|
|
217 |
The default implementation returns one of |
The default implementation returns one of |
218 |
self.draw_polygon_shape, self.draw_arc_shape or |
self.draw_polygon_shape, self.draw_arc_shape or |
219 |
self.draw_point_shape as func and layer as param. Derived |
self.draw_point_shape as func and layer as param. None of the |
220 |
classes can override this method to return more efficient low |
method use the raw shape data. Derived classes can override this |
221 |
level renderers. |
method to return more efficient low level renderers. |
222 |
""" |
""" |
223 |
shapetype = layer.ShapeType() |
shapetype = layer.ShapeType() |
224 |
if shapetype == SHAPETYPE_POINT: |
if shapetype == SHAPETYPE_POINT: |
227 |
func = self.draw_arc_shape |
func = self.draw_arc_shape |
228 |
else: |
else: |
229 |
func = self.draw_polygon_shape |
func = self.draw_polygon_shape |
230 |
return func, layer |
return False, func, layer |
231 |
|
|
232 |
def make_point(self, x, y): |
def make_point(self, x, y): |
233 |
"""Convert (x, y) to a point object. |
"""Convert (x, y) to a point object. |
236 |
""" |
""" |
237 |
raise NotImplementedError |
raise NotImplementedError |
238 |
|
|
239 |
def projected_points(self, layer, shapeid): |
def projected_points(self, layer, points): |
240 |
"""Return the projected coordinates of shape shapeid in layer. |
"""Return the projected coordinates of the points taken from layer. |
241 |
|
|
242 |
Read the shape from the layer through its Shape method and |
Transform all the points in the list of lists of coordinate |
243 |
transform all the points in the list of lists of coordinate |
pairs in points. |
|
pairs returned by the shape's Points method. |
|
244 |
|
|
245 |
The transformation applies the inverse of the layer's projection |
The transformation applies the inverse of the layer's projection |
246 |
if any, then the map's projection if any and finally applies |
if any, then the map's projection if any and finally applies |
259 |
inverse = proj.Inverse |
inverse = proj.Inverse |
260 |
else: |
else: |
261 |
inverse = None |
inverse = None |
262 |
shape = layer.Shape(shapeid) |
result = [] |
|
points = [] |
|
263 |
scale = self.scale |
scale = self.scale |
264 |
offx, offy = self.offset |
offx, offy = self.offset |
265 |
make_point = self.make_point |
make_point = self.make_point |
266 |
for part in shape.Points(): |
for part in points: |
267 |
points.append([]) |
result.append([]) |
268 |
for x, y in part: |
for x, y in part: |
269 |
if inverse: |
if inverse: |
270 |
x, y = inverse(x, y) |
x, y = inverse(x, y) |
271 |
if forward: |
if forward: |
272 |
x, y = forward(x, y) |
x, y = forward(x, y) |
273 |
points[-1].append(make_point(x * scale + offx, |
result[-1].append(make_point(x * scale + offx, |
274 |
-y * scale + offy)) |
-y * scale + offy)) |
275 |
return points |
return result |
276 |
|
|
277 |
def draw_polygon_shape(self, layer, index, pen, brush): |
def draw_polygon_shape(self, layer, points, pen, brush): |
278 |
"""Draw a polygon shape with the given brush and pen |
"""Draw a polygon shape from layer with the given brush and pen |
279 |
|
|
280 |
The shape is indicated by its id (index) and the layer. The |
The shape is given by points argument which is a the return |
281 |
coordinates in the DC's coordinate system are determined with |
value of the shape's Points() method. The coordinates in the |
282 |
|
DC's coordinate system are determined with |
283 |
self.projected_points. |
self.projected_points. |
284 |
""" |
""" |
285 |
points = self.projected_points(layer, index) |
points = self.projected_points(layer, points) |
286 |
|
|
287 |
if brush is not self.TRANSPARENT_BRUSH: |
if brush is not self.TRANSPARENT_BRUSH: |
288 |
polygon = [] |
polygon = [] |
304 |
for part in points: |
for part in points: |
305 |
self.dc.DrawLines(part) |
self.dc.DrawLines(part) |
306 |
|
|
307 |
def draw_arc_shape(self, layer, index, pen, brush): |
def draw_arc_shape(self, layer, points, pen, brush): |
308 |
"""Draw an arc shape with the given brush and pen |
"""Draw an arc shape from layer with the given brush and pen |
309 |
|
|
310 |
The shape is indicated by its id (index) and the layer. The |
The shape is given by points argument which is a the return |
311 |
coordinates in the DC's coordinate system are determined with |
value of the shape's Points() method. The coordinates in the |
312 |
|
DC's coordinate system are determined with |
313 |
self.projected_points. |
self.projected_points. |
314 |
""" |
""" |
315 |
points = self.projected_points(layer, index) |
points = self.projected_points(layer, points) |
316 |
self.dc.SetBrush(brush) |
self.dc.SetBrush(brush) |
317 |
self.dc.SetPen(pen) |
self.dc.SetPen(pen) |
318 |
for part in points: |
for part in points: |
319 |
self.dc.DrawLines(part) |
self.dc.DrawLines(part) |
320 |
|
|
321 |
def draw_point_shape(self, layer, index, pen, brush): |
def draw_point_shape(self, layer, points, pen, brush): |
322 |
"""Draw a point shape with the given brush and pen |
"""Draw a point shape from layer with the given brush and pen |
323 |
|
|
324 |
The shape is indicated by its id (index) and the layer. The |
The shape is given by points argument which is a the return |
325 |
coordinates in the DC's coordinate system are determined with |
value of the shape's Points() method. The coordinates in the |
326 |
|
DC's coordinate system are determined with |
327 |
self.projected_points. |
self.projected_points. |
328 |
|
|
329 |
The point is drawn as a circle centered on the point. |
The point is drawn as a circle centered on the point. |
330 |
""" |
""" |
331 |
points = self.projected_points(layer, index) |
points = self.projected_points(layer, points) |
332 |
if not points: |
if not points: |
333 |
return |
return |
334 |
|
|