From b5598d34547f79e9998e12281cdda8e30a4a7fbc Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sat, 2 Jun 2012 22:04:30 +0200 Subject: [PATCH] Sort cities list --- .../src/net/osmand/CollatorStringMatcher.java | 2 +- .../net/osmand/binary/BinaryInspector.java | 40 ++++++++--------- .../search/SearchCityByNameActivity.java | 44 +++++++++++++++++++ 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/DataExtractionOSM/src/net/osmand/CollatorStringMatcher.java b/DataExtractionOSM/src/net/osmand/CollatorStringMatcher.java index aa0b8fd998..b102066ed8 100644 --- a/DataExtractionOSM/src/net/osmand/CollatorStringMatcher.java +++ b/DataExtractionOSM/src/net/osmand/CollatorStringMatcher.java @@ -17,7 +17,7 @@ public class CollatorStringMatcher implements StringMatcher { private final StringMatcherMode mode; private final String part; - public enum StringMatcherMode { + public static enum StringMatcherMode { CHECK_ONLY_STARTS_WITH, CHECK_STARTS_FROM_SPACE, CHECK_STARTS_FROM_SPACE_NOT_BEGINNING, diff --git a/DataExtractionOSM/src/net/osmand/binary/BinaryInspector.java b/DataExtractionOSM/src/net/osmand/binary/BinaryInspector.java index 1e4e0b6afd..4176e9462b 100644 --- a/DataExtractionOSM/src/net/osmand/binary/BinaryInspector.java +++ b/DataExtractionOSM/src/net/osmand/binary/BinaryInspector.java @@ -48,7 +48,7 @@ public class BinaryInspector { // test cases show info -// inspector(new String[]{"-vmap", /*"-bbox=-121.785,37.35,-121.744,37.33", */"/home/victor/projects/OsmAnd/data/osm-gen/Map.obf"}); +// inspector(new String[]{"-vaddress", /*"-bbox=-121.785,37.35,-121.744,37.33", */"/home/victor/projects/OsmAnd/temp/.obf"}); // test case extract parts // test case } @@ -439,33 +439,33 @@ public class BinaryInspector { for (int j = 0; j < cityType.length; j++) { int type = cityType[j]; for (City c : index.getCities(region, null, type)) { + println("\t\t" + c + " " + c.getId() + " (" + c.getStreets().size() + ")"); index.preloadStreets(c, null); - println("\t\t" + c + getId(c) + "("+c.getStreets().size()+")"); for (Street t : c.getStreets()) { if (verbose.contains(t)) { index.preloadBuildings(t, null); - print("\t\t\t" + t.getName() + getId(t) + "("+t.getBuildings().size()+")"); -// if (type == BinaryMapAddressReaderAdapter.CITY_TOWN_TYPE) { - List buildings = t.getBuildings(); - if (buildings != null && !buildings.isEmpty()) { - print("\t\t ("); - for (Building b : buildings) { - print(b.toString() + ","); - } - print(")"); + print("\t\t\t" + t.getName() + getId(t) + " (" + t.getBuildings().size() + ")"); + // if (type == BinaryMapAddressReaderAdapter.CITY_TOWN_TYPE) { + List buildings = t.getBuildings(); + if (buildings != null && !buildings.isEmpty()) { + print("\t\t ("); + for (Building b : buildings) { + print(b.toString() + ","); } - List streets = t.getIntersectedStreets(); - if (streets != null && !streets.isEmpty()) { - print("\n\t\t\t\t\t\t\t x ("); - for (Street s : streets) { - print(s.getName() +", "); - } - print(")"); + print(")"); + } + List streets = t.getIntersectedStreets(); + if (streets != null && !streets.isEmpty()) { + print("\n\t\t\t\t\t\t\t x ("); + for (Street s : streets) { + print(s.getName() + ", "); } -// } + print(")"); + } + // } println(""); } - + } } } diff --git a/OsmAnd/src/net/osmand/plus/activities/search/SearchCityByNameActivity.java b/OsmAnd/src/net/osmand/plus/activities/search/SearchCityByNameActivity.java index cf2dd51cf9..6b10cd6a0d 100644 --- a/OsmAnd/src/net/osmand/plus/activities/search/SearchCityByNameActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/search/SearchCityByNameActivity.java @@ -1,10 +1,16 @@ package net.osmand.plus.activities.search; +import java.text.Collator; +import java.util.Collections; +import java.util.Comparator; import java.util.List; +import net.osmand.CollatorStringMatcher; +import net.osmand.CollatorStringMatcher.StringMatcherMode; import net.osmand.OsmAndFormatter; import net.osmand.ResultMatcher; import net.osmand.data.City; +import net.osmand.data.City.CityType; import net.osmand.osm.LatLon; import net.osmand.osm.MapUtils; import net.osmand.plus.OsmandApplication; @@ -26,6 +32,44 @@ public class SearchCityByNameActivity extends SearchByNameAbstractActivity protected void onPostExecute(List result) { ((TextView)findViewById(R.id.Label)).setText(R.string.incremental_search_city); progress.setVisibility(View.INVISIBLE); + final Collator cs = Collator.getInstance(); + final boolean en = region.useEnglishNames(); + final String part = getFilter().toString(); + final StringMatcherMode startsWith = CollatorStringMatcher.StringMatcherMode.CHECK_ONLY_STARTS_WITH; + Collections.sort(result, new Comparator() { + @Override + public int compare(City lhs, City rhs) { + int compare = compareCityType(lhs, rhs); + if (compare != 0) { + return compare; + } + boolean st1 = CollatorStringMatcher.cmatches(cs, lhs.getName(en), part, startsWith); + boolean st2 = CollatorStringMatcher.cmatches(cs, rhs.getName(en), part, startsWith); + if(st1 != st2) { + return st1 ? 1 : -1; + } + compare = cs.compare(lhs.getName(en), rhs.getName(en)); + if (compare != 0) { + return compare; + } + if (locationToSearch != null) { + double d1 = MapUtils.getDistance(locationToSearch, lhs.getLocation()); + double d2 = MapUtils.getDistance(locationToSearch, rhs.getLocation()); + return -Double.compare(d1, d2); + } + return 0; + } + + private int compareCityType(City lhs, City rhs) { + boolean c1 = lhs.getType() == CityType.CITY || lhs.getType() == CityType.TOWN; + boolean c2 = rhs.getType() == CityType.CITY || rhs.getType() == CityType.TOWN; + if(c1 == c2){ + return 0; + } + return c1? 1 : -1; + } + }); + finishInitializing(result); }