7 |
|
|
8 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
9 |
|
|
10 |
|
import os |
11 |
from math import log, ceil |
from math import log, ceil |
12 |
|
|
13 |
import shapelib, shptree |
import shapelib, shptree |
19 |
# Some predefined colors for internal use |
# Some predefined colors for internal use |
20 |
_black = Color(0, 0, 0) |
_black = Color(0, 0, 0) |
21 |
|
|
22 |
|
from classification import Classification |
23 |
|
|
24 |
from table import Table |
from table import Table |
25 |
|
|
85 |
"""Set the layer's visibility.""" |
"""Set the layer's visibility.""" |
86 |
self.visible = visible |
self.visible = visible |
87 |
self.issue(LAYER_VISIBILITY_CHANGED, self) |
self.issue(LAYER_VISIBILITY_CHANGED, self) |
88 |
|
|
89 |
|
|
90 |
class Layer(BaseLayer): |
class Layer(BaseLayer): |
91 |
|
|
92 |
"""Represent the information of one geodata file (currently a shapefile) |
"""Represent the information of one geodata file (currently a shapefile) |
121 |
colors are expected to be instances of Color class |
colors are expected to be instances of Color class |
122 |
""" |
""" |
123 |
BaseLayer.__init__(self, title, visible = visible) |
BaseLayer.__init__(self, title, visible = visible) |
124 |
self.filename = filename |
|
125 |
|
# Make the filename absolute. The filename will be |
126 |
|
# interpreted relative to that anyway, but when saving a |
127 |
|
# session we need to compare absolute paths and it's usually |
128 |
|
# safer to always work with absolute paths. |
129 |
|
self.filename = os.path.abspath(filename) |
130 |
|
|
131 |
self.projection = projection |
self.projection = projection |
132 |
self.fill = fill |
self.fill = fill |
133 |
self.stroke = stroke |
self.stroke = stroke |
141 |
self.shapetable = Table(filename) |
self.shapetable = Table(filename) |
142 |
self.table = self.shapetable |
self.table = self.shapetable |
143 |
|
|
144 |
|
self.classification = Classification() |
145 |
|
self.classification.setNull( |
146 |
|
{'stroke':stroke, 'stroke_width':stroke_width, 'fill':fill}) |
147 |
|
|
148 |
def open_shapefile(self): |
def open_shapefile(self): |
149 |
if self.shapefile is None: |
if self.shapefile is None: |
150 |
self.shapefile = shapelib.ShapeFile(self.filename) |
self.shapefile = shapelib.ShapeFile(self.filename) |
171 |
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
self.shapetree = shptree.SHPTree(self.shapefile.cobject(), 2, |
172 |
maxdepth) |
maxdepth) |
173 |
|
|
174 |
|
def Destroy(self): |
175 |
|
BaseLayer.Destroy(self) |
176 |
|
if self.shapefile is not None: |
177 |
|
self.shapefile.close() |
178 |
|
self.shapefile = None |
179 |
|
self.shapetree = None |
180 |
|
self.table.Destroy() |
181 |
|
|
182 |
def BoundingBox(self): |
def BoundingBox(self): |
183 |
"""Return the layer's bounding box in the intrinsic coordinate system. |
"""Return the layer's bounding box in the intrinsic coordinate system. |
184 |
|
|
220 |
"""Return the shape with index index""" |
"""Return the shape with index index""" |
221 |
self.open_shapefile() |
self.open_shapefile() |
222 |
shape = self.shapefile.read_object(index) |
shape = self.shapefile.read_object(index) |
223 |
|
|
224 |
if self.shapetype == SHAPETYPE_POINT: |
if self.shapetype == SHAPETYPE_POINT: |
225 |
points = shape.vertices() |
points = shape.vertices() |
226 |
else: |
else: |
229 |
points = [] |
points = [] |
230 |
for x, y in poly: |
for x, y in poly: |
231 |
points.append((x, y)) |
points.append((x, y)) |
232 |
|
|
233 |
return Shape(points) |
return Shape(points) |
234 |
|
|
235 |
def ShapesInRegion(self, box): |
def ShapesInRegion(self, box): |