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

266 lines
6.2 KiB
Java
Raw Normal View History

package net.osmand.data;
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.io.UnsupportedEncodingException;
import java.util.ArrayList;
2013-11-10 16:54:28 +01:00
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
2013-11-10 16:54:28 +01:00
import java.util.Map;
2015-05-27 00:28:24 +02:00
import java.util.zip.GZIPInputStream;
2013-11-10 16:54:28 +01:00
2014-08-17 15:42:09 +02:00
import net.osmand.Location;
2015-02-18 23:21:57 +01:00
import net.osmand.osm.PoiCategory;
2015-05-27 00:09:48 +02:00
import net.osmand.util.Algorithms;
2014-08-17 15:42:09 +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";
2013-11-24 19:45:00 +01:00
private static final long serialVersionUID = 132083949926339552L;
private String subType;
2015-02-18 23:21:57 +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
public Amenity(){
}
2014-08-17 15:42:09 +02:00
public static class AmenityRoutePoint {
public double deviateDistance;
public Location pointA;
public Location pointB;
}
2015-02-18 23:21:57 +01:00
public PoiCategory getType(){
return type;
}
public String getSubType(){
return subType;
}
2015-02-18 23:21:57 +01:00
public void setType(PoiCategory type) {
this.type = type;
}
public void setSubType(String subType) {
this.subType = subType;
}
public String getOpeningHours() {
2013-11-10 16:54:28 +01:00
// getAdditionalInfo("opening_hours");
return openingHours;
}
2013-11-10 16:54:28 +01:00
public String getAdditionalInfo(String key){
if(additionalInfo == null) {
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);
}
str = bld.toString();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return str;
2013-11-10 16:54:28 +01:00
}
public Map<String, String> getAdditionalInfo() {
if(additionalInfo == null) {
return Collections.emptyMap();
}
return additionalInfo;
}
public void setAdditionalInfo(Map<String, String> additionalInfo) {
this.additionalInfo = additionalInfo;
2013-11-24 19:45:00 +01:00
openingHours = additionalInfo.get(OPENING_HOURS);
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
}
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) {
if(this.additionalInfo == null){
this.additionalInfo = new LinkedHashMap<String, String>();
}
2013-11-24 20:24:14 +01:00
if("name".equals(tag)) {
setName(value);
} else if("name:en".equals(tag)) {
setEnName(value);
} else {
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
}
@Override
public String toString() {
return type.toString() + " : " + subType + " "+ getName();
}
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);
}
2015-06-16 00:03:17 +02:00
public String getContentSelected(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)) {
return enName;
}
2015-06-20 23:43:25 +02:00
int maxLen = 0;
String lng = defLang;
for (String nm : getAdditionalInfo().keySet()) {
2015-06-20 23:43:25 +02:00
if (nm.startsWith(tag+":")) {
String key = nm.substring(tag.length() + 1);
String cnt = getAdditionalInfo(tag+":"+key);
if(!Algorithms.isEmpty(cnt) && cnt.length() > maxLen) {
maxLen = cnt.length();
lng = key;
}
}
}
2015-06-20 23:43:25 +02:00
return lng;
}
2015-05-27 00:09:48 +02:00
public String getName(String lang) {
if (lang != null) {
String translateName;
if (lang.equals("en")) {
translateName = getEnName();
} else {
translateName = getAdditionalInfo("name:" + lang);
}
if (!Algorithms.isEmpty(translateName)) {
return translateName;
}
}
2015-06-14 14:36:49 +02:00
if(!Algorithms.isEmpty(getName())) {
2015-05-27 00:09:48 +02:00
return getName();
}
2015-05-27 00:28:24 +02:00
for (String nm : getAdditionalInfo().keySet()) {
2015-05-27 00:09:48 +02:00
if (nm.startsWith("name:")) {
return getAdditionalInfo(nm);
}
}
return "";
}
2015-05-27 00:28:24 +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()) {
2015-06-14 14:36:49 +02:00
if (nm.startsWith(tag+":")) {
l.add(nm.substring(tag.length() +1));
2015-06-14 19:10:46 +02:00
} else if(nm.equals(tag)) {
l.add(defTag);
}
}
return l;
}
2015-05-27 00:09:48 +02:00
public String getContentLang(String tag, String lang) {
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;
}
public String getDescription(String lang) {
String info = getContentLang(DESCRIPTION, lang);
if(!Algorithms.isEmpty(info)) {
return info;
}
return getContentLang(CONTENT, lang);
}
public void setDescription(String description) {
2013-11-24 19:45:00 +01:00
setAdditionalInfo(DESCRIPTION, description);
}
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
}
}