2010-04-26 18:24:27 +02:00
|
|
|
package com.osmand.data;
|
2010-04-26 17:20:50 +02:00
|
|
|
|
2010-05-14 13:44:35 +02:00
|
|
|
import java.util.ArrayList;
|
2010-05-14 23:05:18 +02:00
|
|
|
import java.util.Collections;
|
2010-05-18 15:36:42 +02:00
|
|
|
import java.util.Comparator;
|
2010-05-14 13:44:35 +02:00
|
|
|
import java.util.List;
|
2010-04-26 17:20:50 +02:00
|
|
|
|
2010-05-18 15:36:42 +02:00
|
|
|
import com.osmand.Algoritms;
|
2010-04-27 14:37:49 +02:00
|
|
|
import com.osmand.osm.Entity;
|
|
|
|
import com.osmand.osm.LatLon;
|
2010-05-14 23:05:18 +02:00
|
|
|
import com.osmand.osm.MapUtils;
|
2010-05-14 13:44:35 +02:00
|
|
|
import com.osmand.osm.Node;
|
2010-05-18 15:36:42 +02:00
|
|
|
import com.osmand.osm.Way;
|
2010-05-14 23:05:18 +02:00
|
|
|
import com.osmand.osm.OSMSettings.OSMTagKey;
|
2010-04-26 17:20:50 +02:00
|
|
|
|
2010-05-27 13:02:02 +02:00
|
|
|
public class Street extends MapObject {
|
2010-04-26 17:20:50 +02:00
|
|
|
|
2010-05-14 23:05:18 +02:00
|
|
|
private List<Building> buildings = new ArrayList<Building>();
|
2010-05-18 15:36:42 +02:00
|
|
|
private List<Way> wayNodes = new ArrayList<Way>();
|
|
|
|
private final City city;
|
2010-04-26 17:20:50 +02:00
|
|
|
|
2010-05-18 15:36:42 +02:00
|
|
|
public Street(City city, String name){
|
|
|
|
this.city = city;
|
2010-04-26 17:20:50 +02:00
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
2010-05-18 15:36:42 +02:00
|
|
|
public Street(City city) {
|
|
|
|
this.city = city;
|
|
|
|
}
|
2010-05-16 23:49:11 +02:00
|
|
|
|
2010-05-14 23:05:18 +02:00
|
|
|
public void registerBuilding(Entity e){
|
|
|
|
Building building = new Building(e);
|
|
|
|
building.setName(e.getTag(OSMTagKey.ADDR_HOUSE_NUMBER));
|
|
|
|
buildings.add(building);
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|
|
|
|
|
2010-05-16 23:49:11 +02:00
|
|
|
public void registerBuilding(Building building){
|
|
|
|
buildings.add(building);
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|
|
|
|
|
2010-05-16 23:49:11 +02:00
|
|
|
public List<Building> getBuildings() {
|
|
|
|
return buildings;
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|
2010-05-14 23:05:18 +02:00
|
|
|
|
|
|
|
protected void calculateCenter(){
|
2010-05-18 15:36:42 +02:00
|
|
|
List<Node> nodes = new ArrayList<Node>();
|
|
|
|
for(Way w : wayNodes){
|
|
|
|
nodes.addAll(w.getNodes());
|
|
|
|
}
|
|
|
|
|
|
|
|
LatLon c = MapUtils.getWeightCenterForNodes(nodes);
|
2010-05-14 23:05:18 +02:00
|
|
|
double dist = Double.POSITIVE_INFINITY;
|
2010-05-18 15:36:42 +02:00
|
|
|
for(Node n : nodes){
|
2010-05-15 14:54:26 +02:00
|
|
|
if (n != null) {
|
|
|
|
double nd = MapUtils.getDistance(n, c);
|
|
|
|
if (nd < dist) {
|
|
|
|
dist = nd;
|
2010-05-27 13:02:02 +02:00
|
|
|
location = n.getLatLon();
|
2010-05-15 14:54:26 +02:00
|
|
|
}
|
2010-05-14 23:05:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-02 17:19:37 +02:00
|
|
|
public boolean isRegisteredInCity(){
|
|
|
|
return city.getStreet(getName()) == this;
|
|
|
|
}
|
|
|
|
|
2010-05-18 15:36:42 +02:00
|
|
|
@Override
|
|
|
|
public void setName(String name) {
|
2010-05-27 13:02:02 +02:00
|
|
|
if (name.equals(getName())) {
|
2010-05-18 15:36:42 +02:00
|
|
|
return;
|
|
|
|
}
|
2010-05-27 13:02:02 +02:00
|
|
|
if (city.getStreet(getName()) == this) {
|
|
|
|
city.unregisterStreet(getName());
|
|
|
|
super.setName(name);
|
2010-06-02 17:19:37 +02:00
|
|
|
Street s = city.registerStreet(this);
|
|
|
|
if(s != this){
|
|
|
|
// that case means that street unregistered
|
|
|
|
// city = null;
|
|
|
|
}
|
2010-05-27 13:02:02 +02:00
|
|
|
} else {
|
|
|
|
super.setName(name);
|
|
|
|
}
|
2010-05-18 15:36:42 +02:00
|
|
|
}
|
|
|
|
|
2010-05-14 13:44:35 +02:00
|
|
|
|
2010-05-18 15:36:42 +02:00
|
|
|
public List<Way> getWayNodes() {
|
2010-05-14 13:44:35 +02:00
|
|
|
return wayNodes;
|
|
|
|
}
|
2010-06-02 17:19:37 +02:00
|
|
|
|
|
|
|
public City getCity() {
|
|
|
|
return city;
|
|
|
|
}
|
2010-04-26 17:20:50 +02:00
|
|
|
|
2010-05-14 23:05:18 +02:00
|
|
|
public void doDataPreparation() {
|
2010-05-18 15:36:42 +02:00
|
|
|
Collections.sort(buildings, new Comparator<Building>(){
|
|
|
|
@Override
|
|
|
|
public int compare(Building o1, Building o2) {
|
|
|
|
int i1 = Algoritms.extractFirstIntegerNumber(o1.getName());
|
|
|
|
int i2 = Algoritms.extractFirstIntegerNumber(o2.getName());
|
|
|
|
return i1 - i2;
|
|
|
|
}
|
|
|
|
});
|
2010-05-27 13:02:02 +02:00
|
|
|
calculateCenter();
|
|
|
|
if(location == null){
|
|
|
|
List<LatLon> nodes = new ArrayList<LatLon>();
|
|
|
|
for(Building b : buildings){
|
|
|
|
nodes.add(b.getLocation());
|
|
|
|
}
|
|
|
|
location = MapUtils.getWeightCenter(nodes);
|
|
|
|
}
|
|
|
|
if (wayNodes.size() > 0) {
|
|
|
|
this.id = wayNodes.get(0).getId();
|
|
|
|
} else {
|
|
|
|
this.id = buildings.get(0).getId();
|
|
|
|
}
|
|
|
|
|
2010-05-14 23:05:18 +02:00
|
|
|
}
|
|
|
|
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|