15 |
import shapelib, shptree |
import shapelib, shptree |
16 |
|
|
17 |
from messages import LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED, \ |
from messages import LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED, \ |
18 |
LAYER_CHANGED, LAYER_SHAPESTORE_REPLACED |
LAYER_CHANGED, LAYER_SHAPESTORE_REPLACED, CLASS_CHANGED |
19 |
|
|
20 |
import classification |
import classification |
21 |
|
|
152 |
visible = visible, |
visible = visible, |
153 |
projection = projection) |
projection = projection) |
154 |
|
|
|
# |
|
|
# this is really important so that when the classification class |
|
|
# tries to set its parent layer the variable will exist |
|
|
# |
|
155 |
self.__classification = None |
self.__classification = None |
156 |
|
self.store = None |
157 |
|
|
158 |
self.SetShapeStore(data) |
self.SetShapeStore(data) |
159 |
|
|
160 |
|
self.SetClassificationField(None) |
161 |
self.SetClassification(None) |
self.SetClassification(None) |
162 |
|
|
163 |
self.__classification.SetDefaultLineColor(stroke) |
self.__classification.SetDefaultLineColor(stroke) |
188 |
return value |
return value |
189 |
|
|
190 |
def SetShapeStore(self, store): |
def SetShapeStore(self, store): |
191 |
|
# Set the classification to None if there is a classification |
192 |
|
# and the new shapestore doesn't have a table with a suitable |
193 |
|
# column, i.e one with the same name and type as before |
194 |
|
# FIXME: Maybe we should keep it the same if the type is |
195 |
|
# compatible enough such as FIELDTYPE_DOUBLE and FIELDTYPE_INT |
196 |
|
if self.__classification is not None: |
197 |
|
fieldname = self.classificationField |
198 |
|
fieldtype = self.GetFieldType(fieldname) |
199 |
|
table = store.Table() |
200 |
|
if (fieldname is not None |
201 |
|
and (not table.HasColumn(fieldname) |
202 |
|
or table.Column(fieldname).type != fieldtype)): |
203 |
|
self.SetClassification(None) |
204 |
|
|
205 |
self.store = store |
self.store = store |
206 |
shapefile = self.store.Shapefile() |
shapefile = self.store.Shapefile() |
207 |
|
|
227 |
|
|
228 |
self.shapetree = shptree.SHPTree(shapefile.cobject(), 2, |
self.shapetree = shptree.SHPTree(shapefile.cobject(), 2, |
229 |
maxdepth) |
maxdepth) |
|
# Set the classification to None if there is a classification |
|
|
# and the new shapestore doesn't have a table with a suitable |
|
|
# column, i.e one with the same name and type as before |
|
|
# FIXME: Maybe we should keep it the same if the type is |
|
|
# compatible enough such as FIELDTYPE_DOUBLE and FIELDTYPE_INT |
|
|
if self.__classification is not None: |
|
|
fieldname = self.__classification.GetField() |
|
|
fieldtype = self.__classification.GetFieldType() |
|
|
table = self.store.Table() |
|
|
if (fieldname is not None |
|
|
and (not table.HasColumn(fieldname) |
|
|
or table.Column(fieldname).type != fieldtype)): |
|
|
self.SetClassification(None) |
|
230 |
self.changed(LAYER_SHAPESTORE_REPLACED, self) |
self.changed(LAYER_SHAPESTORE_REPLACED, self) |
231 |
|
|
232 |
def ShapeStore(self): |
def ShapeStore(self): |
234 |
|
|
235 |
def Destroy(self): |
def Destroy(self): |
236 |
BaseLayer.Destroy(self) |
BaseLayer.Destroy(self) |
237 |
self.GetClassification()._set_layer(None) |
if self.__classification is not None: |
238 |
|
self.__classification.Unsubscribe(CLASS_CHANGED, self.ClassChanged) |
239 |
|
|
240 |
def BoundingBox(self): |
def BoundingBox(self): |
241 |
"""Return the layer's bounding box in the intrinsic coordinate system. |
"""Return the layer's bounding box in the intrinsic coordinate system. |
292 |
return (min(llx), min(lly), max(urx), max(ury)) |
return (min(llx), min(lly), max(urx), max(ury)) |
293 |
|
|
294 |
def GetFieldType(self, fieldName): |
def GetFieldType(self, fieldName): |
295 |
table = self.store.Table() |
if self.store: |
296 |
if table.HasColumn(fieldName): |
table = self.store.Table() |
297 |
return table.Column(fieldName).type |
if table.HasColumn(fieldName): |
298 |
|
return table.Column(fieldName).type |
299 |
return None |
return None |
300 |
|
|
301 |
def HasShapes(self): |
def HasShapes(self): |
339 |
|
|
340 |
return self.shapetree.find_shapes((left, bottom), (right, top)) |
return self.shapetree.find_shapes((left, bottom), (right, top)) |
341 |
|
|
342 |
|
def GetClassificationField(self): |
343 |
|
return self.classificationField |
344 |
|
|
345 |
|
def SetClassificationField(self, field): |
346 |
|
"""Set the field to classifiy on, or None. If field is not None |
347 |
|
and the field does not exist in the table, raise a ValueError. |
348 |
|
""" |
349 |
|
if field: |
350 |
|
fieldType = self.GetFieldType(field) |
351 |
|
if fieldType is None: |
352 |
|
raise ValueError() |
353 |
|
self.classificationField = field |
354 |
|
|
355 |
def HasClassification(self): |
def HasClassification(self): |
356 |
return True |
return True |
357 |
|
|
363 |
|
|
364 |
If 'clazz' is None a default classification is created. |
If 'clazz' is None a default classification is created. |
365 |
|
|
366 |
ValueError is raised if the classification's field name |
This issues a LAYER_CHANGED event. |
|
and type are different (if they aren't None) than what |
|
|
is in the shapestore. The Layer will not be changed in |
|
|
this case. |
|
367 |
""" |
""" |
368 |
|
|
369 |
old_class = self.__classification |
if self.__classification is not None: |
370 |
|
self.__classification.Unsubscribe(CLASS_CHANGED, self.ClassChanged) |
371 |
|
|
372 |
if clazz is None: |
if clazz is None: |
373 |
clazz = classification.Classification() |
clazz = classification.Classification() |
374 |
|
|
375 |
try: |
self.__classification = clazz |
376 |
self.__classification = clazz |
self.__classification.Subscribe(CLASS_CHANGED, self.ClassChanged) |
377 |
clazz._set_layer(self) |
|
378 |
|
self.ClassChanged() |
|
# only change things after a successful call |
|
|
if old_class is not None: |
|
|
old_class._set_layer(None) |
|
|
except ValueError: |
|
|
self.__classification = old_class |
|
|
raise ValueError |
|
|
|
|
|
# we don't need this since a message will be sent |
|
|
# after calling _set_layer() |
|
|
#self.changed(LAYER_CHANGED, self) |
|
379 |
|
|
380 |
def ClassChanged(self): |
def ClassChanged(self): |
381 |
"""Called from the classification object when it has changed.""" |
"""Called from the classification object when it has changed.""" |