Skip to content

Commit 9d9f526

Browse files
authored
Merge pull request #24 from mapcode-foundation/arabic
Added Arabic support
2 parents fc57bf0 + caa9eca commit 9d9f526

20 files changed

+415
-87
lines changed

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,95 @@ limitations under the License.
3939
Original C library created by Pieter Geelen. Work on Java version
4040
of the mapcode library by Rijn Buve. Initial port by Matthew Lowden.
4141

42+
# Using the Mapcode Library
43+
44+
For a description of what mapcodes are, please visit http://mapcode.com.
45+
46+
This library exposes a `Mapcode` class, which represents a full mapcode,
47+
consisting of a code and a territory. You can extract the code and territory
48+
with methods like `getCode()` and `getTerritory()`. Codes may be retrieved
49+
in a multitude of scripts (or 'alphabets', as they are called in this
50+
library), such as Roman, Greek, Hindi and Arabic.
51+
52+
`Mapcode` objects are returned by the `MapcodeCodec` with encodes coordinates
53+
to mapcodes and decodes mapcodes to coordinates (codec means coder/decoder).
54+
55+
# Examples
56+
57+
Here's an example to `decode()` mapcode within a given territory context.
58+
Note that the territory context (`NLD` is this case) is only used to
59+
disambiguate the code if needed. If the provided code is an international
60+
code, the context is simply ignored, because no disambiguation is needed.
61+
62+
final Territory territory = Territory.fromString("NLD");
63+
final String mapcode = "49.4V";
64+
try {
65+
final Point p = MapcodeCodec.decode(mapcode, territory);
66+
// p now points at the (lat, lon) for the mapcode.
67+
} catch (final UnknownMapcodeException ignored) {
68+
// The mapcode was not valid.
69+
}
70+
71+
And to `encode()` a latitude and longitude into a mapcode you would write:
72+
73+
final List<Mapcode> results = MapcodeCodec.encode(lat, lon);
74+
75+
As you see, the returned result is actually a list of mapcodes. That's
76+
because many locations have multiple mapcodes. The last code in the list
77+
is always the longest code, the international code. This means this method
78+
always returns a result (as for every location on Earth, there is at least
79+
one mapcode).
80+
81+
So, the last code is the international code, but notice that the first code
82+
is not always the shortest, nor does it need to have the "correct"
83+
territory associated to it.
84+
85+
If you want to get the shortest code for a coordinate, you should should
86+
specify a territory and use `encodeToShortest` like this:
87+
88+
final Territory territory = Territory.NLD;
89+
final Mapcode mapcode = encodeToShortest(lat, lon, territory);
90+
91+
This makes sure you get the shortest code for a coordinate which has
92+
territory `NLD` (or whatever you choose the territory to be).
93+
94+
This method may fail, as the specified coordinate may not lie in the
95+
specified territory. In that case a `UnknownMapcodeException` is thrown.
96+
97+
The `Mapcode` class offers some additional methods to, for example, get
98+
high-resolution mapcode strings.
99+
100+
// Retrieve an ultra-high precision mapcode.
101+
final String highRez = mapcode.getCode(8);
102+
103+
Or to get full mapcode strings (Unicode), in different scripts (or alphabets):
104+
105+
// Get a Roman version of the mapcode.
106+
final String roman = mapcode.getCodeWithTerritory(Alphabet.ROMAN);
107+
108+
// Get an Arabic version of the mapcode.
109+
final String arabic = mapcode.getCodeWithTerritory(Alphabet.ARABIC);
110+
111+
Note that mapcodes from different scripts can be passed as strings back
112+
to the decoder and they will 'just work'.
113+
114+
There's also a `Territory` class, which allows you get territory codes,
115+
given their ISO 3-character code, or their full names (or even some
116+
supported aliases).
117+
118+
final Territory territory = Territory.fromString("France");
119+
// territory is now Territory.FRA.
120+
121+
Finally, there is a `Point` utility class, which represents coordinates
122+
with a latitude and longitude. The class offers some simple utility methods
123+
like `distanceInMeters` to calculate the distance between two points. Note
124+
that this method is only accurate for pretty short distances, up to, say
125+
a couple of hundred kilometers.
126+
127+
The `Point` class is usually easier to use than individual latitude and
128+
longitude paramters and class makes sure it always wraps latitudes to
129+
a range of `[-90, 90]` and longitude to `[-180, 180>`.
130+
42131
# Coding Formatting Style
43132

44133
The code formatting style used is the default code formatting style from IntelliJ IDEA (version 14).

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<artifactId>mapcode</artifactId>
99

1010
<packaging>jar</packaging>
11-
<version>2.2.5-SNAPSHOT</version>
11+
<version>2.3.0</version>
1212

1313
<name>Mapcode Java Library</name>
1414
<description>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public enum Alphabet {
4545
ARMENIAN(10),
4646
BENGALI(11),
4747
GURMUKHI(12),
48-
TIBETAN(13);
48+
TIBETAN(13),
49+
ARABIC(14);
4950

5051
/**
5152
* The numeric code is synonym for the alphanumeric code. Used in the decoder.

src/main/java/com/mapcode/DataModel.java

100755100644
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,9 @@ public static DataModel getInstance() {
170170
i += 4;
171171
}
172172
} finally {
173-
//noinspection ThrowFromFinallyBlock
174173
outputStream.close();
175174
}
176175
} finally {
177-
//noinspection ThrowFromFinallyBlock
178176
inputStream.close();
179177
}
180178
} catch (final IOException e) {

0 commit comments

Comments
 (0)