269a270,271 > > private static CityFinder cityFinder = null; 272,273c274,275 < CityFinder cityFinder = null; < if (geoNamesFile != null){ --- > synchronized (this) { > if (geoNamesFile != null){ 279a282 > } 281,287c284 < for (Area area : getAreas()) { < area.setName(description); < if (cityFinder == null) < continue; < < // Decide what to call the area < Set found = cityFinder.findCities(area); --- > for (Area area : getAreas()) { 289,291c286,295 < for (City city : found) { < if (bestMatch == null || city.getPopulation() > bestMatch.getPopulation()) { < bestMatch = city; --- > if (cityFinder != null) { > > // Decide what to call the area > Set found = cityFinder.findCities(area); > > // Select largest population city in area/ > for (City city : found) { > if (bestMatch == null || city.getPopulation() > bestMatch.getPopulation()) { > bestMatch = city; > } 294,295c298,302 < if (bestMatch != null) < area.setName(bestMatch.getCountryCode() + '-' + bestMatch.getName()); --- > area.setName( > (bestMatch == null > ? description > : bestMatch.getCountryCode() + '-' + bestMatch.getName()) + > ":"+plusCode(area)); 350c357,402 < } --- > } > > private static final String codes = "23456789CFGHJMPQRVWX"; > > private static final double GarminFactor = 360.0/((double)(1<<24)); > > private String plusCode(Area area) { > double south = area.getMinLat()*GarminFactor; > double west = area.getMinLong()*GarminFactor; > double north = area.getMaxLat()*GarminFactor; > double east = area.getMaxLong()*GarminFactor; > double centerLatitude = 0.5 *(south + north)+90.; > double centerLongitude = 0.5 * (west + east)+180.; > double boxHeight = north - south; > double boxWidth = east - west; > double height = 400.; > double width = 400.; > StringBuilder code = new StringBuilder(); > boolean done = false; > while(code.length() < 8 & !done) { > int p; > height /= 20.; > width /= 20.; > p = (int)Math.floor(centerLatitude/height); > code.append(codes.charAt(p)); > centerLatitude -= p * height; > p = (int)Math.floor(centerLongitude/width); > code.append(codes.charAt(p)); > centerLongitude -= p * width; > done = width <= boxWidth && height <= boxHeight; > } > while(code.length() < 8) code.append("00"); > code.append("+"); > while(!done) { > int la, lo; > height /= 5.; > width /= 4.; > la = (int)Math.floor(centerLatitude/height); > centerLatitude -= la * height; > lo = (int)Math.floor(centerLongitude/width); > centerLongitude -= lo * width; > code.append(codes.charAt(4*la+lo)); > done = width <= boxWidth && height <= boxHeight; > } > return code.toString(); > }