Skip to content

Commit 23d6afc

Browse files
committed
Improved examples
1 parent 166cad4 commit 23d6afc

File tree

2 files changed

+71
-68
lines changed

2 files changed

+71
-68
lines changed

README.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
+----
22
| Copyright (C) 2014 Stichting Mapcode Foundation
33
| For terms of use refer to http://www.mapcode.com/downloads.html
4+
|
5+
| Original C version created by Pieter Geelen. Work on Java version
6+
| of the Mapcode library by Rijn Buve and Matthew Lowden.
47
+----
58

69
This Java project contains example code of how to use the Mapcode
710
Library.
811

912
To run the examples, simply execute:
10-
mvn exec:java -Dexec.mainClass="com.mapcode.example.Example"
1113

12-
For more information, see: http://www.mapcode.com
14+
+----
15+
mvn exec:java -Dexec.mainClass="com.mapcode.example.Example"
16+
+----
1317

14-
Work on Java version of the Mapcode library by
15-
Rijn Buve and Matthew Lowden
18+
For more information, see: http://www.mapcode.com.

src/main/java/com/mapcode/example/Example.java

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -29,99 +29,99 @@ public class Example {
2929
* @param args Arguments (unused).
3030
*/
3131
public static void main(final String[] args) throws UnknownTerritoryException {
32+
exampleDisambiguateTerritory();
33+
exampleGetTerritoryFromISOCode();
34+
exampleDecode();
35+
exampleEncode();
36+
exampleEncodeInAllTerritories();
37+
}
3238

33-
// Example 1 - Disambiguate MN in several conditions.
34-
LOG.info("Example 1 (disambuate MN, Minnesota):");
39+
private static void exampleGetTerritoryFromISOCode() throws UnknownTerritoryException {
40+
LOG.info("Example: Get territory from ISO code");
3541

36-
final String iso = "MN";
37-
LOG.info("ISO code {} without context : {}", iso, Territory.fromString(iso).toString());
38-
LOG.info("ISO code {} in USA context : {}", iso, Territory.fromString(iso, ParentTerritory.USA).toString());
39-
LOG.info("ISO code {} in IND context : {}", iso, Territory.fromString(iso, ParentTerritory.IND).toString());
40-
LOG.info("ISO code {} in RUS context : {}", iso, Territory.fromString(iso, ParentTerritory.RUS).toString());
42+
// Print the full English name of the territory.
43+
final Territory territory = Territory.fromString("NLD");
44+
LOG.info("Territory {}: {}", territory.name(), territory.getFullName());
4145
LOG.info("");
46+
}
4247

43-
// Example 2 - Get the Mapcode Territory for a territory abbreviation 'isocode',
44-
// e.g. entered by the user.
45-
46-
final String isocode = "NLD"; // Abbreviation for The Netherlands
47-
final Territory mapcodeTerritory = Territory.fromString(isocode);
48-
49-
// Example 3 - print the full English name of the territory.
50-
LOG.info("Example 2/3:");
51-
LOG.info("Territory {}: {}", isocode, mapcodeTerritory.getFullName());
48+
private static void exampleDisambiguateTerritory() throws UnknownTerritoryException {
49+
LOG.info("Example: (disambiguate code MN, which can be in USA and IND):");
50+
51+
final String isoCode = "MN";
52+
LOG.info("ISO code {} without context : {}", isoCode,
53+
Territory.fromString(isoCode).toString());
54+
LOG.info("ISO code {} in USA context : {}", isoCode,
55+
Territory.fromString(isoCode, ParentTerritory.USA).toString());
56+
LOG.info("ISO code {} in IND context : {}", isoCode,
57+
Territory.fromString(isoCode, ParentTerritory.IND).toString());
58+
LOG.info("ISO code {} in RUS context : {}", isoCode,
59+
Territory.fromString(isoCode, ParentTerritory.RUS).toString());
5260
LOG.info("");
61+
}
62+
63+
private static void exampleDecode() throws UnknownTerritoryException {
64+
LOG.info("Example: Decode");
5365

54-
// Example 4 - Decode some Mapcodes in this territory.
55-
LOG.info("Example 4 (Decode examples):");
56-
String mapcode = "49.4V";
66+
final Territory territory = Territory.fromString("NLD");
67+
final String mapcode1 = "49.4V";
5768
try {
58-
final Point p = Mapcode.decode(mapcode, mapcodeTerritory);
59-
LOG.info("Mapcode {} in territory {} represents latitude {} , longitude {}",
60-
mapcode, mapcodeTerritory, p.getLatDeg(), p.getLonDeg());
69+
final Point p = Mapcode.decode(mapcode1, territory);
70+
LOG.info("Mapcode {} in territory {} represents latitude {}, longitude {}",
71+
mapcode1, territory, p.getLatDeg(), p.getLonDeg());
6172
}
6273
catch (final UnknownMapcodeException ignored) {
63-
LOG.info("Mapcode {} in territory {} is invalid", mapcode, mapcodeTerritory);
74+
LOG.info("Mapcode {} in territory {} is invalid", mapcode1, territory);
6475
}
6576

66-
mapcode = "49.4A";
77+
final String mapcode2 = "49.4A";
6778
try {
68-
final Point p = Mapcode.decode(mapcode, mapcodeTerritory);
69-
LOG.info("Mapcode {} in territory {} represents latitude {} , longitude {}",
70-
mapcode, mapcodeTerritory, p.getLatDeg(), p.getLonDeg());
79+
final Point p = Mapcode.decode(mapcode2, territory);
80+
LOG.info("Mapcode {} in territory {} represents latitude {}, longitude {}",
81+
mapcode2, territory, p.getLatDeg(), p.getLonDeg());
7182
}
7283
catch (final UnknownMapcodeException ignored) {
73-
LOG.info("Mapcode {} in territory {} is invalid", mapcode, mapcodeTerritory);
84+
LOG.info("Mapcode {} in territory {} is invalid", mapcode2, territory);
7485
}
7586
LOG.info("");
87+
}
7688

77-
// example 5 - Encode a coordinate in this territory.
78-
LOG.info("Example 5 (Encode examples):");
79-
double lat = 52.376514;
80-
double lon = 4.908542;
89+
private static void exampleEncode() throws UnknownTerritoryException {
90+
LOG.info("Example: Encode");
8191

82-
// get all results, exclude "world"
83-
List<MapcodeInfo> results = Mapcode.encode(lat, lon, mapcodeTerritory);
92+
final Territory territory = Territory.NLD;
93+
final double lat = 52.376514;
94+
final double lon = 4.908542;
95+
LOG.info("All possible Mapcodes in {} for latitude {}, longitude {}", territory.getFullName(), lat, lon);
96+
97+
// Get all results for territory NLD.
98+
final List<MapcodeInfo> results = Mapcode.encode(lat, lon, territory);
8499
LOG.info("Point latitude {}, longitude {} has {} possible Mapcodes in {}",
85-
lat, lon, results.size(), mapcodeTerritory.getFullName());
100+
lat, lon, results.size(), territory.getFullName());
101+
86102
int count = 1;
87103
for (final MapcodeInfo result : results) {
88-
final String result_mapcode = result.getMapcode();
89-
final int resultCcode = result.getTerritory().getTerritoryCode();
90-
91-
// let's give non-earth Mapcodes an explicit, non-ambiguous territory prefix
92-
String resultPrefix = "";
93-
if (result_mapcode.length() != 10) {
94-
resultPrefix =
95-
Territory.fromTerritoryCode(resultCcode).toString(
96-
Territory.TerritoryNameFormat.MINIMAL_UNAMBIGUOUS) + ' ';
97-
}
98-
LOG.info(" Alternative {}: {} {}", count, resultPrefix, result_mapcode);
104+
LOG.info(" Alternative {}: {} {}", count,
105+
territory.toNameFormat(Territory.NameFormat.MINIMAL_UNAMBIGUOUS), result.getMapcode());
99106
++count;
100107
}
101108
LOG.info("");
109+
}
102110

