forked from mapcode-foundation/mapcode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapcodeCodec.java
More file actions
302 lines (274 loc) · 13.8 KB
/
Copy pathMapcodeCodec.java
File metadata and controls
302 lines (274 loc) · 13.8 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mapcode;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.regex.Matcher;
import static com.mapcode.CheckArgs.checkDefined;
import static com.mapcode.CheckArgs.checkNonnull;
import static com.mapcode.Mapcode.getPrecisionFormat;
/**
* ----------------------------------------------------------------------------------------------
* Mapcode public interface.
* ----------------------------------------------------------------------------------------------
*
* This class is the external Java interface for encoding and decoding mapcodes.
*/
public final class MapcodeCodec {
private MapcodeCodec() {
// Prevent instantiation.
}
/**
* ------------------------------------------------------------------------------------------
* Encoding latitude, longitude to mapcodes.
* ------------------------------------------------------------------------------------------
*/
/**
* Encode a lat/lon pair to a mapcode with territory information. This produces a non-empty list of mapcode,
* with at the very least 1 mapcodes for the lat/lon, which is the "International" mapcode.
*
* The returned result list will always contain at least 1 mapcode, because every lat/lon pair can be encoded.
*
* The list is ordered in such a way that the last result is the international code. However, you cannot assume
* that the first result is the shortest mapcode. If you want to use the shortest mapcode, use
* {@link #encodeToShortest(double, double, Territory)}.
*
* The international code can be obtained from the list by using: "results.get(results.size() - 1)", or
* you can use {@link #encodeToInternational(double, double)}, which is faster.
*
* @param latDeg Latitude, accepted range: -90..90.
* @param lonDeg Longitude, accepted range: -180..180.
* @return Non-empty, ordered list of mapcode information records, see {@link Mapcode}.
* @throws IllegalArgumentException Thrown if latitude or longitude are out of range.
*/
@Nonnull
public static List<Mapcode> encode(final double latDeg, final double lonDeg)
throws IllegalArgumentException {
return encode(latDeg, lonDeg, null);
}
@Nonnull
public static List<Mapcode> encode(@Nonnull final Point point)
throws IllegalArgumentException {
checkDefined("point", point);
return encode(point.getLatDeg(), point.getLonDeg());
}
/**
* Encode a lat/lon pair to a mapcode with territory information, for a specific territory. This produces a
* potentially empty list of mapcodes (empty if the lat/lon does not fall within the territory for mapcodes).
*
* The returned result list will always contain at least 1 mapcode, because every lat/lon pair can be encoded.
*
* The list is ordered in such a way that the last result is the international code. However, you cannot assume
* that the first result is the shortest mapcode. If you want to use the shortest mapcode, use
* {@link #encodeToShortest(double, double, Territory)}.
*
* @param latDeg Latitude, accepted range: -90..90 (limited to this range if outside).
* @param lonDeg Longitude, accepted range: -180..180 (wrapped to this range if outside).
* @param restrictToTerritory Try to encode only within this territory, see {@link Territory}. May be null.
* @return List of mapcode information records, see {@link Mapcode}. This list is empty if no
* Mapcode can be generated for this territory matching the lat/lon.
* @throws IllegalArgumentException Thrown if latitude or longitude are out of range.
*/
@Nonnull
public static List<Mapcode> encode(final double latDeg, final double lonDeg,
@Nullable final Territory restrictToTerritory)
throws IllegalArgumentException {
// Call Mapcode encoder.
final List<Mapcode> results = Encoder.encode(latDeg, lonDeg, restrictToTerritory, /* Stop with one result: */ false);
assert results != null;
return results;
}
@Nonnull
public static List<Mapcode> encode(@Nonnull final Point point,
@Nullable final Territory restrictToTerritory)
throws IllegalArgumentException {
checkNonnull("point", point);
return encode(point.getLatDeg(), point.getLonDeg(), restrictToTerritory);
}
/**
* Encode a lat/lon pair to its shortest mapcode with territory information.
*
* @param latDeg Latitude, accepted range: -90..90.
* @param lonDeg Longitude, accepted range: -180..180.
* @param restrictToTerritory Try to encode only within this territory, see {@link Territory}. Cannot be null.
* @return Shortest mapcode, see {@link Mapcode}.
* @throws IllegalArgumentException Thrown if latitude or longitude are out of range.
* @throws UnknownMapcodeException Thrown if no mapcode was found for the lat/lon matching the territory.
*/
@Nonnull
public static Mapcode encodeToShortest(final double latDeg, final double lonDeg,
@Nonnull final Territory restrictToTerritory)
throws IllegalArgumentException, UnknownMapcodeException {
checkNonnull("restrictToTerritory", restrictToTerritory);
// Call mapcode encoder.
@Nonnull final List<Mapcode> results =
Encoder.encode(latDeg, lonDeg, restrictToTerritory, /* Stop with one result: */ true);
assert results != null;
assert results.size() <= 1;
if (results.isEmpty()) {
throw new UnknownMapcodeException("No Mapcode for lat=" + latDeg + ", lon=" + lonDeg +
", territory=" + restrictToTerritory);
}
return results.get(0);
}
@Nonnull
public static Mapcode encodeToShortest(@Nonnull final Point point,
@Nonnull final Territory restrictToTerritory)
throws IllegalArgumentException, UnknownMapcodeException {
checkDefined("point", point);
return encodeToShortest(point.getLatDeg(), point.getLonDeg(), restrictToTerritory);
}
/**
* Encode a lat/lon pair to its unambiguous, international mapcode.
*
* @param latDeg Latitude, accepted range: -90..90.
* @param lonDeg Longitude, accepted range: -180..180.
* @return International unambiguous mapcode (always exists), see {@link Mapcode}.
* @throws IllegalArgumentException Thrown if latitude or longitude are out of range.
*/
@Nonnull
public static Mapcode encodeToInternational(final double latDeg, final double lonDeg)
throws IllegalArgumentException {
// Call mapcode encoder.
@Nonnull final List<Mapcode> results = encode(latDeg, lonDeg, Territory.AAA);
assert results != null;
assert results.size() >= 1;
return results.get(results.size() - 1);
}
@Nonnull
public static Mapcode encodeToInternational(@Nonnull final Point point)
throws IllegalArgumentException {
checkDefined("point", point);
return encodeToInternational(point.getLatDeg(), point.getLonDeg());
}
/**
* ------------------------------------------------------------------------------------------
* Decoding mapcodes back to latitude, longitude.
* ------------------------------------------------------------------------------------------
*/
/**
* Decode a mapcode to a Point. The decoding process may fail for local mapcodes,
* because no territory context is supplied (world-wide).
*
* The accepted format is:
* {mapcode}
* {territory-code} {mapcode}
*
* @param mapcode Mapcode.
* @return Point corresponding to mapcode.
* @throws UnknownMapcodeException Thrown if the mapcode has the correct syntax,
* but cannot be decoded into a point.
* @throws UnknownPrecisionFormatException Thrown if the precision format is incorrect.
* @throws IllegalArgumentException Thrown if arguments are null, or if the syntax of the mapcode is incorrect.
*/
@SuppressWarnings("DuplicateThrows")
@Nonnull
public static Point decode(@Nonnull final String mapcode)
throws UnknownMapcodeException, IllegalArgumentException, UnknownPrecisionFormatException {
return decode(mapcode, Territory.AAA);
}
/**
* Decode a mapcode to a Point. A reference territory is supplied for disambiguation (only used if applicable).
*
* The accepted format is:
* {mapcode}
* {territory-code} {mapcode}
*
* Note that if a territory-code is supplied in the string, it takes preferences over the parameter.
*
* @param mapcode Mapcode.
* @param defaultTerritoryContext Default territory context for disambiguation purposes. May be null.
* @return Point corresponding to mapcode. Latitude range: -90..90, longitude range: -180..180.
* @throws UnknownMapcodeException Thrown if the mapcode has the right syntax, but cannot be decoded into a point.
* @throws UnknownPrecisionFormatException Thrown if the precision format is incorrect.
* @throws IllegalArgumentException Thrown if arguments are null, or if the syntax of the mapcode is incorrect.
*/
@SuppressWarnings("DuplicateThrows")
@Nonnull
public static Point decode(
@Nonnull final String mapcode,
@Nullable final Territory defaultTerritoryContext)
throws UnknownMapcodeException, IllegalArgumentException, UnknownPrecisionFormatException {
checkNonnull("mapcode", mapcode);
// Clean up mapcode.
String mapcodeClean = Mapcode.convertStringToPlainAscii(mapcode.trim()).toUpperCase();
// Determine territory from mapcode.
final Territory territory;
final Matcher matcherTerritory = Mapcode.PATTERN_TERRITORY.matcher(mapcodeClean);
if (!matcherTerritory.find()) {
// No territory code was supplied in the string, use specified territory context parameter.
territory = (defaultTerritoryContext != null) ? defaultTerritoryContext : Territory.AAA;
} else {
// Use the territory code from the string.
final String territoryName = mapcodeClean.substring(matcherTerritory.start(), matcherTerritory.end()).trim();
try {
territory = Territory.fromString(territoryName);
} catch (final UnknownTerritoryException ignored) {
throw new UnknownMapcodeException("Wrong territory code: " + territoryName);
}
// Cut off the territory part.
mapcodeClean = mapcodeClean.substring(matcherTerritory.end()).trim();
}
// Throws an exception if the format is incorrect.
getPrecisionFormat(mapcodeClean);
@Nonnull final Point point = Decoder.decode(mapcodeClean, territory);
assert point != null;
// Points can only be undefined within the mapcode implementation. Throw an exception here if undefined.
if (!point.isDefined()) {
throw new UnknownMapcodeException("Unknown Mapcode: " + mapcodeClean +
", territoryContext=" + defaultTerritoryContext);
}
return point;
}
/**
* Is coordinate near multiple territory borders?
*
* @param point Latitude/Longitude in degrees.
* @param territory Territory.
* @return true Iff the coordinate is near more than one territory border (and thus encode(decode(M)) may not produce M).
*/
public static boolean isNearMultipleBorders(@Nonnull final Point point, @Nonnull final Territory territory) {
checkDefined("point", point);
if (territory != Territory.AAA) {
final int ccode = territory.getNumber();
if (territory.getParentTerritory() != null) {
// There is a parent! check its borders as well...
if (isNearMultipleBorders(point, territory.getParentTerritory())) {
return true;
}
}
int nrFound = 0;
final int fromTerritoryRecord = DataAccess.getDataFirstRecord(ccode);
final int uptoTerritoryRecord = DataAccess.getDataLastRecord(ccode);
for (int territoryRecord = uptoTerritoryRecord; territoryRecord >= fromTerritoryRecord; territoryRecord--) {
if (!Data.isRestricted(territoryRecord)) {
final Boundary boundary = Boundary.createFromTerritoryRecord(territoryRecord);
final int xdiv8 = Common.xDivider(boundary.getLatMicroDegMin(), boundary.getLatMicroDegMax()) / 4;
if (boundary.extendBoundary(60, xdiv8).containsPoint(point)) {
if (!boundary.extendBoundary(-60, -xdiv8).containsPoint(point)) {
nrFound++;
if (nrFound > 1) {
return true;
}
}
}
}
}
}
return false;
}
}