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