32 |
shapelib.SHPT_ARC: SHAPETYPE_ARC, |
shapelib.SHPT_ARC: SHAPETYPE_ARC, |
33 |
shapelib.SHPT_POINT: SHAPETYPE_POINT} |
shapelib.SHPT_POINT: SHAPETYPE_POINT} |
34 |
|
|
35 |
|
# |
36 |
|
# Raw shape data formats |
37 |
|
# |
38 |
|
|
39 |
class Shape: |
# Raw data is the same as that returned by the points method. |
40 |
|
RAW_PYTHON = "RAW_PYTHON" |
41 |
|
|
42 |
"""Represent one shape""" |
# Raw data is a shapefile. The Shape object will use the shapeid as the |
43 |
|
# raw data. |
44 |
|
RAW_SHAPEFILE = "RAW_SHAPEFILE" |
45 |
|
|
|
def __init__(self, points): |
|
|
self.points = points |
|
|
#self.compute_bbox() |
|
|
self.bbox = None |
|
46 |
|
|
47 |
def compute_bbox(self): |
class ShapefileShape: |
48 |
if self.bbox is not None: |
|
49 |
return self.bbox |
"""Represent one shape of a shapefile""" |
50 |
|
|
51 |
|
def __init__(self, shapefile, shapeid): |
52 |
|
self.shapefile = shapefile |
53 |
|
self.shapeid = shapeid |
54 |
|
|
55 |
|
def compute_bbox(self): |
56 |
|
""" |
57 |
|
Return the bounding box of the shape as a tuple (minx,miny,maxx,maxy) |
58 |
|
""" |
59 |
xs = [] |
xs = [] |
60 |
ys = [] |
ys = [] |
61 |
for part in self.points: |
for part in self.Points(): |
62 |
for x, y in part: |
for x, y in part: |
63 |
xs.append(x) |
xs.append(x) |
64 |
ys.append(y) |
ys.append(y) |
65 |
self.llx = min(xs) |
return (min(xs), min(ys), max(xs), max(ys)) |
|
self.lly = min(ys) |
|
|
self.urx = max(xs) |
|
|
self.ury = max(ys) |
|
|
|
|
|
self.bbox = (self.llx, self.lly, self.urx, self.ury) |
|
|
|
|
|
return self.bbox |
|
66 |
|
|
67 |
def Points(self): |
def Points(self): |
68 |
return self.points |
"""Return the coordinates of the shape as a list of lists of pairs""" |
69 |
|
shape = self.shapefile.read_object(self.shapeid) |
70 |
|
points = shape.vertices() |
71 |
|
if self.shapefile.info()[1] == shapelib.SHPT_POINT: |
72 |
|
points = [points] |
73 |
|
return points |
74 |
|
|
75 |
|
def RawData(self): |
76 |
|
"""Return the shape id to use with the shapefile""" |
77 |
|
return self.shapeid |
78 |
|
|
79 |
|
def Shapefile(self): |
80 |
|
"""Return the shapefile object""" |
81 |
|
return self.shapefile |
82 |
|
|
83 |
|
|
84 |
class ShapeTable(transientdb.AutoTransientTable): |
class ShapeTable(transientdb.AutoTransientTable): |
161 |
""" |
""" |
162 |
return self.shapetype |
return self.shapetype |
163 |
|
|
164 |
|
def RawShapeFormat(self): |
165 |
|
"""Return the raw data format of the shape data, i.e. RAW_SHAPEFILE""" |
166 |
|
return RAW_SHAPEFILE |
167 |
|
|
168 |
def NumShapes(self): |
def NumShapes(self): |
169 |
"""Return the number of shapes in the shape store""" |
"""Return the number of shapes in the shape store""" |
170 |
return self.numshapes |
return self.numshapes |
202 |
|
|
203 |
def Shape(self, index): |
def Shape(self, index): |
204 |
"""Return the shape with index index""" |
"""Return the shape with index index""" |
205 |
shape = self.shapefile.read_object(index) |
return ShapefileShape(self.shapefile, index) |
|
|
|
|
if self.ShapeType() == SHAPETYPE_POINT: |
|
|
points = [shape.vertices()] |
|
|
else: |
|
|
points = [] |
|
|
for poly in shape.vertices(): |
|
|
part = [] |
|
|
for x, y in poly: |
|
|
part.append((x, y)) |
|
|
points.append(part) |
|
|
|
|
|
return Shape(points) |
|
206 |
|
|
207 |
|
|
208 |
|
|