103-
// example 6 - Encode a coordinate in ALL possible territories.
104-
LOG.info("Example 6:");
105-
lat = 26.87016;
106-
lon = 75.847;
111+
private static void exampleEncodeInAllTerritories() throws UnknownTerritoryException {
112+
LOG.info("Example: Encode in all territories");
113+
114+
final double lat = 26.87016;
115+
final double lon = 75.847;
107116
LOG.info("All possible Mapcodes in the world for latitude {}, longitude {}", lat, lon);
108117

109-
results = Mapcode.encode(lat, lon);
110-
count = 1;
118+
final List<MapcodeInfo> results = Mapcode.encode(lat, lon);
119+
int count = 1;
111120
for (final MapcodeInfo result : results) {
112-
final String result_mapcode = result.getMapcode();
113-
final int result_ccode = result.getTerritory().getTerritoryCode();
114-
115-
// let's give non-earth Mapcodes an explicit, non-ambiguous territory prefix
116-
String result_prefix = "";
117-
if (result_mapcode.length() != 10) {
118-
result_prefix =
119-
Territory.fromTerritoryCode(result_ccode).toString(
120-
Territory.TerritoryNameFormat.MINIMAL_UNAMBIGUOUS) +
121-
' ';
122-
}
123-
LOG.info(" Alternative {}: {} {}", count, result_prefix, result_mapcode);
121+
LOG.info(" Alternative {}: {} {}", count,
122+
result.getTerritory().toNameFormat(Territory.NameFormat.MINIMAL), result.getMapcode());
124123
++count;
125124
}
125+
LOG.info("");
126126
}
127127
}

0 commit comments

Comments
 (0)