/usr/lib/python2.7/dist-packages/cartopy/tests/test_line_string.py is in python-cartopy 0.14.2+dfsg1-2build3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | # (C) British Crown Copyright 2011 - 2016, Met Office
#
# This file is part of cartopy.
#
# cartopy is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cartopy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with cartopy. If not, see <https://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function)
import itertools
import time
import unittest
import numpy as np
import shapely.geometry as sgeom
import cartopy.crs as ccrs
class TestLineString(unittest.TestCase):
def test_out_of_bounds(self):
# Check that a line that is completely out of the map boundary produces
# a valid LineString
projection = ccrs.TransverseMercator(central_longitude=0)
# For both start & end, define a point that results in well-defined
# projection coordinates and one that results in NaN.
start_points = [(86, 0), (130, 0)]
end_points = [(88, 0), (120, 0)]
# Try all four combinations of valid/NaN vs valid/NaN.
for start, end in itertools.product(start_points, end_points):
line_string = sgeom.LineString([start, end])
multi_line_string = projection.project_geometry(line_string)
if start[0] == 130 and end[0] == 120:
expected = 0
else:
expected = 1
self.assertEqual(len(multi_line_string), expected,
'Unexpected line when working from {} '
'to {}'.format(start, end))
def test_simple_fragment_count(self):
projection = ccrs.PlateCarree()
tests = [
([(150, 0), (-150, 0)], 2),
([(10, 0), (90, 0), (180, 0), (-90, 0), (-10, 0)], 2),
([(-10, 0), (10, 0)], 1),
([(-45, 0), (45, 30)], 1),
]
for coords, pieces in tests:
line_string = sgeom.LineString(coords)
multi_line_string = projection.project_geometry(line_string)
# from cartopy.tests.mpl import show
# show(projection, multi_line_string)
self.assertEqual(len(multi_line_string), pieces)
def test_split(self):
projection = ccrs.Robinson(170.5)
line_string = sgeom.LineString([(-10, 30), (10, 60)])
multi_line_string = projection.project_geometry(line_string)
# from cartopy.tests.mpl import show
# show(projection, multi_line_string)
self.assertEqual(len(multi_line_string), 2)
def test_out_of_domain_efficiency(self):
# Check we're efficiently dealing with lines that project
# outside the map domain.
# Because the south pole projects to an *enormous* circle
# (radius ~ 1e23) this will take a *long* time to project if the
# within-domain exactness criteria are used.
line_string = sgeom.LineString([(0, -90), (2, -90)])
tgt_proj = ccrs.NorthPolarStereo()
src_proj = ccrs.PlateCarree()
cutoff_time = time.time() + 1
tgt_proj.project_geometry(line_string, src_proj)
self.assertLess(time.time(), cutoff_time, 'Projection took too long')
class FakeProjection(ccrs.PlateCarree):
def __init__(self, left_offset=0, right_offset=0):
self.left_offset = left_offset
self.right_offset = right_offset
self._half_width = 180
self._half_height = 90
ccrs.PlateCarree.__init__(self)
@property
def boundary(self):
# XXX Should this be a LinearRing?
w, h = self._half_width, self._half_height
return sgeom.LineString([(-w + self.left_offset, -h),
(-w + self.left_offset, h),
(w - self.right_offset, h),
(w - self.right_offset, -h),
(-w + self.left_offset, -h)])
class TestBisect(unittest.TestCase):
# A bunch of tests to check the bisection algorithm is robust for a
# variety of simple and/or pathological cases.
def test_repeated_point(self):
projection = FakeProjection()
line_string = sgeom.LineString([(10, 0), (10, 0)])
multi_line_string = projection.project_geometry(line_string)
self.assertEqual(len(multi_line_string), 1)
self.assertEqual(len(multi_line_string[0].coords), 2)
def test_interior_repeated_point(self):
projection = FakeProjection()
line_string = sgeom.LineString([(0, 0), (10, 0), (10, 0), (20, 0)])
multi_line_string = projection.project_geometry(line_string)
self.assertEqual(len(multi_line_string), 1)
self.assertEqual(len(multi_line_string[0].coords), 4)
def test_circular_repeated_point(self):
projection = FakeProjection()
line_string = sgeom.LineString([(0, 0), (360, 0)])
multi_line_string = projection.project_geometry(line_string)
self.assertEqual(len(multi_line_string), 1)
self.assertEqual(len(multi_line_string[0].coords), 2)
def test_short(self):
projection = FakeProjection()
line_string = sgeom.LineString([(0, 0), (1e-12, 0)])
multi_line_string = projection.project_geometry(line_string)
self.assertEqual(len(multi_line_string), 1)
self.assertEqual(len(multi_line_string[0].coords), 2)
def test_empty(self):
projection = FakeProjection(right_offset=10)
line_string = sgeom.LineString([(175, 0), (175, 10)])
multi_line_string = projection.project_geometry(line_string)
self.assertEqual(len(multi_line_string), 0)
def test_simple_run_in(self):
projection = FakeProjection(right_offset=10)
line_string = sgeom.LineString([(160, 0), (175, 0)])
multi_line_string = projection.project_geometry(line_string)
self.assertEqual(len(multi_line_string), 1)
self.assertEqual(len(multi_line_string[0].coords), 2)
def test_simple_wrap(self):
projection = FakeProjection()
line_string = sgeom.LineString([(160, 0), (-160, 0)])
multi_line_string = projection.project_geometry(line_string)
self.assertEqual(len(multi_line_string), 2)
self.assertEqual(len(multi_line_string[0].coords), 2)
self.assertEqual(len(multi_line_string[1].coords), 2)
def test_simple_run_out(self):
projection = FakeProjection(left_offset=10)
line_string = sgeom.LineString([(-175, 0), (-160, 0)])
multi_line_string = projection.project_geometry(line_string)
self.assertEqual(len(multi_line_string), 1)
self.assertEqual(len(multi_line_string[0].coords), 2)
def test_point_on_boundary(self):
projection = FakeProjection()
line_string = sgeom.LineString([(180, 0), (-160, 0)])
multi_line_string = projection.project_geometry(line_string)
self.assertEqual(len(multi_line_string), 1)
self.assertEqual(len(multi_line_string[0].coords), 2)
# Add a small offset to the left-hand boundary to make things
# even more pathological.
projection = FakeProjection(left_offset=5)
line_string = sgeom.LineString([(180, 0), (-160, 0)])
multi_line_string = projection.project_geometry(line_string)
self.assertEqual(len(multi_line_string), 1)
self.assertEqual(len(multi_line_string[0].coords), 2)
def test_nan_start(self):
projection = ccrs.TransverseMercator(central_longitude=-90)
line_string = sgeom.LineString([(10, 50), (-10, 30)])
multi_line_string = projection.project_geometry(line_string)
self.assertEqual(len(multi_line_string), 1)
for line_string in multi_line_string:
for coord in line_string.coords:
self.assertFalse(any(np.isnan(coord)),
'Unexpected NaN in projected coords.')
def test_nan_end(self):
projection = ccrs.TransverseMercator(central_longitude=-90)
line_string = sgeom.LineString([(-10, 30), (10, 50)])
multi_line_string = projection.project_geometry(line_string)
# from cartopy.tests.mpl import show
# show(projection, multi_line_string)
self.assertEqual(len(multi_line_string), 1)
for line_string in multi_line_string:
for coord in line_string.coords:
self.assertFalse(any(np.isnan(coord)),
'Unexpected NaN in projected coords.')
class TestMisc(unittest.TestCase):
def test_misc(self):
projection = ccrs.TransverseMercator(central_longitude=-90)
line_string = sgeom.LineString([(10, 50), (-10, 30)])
multi_line_string = projection.project_geometry(line_string)
# from cartopy.tests.mpl import show
# show(projection, multi_line_string)
for line_string in multi_line_string:
for coord in line_string.coords:
self.assertFalse(any(np.isnan(coord)),
'Unexpected NaN in projected coords.')
def test_something(self):
projection = ccrs.RotatedPole(pole_longitude=177.5,
pole_latitude=37.5)
line_string = sgeom.LineString([(0, 0), (1e-14, 0)])
multi_line_string = projection.project_geometry(line_string)
self.assertEqual(len(multi_line_string), 1)
self.assertEqual(len(multi_line_string[0].coords), 2)
def test_global_boundary(self):
linear_ring = sgeom.LineString([(-180, -180), (-180, 180),
(180, 180), (180, -180)])
pc = ccrs.PlateCarree()
merc = ccrs.Mercator()
multi_line_string = pc.project_geometry(linear_ring, merc)
assert len(multi_line_string) > 0
# check the identity transform
multi_line_string = merc.project_geometry(linear_ring, merc)
assert len(multi_line_string) > 0
class TestSymmetry(unittest.TestCase):
@unittest.expectedFailure
def test_curve(self):
# Obtain a simple, curved path.
projection = ccrs.PlateCarree()
coords = [(-0.08, 51.53), (132.00, 43.17)] # London to Vladivostock
line_string = sgeom.LineString(coords)
multi_line_string = projection.project_geometry(line_string)
# Compute the reverse path.
line_string = sgeom.LineString(coords[::-1])
multi_line_string2 = projection.project_geometry(line_string)
# Make sure that they generated the same points.
# (Although obviously they will be in the opposite order!)
self.assertEqual(len(multi_line_string), 1)
self.assertEqual(len(multi_line_string2), 1)
coords = multi_line_string[0].coords
coords2 = multi_line_string2[0].coords
np.testing.assert_allclose(coords, coords2[::-1],
err_msg='Asymmetric curve generation')
if __name__ == '__main__':
unittest.main()
|