2010-04-26 18:24:27 +02:00
|
|
|
package com.osmand.data;
|
2010-04-26 17:20:50 +02:00
|
|
|
|
2010-06-01 23:16:56 +02:00
|
|
|
import java.text.Collator;
|
2010-04-26 17:20:50 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
2010-05-14 13:44:35 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
2010-04-26 17:20:50 +02:00
|
|
|
import java.util.HashMap;
|
2010-06-14 23:14:36 +02:00
|
|
|
import java.util.LinkedHashMap;
|
2010-04-26 17:20:50 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2010-04-27 23:42:19 +02:00
|
|
|
import com.osmand.Algoritms;
|
2010-04-26 18:24:27 +02:00
|
|
|
import com.osmand.data.City.CityType;
|
2010-04-27 14:37:49 +02:00
|
|
|
import com.osmand.osm.LatLon;
|
2010-04-27 23:42:19 +02:00
|
|
|
import com.osmand.osm.MapUtils;
|
2010-04-27 14:37:49 +02:00
|
|
|
import com.osmand.osm.Node;
|
2010-05-06 16:55:49 +02:00
|
|
|
import com.osmand.osm.io.OsmBaseStorage;
|
2010-04-26 17:20:50 +02:00
|
|
|
|
2010-05-27 13:02:02 +02:00
|
|
|
public class Region extends MapObject {
|
2010-05-06 00:29:58 +02:00
|
|
|
private DataTileManager<Amenity> amenities = new DataTileManager<Amenity>();
|
2010-05-06 16:55:49 +02:00
|
|
|
private OsmBaseStorage storage;
|
2010-05-14 23:05:18 +02:00
|
|
|
private DataTileManager<City> cityManager = new DataTileManager<City>();
|
2010-06-14 23:14:36 +02:00
|
|
|
private Map<String, List<TransportRoute>> routes = new LinkedHashMap<String, List<TransportRoute>>();
|
2010-05-14 13:44:35 +02:00
|
|
|
private Map<CityType, List<City>> cities = new HashMap<CityType, List<City>>();
|
2010-04-26 17:20:50 +02:00
|
|
|
{
|
2010-05-14 23:05:18 +02:00
|
|
|
cityManager.setZoom(10);
|
2010-04-26 17:20:50 +02:00
|
|
|
for(CityType type : CityType.values()){
|
|
|
|
cities.put(type, new ArrayList<City>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-01 23:16:56 +02:00
|
|
|
public static class CityComparator implements Comparator<City>{
|
|
|
|
private final boolean en;
|
|
|
|
public CityComparator(boolean en){
|
|
|
|
this.en = en;
|
|
|
|
}
|
|
|
|
Collator collator = Collator.getInstance();
|
2010-05-27 13:02:02 +02:00
|
|
|
@Override
|
|
|
|
public int compare(City o1, City o2) {
|
2010-06-01 23:16:56 +02:00
|
|
|
if(en){
|
|
|
|
return collator.compare(o1.getEnName(), o2.getEnName());
|
|
|
|
} else {
|
|
|
|
return collator.compare(o1.getName(), o2.getName());
|
|
|
|
}
|
2010-05-27 13:02:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-16 09:22:09 +02:00
|
|
|
public Region(){
|
2010-05-17 16:35:52 +02:00
|
|
|
name = "Region";
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|
|
|
|
|
2010-05-06 16:55:49 +02:00
|
|
|
public OsmBaseStorage getStorage() {
|
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setStorage(OsmBaseStorage storage) {
|
|
|
|
this.storage = storage;
|
|
|
|
}
|
|
|
|
|
2010-04-26 17:20:50 +02:00
|
|
|
|
|
|
|
public Collection<City> getCitiesByType(CityType type){
|
|
|
|
return cities.get(type);
|
|
|
|
}
|
|
|
|
|
2010-05-14 13:44:35 +02:00
|
|
|
public int getCitiesCount(CityType type) {
|
|
|
|
if (type == null) {
|
|
|
|
int am = 0;
|
|
|
|
for (CityType t : cities.keySet()) {
|
|
|
|
am += cities.get(t).size();
|
|
|
|
}
|
|
|
|
return am;
|
|
|
|
} else if (!cities.containsKey(type)) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return cities.get(type).size();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-04-26 17:20:50 +02:00
|
|
|
public Collection<City> getCitiesByName(String name){
|
|
|
|
return getCityByName(name, true, Integer.MAX_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<City> getSuggestedCities(String name, int number){
|
|
|
|
return getCityByName(name, false, number);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Collection<City> getCityByName(String name, boolean exactMatch, int number){
|
|
|
|
List<City> l = new ArrayList<City>();
|
|
|
|
for(CityType type : CityType.values()){
|
|
|
|
for(City c : cities.get(type)){
|
|
|
|
if( (exactMatch && c.getName().equalsIgnoreCase(name)) ||
|
|
|
|
(!exactMatch && c.getName().toLowerCase().startsWith(name.toLowerCase())
|
|
|
|
)){
|
|
|
|
l.add(c);
|
|
|
|
if(l.size() >= number){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2010-05-14 23:05:18 +02:00
|
|
|
public City getClosestCity(LatLon point) {
|
2010-04-26 17:20:50 +02:00
|
|
|
City closest = null;
|
|
|
|
double relDist = Double.POSITIVE_INFINITY;
|
2010-05-14 23:05:18 +02:00
|
|
|
for (City c : cityManager.getClosestObjects(point.getLatitude(), point.getLongitude())) {
|
2010-05-27 13:02:02 +02:00
|
|
|
double rel = MapUtils.getDistance(c.getLocation(), point) / c.getType().getRadius();
|
2010-05-14 23:05:18 +02:00
|
|
|
if (rel < relDist) {
|
|
|
|
closest = c;
|
|
|
|
relDist = rel;
|
|
|
|
if(relDist < 0.2d){
|
|
|
|
break;
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return closest;
|
|
|
|
}
|
|
|
|
|
2010-05-21 08:43:01 +02:00
|
|
|
|
2010-05-06 11:08:58 +02:00
|
|
|
public DataTileManager<Amenity> getAmenityManager(){
|
|
|
|
return amenities;
|
|
|
|
}
|
|
|
|
|
2010-05-06 00:29:58 +02:00
|
|
|
public void registerAmenity(Amenity a){
|
2010-05-14 23:05:18 +02:00
|
|
|
LatLon location = a.getLocation();
|
2010-05-18 15:36:42 +02:00
|
|
|
if(location != null){
|
|
|
|
amenities.registerObject(location.getLatitude(), location.getLongitude(), a);
|
|
|
|
}
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|
2010-04-30 00:47:07 +02:00
|
|
|
|
2010-05-16 23:49:11 +02:00
|
|
|
public void registerCity(City city){
|
|
|
|
if(city.getType() != null && !Algoritms.isEmpty(city.getName()) && city.getLocation() != null){
|
|
|
|
LatLon l = city.getLocation();
|
|
|
|
cityManager.registerObject(l.getLatitude(), l.getLongitude(), city);
|
|
|
|
cities.get(city.getType()).add(city);
|
|
|
|
}
|
|
|
|
}
|
2010-04-26 17:20:50 +02:00
|
|
|
|
2010-06-02 17:19:37 +02:00
|
|
|
public void unregisterCity(City city){
|
|
|
|
if(city != null && city.getType() != null){
|
|
|
|
LatLon l = city.getLocation();
|
|
|
|
cityManager.unregisterObject(l.getLatitude(), l.getLongitude(), city);
|
|
|
|
cities.get(city.getType()).remove(city);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-26 17:20:50 +02:00
|
|
|
public City registerCity(Node c){
|
|
|
|
City city = new City(c);
|
2010-04-27 23:42:19 +02:00
|
|
|
if(city.getType() != null && !Algoritms.isEmpty(city.getName())){
|
2010-05-14 23:05:18 +02:00
|
|
|
cityManager.registerObject(c.getLatitude(), c.getLongitude(), city);
|
2010-04-26 17:20:50 +02:00
|
|
|
cities.get(city.getType()).add(city);
|
|
|
|
return city;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2010-06-14 23:14:36 +02:00
|
|
|
public Map<String, List<TransportRoute>> getTransportRoutes() {
|
|
|
|
return routes;
|
|
|
|
}
|
|
|
|
|
2010-04-26 17:20:50 +02:00
|
|
|
|
2010-05-14 13:44:35 +02:00
|
|
|
public void doDataPreparation(){
|
2010-06-01 23:16:56 +02:00
|
|
|
CityComparator comp = new CityComparator(false);
|
2010-05-14 13:44:35 +02:00
|
|
|
for(CityType t : cities.keySet()){
|
|
|
|
Collections.sort(cities.get(t), comp);
|
2010-05-14 23:05:18 +02:00
|
|
|
for(City c : cities.get(t)){
|
|
|
|
c.doDataPreparation();
|
|
|
|
}
|
2010-05-14 13:44:35 +02:00
|
|
|
}
|
2010-06-14 23:14:36 +02:00
|
|
|
for(String s : routes.keySet()){
|
|
|
|
List<TransportRoute> trans = routes.get(s);
|
|
|
|
Collections.sort(trans, new Comparator<TransportRoute>(){
|
|
|
|
@Override
|
|
|
|
public int compare(TransportRoute o1, TransportRoute o2) {
|
|
|
|
int i1 = Algoritms.extractFirstIntegerNumber(o1.getRef());
|
|
|
|
int i2 = Algoritms.extractFirstIntegerNumber(o2.getRef());
|
|
|
|
return i1 - i2;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2010-05-14 13:44:35 +02:00
|
|
|
|
2010-05-14 23:05:18 +02:00
|
|
|
|
2010-05-14 13:44:35 +02:00
|
|
|
}
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|