OsmAnd/OsmAnd-java/src/net/osmand/binary/GeocodingUtilities.java

346 lines
12 KiB
Java
Raw Normal View History

2016-01-02 14:56:04 +01:00
package net.osmand.binary;
2015-11-29 15:20:36 +01:00
import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher;
2016-06-21 01:24:25 +02:00
import net.osmand.CollatorStringMatcher.StringMatcherMode;
2015-11-29 15:20:36 +01:00
import net.osmand.binary.BinaryMapIndexReader.SearchRequest;
import net.osmand.data.Building;
2015-11-30 00:41:35 +01:00
import net.osmand.data.Building.BuildingInterpolation;
2015-11-29 15:20:36 +01:00
import net.osmand.data.City;
import net.osmand.data.LatLon;
import net.osmand.data.MapObject;
import net.osmand.data.Street;
import net.osmand.router.BinaryRoutePlanner;
import net.osmand.router.BinaryRoutePlanner.RouteSegmentPoint;
2015-11-30 00:41:35 +01:00
import net.osmand.router.RoutePlannerFrontEnd;
2015-11-29 15:20:36 +01:00
import net.osmand.router.RoutingContext;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
2015-11-30 00:41:35 +01:00
import org.apache.commons.logging.Log;
2015-12-07 09:30:46 +01:00
import java.io.IOException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import gnu.trove.set.hash.TLongHashSet;
2015-11-29 15:20:36 +01:00
public class GeocodingUtilities {
private static final Log log = PlatformUtil.getLog(GeocodingUtilities.class);
2016-06-24 16:53:13 +02:00
2017-07-20 13:32:56 +02:00
// Location to test parameters https://www.openstreetmap.org/#map=18/53.896473/27.540071 (hno 44)
// BUG https://www.openstreetmap.org/#map=19/50.9356/13.35348 (hno 26) street is
public static final float THRESHOLD_MULTIPLIER_SKIP_STREETS_AFTER = 5;
public static final float STOP_SEARCHING_STREET_WITH_MULTIPLIER_RADIUS = 250;
2015-12-02 10:22:33 +01:00
public static final float STOP_SEARCHING_STREET_WITHOUT_MULTIPLIER_RADIUS = 400;
2016-06-24 16:53:13 +02:00
public static final int DISTANCE_STREET_NAME_PROXIMITY_BY_NAME = 15000;
2015-12-02 13:45:47 +01:00
public static final float DISTANCE_STREET_FROM_CLOSEST_WITH_SAME_NAME = 1000;
2016-06-24 16:53:13 +02:00
2015-12-02 10:22:33 +01:00
public static final float THRESHOLD_MULTIPLIER_SKIP_BUILDINGS_AFTER = 1.5f;
2016-06-24 16:53:13 +02:00
public static final float DISTANCE_BUILDING_PROXIMITY = 100;
2015-11-29 15:20:36 +01:00
public static final Comparator<GeocodingResult> DISTANCE_COMPARATOR = new Comparator<GeocodingResult>() {
@Override
public int compare(GeocodingResult o1, GeocodingResult o2) {
LatLon l1 = o1.getLocation();
LatLon l2 = o2.getLocation();
2016-06-24 16:53:13 +02:00
if (l1 == null || l2 == null) {
2015-11-29 15:20:36 +01:00
return l2 == l1 ? 0 : (l1 == null ? -1 : 1);
}
return Double.compare(MapUtils.getDistance(l1, o1.searchPoint),
MapUtils.getDistance(l2, o2.searchPoint));
}
};
2016-06-24 16:53:13 +02:00
2015-11-29 15:20:36 +01:00
public static class GeocodingResult {
2016-06-24 16:53:13 +02:00
public GeocodingResult() {
2015-11-29 15:20:36 +01:00
}
2016-06-24 16:53:13 +02:00
public GeocodingResult(GeocodingResult r) {
2015-11-29 15:20:36 +01:00
this.searchPoint = r.searchPoint;
this.regionFP = r.regionFP;
this.regionLen = r.regionLen;
this.connectionPoint = r.connectionPoint;
this.streetName = r.streetName;
this.point = r.point;
this.building = r.building;
this.city = r.city;
this.street = r.street;
}
2016-06-24 16:53:13 +02:00
2015-11-29 15:20:36 +01:00
// input
public LatLon searchPoint;
// 1st step
public LatLon connectionPoint;
public int regionFP;
public int regionLen;
public RouteSegmentPoint point;
public String streetName;
// justification
public Building building;
2015-11-30 00:41:35 +01:00
public String buildingInterpolation;
2015-11-29 15:20:36 +01:00
public Street street;
public City city;
private double dist = -1;
2016-06-24 16:53:13 +02:00
2015-11-29 15:20:36 +01:00
public LatLon getLocation() {
return connectionPoint;
}
2016-06-24 16:53:13 +02:00
2015-11-29 15:20:36 +01:00
public double getDistance() {
2016-06-24 16:53:13 +02:00
if (dist == -1 && connectionPoint != null && searchPoint != null) {
2015-11-29 15:20:36 +01:00
dist = MapUtils.getDistance(connectionPoint, searchPoint);
}
return dist;
}
public double getDistanceP() {
2016-06-24 16:53:13 +02:00
if (point != null && searchPoint != null) {
2016-01-02 15:35:38 +01:00
// Need distance between searchPoint and nearest RouteSegmentPoint here, to approximate distance from neareest named road
2016-01-02 15:19:41 +01:00
return Math.sqrt(point.distSquare);
} else {
return -1;
}
}
2015-11-29 15:20:36 +01:00
@Override
public String toString() {
StringBuilder bld = new StringBuilder();
2016-06-24 16:53:13 +02:00
if (building != null) {
2018-03-25 15:44:35 +02:00
if(buildingInterpolation != null) {
bld.append(buildingInterpolation);
} else {
bld.append(building.getName());
}
2015-11-29 15:20:36 +01:00
}
2016-06-24 16:53:13 +02:00
if (street != null) {
2015-11-29 15:20:36 +01:00
bld.append(" str. ").append(street.getName()).append(" city ").append(city.getName());
2016-06-24 16:53:13 +02:00
} else if (streetName != null) {
2015-11-29 15:20:36 +01:00
bld.append(" str. ").append(streetName);
2016-06-24 16:53:13 +02:00
} else if (city != null) {
2015-11-29 15:20:36 +01:00
bld.append(" city ").append(city.getName());
}
2016-06-24 16:53:13 +02:00
if (connectionPoint != null && searchPoint != null) {
2015-11-29 15:20:36 +01:00
bld.append(" dist=").append((int) getDistance());
}
return bld.toString();
}
}
2015-12-02 10:14:24 +01:00
2016-06-24 16:53:13 +02:00
2017-10-15 09:17:13 +02:00
public List<GeocodingResult> reverseGeocodingSearch(RoutingContext ctx, double lat, double lon, boolean allowEmptyNames) throws IOException {
2015-11-29 15:20:36 +01:00
RoutePlannerFrontEnd rp = new RoutePlannerFrontEnd(false);
List<GeocodingResult> lst = new ArrayList<GeocodingUtilities.GeocodingResult>();
List<RouteSegmentPoint> listR = new ArrayList<BinaryRoutePlanner.RouteSegmentPoint>();
rp.findRouteSegment(lat, lon, ctx, listR);
2015-12-09 23:24:06 +01:00
double distSquare = 0;
2015-11-29 15:20:36 +01:00
TLongHashSet set = new TLongHashSet();
Set<String> streetNames = new HashSet<String>();
2016-06-24 16:53:13 +02:00
for (RouteSegmentPoint p : listR) {
2015-11-29 15:20:36 +01:00
RouteDataObject road = p.getRoad();
2016-06-24 16:53:13 +02:00
if (!set.add(road.getId())) {
2015-11-29 15:20:36 +01:00
continue;
}
2015-12-17 17:02:52 +01:00
// System.out.println(road.toString() + " " + Math.sqrt(p.distSquare));
2017-10-15 09:17:13 +02:00
String name = Algorithms.isEmpty(road.getName()) ? road.getRef("", false, true) : road.getName();
if (allowEmptyNames || !Algorithms.isEmpty(name)) {
2016-06-24 16:53:13 +02:00
if (distSquare == 0 || distSquare > p.distSquare) {
2015-12-09 23:24:06 +01:00
distSquare = p.distSquare;
2015-11-29 15:20:36 +01:00
}
GeocodingResult sr = new GeocodingResult();
sr.searchPoint = new LatLon(lat, lon);
2017-10-15 09:17:13 +02:00
sr.streetName = name == null ? "" : name;
2015-11-29 15:20:36 +01:00
sr.point = p;
sr.connectionPoint = new LatLon(MapUtils.get31LatitudeY(p.preciseY), MapUtils.get31LongitudeX(p.preciseX));
sr.regionFP = road.region.getFilePointer();
sr.regionLen = road.region.getLength();
2016-06-24 16:53:13 +02:00
if (streetNames.add(sr.streetName)) {
2015-11-29 15:20:36 +01:00
lst.add(sr);
}
}
2016-06-24 16:53:13 +02:00
if (p.distSquare > STOP_SEARCHING_STREET_WITH_MULTIPLIER_RADIUS * STOP_SEARCHING_STREET_WITH_MULTIPLIER_RADIUS &&
distSquare != 0 && p.distSquare > THRESHOLD_MULTIPLIER_SKIP_STREETS_AFTER * distSquare) {
2015-11-29 15:20:36 +01:00
break;
}
2016-06-24 16:53:13 +02:00
if (p.distSquare > STOP_SEARCHING_STREET_WITHOUT_MULTIPLIER_RADIUS * STOP_SEARCHING_STREET_WITHOUT_MULTIPLIER_RADIUS) {
2015-11-29 15:20:36 +01:00
break;
}
}
Collections.sort(lst, GeocodingUtilities.DISTANCE_COMPARATOR);
return lst;
}
2016-06-24 16:53:13 +02:00
public List<String> prepareStreetName(String s, boolean addCommonWords) {
2015-12-02 00:33:25 +01:00
List<String> ls = new ArrayList<String>();
2015-12-02 00:31:47 +01:00
int beginning = 0;
2015-12-02 00:33:25 +01:00
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == ' ') {
addWord(ls, s.substring(beginning, i), addCommonWords);
2015-12-02 00:31:47 +01:00
beginning = i;
2015-12-02 00:33:25 +01:00
} else if (s.charAt(i) == '(') {
addWord(ls, s.substring(beginning, i), addCommonWords);
2015-12-02 00:31:47 +01:00
while (i < s.length()) {
char c = s.charAt(i);
i++;
beginning = i;
if (c == ')')
break;
}
2015-12-02 00:33:25 +01:00
2015-12-02 00:31:47 +01:00
}
}
2015-12-02 00:33:25 +01:00
if (beginning < s.length()) {
String lastWord = s.substring(beginning, s.length());
addWord(ls, lastWord, addCommonWords);
2015-12-02 00:33:25 +01:00
}
2015-12-02 00:31:47 +01:00
Collections.sort(ls, Collator.getInstance());
return ls;
}
private void addWord(List<String> ls, String word, boolean addCommonWords) {
2015-12-02 00:31:47 +01:00
String w = word.trim().toLowerCase();
if (!Algorithms.isEmpty(w)) {
if(!addCommonWords && CommonWords.getCommonGeocoding(word) != -1) {
return;
}
2015-12-02 00:31:47 +01:00
ls.add(w);
}
}
2016-06-24 16:53:13 +02:00
2015-12-02 02:19:45 +01:00
public List<GeocodingResult> justifyReverseGeocodingSearch(final GeocodingResult road, BinaryMapIndexReader reader,
2016-06-24 16:59:32 +02:00
double knownMinBuildingDistance, final ResultMatcher<GeocodingResult> result) throws IOException {
2015-11-29 15:20:36 +01:00
// test address index search
final List<GeocodingResult> streetsList = new ArrayList<GeocodingResult>();
final List<String> streetNamesUsed = prepareStreetName(road.streetName, true);
final List<String> streetNamesPacked = streetNamesUsed.size() == 0 ?
prepareStreetName(road.streetName, false) : streetNamesUsed;
if (streetNamesPacked.size() > 0) {
log.info("Search street by name " + road.streetName + " " + streetNamesPacked);
2015-12-02 00:31:47 +01:00
String mainWord = "";
for (int i = 0; i < streetNamesPacked.size(); i++) {
String s = streetNamesPacked.get(i);
if (s.length() > mainWord.length()) {
2015-12-02 00:31:47 +01:00
mainWord = s;
2015-11-29 15:20:36 +01:00
}
}
2015-12-02 00:31:47 +01:00
SearchRequest<MapObject> req = BinaryMapIndexReader.buildAddressByNameRequest(
new ResultMatcher<MapObject>() {
@Override
public boolean publish(MapObject object) {
if (object instanceof Street
&& prepareStreetName(object.getName(), true).equals(streetNamesUsed)) {
2015-12-02 02:19:45 +01:00
double d = MapUtils.getDistance(object.getLocation(), road.searchPoint.getLatitude(),
road.searchPoint.getLongitude());
// double check to suport old format
2015-12-02 00:31:47 +01:00
if (d < DISTANCE_STREET_NAME_PROXIMITY_BY_NAME) {
2015-12-02 02:19:45 +01:00
GeocodingResult rs = new GeocodingResult(road);
2015-12-02 00:31:47 +01:00
rs.street = (Street) object;
2015-12-02 02:19:45 +01:00
// set connection point to sort
rs.connectionPoint = rs.street.getLocation();
2015-12-02 00:31:47 +01:00
rs.city = rs.street.getCity();
streetsList.add(rs);
return true;
}
return false;
}
return false;
}
@Override
public boolean isCancelled() {
2015-12-18 12:53:47 +01:00
return result != null && result.isCancelled();
2015-12-02 00:31:47 +01:00
}
2016-07-02 23:56:40 +02:00
}, mainWord, StringMatcherMode.CHECK_EQUALS_FROM_SPACE);
req.setBBoxRadius(road.getLocation().getLatitude(), road.getLocation().getLongitude(), DISTANCE_STREET_NAME_PROXIMITY_BY_NAME);
2015-12-02 00:31:47 +01:00
reader.searchAddressDataByName(req);
}
2016-06-24 16:53:13 +02:00
2015-11-29 15:20:36 +01:00
final List<GeocodingResult> res = new ArrayList<GeocodingResult>();
2016-06-24 16:53:13 +02:00
if (streetsList.size() == 0) {
2015-12-02 02:19:45 +01:00
res.add(road);
2015-11-29 15:57:46 +01:00
} else {
2015-12-02 02:19:45 +01:00
Collections.sort(streetsList, DISTANCE_COMPARATOR);
double streetDistance = 0;
for (GeocodingResult street : streetsList) {
2016-06-24 16:53:13 +02:00
if (streetDistance == 0) {
2015-12-02 02:19:45 +01:00
streetDistance = street.getDistance();
2016-06-24 16:53:13 +02:00
} else if (street.getDistance() > streetDistance + DISTANCE_STREET_FROM_CLOSEST_WITH_SAME_NAME) {
2015-12-02 02:19:45 +01:00
continue;
2015-11-29 15:20:36 +01:00
}
2015-12-02 02:19:45 +01:00
street.connectionPoint = road.connectionPoint;
final List<GeocodingResult> streetBuildings = loadStreetBuildings(road, reader, street);
2015-11-29 15:57:46 +01:00
Collections.sort(streetBuildings, DISTANCE_COMPARATOR);
if (streetBuildings.size() > 0) {
Iterator<GeocodingResult> it = streetBuildings.iterator();
2016-06-24 16:59:32 +02:00
if (knownMinBuildingDistance == 0) {
2015-11-29 15:57:46 +01:00
GeocodingResult firstBld = it.next();
2016-06-24 16:59:32 +02:00
knownMinBuildingDistance = firstBld.getDistance();
2015-11-29 15:57:46 +01:00
res.add(firstBld);
}
while (it.hasNext()) {
GeocodingResult nextBld = it.next();
2016-06-24 16:59:32 +02:00
if (nextBld.getDistance() > knownMinBuildingDistance
2015-11-29 15:57:46 +01:00
* THRESHOLD_MULTIPLIER_SKIP_BUILDINGS_AFTER) {
break;
}
res.add(nextBld);
2015-11-29 15:20:36 +01:00
}
}
2015-12-02 02:19:45 +01:00
res.add(street);
2015-11-29 15:20:36 +01:00
}
}
Collections.sort(res, DISTANCE_COMPARATOR);
return res;
}
2015-12-02 02:19:45 +01:00
private List<GeocodingResult> loadStreetBuildings(final GeocodingResult road, BinaryMapIndexReader reader,
GeocodingResult street) throws IOException {
final List<GeocodingResult> streetBuildings = new ArrayList<GeocodingResult>();
reader.preloadBuildings(street.street, null);
log.info("Preload buildings " + street.street.getName() + " " + street.city.getName() + " " + street.street.getId());
for (Building b : street.street.getBuildings()) {
2016-06-24 16:53:13 +02:00
if (b.getLatLon2() != null) {
2015-12-02 02:19:45 +01:00
double slat = b.getLocation().getLatitude();
double slon = b.getLocation().getLongitude();
double tolat = b.getLatLon2().getLatitude();
double tolon = b.getLatLon2().getLongitude();
double coeff = MapUtils.getProjectionCoeff(road.searchPoint.getLatitude(), road.searchPoint.getLongitude(),
slat, slon, tolat, tolon);
double plat = slat + (tolat - slat) * coeff;
double plon = slon + (tolon - slon) * coeff;
2016-06-24 16:53:13 +02:00
if (MapUtils.getDistance(road.searchPoint, plat, plon) < DISTANCE_BUILDING_PROXIMITY) {
2015-12-02 02:19:45 +01:00
GeocodingResult bld = new GeocodingResult(street);
bld.building = b;
2018-03-25 15:44:35 +02:00
//bld.connectionPoint = b.getLocation();
bld.connectionPoint = new LatLon(plat, plon);
2016-06-24 16:53:13 +02:00
streetBuildings.add(bld);
String nm = b.getInterpolationName(coeff);
if(!Algorithms.isEmpty(nm)) {
bld.buildingInterpolation = nm;
2015-12-02 02:19:45 +01:00
}
}
2016-06-24 16:53:13 +02:00
} else if (MapUtils.getDistance(b.getLocation(), road.searchPoint) < DISTANCE_BUILDING_PROXIMITY) {
2015-12-02 02:19:45 +01:00
GeocodingResult bld = new GeocodingResult(street);
bld.building = b;
bld.connectionPoint = b.getLocation();
streetBuildings.add(bld);
}
}
return streetBuildings;
}
2015-11-29 15:20:36 +01:00
}