source: cows/trunk/cows/test/test_wms_gdal.py @ 4572

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/test/test_wms_gdal.py@4572
Revision 4572, 3.1 KB checked in by spascoe, 12 years ago (diff)

Added work arround for ticket:1014.

Line 
1"""
2Test cows.service.wms_gdal.
3
4GDAL interface doesn't work so I've deactivated it.
5
6"""
7
8__test__ = False
9
10from cows.service.wms_gdal import *
11from pkg_resources import resource_filename
12from osgeo import gdal, osr
13from osgeo.gdalconst import *
14
15import unittest
16
17
18
19def geoTransform(x, y, T):
20    xg = T[0] + float(x)*T[1] + float(y)*T[2]
21    yg = T[3] + float(x)*T[4] + float(y)*T[5]
22
23    return (xg, yg)
24
25class StaticDataSource(IGDALDataSource):
26    def __init__(self, filename):
27        self._ds = gdal.Open(filename, GA_ReadOnly)
28        if self._ds is None:
29            raise ValueError('Dataset open failed')
30
31        self.title = 'Static data source %s' % filename
32        self.abstract = None
33        self.dimensions = {}
34        self.units = None
35        # Deduce CRS from the WKT
36        sr = osr.SpatialReference()
37        sr.ImportFromWkt(self._ds.GetProjection())
38
39        if sr.GetAuthorityName('PROJCS'):
40            self.crs = '%s:%s' % (sr.GetAuthorityName('PROJCS'),
41                                  sr.GetAuthorityCode('PROJCS'))
42        else:
43            self.crs = '%s:%s' % (sr.GetAuthorityName('GEOGCS'),
44                                  sr.GetAuthorityCode('GEOGCS'))
45
46    def getWKT(self):
47        return self._ds.GetProjection()
48
49    def getBBox(self):
50        # Calculate the bounding box from the geoTransform.
51        T = self._ds.GetGeoTransform()
52        w, h = self._ds.RasterXSize, self._ds.RasterYSize
53
54        # the GeoTransform is relative to the top-left corner of pixel 0,0
55        # therefore to encompass all of the pixels we add 1 to w and h.
56        llx, lly = geoTransform(0, h+1, T)
57        urx, ury = geoTransform(w+1, 0, T)
58        bbox = (llx, lly, urx, ury)
59
60        return bbox
61
62    def getDataset(self, dimValues=None, renderOpts={}):
63        return self._ds
64
65class TestGDAL(unittest.TestSuite):
66    def setUp(self):
67        filename = resource_filename('cows.test', 'data/obs_gdal.png')
68        self.globalDS = StaticDataSource(filename)
69        fn2 = resource_filename('cows.test', 'data/ukcip_rainfall.png')
70        self.ukDS = StaticDataSource(fn2)
71
72    def setupWarp(self):
73        layer = GDALLayer(self.ukDS)
74        sr = osr.SpatialReference()
75        sr.ImportFromEPSG(4326)
76        layer.warpCRS['EPSG:4326'] = sr.ExportToWkt()
77
78        return layer
79
80    def test1(self):
81        layer = GDALLayer(self.globalDS)
82        assert layer.crss == ['EPSG:4326']
83
84    def test2(self):
85        layer = GDALLayer(self.globalDS)
86        slab = layer.getSlab('EPSG:4326')
87        assert slab.bbox == (-180.0, -90.0, 180.0, 90.0)
88
89    def test3(self):
90        layer = GDALLayer(self.globalDS)
91        slab = layer.getSlab('EPSG:4326')
92        img = slab.getImage((-10.0, 45.0, 5.0, 60.0), 100, 100)
93        img.save('out1.png')
94
95    def test4(self):
96        layer = self.setupWarp()
97        print layer.crss
98        assert layer.crss == ['EPSG:27700', 'EPSG:4326']
99
100    def test5(self):
101        layer = self.setupWarp()
102        slab = layer.getSlab('EPSG:4326')
103
104        # Create a copy of the warped image
105        ds = slab._data
106        import pdb; pdb.set_trace()
107
108        img = slab.getImage((-3.0, 54.0, 3.0, 60.0), 100, 100)
109        img.save('out2.png')
Note: See TracBrowser for help on using the repository browser.