Search in sources :

Example 1 with GeocodingUtilities

use of net.osmand.binary.GeocodingUtilities in project OsmAnd-tools by osmandapp.

the class MapAddressLayer method whereAmI.

private List<Entity> whereAmI(double lat, double lon, final DataTileManager<Entity> points) throws IOException {
    List<Entity> results = new ArrayList<Entity>();
    int x = MapUtils.get31TileNumberX(lon);
    int y = MapUtils.get31TileNumberY(lat);
    List<BinaryMapIndexReader> list = new ArrayList<BinaryMapIndexReader>();
    for (File f : new File(DataExtractionSettings.getSettings().getBinaryFilesDir()).listFiles()) {
        if (f.getName().endsWith(".obf")) {
            // $NON-NLS-1$ //$NON-NLS-2$
            RandomAccessFile raf = new RandomAccessFile(f, "r");
            BinaryMapIndexReader rd = new BinaryMapIndexReader(raf, f);
            if (rd.containsAddressData() && rd.containsRouteData(x, y, x, y, 15)) {
                list.add(rd);
            } else {
                rd.close();
                raf.close();
            }
        }
    }
    RoutingConfiguration cfg = DataExtractionSettings.getSettings().getRoutingConfig().build("geocoding", 100, new HashMap<String, String>());
    RoutingContext ctx = new RoutePlannerFrontEnd(false).buildRoutingContext(cfg, null, list.toArray(new BinaryMapIndexReader[list.size()]));
    GeocodingUtilities su = new GeocodingUtilities();
    double minBuildingDistance = 0;
    List<GeocodingResult> complete = new ArrayList<GeocodingUtilities.GeocodingResult>();
    List<GeocodingResult> res = su.reverseGeocodingSearch(ctx, lat, lon, false);
    minBuildingDistance = justifyResults(list, su, complete, res);
    // complete.addAll(res);
    Collections.sort(complete, GeocodingUtilities.DISTANCE_COMPARATOR);
    long lid = -1;
    for (GeocodingResult r : complete) {
        if (r.building != null && r.getDistance() > minBuildingDistance * GeocodingUtilities.THRESHOLD_MULTIPLIER_SKIP_BUILDINGS_AFTER) {
            continue;
        }
        Node n = new Node(r.getLocation().getLatitude(), r.getLocation().getLongitude(), lid--);
        n.putTag(OSMTagKey.NAME.getValue(), r.toString());
        results.add(n);
    }
    for (BinaryMapIndexReader l : list) {
        l.close();
    }
    return results;
}
Also used : Entity(net.osmand.osm.edit.Entity) GeocodingResult(net.osmand.binary.GeocodingUtilities.GeocodingResult) Node(net.osmand.osm.edit.Node) ArrayList(java.util.ArrayList) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) Point(java.awt.Point) RoutingContext(net.osmand.router.RoutingContext) RandomAccessFile(java.io.RandomAccessFile) RoutingConfiguration(net.osmand.router.RoutingConfiguration) RoutePlannerFrontEnd(net.osmand.router.RoutePlannerFrontEnd) GeocodingUtilities(net.osmand.binary.GeocodingUtilities) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 2 with GeocodingUtilities

use of net.osmand.binary.GeocodingUtilities in project Osmand by osmandapp.

the class CurrentPositionHelper method justifyResult.

private void justifyResult(List<GeocodingResult> res, final ResultMatcher<GeocodingResult> result) {
    List<GeocodingResult> complete = new ArrayList<>();
    double minBuildingDistance = 0;
    if (res != null) {
        for (GeocodingResult r : res) {
            BinaryMapIndexReader foundRepo = null;
            List<BinaryMapIndexReader> rts = usedReaders;
            for (BinaryMapIndexReader reader : rts) {
                for (RouteRegion rb : reader.getRoutingIndexes()) {
                    if (r.regionFP == rb.getFilePointer() && r.regionLen == rb.getLength()) {
                        foundRepo = reader;
                        break;
                    }
                }
            }
            if (result.isCancelled()) {
                break;
            } else if (foundRepo != null) {
                List<GeocodingResult> justified = null;
                try {
                    justified = new GeocodingUtilities().justifyReverseGeocodingSearch(r, foundRepo, minBuildingDistance, result);
                } catch (IOException e) {
                    log.error("Exception happened during reverse geocoding", e);
                    e.printStackTrace();
                }
                if (justified != null && !justified.isEmpty()) {
                    double md = justified.get(0).getDistance();
                    if (minBuildingDistance == 0) {
                        minBuildingDistance = md;
                    } else {
                        minBuildingDistance = Math.min(md, minBuildingDistance);
                    }
                    complete.addAll(justified);
                }
            } else {
                complete.add(r);
            }
        }
    }
    if (result.isCancelled()) {
        app.runInUIThread(new Runnable() {

            public void run() {
                result.publish(null);
            }
        });
        return;
    }
    Collections.sort(complete, GeocodingUtilities.DISTANCE_COMPARATOR);
    // for(GeocodingResult rt : complete) {
    // System.out.println(rt.toString());
    // }
    final GeocodingResult rts = complete.size() > 0 ? complete.get(0) : new GeocodingResult();
    app.runInUIThread(new Runnable() {

        public void run() {
            result.publish(rts);
        }
    });
}
Also used : GeocodingResult(net.osmand.binary.GeocodingUtilities.GeocodingResult) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) ArrayList(java.util.ArrayList) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) GeocodingUtilities(net.osmand.binary.GeocodingUtilities)

