Skip to content

Commit 21c17b9

Browse files
committed
Removed microdegrees from Point, added documentation
1 parent a0dd207 commit 21c17b9

File tree

15 files changed

+193
-200
lines changed

15 files changed

+193
-200
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ and to decode mapcodes back to latitude/longitude pairs.
1313

1414
**An example of how to use this library can be found at: https://github.com/mapcode-foundation/mapcode-java-example**
1515

16+
If you wish to use mapcodes in your own application landscape, consider using running an instance of the
17+
Mapcode REST API, which can be found on: **https://github.com/mapcode-foundation/mapcode-rest-service**
18+
1619
# License
1720

1821
Licensed under the Apache License, Version 2.0 (the "License");

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<name>Matthew Lowden</name>
4646
<organization>Mapcode</organization>
4747
<roles>
48-
<role>Developer</role>
48+
<role>Original Port</role>
4949
</roles>
5050
</developer>
5151
</developers>

src/main/java/com/mapcode/Alphabet.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
package com.mapcode;
1818

1919
import javax.annotation.Nonnull;
20-
import java.util.HashSet;
21-
import java.util.Set;
2220

2321
import static com.mapcode.CheckArgs.checkNonnull;
2422

2523
/**
2624
* This enum defines all alphabets supported for mapcodes. Mapcodes can be safely converted between
27-
* alphabets and fed to the mapcode decoder.
25+
* alphabets and fed to the mapcode decoder in the regular ASCII Roman alphabet or any other.
2826
*/
2927
public enum Alphabet {
3028
ROMAN(0),
@@ -42,6 +40,10 @@ public enum Alphabet {
4240
GURMUKHI(12),
4341
TIBETAN(13);
4442

43+
/**
44+
* The numeric code is synonym for the alphanumeric code. It can be used in the decoder
45+
* to define a territory as well.
46+
*/
4547
private final int code;
4648

4749
private Alphabet(final int code) {
@@ -52,12 +54,17 @@ public int getCode() {
5254
return code;
5355
}
5456

57+
/**
58+
* Get an alphabet from a numeric code.
59+
*
60+
* @param code Numeric code.
61+
* @return Alphabet.
62+
* @throws UnknownAlphabetException Thrown if code out of range.
63+
*/
5564
@Nonnull
5665
public static Alphabet fromCode(final int code) throws UnknownAlphabetException {
57-
for (final Alphabet alphabet : values()) {
58-
if (alphabet.code == code) {
59-
return alphabet;
60-
}
66+
if ((code >= 0) && (code < Alphabet.values().length)) {
67+
return Alphabet.values()[code];
6168
}
6269
throw new UnknownAlphabetException(code);
6370
}
@@ -89,20 +96,12 @@ public static Alphabet fromString(@Nonnull final String numericOrAlpha) throws U
8996
* Static checking of the static data structures.
9097
*/
9198
static {
92-
final String errorPrefix = "Initializing error: ";
93-
final Set<Integer> alphabetCodeList = new HashSet<>();
94-
99+
int i = 0;
95100
for (final Alphabet alphabet : Alphabet.values()) {
96-
final int alphabetCode = alphabet.getCode();
97-
if ((alphabetCode < 0) || (alphabetCode >= Alphabet.values().length)) {
98-
throw new ExceptionInInitializerError(errorPrefix + "alphabet code out of range: " + alphabetCode);
99-
100-
}
101-
if (alphabetCodeList.contains(alphabetCode)) {
102-
throw new ExceptionInInitializerError(errorPrefix + "non-unique alphabet code: " + alphabetCode);
101+
if (Alphabet.values()[i].code != i) {
102+
throw new ExceptionInInitializerError("Incorrect alphabet code: " + alphabet + ".code should be " + i);
103103
}
104-
alphabetCodeList.add(alphabet.getCode());
104+
++i;
105105
}
106-
assert alphabetCodeList.size() == Alphabet.values().length;
107106
}
108107
}

src/main/java/com/mapcode/CheckArgs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import static com.mapcode.Mapcode.isValidMapcodeFormat;
2323

2424
/**
25-
* Package private helper methods.
25+
* Package private helper methods to check arguments for validity.
2626
*/
2727
class CheckArgs {
2828

src/main/java/com/mapcode/Decoder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ static Point decode(@Nonnull final String argMapcode,
165165
LOG.trace("decode: result=({}, {})",
166166
result.isDefined() ? result.getLatDeg() : Double.NaN,
167167
result.isDefined() ? result.getLonDeg() : Double.NaN);
168-
result = Point.restrictLatLon(result);
169-
return result;
168+
return result.wrap();
170169
}
171170

172171
// ----------------------------------------------------------------------

src/main/java/com/mapcode/Encoder.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,10 @@ private static List<Mapcode> encode(final double argLatDeg, final double argLonD
6464
argLatDeg, argLonDeg, (territory == null) ? null : territory.name(), isRecursive, limitToOneResult,
6565
allowWorld);
6666

67-
double latDeg = argLatDeg;
68-
double lonDeg = argLonDeg;
67+
double latDeg = Point.mapToLat(argLatDeg);
68+
double lonDeg = Point.mapToLon(argLonDeg);
6969
Territory stateOverride = argStateOverride;
7070

71-
if (latDeg > 90) {
72-
latDeg -= 180;
73-
} else if (latDeg < -90) {
74-
latDeg += 180;
75-
}
76-
if (lonDeg > 179.999999) {
77-
lonDeg -= 360;
78-
} else if (lonDeg < -180) {
79-
lonDeg += 180;
80-
}
81-
8271
final Point pointToEncode = Point.fromDeg(latDeg, lonDeg);
8372
final List<SubArea> areas = SubArea.getAreasForPoint(pointToEncode);
8473
final List<Mapcode> results = new ArrayList<>();

src/main/java/com/mapcode/MapcodeCodec.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.regex.Matcher;
2626

2727
import static com.mapcode.CheckArgs.checkNonnull;
28-
import static com.mapcode.CheckArgs.checkRange;
2928

3029
/**
3130
* ----------------------------------------------------------------------------------------------
@@ -86,8 +85,8 @@ public static List<Mapcode> encode(@Nonnull final Point point) throws IllegalArg
8685
* that the first result is the shortest mapcode. If you want to use the shortest mapcode, use
8786
* {@link #encodeToShortest(double, double, Territory)}.
8887
*
89-
* @param latDeg Latitude, accepted range: -90..90.
90-
* @param lonDeg Longitude, accepted range: -180..180.
88+
* @param latDeg Latitude, accepted range: -90..90 (limited to this range if outside).
89+
* @param lonDeg Longitude, accepted range: -180..180 (wrapped to this range if outside).
9190
* @param restrictToTerritory Try to encode only within this territory, see {@link Territory}. May be null.
9291
* @return List of mapcode information records, see {@link Mapcode}. This list is empty if no
9392
* Mapcode can be generated for this territory matching the lat/lon.
@@ -96,9 +95,6 @@ public static List<Mapcode> encode(@Nonnull final Point point) throws IllegalArg
9695
@Nonnull
9796
public static List<Mapcode> encode(final double latDeg, final double lonDeg,
9897
@Nullable final Territory restrictToTerritory) throws IllegalArgumentException {
99-
checkRange("latDeg", latDeg, Point.LAT_DEG_MIN, Point.LAT_DEG_MAX);
100-
checkRange("lonDeg", lonDeg, Point.LON_DEG_MIN, Point.LON_DEG_MAX);
101-
10298
// Call Mapcode encoder.
10399
final List<Mapcode> results = Encoder.encode(latDeg, lonDeg, restrictToTerritory, false, false, (restrictToTerritory == null));
104100
assert results != null;
@@ -149,9 +145,6 @@ public static Mapcode encodeToShortest(@Nonnull final Point point) throws Illega
149145
@Nonnull
150146
public static Mapcode encodeToShortest(final double latDeg, final double lonDeg,
151147
@Nullable final Territory restrictToTerritory) throws IllegalArgumentException, UnknownMapcodeException {
152-
checkRange("latDeg", latDeg, Point.LAT_DEG_MIN, Point.LAT_DEG_MAX);
153-
checkRange("lonDeg", lonDeg, Point.LON_DEG_MIN, Point.LON_DEG_MAX);
154-
155148
// Call mapcode encoder.
156149
@Nonnull final List<Mapcode> results =
157150
Encoder.encode(latDeg, lonDeg, restrictToTerritory, false, true, (restrictToTerritory == null));
@@ -181,8 +174,6 @@ public static Mapcode encodeToShortest(@Nonnull final Point point,
181174
*/
182175
@Nonnull
183176
public static Mapcode encodeToInternational(final double latDeg, final double lonDeg) throws IllegalArgumentException {
184-
checkRange("latDeg", latDeg, Point.LAT_DEG_MIN, Point.LAT_DEG_MAX);
185-
checkRange("lonDeg", lonDeg, Point.LON_DEG_MIN, Point.LON_DEG_MAX);
186177

187178
// Call mapcode encoder.
188179
@Nonnull final List<Mapcode> results = encode(latDeg, lonDeg, Territory.AAA);
@@ -280,8 +271,6 @@ public static Point decode(
280271
throw new UnknownMapcodeException("Unknown Mapcode: " + mapcodeClean +
281272
", territoryContext=" + defaultTerritoryContext);
282273
}
283-
assert (Point.LAT_DEG_MIN <= point.getLatDeg()) && (point.getLatDeg() <= Point.LAT_DEG_MAX) : point.getLatDeg();
284-
assert (Point.LON_DEG_MIN <= point.getLonDeg()) && (point.getLonDeg() <= Point.LON_DEG_MAX) : point.getLonDeg();
285274
return point;
286275
}
287276
}

0 commit comments

Comments
 (0)