tests.test_force_srs()   B
last analyzed

Complexity

Conditions 1

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 44
rs 8.8571
1
from patty.srs import set_srs, force_srs, is_registered
2
import pcl
3
import numpy as np
4
from osgeo import osr
5
6
from numpy.testing import assert_array_almost_equal
7
from nose.tools import assert_true,assert_false,assert_less
8
9
def test_force_srs():
10
    """Test the force_srs() function"""
11
12
    rdnew = osr.SpatialReference()
13
    rdnew.SetFromUserInput( "EPSG:28992" )
14
15
    latlon = osr.SpatialReference()
16
    latlon.SetFromUserInput( "EPSG:4326" )
17
18
    # using offset and srs (text)
19
    pcA = pcl.PointCloud( [[1,2,3]] )
20
21
    force_srs( pcA, offset=[0,0,0], srs="EPSG:28992" )
22
    assert_true( is_registered( pcA ) )
23
24
    assert_array_almost_equal( pcA.offset, np.zeros(3, dtype=np.float64), 15,
25
        "Offset not zero to 15 decimals" )
26
27
    assert_true( pcA.srs.IsSame( rdnew ) )
28
29
    # using same_as
30
    pcB = pcl.PointCloud( [[1,2,3]] )
31
32
    force_srs( pcB, same_as=pcA )
33
    assert_true( is_registered( pcB ) )
34
35
    assert_array_almost_equal( pcB.offset, np.zeros(3, dtype=np.float64), 15,
36
        "Offset not zero to 15 decimals" )
37
    assert_true( pcB.srs.IsSame( rdnew ) )
38
39
    # using no offset and osr.SpatialReference()
40
    pcC = pcl.PointCloud( [[1,2,3]] )
41
42
    force_srs( pcC, srs=rdnew )
43
44
    assert_true( pcC.srs.IsSame( rdnew ) )
45
    assert_false( hasattr( pcC, "offset" ) )
46
47
    # testing if no actual transform occurs on the points
48
    force_srs(pcC, srs=latlon )
49
    assert_false( pcC.srs.IsSame( pcA.srs ) )
50
51
    assert_array_almost_equal( np.asarray(pcA), np.asarray(pcC), 8,
52
        "force_srs() should not alter points" )
53
54
55
def test_set_srs():
56
    """Test the set_srs() function"""
57
58
    rdnew = osr.SpatialReference()
59
    rdnew.SetFromUserInput( "EPSG:28992" )
60
61
    latlon = osr.SpatialReference()
62
    latlon.SetFromUserInput( "EPSG:4326" )
63
64
    adam_latlon = np.array(
65
        [4.904153991281891, 52.372337993959924, 42.97214563656598],
66
        dtype=np.float64)
67
68
    adam_rdnew = np.array([122104.0, 487272.0, 0.0], dtype=np.float64)
69
70
    pcA = pcl.PointCloud ( [[0.,0.,0.]] )
71
    force_srs( pcA, srs=latlon, offset=adam_latlon )
72
73
    pcB = pcl.PointCloud ( [[0., 0., 0.]] )
74
    force_srs( pcB, srs=rdnew, offset=adam_rdnew )
75
76
    # latlon [degrees] -> rdnew [m]
77
    set_srs( pcA, same_as=pcB )
78
79
    assert_less( np.max( np.square( np.asarray( pcA ) ) ), 1e-3 ** 2,
80
        "Coordinate transform not accurate to 1 mm %s" % np.asarray(pcA) )
81
82
83
    # rdnew [m] -> latlon [degrees]
84
    set_srs( pcB, srs=latlon, offset=[0,0,0] )
85
86
    # check horizontal error [degrees]
87
    error = np.asarray(pcB)[0] - adam_latlon
88
89
    assert_less( np.max( np.square(error[0:2]) ), (1e-6) ** 2,
90
        "Coordinate transform rdnew->latlon not accurate to 1e-6 degree %s"
91
         % error[0:2] )
92
93
    # check vertical error [m]
94
    assert_less( abs(error[2]) , (1e-6) ,
95
        "Vertical Coordinate in of transform not accurate to 1e-6 meter %s"
96
         % abs(error[2]) )
97