2010-04-26 18:24:27 +02:00
|
|
|
package com.osmand.data;
|
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;
|
|
|
|
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-05-17 14:03:16 +02:00
|
|
|
import com.osmand.osm.Entity;
|
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-17 14:03:16 +02:00
|
|
|
public class Region extends MapObject<Entity> {
|
2010-04-26 17:20:50 +02:00
|
|
|
|
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 13:44:35 +02:00
|
|
|
private static class CityComparator implements Comparator<City>{
|
|
|
|
@Override
|
|
|
|
public int compare(City o1, City o2) {
|
|
|
|
return o1.getName().compareTo(o2.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-14 23:05:18 +02:00
|
|
|
private DataTileManager<City> cityManager = new DataTileManager<City>();
|
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-05-16 09:22:09 +02:00
|
|
|
public Region(){
|
|
|
|
this.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())) {
|
|
|
|
double rel = MapUtils.getDistance(c.getEntity(), point) / c.getType().getRadius();
|
|
|
|
if (rel < relDist) {
|
|
|
|
closest = c;
|
|
|
|
relDist = rel;
|
|
|
|
if(relDist < 0.2d){
|
|
|
|
break;
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return closest;
|
|
|
|
}
|
|
|
|
|
2010-05-06 00:29:58 +02:00
|
|
|
public List<Amenity> getClosestAmenities(double latitude, double longitude){
|
2010-05-14 23:05:18 +02:00
|
|
|
return amenities.getClosestObjects(latitude, longitude);
|
2010-04-26 17:20:50 +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();
|
|
|
|
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
|
|
|
|
|
|
|
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-05-14 13:44:35 +02:00
|
|
|
public void doDataPreparation(){
|
|
|
|
CityComparator comp = new CityComparator();
|
|
|
|
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-05-14 23:05:18 +02:00
|
|
|
|
2010-05-14 13:44:35 +02:00
|
|
|
}
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|