diff --git a/DataExtractionOSM/src/net/osmand/data/Boundary.java b/DataExtractionOSM/src/net/osmand/data/Boundary.java index b40f9a9236..30255f5025 100644 --- a/DataExtractionOSM/src/net/osmand/data/Boundary.java +++ b/DataExtractionOSM/src/net/osmand/data/Boundary.java @@ -20,6 +20,7 @@ public class Boundary { private List outerWays = new ArrayList(1); private List innerWays = new ArrayList(0); private boolean closedWay; + private long adminCenterId; public Boundary(boolean closedWay){ this.closedWay = closedWay; @@ -166,4 +167,12 @@ public class Boundary { return getName() + " alevel:" + getAdminLevel() + " type: relation closed:" + isClosedWay(); } + public void setAdminCenterId(long l) { + this.adminCenterId = l; + } + + public long getAdminCenterId() { + return adminCenterId; + } + } diff --git a/DataExtractionOSM/src/net/osmand/data/preparation/IndexAddressCreator.java b/DataExtractionOSM/src/net/osmand/data/preparation/IndexAddressCreator.java index d0b96808d6..cb1e6d095a 100644 --- a/DataExtractionOSM/src/net/osmand/data/preparation/IndexAddressCreator.java +++ b/DataExtractionOSM/src/net/osmand/data/preparation/IndexAddressCreator.java @@ -17,6 +17,7 @@ import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -142,7 +143,10 @@ public class IndexAddressCreator extends AbstractIndexPartCreator{ City cityFound = null; String boundaryName = boundary.getName().toLowerCase(); for (City c : citiesToSearch) { - if (boundary.containsPoint(c.getLocation())) { + if (c.getId() == boundary.getAdminCenterId()) { + cityFound = c; + break; + } else if (boundary.containsPoint(c.getLocation())) { if (boundaryName.equalsIgnoreCase(c.getName())) { cityFound = c; break; @@ -239,42 +243,52 @@ public class IndexAddressCreator extends AbstractIndexPartCreator{ private Boundary putCityBoundary(Boundary boundary, City cityFound) { final Boundary oldBoundary = cityBoundaries.get(cityFound); if (oldBoundary != null) { + if (boundary.getAdminCenterId() == cityFound.getId() + && badBoundary(cityFound, oldBoundary)) { + cityBoundaries.put(cityFound, boundary); + logBoundaryChanged(boundary, cityFound); + } else // try to found the biggest area (not small center district) - if (oldBoundary.getAdminLevel() > boundary.getAdminLevel() && !oldBoundary.getName().equalsIgnoreCase(cityFound.getName())) { + if (oldBoundary.getAdminLevel() > boundary.getAdminLevel() + && badBoundary(cityFound, oldBoundary)) { cityBoundaries.put(cityFound, boundary); - String s = "City: " + cityFound.getName() + " boundary: " + boundary.getName(); - if(logMapDataWarn != null) { - logMapDataWarn.info(s); - } else { - log.info(s); - } - } else if(boundary.getName().equalsIgnoreCase(cityFound.getName()) && - !oldBoundary.getName().equalsIgnoreCase(cityFound.getName())){ + logBoundaryChanged(boundary, cityFound); + } else if (boundary.getName().equalsIgnoreCase(cityFound.getName()) + && badBoundary(cityFound, oldBoundary)) { cityBoundaries.put(cityFound, boundary); - String s = "City: " + cityFound.getName() + " boundary: " + boundary.getName(); - if(logMapDataWarn != null) { - logMapDataWarn.info(s); - } else { - log.info(s); - } - } else if(oldBoundary.getAdminLevel() == boundary.getAdminLevel() && - oldBoundary != boundary && boundary.getName().equalsIgnoreCase(oldBoundary.getName())){ - if(!oldBoundary.isClosedWay() && !boundary.isClosedWay()){ + logBoundaryChanged(boundary, cityFound); + } else if (oldBoundary.getAdminLevel() == boundary.getAdminLevel() + && oldBoundary != boundary + && boundary.getName().equalsIgnoreCase( + oldBoundary.getName())) { + if (!oldBoundary.isClosedWay() && !boundary.isClosedWay()) { oldBoundary.getInnerWays().addAll(boundary.getInnerWays()); oldBoundary.getOuterWays().addAll(boundary.getOuterWays()); } } } else { cityBoundaries.put(cityFound, boundary); - if(logMapDataWarn != null) { - logMapDataWarn.info("City: " + cityFound.getName() + " boundary: " + boundary.getName()); - } else { - log.info("City: " + cityFound.getName() + " boundary: " + boundary.getName()); - } + logBoundaryChanged(boundary, cityFound); } return oldBoundary; } + + private boolean badBoundary(City cityFound, final Boundary oldBoundary) { + return oldBoundary.getAdminCenterId() != cityFound.getId() || + !oldBoundary.getName().equalsIgnoreCase(cityFound.getName()); + } + + + private void logBoundaryChanged(Boundary boundary, City cityFound) { + String s = "City: " + cityFound.getName() + " boundary: " + boundary.getName(); + if(logMapDataWarn != null) { + logMapDataWarn.info(s); + } else { + log.info(s); + } + } + private boolean isBoundary(Entity e) { return "administrative".equals(e.getTag(OSMTagKey.BOUNDARY)) && (e instanceof Relation || e instanceof Way); } @@ -303,6 +317,8 @@ public class IndexAddressCreator extends AbstractIndexPartCreator{ } boundary.getOuterWays().add((Way) es); } + } else if (es instanceof Node && ("admin_centre".equals(entities.get(es)) || "admin_center".equals(entities.get(es)))) { + boundary.setAdminCenterId(es.getId()); } } boundary.computeIsClosedWay(); @@ -521,7 +537,7 @@ public class IndexAddressCreator extends AbstractIndexPartCreator{ } name = normalizeStreetName(name); - Set result = new HashSet(); + Set result = new LinkedHashSet(); List nearestObjects = new ArrayList(); nearestObjects.addAll(cityManager.getClosestObjects(location.getLatitude(),location.getLongitude())); nearestObjects.addAll(cityVillageManager.getClosestObjects(location.getLatitude(),location.getLongitude())); diff --git a/DataExtractionOSM/src/net/osmand/osm/OSMSettings.java b/DataExtractionOSM/src/net/osmand/osm/OSMSettings.java index 674b17ab28..9eaca0150e 100644 --- a/DataExtractionOSM/src/net/osmand/osm/OSMSettings.java +++ b/DataExtractionOSM/src/net/osmand/osm/OSMSettings.java @@ -58,7 +58,7 @@ public class OSMSettings { URL("url"), //$NON-NLS-1$ WIKIPEDIA("wikipedia"), //$NON-NLS-1$ - ADMIN_LEVEL("admin_level"), //$NON-NLS-1$ + ADMIN_LEVEL("admin_level") //$NON-NLS-1$ ; private final String value;