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

294 lines
7.3 KiB
Java
Raw Normal View History

package net.osmand.data;
2015-08-21 16:48:27 +02:00
import net.osmand.Location;
import net.osmand.osm.PoiCategory;
2017-04-07 16:05:54 +02:00
import net.osmand.osm.edit.Node;
2015-08-21 16:48:27 +02:00
import net.osmand.util.Algorithms;
2015-05-27 00:28:24 +02:00
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
2013-11-10 16:54:28 +01:00
import java.util.Collections;
2015-06-29 17:26:32 +02:00
import java.util.Iterator;
2013-11-10 16:54:28 +01:00
import java.util.LinkedHashMap;
import java.util.List;
2013-11-10 16:54:28 +01:00
import java.util.Map;
2015-06-29 17:26:32 +02:00
import java.util.Map.Entry;
2015-05-27 00:28:24 +02:00
import java.util.zip.GZIPInputStream;
2013-11-10 16:54:28 +01:00
2017-04-07 16:05:54 +02:00
import gnu.trove.list.array.TIntArrayList;
2014-08-17 15:42:09 +02:00
2016-06-02 20:55:46 +02:00
public class Amenity extends MapObject {
2013-11-24 19:45:00 +01:00
public static final String WEBSITE = "website";
public static final String PHONE = "phone";
public static final String DESCRIPTION = "description";
public static final String OPENING_HOURS = "opening_hours";
2015-05-27 00:09:48 +02:00
public static final String CONTENT = "content";
public static final String CUISINE = "cuisine";
2016-06-02 20:55:46 +02:00
private String subType;
2015-11-12 10:30:43 +01:00
private PoiCategory type;
2013-11-10 16:54:28 +01:00
// duplicate for fast access
private String openingHours;
2013-11-10 16:54:28 +01:00
private Map<String, String> additionalInfo;
2014-08-17 15:42:09 +02:00
private AmenityRoutePoint routePoint; // for search on path
2016-06-02 20:55:46 +02:00
public Amenity() {
}
2016-06-02 20:55:46 +02:00
2014-08-17 15:42:09 +02:00
public static class AmenityRoutePoint {
public double deviateDistance;
public boolean deviationDirectionRight;
2014-08-17 15:42:09 +02:00
public Location pointA;
public Location pointB;
}
2016-06-02 20:55:46 +02:00
public PoiCategory getType() {
return type;
}
2016-06-02 20:55:46 +02:00
public String getSubType() {
return subType;
}
2016-06-02 20:55:46 +02:00
2015-02-18 23:21:57 +01:00
public void setType(PoiCategory type) {
this.type = type;
}
2016-06-02 20:55:46 +02:00
public void setSubType(String subType) {
this.subType = subType;
}
2016-06-02 20:55:46 +02:00
public String getOpeningHours() {
2013-11-10 16:54:28 +01:00
// getAdditionalInfo("opening_hours");
return openingHours;
}
2016-06-02 20:55:46 +02:00
public String getAdditionalInfo(String key) {
if (additionalInfo == null) {
2013-11-10 16:54:28 +01:00
return null;
}
2015-05-27 00:28:24 +02:00
String str = additionalInfo.get(key);
2015-06-14 14:36:49 +02:00
str = unzipContent(str);
return str;
}
public String unzipContent(String str) {
2015-05-27 00:28:24 +02:00
if (str != null) {
if (str.startsWith(" gz ")) {
try {
int ind = 4;
byte[] bytes = new byte[str.length() - ind];
for (int i = ind; i < str.length(); i++) {
char ch = str.charAt(i);
bytes[i - ind] = (byte) ((int) ch - 128 - 32);
}
GZIPInputStream gzn = new GZIPInputStream(new ByteArrayInputStream(bytes));
BufferedReader br = new BufferedReader(new InputStreamReader(gzn, "UTF-8"));
StringBuilder bld = new StringBuilder();
String s;
while ((s = br.readLine()) != null) {
bld.append(s);
}
2015-11-18 15:52:15 +01:00
br.close();
2015-05-27 00:28:24 +02:00
str = bld.toString();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return str;
2013-11-10 16:54:28 +01:00
}
2016-06-02 20:55:46 +02:00
2013-11-10 16:54:28 +01:00
public Map<String, String> getAdditionalInfo() {
2016-06-02 20:55:46 +02:00
if (additionalInfo == null) {
2013-11-10 16:54:28 +01:00
return Collections.emptyMap();
}
return additionalInfo;
}
2016-06-02 20:55:46 +02:00
2013-11-10 16:54:28 +01:00
public void setAdditionalInfo(Map<String, String> additionalInfo) {
2015-06-29 17:26:32 +02:00
this.additionalInfo = null;
openingHours = null;
2016-06-02 20:55:46 +02:00
if (additionalInfo != null) {
2015-06-29 17:26:32 +02:00
Iterator<Entry<String, String>> it = additionalInfo.entrySet().iterator();
2016-06-02 20:55:46 +02:00
while (it.hasNext()) {
2015-06-29 17:26:32 +02:00
Entry<String, String> e = it.next();
setAdditionalInfo(e.getKey(), e.getValue());
}
}
2013-11-10 16:54:28 +01:00
}
2014-08-17 15:42:09 +02:00
public void setRoutePoint(AmenityRoutePoint routePoint) {
this.routePoint = routePoint;
2014-08-05 11:09:15 +02:00
}
2016-06-02 20:55:46 +02:00
2014-08-17 15:42:09 +02:00
public AmenityRoutePoint getRoutePoint() {
return routePoint;
2014-08-05 11:09:15 +02:00
}
2013-11-13 14:13:48 +01:00
2013-11-10 16:54:28 +01:00
public void setAdditionalInfo(String tag, String value) {
2016-06-02 20:55:46 +02:00
if ("name".equals(tag)) {
2013-11-24 20:24:14 +01:00
setName(value);
2016-06-02 20:55:46 +02:00
} else if (tag.startsWith("name:")) {
2015-06-29 17:26:32 +02:00
setName(tag.substring("name:".length()), value);
2013-11-24 20:24:14 +01:00
} else {
2016-06-02 20:55:46 +02:00
if (this.additionalInfo == null) {
2015-06-29 17:26:32 +02:00
this.additionalInfo = new LinkedHashMap<String, String>();
}
2013-11-24 20:24:14 +01:00
this.additionalInfo.put(tag, value);
if (OPENING_HOURS.equals(tag)) {
this.openingHours = value;
}
2013-11-13 14:13:48 +01:00
}
2013-11-10 16:54:28 +01:00
}
2016-06-02 20:55:46 +02:00
2016-09-18 15:48:22 +02:00
@Override
public String toStringEn() {
return super.toStringEn() + ":" + type.getKeyName() + ":" + subType;
}
@Override
public String toString() {
2016-06-02 20:55:46 +02:00
return type.getKeyName() + " : " + subType + " " + getName();
}
2016-06-02 20:55:46 +02:00
public String getSite() {
2013-11-24 19:45:00 +01:00
return getAdditionalInfo(WEBSITE);
}
public void setSite(String site) {
2013-11-24 19:45:00 +01:00
setAdditionalInfo(WEBSITE, site);
}
public String getPhone() {
2013-11-24 19:45:00 +01:00
return getAdditionalInfo(PHONE);
}
public void setPhone(String phone) {
2013-11-24 19:45:00 +01:00
setAdditionalInfo(PHONE, phone);
}
2016-06-02 20:55:46 +02:00
2016-11-20 14:14:06 +01:00
public String getContentLanguage(String tag, String lang, String defLang) {
if (lang != null) {
2015-06-16 00:03:17 +02:00
String translateName = getAdditionalInfo(tag + ":" + lang);
if (!Algorithms.isEmpty(translateName)) {
return lang;
}
}
2015-06-20 23:43:25 +02:00
String plainContent = getAdditionalInfo(tag);
if (!Algorithms.isEmpty(plainContent)) {
2015-06-15 10:41:52 +02:00
return defLang;
}
2015-06-16 00:03:17 +02:00
String enName = getAdditionalInfo(tag + ":en");
if (!Algorithms.isEmpty(enName)) {
2016-11-20 14:01:10 +01:00
return "en";
2015-06-16 00:03:17 +02:00
}
2016-06-02 20:55:46 +02:00
int maxLen = 0;
2015-06-20 23:43:25 +02:00
String lng = defLang;
for (String nm : getAdditionalInfo().keySet()) {
2016-06-02 20:55:46 +02:00
if (nm.startsWith(tag + ":")) {
2015-06-20 23:43:25 +02:00
String key = nm.substring(tag.length() + 1);
2016-06-02 20:55:46 +02:00
String cnt = getAdditionalInfo(tag + ":" + key);
if (!Algorithms.isEmpty(cnt) && cnt.length() > maxLen) {
2015-06-20 23:43:25 +02:00
maxLen = cnt.length();
lng = key;
}
}
}
2015-06-20 23:43:25 +02:00
return lng;
}
2016-06-02 20:55:46 +02:00
2015-06-14 19:10:46 +02:00
public List<String> getNames(String tag, String defTag) {
List<String> l = new ArrayList<String>();
for (String nm : getAdditionalInfo().keySet()) {
2016-06-02 20:55:46 +02:00
if (nm.startsWith(tag + ":")) {
l.add(nm.substring(tag.length() + 1));
} else if (nm.equals(tag)) {
2015-06-14 19:10:46 +02:00
l.add(defTag);
}
}
return l;
}
2016-06-02 20:55:46 +02:00
2016-11-20 14:14:06 +01:00
public String getTagContent(String tag, String lang) {
2015-05-27 00:09:48 +02:00
if (lang != null) {
String translateName = getAdditionalInfo(tag + ":" + lang);
if (!Algorithms.isEmpty(translateName)) {
return translateName;
}
}
String plainName = getAdditionalInfo(tag);
if (!Algorithms.isEmpty(plainName)) {
return plainName;
}
String enName = getAdditionalInfo(tag + ":en");
if (!Algorithms.isEmpty(enName)) {
return enName;
}
2015-05-27 00:28:24 +02:00
for (String nm : getAdditionalInfo().keySet()) {
2015-05-27 00:09:48 +02:00
if (nm.startsWith(tag + ":")) {
return getAdditionalInfo(nm);
}
}
return null;
}
2016-06-02 20:55:46 +02:00
2015-05-27 00:09:48 +02:00
public String getDescription(String lang) {
2016-11-20 14:14:06 +01:00
String info = getTagContent(DESCRIPTION, lang);
2016-06-02 20:55:46 +02:00
if (!Algorithms.isEmpty(info)) {
2015-05-27 00:09:48 +02:00
return info;
}
2016-11-20 14:14:06 +01:00
return getTagContent(CONTENT, lang);
}
2016-06-02 20:55:46 +02:00
public void setDescription(String description) {
2013-11-24 19:45:00 +01:00
setAdditionalInfo(DESCRIPTION, description);
}
2016-06-02 20:55:46 +02:00
2013-11-13 14:13:48 +01:00
public void setOpeningHours(String openingHours) {
2013-11-24 19:45:00 +01:00
setAdditionalInfo(OPENING_HOURS, openingHours);
2013-11-13 14:13:48 +01:00
}
2016-07-29 11:38:02 +02:00
2017-09-03 23:04:22 +02:00
public boolean comparePoi(Amenity thatObj) {
if (this.id == thatObj.id &&
Algorithms.objectEquals(this.type.getKeyName(), thatObj.type.getKeyName()) &&
Algorithms.objectEquals(getLocation(), thatObj.getLocation()) &&
Algorithms.objectEquals(this.subType, thatObj.subType) &&
Algorithms.objectEquals(this.additionalInfo, thatObj.additionalInfo) &&
Algorithms.objectEquals(this.getNamesMap(true), thatObj.getNamesMap(true))) {
return true;
}
return false;
}
2016-07-29 11:38:02 +02:00
@Override
public int compareTo(MapObject o) {
int cmp = super.compareTo(o);
if(cmp == 0 && o instanceof Amenity) {
2016-07-31 20:16:18 +02:00
int kn = ((Amenity) o).getType().getKeyName().compareTo(getType().getKeyName());
if(kn == 0) {
kn = ((Amenity) o).getSubType().compareTo(getSubType());
}
return kn;
2016-07-29 11:38:02 +02:00
}
return cmp;
}
2016-06-02 20:55:46 +02:00
2016-07-29 13:44:31 +02:00
@Override
public boolean equals(Object o) {
boolean res = super.equals(o);
if (res && o instanceof Amenity) {
2016-07-31 20:16:18 +02:00
return Algorithms.stringsEqual(((Amenity) o).getType().getKeyName(), getType().getKeyName())
&& Algorithms.stringsEqual(((Amenity) o).getSubType(), getSubType());
2016-07-29 13:44:31 +02:00
}
return res;
}
}