Formatting
This commit is contained in:
parent
7c9215454a
commit
0a09cfdc3b
1 changed files with 75 additions and 81 deletions
|
@ -37,7 +37,7 @@ import net.osmand.util.MapUtils;
|
|||
public class OsmandRegions {
|
||||
|
||||
public static final String MAP_TYPE = "region_map";
|
||||
|
||||
|
||||
public static final String FIELD_DOWNLOAD_NAME = "download_name";
|
||||
public static final String FIELD_NAME = "name";
|
||||
public static final String FIELD_NAME_EN = "name:en";
|
||||
|
@ -58,11 +58,10 @@ public class OsmandRegions {
|
|||
Map<String, WorldRegion> fullNamesToRegionData = new HashMap<String, WorldRegion>();
|
||||
Map<String, String> downloadNamesToFullNames = new HashMap<String, String>();
|
||||
Map<String, LinkedList<BinaryMapDataObject>> countriesByDownloadName = new HashMap<String, LinkedList<BinaryMapDataObject>>();
|
||||
|
||||
|
||||
|
||||
QuadTree<String> quadTree ;
|
||||
MapIndexFields mapIndexFields ;
|
||||
QuadTree<String> quadTree;
|
||||
MapIndexFields mapIndexFields;
|
||||
RegionTranslation translator;
|
||||
|
||||
private class MapIndexFields {
|
||||
|
@ -80,41 +79,40 @@ public class OsmandRegions {
|
|||
Integer populationType = null;
|
||||
|
||||
public String get(Integer tp, BinaryMapDataObject object) {
|
||||
if(tp == null) {
|
||||
if (tp == null) {
|
||||
return null;
|
||||
}
|
||||
return object.getNameByType(tp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void setTranslator(RegionTranslation translator) {
|
||||
this.translator = translator;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void prepareFile(String fileName) throws IOException {
|
||||
reader = new BinaryMapIndexReader(new RandomAccessFile(fileName, "r"), new File(fileName));
|
||||
// final Collator clt = OsmAndCollator.primaryCollator();
|
||||
final Map<String, String> parentRelations = new LinkedHashMap<String, String>();
|
||||
final ResultMatcher<BinaryMapDataObject> resultMatcher = new ResultMatcher<BinaryMapDataObject>() {
|
||||
|
||||
|
||||
@Override
|
||||
public boolean publish(BinaryMapDataObject object) {
|
||||
initTypes(object);
|
||||
int[] types = object.getTypes();
|
||||
for(int i = 0; i < types.length; i++ ) {
|
||||
for (int i = 0; i < types.length; i++) {
|
||||
TagValuePair tp = object.getMapIndex().decodeType(types[i]);
|
||||
if("boundary".equals(tp.value)) {
|
||||
if ("boundary".equals(tp.value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
WorldRegion rd = initRegionData(parentRelations, object);
|
||||
if(rd == null) {
|
||||
if (rd == null) {
|
||||
return false;
|
||||
}
|
||||
if(rd.regionDownloadName != null) {
|
||||
if (rd.regionDownloadName != null) {
|
||||
downloadNamesToFullNames.put(rd.regionDownloadName, rd.regionFullName);
|
||||
}
|
||||
fullNamesToRegionData.put(rd.regionFullName, rd);
|
||||
|
@ -128,24 +126,24 @@ public class OsmandRegions {
|
|||
};
|
||||
iterateOverAllObjects(resultMatcher);
|
||||
// post process download names
|
||||
for(Map.Entry<String, String> e : parentRelations.entrySet()) {
|
||||
for (Map.Entry<String, String> e : parentRelations.entrySet()) {
|
||||
String fullName = e.getKey();
|
||||
String parentFullName = e.getValue();
|
||||
// String parentParentFulName = parentRelations.get(parentFullName); // could be used for japan/russia
|
||||
WorldRegion rd = fullNamesToRegionData.get(fullName);
|
||||
WorldRegion parent = fullNamesToRegionData.get(parentFullName);
|
||||
if(parent != null && rd != null) {
|
||||
if (parent != null && rd != null) {
|
||||
parent.addSubregion(rd);
|
||||
}
|
||||
}
|
||||
structureWorldRegions(new ArrayList<WorldRegion>(fullNamesToRegionData.values()));
|
||||
}
|
||||
|
||||
public boolean containsCountry(String name){
|
||||
public boolean containsCountry(String name) {
|
||||
return countriesByDownloadName.containsKey(name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getLocaleName(String downloadName, boolean includingParent) {
|
||||
final String lc = downloadName.toLowerCase();
|
||||
if (downloadNamesToFullNames.containsKey(lc)) {
|
||||
|
@ -157,20 +155,20 @@ public class OsmandRegions {
|
|||
|
||||
public String getLocaleNameByFullName(String fullName, boolean includingParent) {
|
||||
WorldRegion rd = fullNamesToRegionData.get(fullName);
|
||||
if(rd == null) {
|
||||
if (rd == null) {
|
||||
return fullName.replace('_', ' ');
|
||||
}
|
||||
if (includingParent && rd.getSuperregion() != null && rd.getSuperregion().getSuperregion() != null) {
|
||||
WorldRegion parentParent = rd.getSuperregion().getSuperregion();
|
||||
WorldRegion parent = rd.getSuperregion();
|
||||
if(parentParent.getRegionId().equals(WorldRegion.WORLD) &&
|
||||
if (parentParent.getRegionId().equals(WorldRegion.WORLD) &&
|
||||
!parent.getRegionId().equals(WorldRegion.RUSSIA_REGION_ID)) {
|
||||
return rd.getLocaleName();
|
||||
}
|
||||
if(parentParent.getRegionId().equals(WorldRegion.RUSSIA_REGION_ID)) {
|
||||
if (parentParent.getRegionId().equals(WorldRegion.RUSSIA_REGION_ID)) {
|
||||
return parentParent.getLocaleName() + " " + rd.getLocaleName();
|
||||
}
|
||||
if(parentParent.getRegionId().equals(WorldRegion.JAPAN_REGION_ID)) {
|
||||
if (parentParent.getRegionId().equals(WorldRegion.JAPAN_REGION_ID)) {
|
||||
return parentParent.getLocaleName() + " " + rd.getLocaleName();
|
||||
}
|
||||
return parent.getLocaleName() + " " + rd.getLocaleName();
|
||||
|
@ -178,13 +176,13 @@ public class OsmandRegions {
|
|||
return rd.getLocaleName();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public WorldRegion getWorldRegion() {
|
||||
return worldRegion;
|
||||
}
|
||||
|
||||
|
||||
public boolean isInitialized(){
|
||||
public boolean isInitialized() {
|
||||
return reader != null;
|
||||
}
|
||||
|
||||
|
@ -201,42 +199,42 @@ public class OsmandRegions {
|
|||
}
|
||||
return t % 2 == 1;
|
||||
}
|
||||
|
||||
|
||||
public boolean intersect(BinaryMapDataObject bo, int lx, int ty, int rx, int by) {
|
||||
// 1. polygon in object
|
||||
if(contain(bo, lx, ty)) {
|
||||
if (contain(bo, lx, ty)) {
|
||||
return true;
|
||||
}
|
||||
// 2. object in polygon
|
||||
if(bo.getPointsLength() == 0) {
|
||||
if (bo.getPointsLength() == 0) {
|
||||
return false;
|
||||
}
|
||||
if(bo.getPoint31XTile(0) >= lx && bo.getPoint31XTile(0) <= rx &&
|
||||
bo.getPoint31YTile(0) >= ty && bo.getPoint31YTile(0) <= by ){
|
||||
if (bo.getPoint31XTile(0) >= lx && bo.getPoint31XTile(0) <= rx &&
|
||||
bo.getPoint31YTile(0) >= ty && bo.getPoint31YTile(0) <= by) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// 3. find intersection
|
||||
for (int i = 1; i < bo.getPointsLength(); i++) {
|
||||
int px = bo.getPoint31XTile(i - 1);
|
||||
int x = bo.getPoint31XTile(i);
|
||||
int py = bo.getPoint31YTile(i - 1);
|
||||
int y = bo.getPoint31YTile(i);
|
||||
if(x < lx && px < lx) {
|
||||
if (x < lx && px < lx) {
|
||||
continue;
|
||||
} else if(x > rx && px > rx) {
|
||||
} else if (x > rx && px > rx) {
|
||||
continue;
|
||||
} else if(y > by && py > by) {
|
||||
} else if (y > by && py > by) {
|
||||
continue;
|
||||
} else if(y < ty && py < ty) {
|
||||
} else if (y < ty && py < ty) {
|
||||
continue;
|
||||
}
|
||||
long in = MapAlgorithms.calculateIntersection(px, py, x, y, lx, rx, by, ty);
|
||||
if(in != -1) {
|
||||
if (in != -1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -266,7 +264,7 @@ public class OsmandRegions {
|
|||
|
||||
|
||||
public List<BinaryMapDataObject> query(final int tile31x, final int tile31y) throws IOException {
|
||||
if(quadTree != null) {
|
||||
if (quadTree != null) {
|
||||
return getCountries(tile31x, tile31y);
|
||||
}
|
||||
return queryNoInit(tile31x, tile31y);
|
||||
|
@ -301,7 +299,7 @@ public class OsmandRegions {
|
|||
}
|
||||
}
|
||||
);
|
||||
if(reader != null) {
|
||||
if (reader != null) {
|
||||
reader.searchMapIndex(sr);
|
||||
}
|
||||
return result;
|
||||
|
@ -335,33 +333,33 @@ public class OsmandRegions {
|
|||
}
|
||||
);
|
||||
sr.log = false;
|
||||
if(reader != null) {
|
||||
if (reader != null) {
|
||||
reader.searchMapIndex(sr);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public void setLocale(String locale) {
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public WorldRegion getRegionData(String fullname) {
|
||||
return fullNamesToRegionData.get(fullname);
|
||||
}
|
||||
|
||||
|
||||
public WorldRegion getRegionDataByDownloadName(String downloadName) {
|
||||
return getRegionData(downloadNamesToFullNames.get(downloadName.toLowerCase()));
|
||||
}
|
||||
|
||||
|
||||
public String getDownloadName(BinaryMapDataObject o) {
|
||||
return mapIndexFields.get(mapIndexFields.downloadNameType, o);
|
||||
}
|
||||
|
||||
|
||||
public String getFullName(BinaryMapDataObject o) {
|
||||
return mapIndexFields.get(mapIndexFields.fullNameType, o);
|
||||
}
|
||||
|
||||
|
||||
public List<WorldRegion> getAllRegionData() {
|
||||
return new ArrayList<WorldRegion>(fullNamesToRegionData.values());
|
||||
}
|
||||
|
@ -370,7 +368,7 @@ public class OsmandRegions {
|
|||
private WorldRegion initRegionData(final Map<String, String> parentRelations, BinaryMapDataObject object) {
|
||||
String regionDownloadName = mapIndexFields.get(mapIndexFields.downloadNameType, object);
|
||||
String regionFullName = mapIndexFields.get(mapIndexFields.fullNameType, object);
|
||||
if(Algorithms.isEmpty(regionFullName)) {
|
||||
if (Algorithms.isEmpty(regionFullName)) {
|
||||
return null;
|
||||
}
|
||||
WorldRegion rd = new WorldRegion(regionFullName, regionDownloadName);
|
||||
|
@ -380,16 +378,16 @@ public class OsmandRegions {
|
|||
cx += object.getPoint31XTile(i);
|
||||
cy += object.getPoint31YTile(i);
|
||||
}
|
||||
if(object.getPointsLength() > 0) {
|
||||
if (object.getPointsLength() > 0) {
|
||||
cx /= object.getPointsLength();
|
||||
cy /= object.getPointsLength();
|
||||
rd.regionCenter = new LatLon(MapUtils.get31LatitudeY((int) cy), MapUtils.get31LongitudeX((int) cx));
|
||||
}
|
||||
|
||||
|
||||
rd.regionParentFullName = mapIndexFields.get(mapIndexFields.parentFullName, object);
|
||||
if(!Algorithms.isEmpty(rd.regionParentFullName)) {
|
||||
if (!Algorithms.isEmpty(rd.regionParentFullName)) {
|
||||
parentRelations.put(rd.regionFullName, rd.regionParentFullName);
|
||||
}
|
||||
}
|
||||
rd.regionName = mapIndexFields.get(mapIndexFields.nameType, object);
|
||||
rd.regionNameLocale = mapIndexFields.get(mapIndexFields.nameLocaleType, object);
|
||||
rd.regionNameEn = mapIndexFields.get(mapIndexFields.nameEnType, object);
|
||||
|
@ -408,28 +406,26 @@ public class OsmandRegions {
|
|||
MapIndex mi = object.getMapIndex();
|
||||
TIntObjectIterator<String> it = object.getObjectNames().iterator();
|
||||
StringBuilder ind = new StringBuilder();
|
||||
while(it.hasNext()) {
|
||||
while (it.hasNext()) {
|
||||
it.advance();
|
||||
TagValuePair tp = mi.decodeType(it.key());
|
||||
if(tp.tag.startsWith("name") || tp.tag.equals("key_name")) {
|
||||
if (tp.tag.startsWith("name") || tp.tag.equals("key_name")) {
|
||||
final String vl = it.value().toLowerCase();
|
||||
// if (!CollatorStringMatcher.ccontains(clt, ind.toString(), vl)) {
|
||||
if(ind.indexOf(vl) == -1) {
|
||||
if (ind.indexOf(vl) == -1) {
|
||||
ind.append(" ").append(vl);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ind.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean isDownloadOfType(BinaryMapDataObject object, String type) {
|
||||
int[] addtypes = object.getAdditionalTypes();
|
||||
for(int i = 0; i < addtypes.length; i++) {
|
||||
for (int i = 0; i < addtypes.length; i++) {
|
||||
TagValuePair tp = object.getMapIndex().decodeType(addtypes[i]);
|
||||
if(type.equals(tp.tag) && "yes".equals(tp.value)) {
|
||||
if (type.equals(tp.tag) && "yes".equals(tp.value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -495,12 +491,12 @@ public class OsmandRegions {
|
|||
return true;
|
||||
}
|
||||
}, resultMatcher);
|
||||
if(reader != null) {
|
||||
if (reader != null) {
|
||||
reader.searchMapIndex(sr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void initTypes(BinaryMapDataObject object) {
|
||||
if (mapIndexFields == null) {
|
||||
mapIndexFields = new MapIndexFields();
|
||||
|
@ -508,7 +504,7 @@ public class OsmandRegions {
|
|||
mapIndexFields.downloadNameType = object.getMapIndex().getRule(FIELD_DOWNLOAD_NAME, null);
|
||||
mapIndexFields.nameType = object.getMapIndex().getRule(FIELD_NAME, null);
|
||||
mapIndexFields.nameEnType = object.getMapIndex().getRule(FIELD_NAME_EN, null);
|
||||
mapIndexFields.nameLocaleType = object.getMapIndex().getRule(FIELD_NAME+":"+locale, null);
|
||||
mapIndexFields.nameLocaleType = object.getMapIndex().getRule(FIELD_NAME + ":" + locale, null);
|
||||
mapIndexFields.parentFullName = object.getMapIndex().getRule(FIELD_REGION_PARENT_NAME, null);
|
||||
mapIndexFields.fullNameType = object.getMapIndex().getRule(FIELD_REGION_FULL_NAME, null);
|
||||
mapIndexFields.langType = object.getMapIndex().getRule(FIELD_LANG, null);
|
||||
|
@ -530,10 +526,10 @@ public class OsmandRegions {
|
|||
|
||||
for (BinaryMapDataObject b : cs) {
|
||||
String nm = b.getNameByType(or.mapIndexFields.nameEnType);
|
||||
if(nm == null) {
|
||||
if (nm == null) {
|
||||
nm = b.getName();
|
||||
}
|
||||
if(or.isDownloadOfType(b, MAP_TYPE)) {
|
||||
if (or.isDownloadOfType(b, MAP_TYPE)) {
|
||||
found.add(nm.toLowerCase());
|
||||
}
|
||||
}
|
||||
|
@ -558,8 +554,8 @@ public class OsmandRegions {
|
|||
// if(i++ <=5)
|
||||
// lst.addAll(wd.getSubregions());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
or.cacheAllCountries();
|
||||
// long t = System.currentTimeMillis();
|
||||
// or.cacheAllCountries();
|
||||
|
@ -568,30 +564,30 @@ public class OsmandRegions {
|
|||
//testCountry(or, 15.8, 23.09, "chad");
|
||||
testCountry(or, 52.10, 4.92, "the netherlands", "utrecht");
|
||||
testCountry(or, 52.15, 7.50, "north rhine-westphalia");
|
||||
testCountry(or, 28.8056, 29.9858, "egypt" );
|
||||
testCountry(or, 28.8056, 29.9858, "egypt");
|
||||
// testCountry(or, 40.0760, 9.2807, "italy", "sardinia");
|
||||
testCountry(or, 35.7521, 139.7887, "japan");
|
||||
testCountry(or, 46.5145, 102.2580, "mongolia");
|
||||
testCountry(or, 62.54, 43.36, "arkhangelsk oblast", "northwestern federal district");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public interface RegionTranslation {
|
||||
|
||||
|
||||
public String getTranslation(String id);
|
||||
}
|
||||
|
||||
|
||||
private void initWorldRegion(WorldRegion world, String id) {
|
||||
WorldRegion rg = new WorldRegion(id);
|
||||
rg.regionParentFullName = world.regionFullName;
|
||||
if(translator != null) {
|
||||
if (translator != null) {
|
||||
rg.regionName = translator.getTranslation(id);
|
||||
}
|
||||
world.addSubregion(rg);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void structureWorldRegions(List<WorldRegion> loadedItems ) {
|
||||
public void structureWorldRegions(List<WorldRegion> loadedItems) {
|
||||
if (loadedItems.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
@ -605,22 +601,22 @@ public class OsmandRegions {
|
|||
initWorldRegion(world, WorldRegion.SOUTH_AMERICA_REGION_ID);
|
||||
initWorldRegion(world, WorldRegion.AUSTRALIA_AND_OCEANIA_REGION_ID);
|
||||
Iterator<WorldRegion> it = loadedItems.iterator();
|
||||
while(it.hasNext()) {
|
||||
while (it.hasNext()) {
|
||||
WorldRegion region = it.next();
|
||||
if(region.superregion == null) {
|
||||
if (region.superregion == null) {
|
||||
boolean found = false;
|
||||
for(WorldRegion worldSubregion : world.subregions) {
|
||||
if(worldSubregion.getRegionId().equalsIgnoreCase(region.regionFullName)) {
|
||||
for(WorldRegion rg : region.subregions) {
|
||||
for (WorldRegion worldSubregion : world.subregions) {
|
||||
if (worldSubregion.getRegionId().equalsIgnoreCase(region.regionFullName)) {
|
||||
for (WorldRegion rg : region.subregions) {
|
||||
worldSubregion.addSubregion(rg);
|
||||
}
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(found) {
|
||||
if (found) {
|
||||
it.remove();
|
||||
} else if(region.getRegionId().contains("basemap")) {
|
||||
} else if (region.getRegionId().contains("basemap")) {
|
||||
it.remove();
|
||||
}
|
||||
} else {
|
||||
|
@ -645,7 +641,6 @@ public class OsmandRegions {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void sortSubregions(WorldRegion region, Comparator<WorldRegion> comparator) {
|
||||
Collections.sort(region.subregions, comparator);
|
||||
|
@ -656,5 +651,4 @@ public class OsmandRegions {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue