1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.projection;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 |
|
---|
6 | import java.util.Comparator;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Map;
|
---|
10 | import java.util.Map.Entry;
|
---|
11 | import java.util.stream.Collectors;
|
---|
12 |
|
---|
13 | import org.junit.Test;
|
---|
14 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
15 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
16 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Tests for {@link ShiftedProjecting}
|
---|
20 | * @author Michael Zangl
|
---|
21 | */
|
---|
22 | public class ShiftedProjectionTest {
|
---|
23 | private static final class ProjectingBase implements Projecting {
|
---|
24 | @Override
|
---|
25 | public EastNorth latlon2eastNorth(LatLon ll) {
|
---|
26 | return new EastNorth(ll.lat() * 2, ll.lon() * 3);
|
---|
27 | }
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | public Map<ProjectionBounds, Projecting> getProjectingsForArea(ProjectionBounds area) {
|
---|
31 | HashMap<ProjectionBounds, Projecting> map = new HashMap<>();
|
---|
32 | // split at east = 0
|
---|
33 | if (area.minEast < 0) {
|
---|
34 | map.put(new ProjectionBounds(area.minEast, area.minNorth, Math.min(area.maxEast, 0), area.maxNorth), this);
|
---|
35 | }
|
---|
36 | if (area.maxEast > 0) {
|
---|
37 | map.put(new ProjectionBounds(Math.max(area.minEast, 0), area.minNorth, area.maxEast, area.maxNorth), this);
|
---|
38 | }
|
---|
39 |
|
---|
40 | return map;
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public Projection getBaseProjection() {
|
---|
45 | throw new AssertionError();
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public LatLon eastNorth2latlonClamped(EastNorth en) {
|
---|
50 | return new LatLon(en.east() / 2, en.north() / 3);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Test {@link ShiftedProjecting#latlon2eastNorth(LatLon)}
|
---|
56 | */
|
---|
57 | @Test
|
---|
58 | public void testLatlon2eastNorth() {
|
---|
59 | Projecting base = new ProjectingBase();
|
---|
60 |
|
---|
61 | ShiftedProjecting unshifted = new ShiftedProjecting(base, new EastNorth(0, 0));
|
---|
62 | EastNorth unshift_00 = unshifted.latlon2eastNorth(new LatLon(0, 0));
|
---|
63 | assertEquals(0, unshift_00.east(), 1e-10);
|
---|
64 | assertEquals(0, unshift_00.north(), 1e-10);
|
---|
65 | EastNorth unshift_12 = unshifted.latlon2eastNorth(new LatLon(1, 2));
|
---|
66 | assertEquals(2, unshift_12.east(), 1e-10);
|
---|
67 | assertEquals(6, unshift_12.north(), 1e-10);
|
---|
68 |
|
---|
69 | ShiftedProjecting shifted = new ShiftedProjecting(base, new EastNorth(5, 7));
|
---|
70 | EastNorth shift_00 = shifted.latlon2eastNorth(new LatLon(0, 0));
|
---|
71 | assertEquals(5, shift_00.east(), 1e-10);
|
---|
72 | assertEquals(7, shift_00.north(), 1e-10);
|
---|
73 | EastNorth shift_12 = shifted.latlon2eastNorth(new LatLon(1, 2));
|
---|
74 | assertEquals(2 + 5, shift_12.east(), 1e-10);
|
---|
75 | assertEquals(6 + 7, shift_12.north(), 1e-10);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Test {@link ShiftedProjecting#eastNorth2latlonClamped(EastNorth)}
|
---|
80 | */
|
---|
81 | @Test
|
---|
82 | public void testEastNorth2latlonClamped() {
|
---|
83 | Projecting base = new ProjectingBase();
|
---|
84 |
|
---|
85 | ShiftedProjecting unshifted = new ShiftedProjecting(base, new EastNorth(0, 0));
|
---|
86 | LatLon unshift_00 = unshifted.eastNorth2latlonClamped(new EastNorth(0, 0));
|
---|
87 | assertEquals(0, unshift_00.lat(), 1e-10);
|
---|
88 | assertEquals(0, unshift_00.lon(), 1e-10);
|
---|
89 | LatLon unshift_12 = unshifted.eastNorth2latlonClamped(new EastNorth(2, 6));
|
---|
90 | assertEquals(1, unshift_12.lat(), 1e-10);
|
---|
91 | assertEquals(2, unshift_12.lon(), 1e-10);
|
---|
92 |
|
---|
93 | ShiftedProjecting shifted = new ShiftedProjecting(base, new EastNorth(5, 7));
|
---|
94 | LatLon shift_00 = shifted.eastNorth2latlonClamped(new EastNorth(5, 7));
|
---|
95 | assertEquals(0, shift_00.lat(), 1e-10);
|
---|
96 | assertEquals(0, shift_00.lon(), 1e-10);
|
---|
97 | LatLon shift_12 = shifted.eastNorth2latlonClamped(new EastNorth(2 + 5, 6 + 7));
|
---|
98 | assertEquals(1, shift_12.lat(), 1e-10);
|
---|
99 | assertEquals(2, shift_12.lon(), 1e-10);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Test {@link ShiftedProjecting#getProjectingsForArea(ProjectionBounds)}, single area case
|
---|
104 | */
|
---|
105 | @Test
|
---|
106 | public void testGetProjectingsForArea() {
|
---|
107 | Projecting base = new ProjectingBase();
|
---|
108 | ShiftedProjecting shifted = new ShiftedProjecting(base, new EastNorth(5, 7));
|
---|
109 |
|
---|
110 | ProjectionBounds area = new ProjectionBounds(10, 0, 20, 20);
|
---|
111 |
|
---|
112 | Map<ProjectionBounds, Projecting> areas = shifted.getProjectingsForArea(area);
|
---|
113 | assertEquals(1, areas.size());
|
---|
114 | ProjectionBounds pb = areas.keySet().iterator().next();
|
---|
115 | assertEquals(area.minEast, pb.minEast, 1e-7);
|
---|
116 | assertEquals(area.maxEast, pb.maxEast, 1e-7);
|
---|
117 | assertEquals(area.minNorth, pb.minNorth, 1e-7);
|
---|
118 | assertEquals(area.maxNorth, pb.maxNorth, 1e-7);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Test {@link ShiftedProjecting#getProjectingsForArea(ProjectionBounds)}, multiple area case
|
---|
123 | */
|
---|
124 | @Test
|
---|
125 | public void testGetProjectingsForAreaMultiple() {
|
---|
126 | Projecting base = new ProjectingBase();
|
---|
127 | ShiftedProjecting shifted = new ShiftedProjecting(base, new EastNorth(5, 7));
|
---|
128 |
|
---|
129 | ProjectionBounds area = new ProjectionBounds(-10, 0, 20, 20);
|
---|
130 |
|
---|
131 | // breach is at:
|
---|
132 | EastNorth breachAt = shifted.latlon2eastNorth(base.eastNorth2latlonClamped(new EastNorth(0, 0)));
|
---|
133 | assertEquals(5, breachAt.east(), 1e-7);
|
---|
134 |
|
---|
135 | Map<ProjectionBounds, Projecting> areas = shifted.getProjectingsForArea(area);
|
---|
136 | assertEquals(2, areas.size());
|
---|
137 | List<Entry<ProjectionBounds, Projecting>> entries = areas.entrySet().stream()
|
---|
138 | .sorted(Comparator.comparingDouble(b -> b.getKey().minEast)).collect(Collectors.toList());
|
---|
139 | assertEquals(area.minEast, entries.get(0).getKey().minEast, 1e-7);
|
---|
140 | assertEquals(5, entries.get(0).getKey().maxEast, 1e-7);
|
---|
141 | assertEquals(area.minNorth, entries.get(0).getKey().minNorth, 1e-7);
|
---|
142 | assertEquals(area.maxNorth, entries.get(0).getKey().maxNorth, 1e-7);
|
---|
143 | assertEquals(5, entries.get(1).getKey().minEast, 1e-7);
|
---|
144 | assertEquals(area.maxEast, entries.get(1).getKey().maxEast, 1e-7);
|
---|
145 | assertEquals(area.minNorth, entries.get(1).getKey().minNorth, 1e-7);
|
---|
146 | assertEquals(area.maxNorth, entries.get(1).getKey().maxNorth, 1e-7);
|
---|
147 | }
|
---|
148 | }
|
---|