Coverage for gws-app/gws/gis/bounds/_test.py: 0%

83 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +0200

1"""Tests for the bounds module.""" 

2import math 

3 

4import gws 

5import gws.test.util as u 

6import gws.gis.bounds as bounds 

7import gws.gis.crs 

8 

9 

10def test_from_request_bbox(): 

11 bbox = '189000,834000,285000,962000,urn:x-ogc:def:crs:EPSG:4326' 

12 crs = gws.gis.crs.WGS84 

13 bound = gws.Bounds(crs=crs, extent=(834000.0, 189000.0, 962000.0, 285000.0)) 

14 assert bounds.from_request_bbox(bbox).crs == bound.crs 

15 assert bounds.from_request_bbox(bbox).extent == bound.extent 

16 

17 

18def test_from_request_bbox_no_crs(): 

19 bbox = '189000,834000,285000,962000' 

20 assert not bounds.from_request_bbox(bbox) 

21 

22 

23def test_from_request_bbox_default_crs(): 

24 bbox = '189000,834000,285000,962000' 

25 crs = gws.gis.crs.WGS84 

26 bound = gws.Bounds(crs=crs, extent=(834000.0, 189000.0, 962000.0, 285000.0)) 

27 assert bounds.from_request_bbox(bbox, crs).crs == bound.crs 

28 assert bounds.from_request_bbox(bbox, crs).extent == bound.extent 

29 

30 

31def test_from_request_bbox_empty(): 

32 assert not bounds.from_request_bbox('') 

33 

34 

35def test_from_request_bbox_wrong_bbox(): 

36 bbox = '189000,962000,urn:x-ogc:def:crs:EPSG:4326' 

37 assert not bounds.from_request_bbox(bbox) 

38 

39 

40def test_from_request_bbox_alwaysxy(): 

41 bbox = '189000,834000,285000,962000' 

42 crs = gws.gis.crs.WGS84 

43 bound = gws.Bounds(crs=crs, extent=(189000.0, 834000.0, 285000.0, 962000.0)) 

44 assert bounds.from_request_bbox(bbox, crs, always_xy=True).crs == bound.crs 

45 assert bounds.from_request_bbox(bbox, crs, always_xy=True).extent == bound.extent 

46 

47 

48def test_from_extent(): 

49 extent = (100, 200, 300, 400) 

50 crs = gws.gis.crs.WGS84 

51 assert bounds.from_extent(extent, crs).extent == (200.0, 100.0, 400.0, 300.0) 

52 

53 

54def test_from_extent_alwaysxy(): 

55 extent = (100, 200, 300, 400) 

56 crs = gws.gis.crs.WGS84 

57 assert bounds.from_extent(extent, crs, always_xy=True).extent == (100.0, 200.0, 300.0, 400.0) 

58 

59 

60def test_copy(): 

61 crs = gws.gis.crs.WGS84 

62 extent = (100, 200, 300, 400) 

63 bound = gws.Bounds(crs=crs, extent=extent) 

64 assert not bounds.copy(bound) == bound 

65 assert bounds.copy(bound).crs == bound.crs 

66 assert bounds.copy(bound).extent == bound.extent 

67 

68 

69def test_union(): 

70 wg = gws.gis.crs.WGS84 

71 web = gws.gis.crs.WEBMERCATOR 

72 bound1 = gws.Bounds(crs=wg, extent=(1, 100, 200, 200)) 

73 bound2 = gws.Bounds(crs=web, extent=(100, 2, 200, 200)) 

74 bound3 = gws.Bounds(crs=web, extent=(100, 100, 300, 200)) 

75 bound4 = gws.Bounds(crs=web, extent=(100, 100, 200, 400)) 

76 

77 bound = gws.Bounds(crs=wg, extent=(0.0008983152841195213, 1.7966305682390134e-05, 200, 200)) 

78 assert bounds.union([bound1, bound2, bound3, bound4]).extent == bound.extent 

79 assert bounds.union([bound1, bound2, bound3, bound4]).crs == bound.crs 

80 

81 

82def test_union_empty(): 

83 with u.raises(Exception): 

84 bounds.union([]) 

85 

86 

87def test_intersect(): 

88 crs = gws.gis.crs.WEBMERCATOR 

89 b1 = gws.Bounds(crs=crs, extent=(100, 100, 400, 400)) 

90 b2 = gws.Bounds(crs=crs, extent=(300, 300, 500, 500)) 

91 

92 assert bounds.intersect(b1, b2) 

93 

94 

95def test_intersect_empty(): 

96 crs = gws.gis.crs.WEBMERCATOR 

97 b1 = gws.Bounds(crs=crs, extent=(100, 100, 400, 400)) 

98 b2 = gws.Bounds(crs=crs, extent=(500, 500, 600, 600)) 

99 

100 assert not bounds.intersect(b1, b2) 

101 

102 

103def test_transform(): 

104 crs = gws.gis.crs.WEBMERCATOR 

105 to_crs = gws.gis.crs.WGS84 

106 b1 = gws.Bounds(crs=crs, extent=(100, 100, 200, 200)) 

107 b2 = gws.Bounds(crs=to_crs, extent=(8983, 8983, 17966, 17966)) 

108 ext = bounds.transform(b1, to_crs).extent 

109 ext = ( 

110 math.trunc(ext[0] * 10000000), 

111 math.trunc(ext[1] * 10000000), 

112 math.trunc(ext[2] * 10000000), 

113 math.trunc(ext[3] * 10000000) 

114 ) 

115 assert bounds.transform(b1, to_crs).crs == b2.crs 

116 assert ext == b2.extent 

117 

118 

119def test_buffer(): 

120 crs = gws.gis.crs.WGS84 

121 b = gws.Bounds(crs=crs, extent=(100, 100, 400, 400)) 

122 assert bounds.buffer(b, 50).extent == (50, 50, 450, 450) 

123 assert bounds.buffer(b, 50).crs == crs