示例#1
0
 @Override
 public int compare(GeocodingResult o1, GeocodingResult o2) {
   LatLon l1 = o1.getLocation();
   LatLon l2 = o2.getLocation();
   if (l1 == null || l2 == null) {
     return l2 == l1 ? 0 : (l1 == null ? -1 : 1);
   }
   return Double.compare(
       MapUtils.getDistance(l1, o1.searchPoint), MapUtils.getDistance(l2, o2.searchPoint));
 }
示例#2
0
 public List<GeocodingResult> reverseGeocodingSearch(RoutingContext ctx, double lat, double lon)
     throws IOException {
   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);
   double distSquare = 0;
   TLongHashSet set = new TLongHashSet();
   Set<String> streetNames = new HashSet<String>();
   for (RouteSegmentPoint p : listR) {
     RouteDataObject road = p.getRoad();
     if (!set.add(road.getId())) {
       continue;
     }
     //			System.out.println(road.toString() +  " " + Math.sqrt(p.distSquare));
     boolean emptyName =
         Algorithms.isEmpty(road.getName()) && Algorithms.isEmpty(road.getRef("", false, true));
     if (!emptyName) {
       if (distSquare == 0 || distSquare > p.distSquare) {
         distSquare = p.distSquare;
       }
       GeocodingResult sr = new GeocodingResult();
       sr.searchPoint = new LatLon(lat, lon);
       sr.streetName =
           Algorithms.isEmpty(road.getName()) ? road.getRef("", false, true) : road.getName();
       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();
       if (streetNames.add(sr.streetName)) {
         lst.add(sr);
       }
     }
     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) {
       break;
     }
     if (p.distSquare
         > STOP_SEARCHING_STREET_WITHOUT_MULTIPLIER_RADIUS
             * STOP_SEARCHING_STREET_WITHOUT_MULTIPLIER_RADIUS) {
       break;
     }
   }
   Collections.sort(lst, GeocodingUtilities.DISTANCE_COMPARATOR);
   return lst;
 }
示例#3
0
  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()) {
      if (b.getLatLon2() != null) {
        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;
        if (MapUtils.getDistance(road.searchPoint, plat, plon) < DISTANCE_BUILDING_PROXIMITY) {
          GeocodingResult bld = new GeocodingResult(street);
          bld.building = b;
          bld.connectionPoint = b.getLocation();
          streetBuildings.add(bld);
          if (!Algorithms.isEmpty(b.getName2())) {
            int fi = Algorithms.extractFirstIntegerNumber(b.getName());
            int si = Algorithms.extractFirstIntegerNumber(b.getName2());
            if (si != 0 && fi != 0) {
              int num = (int) (fi + (si - fi) * coeff);
              BuildingInterpolation type = b.getInterpolationType();
              if (type == BuildingInterpolation.EVEN || type == BuildingInterpolation.ODD) {
                if (num % 2 == (type == BuildingInterpolation.EVEN ? 1 : 0)) {
                  num--;
                }
              } else if (b.getInterpolationInterval() > 0) {
                int intv = b.getInterpolationInterval();
                if ((num - fi) % intv != 0) {
                  num = ((num - fi) / intv) * intv + fi;
                }
              }
              bld.buildingInterpolation = num + "";
            }
          }
        }

      } else if (MapUtils.getDistance(b.getLocation(), road.searchPoint)
          < DISTANCE_BUILDING_PROXIMITY) {
        GeocodingResult bld = new GeocodingResult(street);
        bld.building = b;
        bld.connectionPoint = b.getLocation();
        streetBuildings.add(bld);
      }
    }
    return streetBuildings;
  }
示例#4
0
  public List<GeocodingResult> justifyReverseGeocodingSearch(
      final GeocodingResult road,
      BinaryMapIndexReader reader,
      double knownMinBuildingDistance,
      final ResultMatcher<GeocodingResult> result)
      throws IOException {
    // test address index search
    final List<GeocodingResult> streetsList = new ArrayList<GeocodingResult>();
    final List<String> streetNamePacked = prepareStreetName(road.streetName);
    if (streetNamePacked.size() > 0) {
      log.info("Search street by name " + road.streetName + " " + streetNamePacked);
      String mainWord = "";
      for (int i = 0; i < streetNamePacked.size(); i++) {
        String s = streetNamePacked.get(i);
        if (!getSuffixesSet().contains(s) && s.length() > mainWord.length()) {
          mainWord = s;
        }
      }
      if (Algorithms.isEmpty(mainWord)) {
        mainWord = streetNamePacked.get(0);
      }
      SearchRequest<MapObject> req =
          BinaryMapIndexReader.buildAddressByNameRequest(
              new ResultMatcher<MapObject>() {
                @Override
                public boolean publish(MapObject object) {
                  if (object instanceof Street
                      && prepareStreetName(object.getName()).equals(streetNamePacked)) {
                    double d =
                        MapUtils.getDistance(
                            object.getLocation(),
                            road.searchPoint.getLatitude(),
                            road.searchPoint.getLongitude());
                    // double check to suport old format
                    if (d < DISTANCE_STREET_NAME_PROXIMITY_BY_NAME) {
                      GeocodingResult rs = new GeocodingResult(road);
                      rs.street = (Street) object;
                      // set connection point to sort
                      rs.connectionPoint = rs.street.getLocation();
                      rs.city = rs.street.getCity();
                      streetsList.add(rs);
                      return true;
                    }
                    return false;
                  }
                  return false;
                }

                @Override
                public boolean isCancelled() {
                  return result != null && result.isCancelled();
                }
              },
              mainWord,
              StringMatcherMode.CHECK_EQUALS_FROM_SPACE);
      req.setBBoxRadius(
          road.getLocation().getLatitude(),
          road.getLocation().getLongitude(),
          DISTANCE_STREET_NAME_PROXIMITY_BY_NAME);
      reader.searchAddressDataByName(req);
    }

    final List<GeocodingResult> res = new ArrayList<GeocodingResult>();
    if (streetsList.size() == 0) {
      res.add(road);
    } else {
      Collections.sort(streetsList, DISTANCE_COMPARATOR);
      double streetDistance = 0;
      for (GeocodingResult street : streetsList) {
        if (streetDistance == 0) {
          streetDistance = street.getDistance();
        } else if (street.getDistance()
            > streetDistance + DISTANCE_STREET_FROM_CLOSEST_WITH_SAME_NAME) {
          continue;
        }
        street.connectionPoint = road.connectionPoint;
        final List<GeocodingResult> streetBuildings = loadStreetBuildings(road, reader, street);
        Collections.sort(streetBuildings, DISTANCE_COMPARATOR);
        if (streetBuildings.size() > 0) {
          Iterator<GeocodingResult> it = streetBuildings.iterator();
          if (knownMinBuildingDistance == 0) {
            GeocodingResult firstBld = it.next();
            knownMinBuildingDistance = firstBld.getDistance();
            res.add(firstBld);
          }
          while (it.hasNext()) {
            GeocodingResult nextBld = it.next();
            if (nextBld.getDistance()
                > knownMinBuildingDistance * THRESHOLD_MULTIPLIER_SKIP_BUILDINGS_AFTER) {
              break;
            }
            res.add(nextBld);
          }
        }
        res.add(street);
      }
    }
    Collections.sort(res, DISTANCE_COMPARATOR);
    return res;
  }