1 | """ |
---|
2 | Test ows_common.service.wms_gdal. |
---|
3 | |
---|
4 | """ |
---|
5 | |
---|
6 | from ows_common.service.wms_gdal import * |
---|
7 | from pkg_resources import resource_filename |
---|
8 | from osgeo import gdal, osr |
---|
9 | from osgeo.gdalconst import * |
---|
10 | |
---|
11 | import unittest |
---|
12 | |
---|
13 | |
---|
14 | |
---|
15 | def geoTransform(x, y, T): |
---|
16 | xg = T[0] + float(x)*T[1] + float(y)*T[2] |
---|
17 | yg = T[3] + float(x)*T[4] + float(y)*T[5] |
---|
18 | |
---|
19 | return (xg, yg) |
---|
20 | |
---|
21 | class StaticDataSource(IGDALDataSource): |
---|
22 | def __init__(self, filename): |
---|
23 | self._ds = gdal.Open(filename, GA_ReadOnly) |
---|
24 | if self._ds is None: |
---|
25 | raise ValueError('Dataset open failed') |
---|
26 | |
---|
27 | self.title = 'Static data source %s' % filename |
---|
28 | self.abstract = None |
---|
29 | self.dimensions = {} |
---|
30 | self.units = None |
---|
31 | # Deduce CRS from the WKT |
---|
32 | sr = osr.SpatialReference() |
---|
33 | sr.ImportFromWkt(self._ds.GetProjection()) |
---|
34 | self.crs = '%s:%s' % (sr.GetAuthorityName('GEOGCS'), |
---|
35 | sr.GetAuthorityCode('GEOGCS')) |
---|
36 | |
---|
37 | def getWKT(self): |
---|
38 | return self._ds.GetProjection() |
---|
39 | |
---|
40 | def getBBox(self): |
---|
41 | # Calculate the bounding box from the geoTransform. |
---|
42 | T = self._ds.GetGeoTransform() |
---|
43 | w, h = self._ds.RasterXSize, self._ds.RasterYSize |
---|
44 | |
---|
45 | bbox = geoTransform(0, h, T) + geoTransform(w, 0, T) |
---|
46 | |
---|
47 | return bbox |
---|
48 | |
---|
49 | def getDataset(self, dimValues=None, renderOpts={}): |
---|
50 | return self._ds |
---|
51 | |
---|
52 | class TestGDAL(unittest.TestSuite): |
---|
53 | def setUp(self): |
---|
54 | filename = resource_filename('ows_common.test', 'data/obs_gdal.png') |
---|
55 | self.globalDS = StaticDataSource(filename) |
---|
56 | fn2 = resource_filename('ows_common.test', 'data/ukcip_rainfall.png') |
---|
57 | self.ukDS = StaticDataSource(fn2) |
---|
58 | |
---|
59 | def setupWarp(self): |
---|
60 | layer = GDALLayer(self.ukDS) |
---|
61 | sr = osr.SpatialReference() |
---|
62 | sr.ImportFromEPSG(4326) |
---|
63 | layer.warpCRS['EPSG:4326'] = sr.ExportToWkt() |
---|
64 | |
---|
65 | return layer |
---|
66 | |
---|
67 | def test1(self): |
---|
68 | layer = GDALLayer(self.globalDS) |
---|
69 | assert layer.crss == ['EPSG:4326'] |
---|
70 | |
---|
71 | def test2(self): |
---|
72 | layer = GDALLayer(self.globalDS) |
---|
73 | slab = layer.getSlab('EPSG:4326') |
---|
74 | assert slab.bbox == (-180.0, -90.0, 180.0, 90.0) |
---|
75 | |
---|
76 | def test3(self): |
---|
77 | layer = GDALLayer(self.globalDS) |
---|
78 | slab = layer.getSlab('EPSG:4326') |
---|
79 | img = slab.getImage((-10.0, 45.0, 5.0, 60.0), 100, 100) |
---|
80 | img.save('out1.png') |
---|
81 | |
---|
82 | def test4(self): |
---|
83 | layer = self.setupWarp() |
---|
84 | assert layer.crss == ['EPSG:27700', 'EPSG:4326'] |
---|
85 | |
---|
86 | def test5(self): |
---|
87 | layer = self.setupWarp() |
---|
88 | slab = layer.getSlab('EPSG:4326') |
---|
89 | img = slab.getImage((-3.46, 55.08, 3.46, 61.08), 100, 100) |
---|
90 | img.save('out2.png') |
---|