Sort cities list

This commit is contained in:
Victor Shcherb 2012-06-02 22:04:30 +02:00
parent 75211b4f19
commit b5598d3454
3 changed files with 65 additions and 21 deletions

View file

@ -17,7 +17,7 @@ public class CollatorStringMatcher implements StringMatcher {
private final StringMatcherMode mode; private final StringMatcherMode mode;
private final String part; private final String part;
public enum StringMatcherMode { public static enum StringMatcherMode {
CHECK_ONLY_STARTS_WITH, CHECK_ONLY_STARTS_WITH,
CHECK_STARTS_FROM_SPACE, CHECK_STARTS_FROM_SPACE,
CHECK_STARTS_FROM_SPACE_NOT_BEGINNING, CHECK_STARTS_FROM_SPACE_NOT_BEGINNING,

View file

@ -48,7 +48,7 @@ public class BinaryInspector {
// test cases show info // 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 extract parts
// test case // test case
} }
@ -439,30 +439,30 @@ public class BinaryInspector {
for (int j = 0; j < cityType.length; j++) { for (int j = 0; j < cityType.length; j++) {
int type = cityType[j]; int type = cityType[j];
for (City c : index.getCities(region, null, type)) { for (City c : index.getCities(region, null, type)) {
println("\t\t" + c + " " + c.getId() + " (" + c.getStreets().size() + ")");
index.preloadStreets(c, null); index.preloadStreets(c, null);
println("\t\t" + c + getId(c) + "("+c.getStreets().size()+")");
for (Street t : c.getStreets()) { for (Street t : c.getStreets()) {
if (verbose.contains(t)) { if (verbose.contains(t)) {
index.preloadBuildings(t, null); index.preloadBuildings(t, null);
print("\t\t\t" + t.getName() + getId(t) + "("+t.getBuildings().size()+")"); print("\t\t\t" + t.getName() + getId(t) + " (" + t.getBuildings().size() + ")");
// if (type == BinaryMapAddressReaderAdapter.CITY_TOWN_TYPE) { // if (type == BinaryMapAddressReaderAdapter.CITY_TOWN_TYPE) {
List<Building> buildings = t.getBuildings(); List<Building> buildings = t.getBuildings();
if (buildings != null && !buildings.isEmpty()) { if (buildings != null && !buildings.isEmpty()) {
print("\t\t ("); print("\t\t (");
for (Building b : buildings) { for (Building b : buildings) {
print(b.toString() + ","); print(b.toString() + ",");
}
print(")");
} }
List<Street> streets = t.getIntersectedStreets(); print(")");
if (streets != null && !streets.isEmpty()) { }
print("\n\t\t\t\t\t\t\t x ("); List<Street> streets = t.getIntersectedStreets();
for (Street s : streets) { if (streets != null && !streets.isEmpty()) {
print(s.getName() +", "); print("\n\t\t\t\t\t\t\t x (");
} for (Street s : streets) {
print(")"); print(s.getName() + ", ");
} }
// } print(")");
}
// }
println(""); println("");
} }

View file

@ -1,10 +1,16 @@
package net.osmand.plus.activities.search; package net.osmand.plus.activities.search;
import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import net.osmand.CollatorStringMatcher;
import net.osmand.CollatorStringMatcher.StringMatcherMode;
import net.osmand.OsmAndFormatter; import net.osmand.OsmAndFormatter;
import net.osmand.ResultMatcher; import net.osmand.ResultMatcher;
import net.osmand.data.City; import net.osmand.data.City;
import net.osmand.data.City.CityType;
import net.osmand.osm.LatLon; import net.osmand.osm.LatLon;
import net.osmand.osm.MapUtils; import net.osmand.osm.MapUtils;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
@ -26,6 +32,44 @@ public class SearchCityByNameActivity extends SearchByNameAbstractActivity<City>
protected void onPostExecute(List<City> result) { protected void onPostExecute(List<City> result) {
((TextView)findViewById(R.id.Label)).setText(R.string.incremental_search_city); ((TextView)findViewById(R.id.Label)).setText(R.string.incremental_search_city);
progress.setVisibility(View.INVISIBLE); 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<City>() {
@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); finishInitializing(result);
} }