Skip to content

Commit bca9b53

Browse files
Stricter unit test
1 parent 4bbeeb3 commit bca9b53

File tree

3 files changed

+41
-42
lines changed

3 files changed

+41
-42
lines changed

src/main/java/com/mapcode/Mapcode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ public static boolean containsTerritory(@Nonnull final String mapcode) throws Il
400400
*/
401401
private static final double[] PRECISION_0_MAX_OFFSET_METERS = {
402402
7.49, // PRECISION_0: 7.49 meters or less 7.5 m
403-
1.45, // PRECISION_1: 1.45 meters or less 1.5 m
403+
1.39, // PRECISION_1: 1.39 meters or less 1.4 m
404404
0.251, // PRECISION_2: 25.1 cm or less 25 cm
405405
0.0462, // PRECISION_3: 4.62 cm or less 5 cm
406406
0.00837, // PRECISION_4: 8.37 mm or less 1 cm

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

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -252,43 +252,43 @@ public static Point decode(
252252
}
253253

254254
/**
255-
* Is coordinate within a given territory?
255+
* Is coordinate near multiple territory borders?
256256
*
257257
* @param point Latitude/Longitude in degrees
258258
* @param territory Territory
259-
* @return true iff the coordinate is inside the specified territory.
259+
* @return true iff the coordinate is near more than one territory border
260+
* (and thus encode(decode(M)) may not produce M)
260261
*/
261-
public static boolean isInsideTerritory(@Nonnull final Point point, @Nonnull final Territory territory) {
262-
final int ccode = territory.getNumber();
263-
final int from = DataAccess.dataFirstRecord(ccode);
264-
final int upto = DataAccess.dataLastRecord(ccode);
265-
if (Data.getBoundaries(upto).containsPoint(point)) {
266-
for (int m = upto; m >= from; m--) {
267-
if (!Data.isRestricted(m)) {
268-
if (Data.getBoundaries(m).containsPoint(point)) {
269-
return true;
262+
263+
public static boolean multipleBordersNearby(@Nonnull final Point point, @Nonnull final Territory territory) {
264+
if (territory != territory.AAA) {
265+
final int ccode = territory.getNumber();
266+
if (territory.getParentTerritory() != null) {
267+
// there is a parent! check its borders as well...
268+
if (multipleBordersNearby(point, territory.getParentTerritory())) {
269+
return true;
270+
}
271+
}
272+
{
273+
int nrFound = 0;
274+
final int from = DataAccess.dataFirstRecord(ccode);
275+
final int upto = DataAccess.dataLastRecord(ccode);
276+
for (int m = upto; m >= from; m--) {
277+
if (!Data.isRestricted(m)) {
278+
final SubArea boundaries = Data.getBoundaries(m);
279+
final int xdiv8 = Common.xDivider(boundaries.getMinY(),boundaries.getMaxY()) / 4; // @@@
280+
if (boundaries.extendBounds(xdiv8, 60).containsPoint(point)) {
281+
if (!boundaries.extendBounds(-xdiv8, -60).containsPoint(point)) {
282+
nrFound++;
283+
if (nrFound > 1) {
284+
return true;
285+
}
286+
}
287+
}
270288
}
271289
}
272290
}
273291
}
274292
return false;
275293
}
276-
277-
/**
278-
* Is coordinate "fully within" a given territory?
279-
*
280-
* @param point Latitude/Longitude in degrees
281-
* @param territory Territory
282-
* @return true iff the coordinate is inside the specified territory, and furthermore,
283-
* IF the territory has a perent territory: inside the parent territory.
284-
*
285-
* Note that for the mapcode system, the following should hold: IF a point p has a
286-
* mapcode M, THEN decode(M) delivers a point q within maxErrorInMeters() of p.
287-
* Furthermore, encode(q) must yield back M *unless* point q is not "fully inside"
288-
* the mapcode territory.
289-
*/
290-
public static boolean isFullyInsideTerritory(@Nonnull final Point point, @Nonnull final Territory territory) {
291-
final Territory parentTerritory = territory.getParentTerritory();
292-
return isInsideTerritory(point, territory) && ((parentTerritory == null) || isInsideTerritory(point, parentTerritory));
293-
}
294294
}

src/test/java/com/mapcode/EncodeDecodeTest.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,19 @@ public void run() {
128128
break;
129129
}
130130
}
131-
if (!found) { // not found?
132-
if (MapcodeCodec.isFullyInsideTerritory(decodeLocation, territory)) { // but should be found!
133-
// perhaps it was inherited from the parent?
134-
final Territory parentTerritory = territory.getParentTerritory();
135-
if (parentTerritory != null) {
136-
final List<Mapcode> recodedMapcodesFromParent = MapcodeCodec.encode(decodeLocation,parentTerritory);
137-
for (final Mapcode candidate : recodedMapcodesFromParent) {
138-
if (codePrecision.equals(candidate.getCode(nrDigits))) {
139-
found = true;
140-
break;
141-
}
131+
if (!found) {
132+
// perhaps it was inherited from the parent?
133+
final Territory parentTerritory = territory.getParentTerritory();
134+
if (parentTerritory != null) {
135+
final List<Mapcode> recodedMapcodesFromParent = MapcodeCodec.encode(decodeLocation,parentTerritory);
136+
for (final Mapcode candidate : recodedMapcodesFromParent) {
137+
if (codePrecision.equals(candidate.getCode(nrDigits))) {
138+
found = true;
139+
break;
142140
}
143141
}
144-
if (!found) {
142+
}
143+
if (!found) {
145144
LOG.error("Re-encode{} of {} failed for {} {} from ({},{})", nrDigits, decodeLocation, territory, codePrecision, latDeg, lonDeg);
146145
errors.getAndIncrement();
147146
for (final Mapcode candidate : recodedMapcodes) {

0 commit comments

Comments
 (0)