26 |
from Thuban.Model.data import SHAPETYPE_POLYGON, SHAPETYPE_ARC, SHAPETYPE_POINT |
from Thuban.Model.data import SHAPETYPE_POLYGON, SHAPETYPE_ARC, SHAPETYPE_POINT |
27 |
from Thuban.Model.data import RAW_PYTHON, RAW_SHAPEFILE, RAW_WKT |
from Thuban.Model.data import RAW_PYTHON, RAW_SHAPEFILE, RAW_WKT |
28 |
|
|
29 |
SHAPETYPE_UNKNOWN = "unknown" |
SHAPETYPE_UNKNOWN = ogr.wkbUnknown |
30 |
|
|
31 |
def has_ogr_support(): |
def has_ogr_support(): |
32 |
"""Return whether this Thuban instance supports ogr file formats |
"""Return whether this Thuban instance supports ogr file formats |
54 |
|
|
55 |
def __init__(self, ogrlayer, shapeid): |
def __init__(self, ogrlayer, shapeid): |
56 |
self.ogrlayer = ogrlayer |
self.ogrlayer = ogrlayer |
57 |
|
self.feature = self.ogrlayer.GetFeature(shapeid) |
58 |
self.shapeid = shapeid |
self.shapeid = shapeid |
59 |
|
self.geom = self.feature.GetGeometryRef() |
60 |
|
self.shapetype = self.geom.GetGeometryType() |
61 |
|
|
62 |
def compute_bbox(self): |
def compute_bbox(self): |
63 |
""" |
""" |
64 |
Return the bounding box of the shape as a tuple (minx,miny,maxx,maxy) |
Return the bounding box of the shape as a tuple (minx,miny,maxx,maxy) |
65 |
""" |
""" |
66 |
shape = self.ogrlayer.GetFeature(self.shapeid) |
minx, maxx, miny, maxy = self.geom.GetEnvelope() |
|
geom = shape.GetGeometryRef() |
|
|
minx, maxx, miny, maxy = geom.GetEnvelope() |
|
67 |
return (minx, miny, maxx, maxy) |
return (minx, miny, maxx, maxy) |
68 |
|
|
69 |
def ShapeID(self): |
def ShapeID(self): |
71 |
|
|
72 |
def Points(self): |
def Points(self): |
73 |
"""Return the coordinates of the shape as a list of lists of pairs""" |
"""Return the coordinates of the shape as a list of lists of pairs""" |
|
print "FID: %s" %(self.shapeid) |
|
74 |
shape = [] |
shape = [] |
75 |
#spatialFilter = self.ogrlayer.GetSpatialFilter() |
#spatialFilter = self.ogrlayer.GetSpatialFilter() |
|
|
|
|
# if spatialFilter is not None: |
|
|
self.ogrlayer.SetSpatialFilter(None) |
|
|
# feature = self.ogrlayer.GetFeature(self.shapeid) |
|
|
# self.ogrlayer.SetSpatialFilter(spatialFilter) |
|
|
# else: |
|
|
feature = self.ogrlayer.GetFeature(self.shapeid) |
|
|
|
|
|
if feature is None: |
|
|
print "feature is none.........................." |
|
|
return shape.append([]) |
|
|
geom = feature.GetGeometryRef() |
|
76 |
|
|
77 |
if geom is None: |
#if spatialFilter is not None: |
78 |
print "geom is none................................" |
#self.ogrlayer.SetSpatialFilter(None) |
79 |
|
#feature = self.ogrlayer.GetFeature(self.shapeid) |
80 |
|
#self.ogrlayer.SetSpatialFilter(spatialFilter) |
81 |
|
#else: |
82 |
|
#feature = self.ogrlayer.GetFeature(self.shapeid) |
83 |
|
|
84 |
|
#if feature is None: |
85 |
|
# return shape.append([]) |
86 |
|
#geom = feature.GetGeometryRef() |
87 |
|
|
88 |
|
if self.geom is None: |
89 |
return shape.append([]) |
return shape.append([]) |
90 |
|
|
91 |
# if geometry object is of type point or line |
# if geometry object is of type point or line |
92 |
if geom.GetGeometryCount() == 0: |
if self.geom.GetGeometryCount() == 0: |
93 |
points =[] |
points =[] |
94 |
for point in range(geom.GetPointCount()): |
for point in range(self.geom.GetPointCount()): |
95 |
x = geom.GetX(point) |
x = self.geom.GetX(point) |
96 |
y = geom.GetY(point) |
y = self.geom.GetY(point) |
97 |
points.append((x, y)) |
points.append((x, y)) |
|
print points |
|
98 |
return [points] |
return [points] |
99 |
# if geometry object is of type polygon or multipolygon |
# if geometry object is of type polygon or multipolygon |
100 |
for i in range(geom.GetGeometryCount()): |
for i in range(self.geom.GetGeometryCount()): |
101 |
points = [] |
points = [] |
102 |
geometry = geom.GetGeometryRef(i) |
geometry = self.geom.GetGeometryRef(i) |
103 |
# if geometry object is polygon |
# if geometry object is polygon |
104 |
if geometry.GetGeometryCount() == 0: |
if geometry.GetGeometryCount() == 0: |
105 |
for point in range(geometry.GetPointCount()): |
for point in range(geometry.GetPointCount()): |
117 |
y = subgeom.GetY(point) |
y = subgeom.GetY(point) |
118 |
points.append((x, y)) |
points.append((x, y)) |
119 |
shape.append(points) |
shape.append(points) |
|
print shape |
|
120 |
return shape |
return shape |
121 |
|
|
122 |
def RawData(self): |
def RawData(self): |
127 |
"""Return the ogrlayer object""" |
"""Return the ogrlayer object""" |
128 |
return self.ogrlayer |
return self.ogrlayer |
129 |
|
|
130 |
|
def ShapeType(self): |
131 |
|
return self.shapetype |
132 |
|
|
133 |
|
|
134 |
class OGRShapeStore: |
class OGRShapeStore: |
135 |
|
|
137 |
providing all methods Thuban needs. |
providing all methods Thuban needs. |
138 |
""" |
""" |
139 |
|
|
140 |
def __init__(self, session, filename, layername): |
def __init__(self, filename, layername, id_column = None): |
141 |
# Make the filename absolute. The filename will be |
# Make the filename absolute. The filename will be |
142 |
# interpreted relative to that anyway, but when saving a |
# interpreted relative to that anyway, but when saving a |
143 |
# session we need to compare absolute paths and it's usually |
# session we need to compare absolute paths and it's usually |
172 |
if self.shapetype is not ogr.wkbUnknown: |
if self.shapetype is not ogr.wkbUnknown: |
173 |
self.shapetype = ogrlib_shapetypes[self.shapetype] |
self.shapetype = ogrlib_shapetypes[self.shapetype] |
174 |
#else: |
#else: |
175 |
# this should be ogr.wkbUnknown, but Thuban does not know how |
# this should be ogr.wkbUnknown, but Thuban does not know how |
176 |
# to handle an unknown shapetype (e.g. Session Tree) |
# to handle an unknown shapetype (e.g. Session Tree) |
177 |
#self.shapetype = ogrlib_shapetypes[ogr.wkbPoint] |
#self.shapetype = ogrlib_shapetypes[ogr.wkbPoint] |
178 |
|
|
179 |
def OGRLayer(self): |
def OGRLayer(self): |
222 |
ogrlayer = self.ogrlayer |
ogrlayer = self.ogrlayer |
223 |
|
|
224 |
left, bottom, right, top = bbox |
left, bottom, right, top = bbox |
|
print "features in bbox: " |
|
|
print ('Polygon((%s %s, %s %s, %s %s,%s %s, %s %s))' |
|
|
%(left, bottom, left, top, right, top, |
|
|
right, bottom, left, bottom)) |
|
225 |
# create a geometry which can be passed to the layer as spatial filter |
# create a geometry which can be passed to the layer as spatial filter |
226 |
bboxpolygon = ogr.CreateGeometryFromWkt( |
bboxpolygon = ogr.CreateGeometryFromWkt( |
227 |
('Polygon((%s %s, %s %s, %s %s,%s %s, %s %s))' |
('Polygon((%s %s, %s %s, %s %s,%s %s, %s %s))' |
234 |
ogrlayer.ResetReading() |
ogrlayer.ResetReading() |
235 |
#ogrlayer.SetSpatialFilterRect(left, bottom, right, top) |
#ogrlayer.SetSpatialFilterRect(left, bottom, right, top) |
236 |
ogrlayer.SetSpatialFilter(bboxpolygon) |
ogrlayer.SetSpatialFilter(bboxpolygon) |
237 |
|
|
238 |
numFeatures = ogrlayer.GetFeatureCount() |
numFeatures = ogrlayer.GetFeatureCount() |
|
print numFeatures |
|
239 |
for feature in range(numFeatures): |
for feature in range(numFeatures): |
240 |
nextFeature = ogrlayer.GetNextFeature() |
nextFeature = ogrlayer.GetNextFeature() |
241 |
yield cls(ogrlayer, nextFeature.GetFID()) |
yield cls(ogrlayer, nextFeature.GetFID()) |
242 |
|
|
243 |
ogrlayer.SetSpatialFilter(None) |
ogrlayer.SetSpatialFilter(None) |
244 |
bboxpolygon.Destroy() |
bboxpolygon.Destroy() |
245 |
|
|
267 |
"""Return None.""" |
"""Return None.""" |
268 |
return None |
return None |
269 |
|
|
270 |
|
def Id_column(self): |
271 |
|
"""Return the id_column.""" |
272 |
|
return self.id_column |
273 |
|
|
274 |
class OGRTable: |
class OGRTable: |
275 |
|
|
483 |
self.datasource.ReleaseResultSet(lay) |
self.datasource.ReleaseResultSet(lay) |
484 |
return result |
return result |
485 |
|
|
486 |
|
def Id_column(self): |
487 |
|
"""Return the id_column.""" |
488 |
|
return self.id_column |
489 |
|
|
490 |
|
|
491 |
class OGRColumn: |
class OGRColumn: |
492 |
|
|