source: TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common/test/test_wms_gdal.py @ 3677

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common/test/test_wms_gdal.py@3677
Revision 3677, 2.3 KB checked in by spascoe, 13 years ago (diff)

Slowly improving gdal"

Line 
1"""
2Test ows_common.service.wms_gdal.
3
4"""
5
6from ows_common.service.wms_gdal import *
7from pkg_resources import resource_filename
8from osgeo import gdal, osr
9from osgeo.gdalconst import *
10
11import unittest
12
13
14
15def 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
21class 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
52class TestGDAL(unittest.TestSuite):
53    def setUp(self):
54        filename = resource_filename('ows_common.test', 'data/obs_gdal.png')
55        self.ds = StaticDataSource(filename)
56
57    def setupWarp(self):
58        layer = GDALLayer(self.ds)
59        sr = osr.SpatialReference()
60        sr.ImportFromEPSG(27700)
61        layer.warpCRS['EPSG:27700'] = sr.ExportToWkt()
62
63        return layer
64
65    def test1(self):
66        layer = GDALLayer(self.ds)
67        assert layer.crss == ['EPSG:4326']
68
69    def test2(self):
70        layer = GDALLayer(self.ds)
71        slab = layer.getSlab('EPSG:4326')
72        assert slab.bbox == (-180.0, -90.0, 180.0, 90.0)
73
74    def test3(self):
75        layer = GDALLayer(self.ds)
76        slab = layer.getSlab('EPSG:4326')
77        img = slab.getImage((-10.0, 45.0, 5.0, 60.0), 100, 100)
78        img.save('out1.png')
79
80    def test4(self):
81        layer = self.setupWarp()
82        assert layer.crss == ['EPSG:4326', 'EPSG:27700']
83
84    def test5(self):
85        layer = self.setupWarp()
86        slab = layer.getSlab('EPSG:27700')
87       
Note: See TracBrowser for help on using the repository browser.