Example 3 with GeocodingUtilities

use of net.osmand.binary.GeocodingUtilities in project Osmand by osmandapp.

the class CurrentPositionHelper method justifyResult.

private void justifyResult(List<GeocodingResult> res, final ResultMatcher<GeocodingResult> result) {
    List<GeocodingResult> complete = new ArrayList<>();
    double minBuildingDistance = 0;
    if (res != null) {
        for (GeocodingResult r : res) {
            BinaryMapIndexReader foundRepo = null;
            List<BinaryMapReaderResource> rts = usedReaders;
            for (BinaryMapReaderResource rt : rts) {
                if (rt.isClosed()) {
                    continue;
                }
                BinaryMapIndexReader reader = rt.getReader(BinaryMapReaderResourceType.STREET_LOOKUP);
                for (RouteRegion rb : reader.getRoutingIndexes()) {
                    if (r.regionFP == rb.getFilePointer() && r.regionLen == rb.getLength()) {
                        foundRepo = reader;
                        break;
                    }
                }
            }
            if (result.isCancelled()) {
                break;
            } else if (foundRepo != null) {
                List<GeocodingResult> justified = null;
                try {
                    justified = new GeocodingUtilities().justifyReverseGeocodingSearch(r, foundRepo, minBuildingDistance, result);
                } catch (IOException e) {
                    log.error("Exception happened during reverse geocoding", e);
                    e.printStackTrace();
                }
                if (justified != null && !justified.isEmpty()) {
                    double md = justified.get(0).getDistance();
                    if (minBuildingDistance == 0) {
                        minBuildingDistance = md;
                    } else {
                        minBuildingDistance = Math.min(md, minBuildingDistance);
                    }
                    complete.addAll(justified);
                }
            } else {
                complete.add(r);
            }
        }
    }
    if (result.isCancelled()) {
        app.runInUIThread(new Runnable() {

            public void run() {
                result.publish(null);
            }
        });
        return;
    }
    Collections.sort(complete, GeocodingUtilities.DISTANCE_COMPARATOR);
    // for(GeocodingResult rt : complete) {
    // System.out.println(rt.toString());
    // }
    final GeocodingResult rts = complete.size() > 0 ? complete.get(0) : new GeocodingResult();
    app.runInUIThread(new Runnable() {

        public void run() {
            result.publish(rts);
        }
    });
}
Also used : BinaryMapReaderResource(net.osmand.plus.resources.ResourceManager.BinaryMapReaderResource) GeocodingResult(net.osmand.binary.GeocodingUtilities.GeocodingResult) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) ArrayList(java.util.ArrayList) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) GeocodingUtilities(net.osmand.binary.GeocodingUtilities)

Aggregations

ArrayList (java.util.ArrayList)3 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)3 GeocodingUtilities (net.osmand.binary.GeocodingUtilities)3 GeocodingResult (net.osmand.binary.GeocodingUtilities.GeocodingResult)3 IOException (java.io.IOException)2 List (java.util.List)2 RouteRegion (net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion)2 Point (java.awt.Point)1 File (java.io.File)1 RandomAccessFile (java.io.RandomAccessFile)1 Entity (net.osmand.osm.edit.Entity)1 Node (net.osmand.osm.edit.Node)1 BinaryMapReaderResource (net.osmand.plus.resources.ResourceManager.BinaryMapReaderResource)1 RoutePlannerFrontEnd (net.osmand.router.RoutePlannerFrontEnd)1 RoutingConfiguration (net.osmand.router.RoutingConfiguration)1 RoutingContext (net.osmand.router.RoutingContext)1