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;
|
|
|
|
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-04-27 14:37:49 +02:00
|
|
|
import com.osmand.osm.Entity;
|
|
|
|
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-04-27 23:42:19 +02:00
|
|
|
import com.osmand.osm.OSMSettings.OSMTagKey;
|
2010-04-26 17:20:50 +02:00
|
|
|
|
|
|
|
public class Region {
|
|
|
|
private Entity entity;
|
|
|
|
|
|
|
|
private DataTileManager<Node> amenities = new DataTileManager<Node>();
|
|
|
|
|
|
|
|
private Map<CityType, Collection<City>> cities = new HashMap<CityType, Collection<City>>();
|
|
|
|
{
|
|
|
|
for(CityType type : CityType.values()){
|
|
|
|
cities.put(type, new ArrayList<City>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Region(Entity entity){
|
|
|
|
this.entity = entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setEntity(Entity e){
|
|
|
|
this.entity = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName(){
|
2010-04-27 23:42:19 +02:00
|
|
|
return entity == null ? "" : entity.getTag(OSMTagKey.NAME);
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<City> getCitiesByType(CityType type){
|
|
|
|
return cities.get(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public City getClosestCity(LatLon point){
|
|
|
|
City closest = null;
|
|
|
|
double relDist = Double.POSITIVE_INFINITY;
|
|
|
|
for(CityType t : CityType.values()){
|
|
|
|
for(City c : cities.get(t)){
|
2010-04-27 23:42:19 +02:00
|
|
|
double rel = MapUtils.getDistance(c.getNode(), point) / t.getRadius();
|
2010-04-26 17:20:50 +02:00
|
|
|
if(rel < 1) {
|
|
|
|
return c; // we are in that city
|
|
|
|
}
|
|
|
|
if(rel < relDist){
|
|
|
|
closest = c;
|
|
|
|
relDist = rel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return closest;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Node> getClosestAmenities(double latitude, double longitude){
|
|
|
|
return amenities.getClosestObjects(latitude, longitude, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void registerAmenity(Node n){
|
|
|
|
amenities.registerObject(n.getLatitude(), n.getLongitude(), n);
|
|
|
|
}
|
2010-04-30 00:47:07 +02:00
|
|
|
|
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-04-26 17:20:50 +02:00
|
|
|
cities.get(city.getType()).add(city);
|
|
|
|
return city;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|