OsmAnd/OsmAnd-java/src/net/osmand/data/City.java

155 lines
3.2 KiB
Java
Raw Normal View History

package net.osmand.data;
2015-06-29 17:26:32 +02:00
import java.util.ArrayList;
import java.util.Collection;
2015-06-29 17:26:32 +02:00
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
2014-04-12 23:37:18 +02:00
import net.osmand.OsmAndCollator;
import net.osmand.util.Algorithms;
public class City extends MapObject {
public enum CityType {
// that's tricky way to play with that numbers (to avoid including suburbs in city & vice verse)
// district special type and it is not registered as a city
CITY(10000), TOWN(5000), VILLAGE(1300), HAMLET(1000), SUBURB(400), DISTRICT(400), NEIGHBOURHOOD(300);
private double radius;
2016-05-17 15:45:12 +02:00
CityType(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public static String valueToString(CityType t) {
return t.toString().toLowerCase();
}
public static CityType valueFromString(String place) {
if (place == null) {
return null;
}
2016-05-17 15:45:12 +02:00
if ("township".equals(place)) {
2015-08-04 11:57:28 +02:00
return CityType.TOWN;
}
2016-05-17 15:45:12 +02:00
if ("borough".equals(place)) {
2015-09-23 14:17:02 +02:00
return CityType.SUBURB;
}
for (CityType t : CityType.values()) {
if (t.name().equalsIgnoreCase(place)) {
return t;
}
}
return null;
}
}
private CityType type = null;
2015-06-29 17:26:32 +02:00
private List<Street> listOfStreets = new ArrayList<Street>();
private String postcode = null;
2014-07-14 02:02:32 +02:00
private City closestCity = null;
2015-06-29 17:26:32 +02:00
private static long POSTCODE_INTERNAL_ID = -1000;
public static City createPostcode(String postcode){
return new City(postcode, POSTCODE_INTERNAL_ID--);
}
2016-05-16 14:54:50 +02:00
public City(CityType type) {
2016-05-17 15:45:12 +02:00
if (type == null) {
throw new NullPointerException();
}
this.type = type;
}
public City(String postcode, long id) {
this.type = null;
this.name = this.enName = postcode;
this.id = id;
}
public String getIsInValue() {
return isin;
}
public boolean isPostcode(){
return type == null;
}
2015-06-29 17:26:32 +02:00
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
2014-07-14 02:02:32 +02:00
public City getClosestCity() {
return closestCity;
}
public void setClosestCity(City closestCity) {
this.closestCity = closestCity;
}
2015-06-29 17:26:32 +02:00
public void registerStreet(Street street) {
listOfStreets.add(street);
}
public void unregisterStreet(Street candidate) {
listOfStreets.remove(candidate);
}
public CityType getType() {
return type;
}
2016-05-17 23:29:32 +02:00
public List<Street> getStreets() {
2015-06-29 17:26:32 +02:00
return listOfStreets;
}
@Override
public String toString() {
2016-05-17 15:45:12 +02:00
if (isPostcode()) {
2016-05-18 01:35:52 +02:00
return "Postcode : " + getName() + " " + getLocation(); //$NON-NLS-1$ //$NON-NLS-2$
}
2016-05-18 01:35:52 +02:00
return "City [" + type + "] " + getName() + " " + getLocation(); //$NON-NLS-1$ //$NON-NLS-2$
}
2015-06-29 17:26:32 +02:00
public Street getStreetByName(String name) {
2016-05-17 15:45:12 +02:00
for (Street s : listOfStreets) {
if (s.getName().equalsIgnoreCase(name)) {
2015-06-29 17:26:32 +02:00
return s;
}
}
return null;
}
// GENERATION
// Be attentive ! Working with street names ignoring case
private String isin = null;
public void setIsin(String isin) {
this.isin = isin;
}
2016-05-17 22:17:49 +02:00
public void mergeWith(City city) {
for (Street street : city.listOfStreets) {
if (listOfStreets.contains(street)) {
listOfStreets.get(listOfStreets.indexOf(street)).mergeWith(street);
} else {
listOfStreets.add(street);
}
}
copyNames(city);
}
}