OsmAnd/OsmAnd-java/src/main/java/net/osmand/data/Street.java

146 lines
3.5 KiB
Java
Raw Normal View History

package net.osmand.data;
2018-12-02 22:01:34 +01:00
import net.osmand.util.Algorithms;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Street extends MapObject {
2016-06-06 12:16:23 +02:00
protected List<Building> buildings = new ArrayList<Building>();
protected List<Street> intersectedStreets = null;
protected final City city;
public Street(City city) {
this.city = city;
}
2016-06-06 12:16:23 +02:00
public void addBuilding(Building building) {
buildings.add(building);
}
2016-06-06 12:16:23 +02:00
public List<Street> getIntersectedStreets() {
2016-06-06 12:16:23 +02:00
if (intersectedStreets == null) {
return Collections.emptyList();
}
return intersectedStreets;
}
2016-06-06 12:16:23 +02:00
public void addIntersectedStreet(Street s) {
if (intersectedStreets == null) {
intersectedStreets = new ArrayList<Street>();
}
intersectedStreets.add(s);
}
2016-06-06 12:16:23 +02:00
public void addBuildingCheckById(Building building) {
for (Building b : buildings) {
2016-08-04 09:28:49 +02:00
if (b.equals(building)) {
return;
}
}
buildings.add(building);
}
2016-06-06 12:16:23 +02:00
public List<Building> getBuildings() {
return buildings;
}
2016-06-06 12:16:23 +02:00
public City getCity() {
return city;
}
2016-06-06 12:16:23 +02:00
public void sortBuildings() {
Collections.sort(buildings, new Comparator<Building>() {
@Override
public int compare(Building o1, Building o2) {
String s1 = o1.getName();
String s2 = o2.getName();
int i1 = Algorithms.extractFirstIntegerNumber(s1);
int i2 = Algorithms.extractFirstIntegerNumber(s2);
2016-06-06 12:16:23 +02:00
if (i1 == i2) {
String t1 = Algorithms.extractIntegerSuffix(s1);
String t2 = Algorithms.extractIntegerSuffix(s2);
return t1.compareTo(t2);
}
return i1 - i2;
}
});
}
2016-06-06 12:16:23 +02:00
2015-06-29 17:26:32 +02:00
/// GENERATION
2016-06-06 12:16:23 +02:00
public void mergeWith(Street street) {
buildings.addAll(street.getBuildings());
2015-06-29 17:26:32 +02:00
copyNames(street);
}
public String getNameWithoutCityPart(String lang, boolean transliterate) {
String nm = getName(lang, transliterate);
2015-06-29 17:26:32 +02:00
int t = nm.lastIndexOf('(');
if (t > 0) {
return nm.substring(0, t);
2015-06-29 17:26:32 +02:00
}
return nm;
}
2018-12-02 22:01:34 +01:00
public JSONObject toJSON() {
2018-12-03 19:56:09 +01:00
return toJSON(true);
}
public JSONObject toJSON(boolean includingBuildings) {
2018-12-02 22:01:34 +01:00
JSONObject json = super.toJSON();
2018-12-03 19:56:09 +01:00
if (buildings.size() > 0 && includingBuildings) {
2018-12-02 22:01:34 +01:00
JSONArray buildingsArr = new JSONArray();
for (Building b : buildings) {
buildingsArr.put(b.toJSON());
}
json.put("buildings", buildingsArr);
}
if (intersectedStreets != null) {
JSONArray intersectedStreetsArr = new JSONArray();
for (Street s : intersectedStreets) {
intersectedStreetsArr.put(s.toJSON());
}
json.put("intersectedStreets", intersectedStreetsArr);
}
return json;
}
public static Street parseJSON(City city, JSONObject json) throws IllegalArgumentException {
Street s = new Street(city);
MapObject.parseJSON(json, s);
if (json.has("buildings")) {
JSONArray buildingsArr = json.getJSONArray("buildings");
s.buildings = new ArrayList<>();
for (int i = 0; i < buildingsArr.length(); i++) {
JSONObject buildingObj = buildingsArr.getJSONObject(i);
Building building = Building.parseJSON(buildingObj);
if (building != null) {
s.buildings.add(building);
}
}
}
if (json.has("intersectedStreets")) {
JSONArray streetsArr = json.getJSONArray("intersectedStreets");
s.intersectedStreets = new ArrayList<>();
for (int i = 0; i < streetsArr.length(); i++) {
JSONObject streetObj = streetsArr.getJSONObject(i);
Street street = parseJSON(city, streetObj);
if (street != null) {
s.intersectedStreets.add(street);
}
}
}
return s;
}
}