Search in sources :

Example 1 with GeocodingResult

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

the class MapAddressLayer method justifyResults.

private double justifyResults(List<BinaryMapIndexReader> list, GeocodingUtilities su, List<GeocodingResult> complete, List<GeocodingResult> res) throws IOException {
    double minBuildingDistance = 0;
    for (GeocodingResult r : res) {
        BinaryMapIndexReader reader = null;
        for (BinaryMapIndexReader b : list) {
            for (RouteRegion rb : b.getRoutingIndexes()) {
                if (r.regionFP == rb.getFilePointer() && r.regionLen == rb.getLength()) {
                    reader = b;
                    break;
                }
            }
            if (reader != null) {
                break;
            }
        }
        if (reader != null) {
            List<GeocodingResult> justified = su.justifyReverseGeocodingSearch(r, reader, minBuildingDistance, null);
            if (!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);
        }
    }
    return minBuildingDistance;
}
Also used : GeocodingResult(net.osmand.binary.GeocodingUtilities.GeocodingResult) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader)

Example 2 with GeocodingResult

use of net.osmand.binary.GeocodingUtilities.GeocodingResult 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 3 with GeocodingResult

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

the class GeocodingLookupService method geocode.

private boolean geocode(final LatLon latLon) {
    Location loc = new Location("");
    loc.setLatitude(latLon.getLatitude());
    loc.setLongitude(latLon.getLongitude());
    return app.getLocationProvider().getGeocodingResult(loc, new ResultMatcher<GeocodingResult>() {

        @Override
        public boolean publish(GeocodingResult object) {
            String result = null;
            if (object != null) {
                String lang = SampleApplication.LANGUAGE;
                boolean transliterate = SampleApplication.TRANSLITERATE;
                String geocodingResult = "";
                double relevantDistance = -1;
                if (object.building != null) {
                    String bldName = object.building.getName(lang, transliterate);
                    if (!Algorithms.isEmpty(object.buildingInterpolation)) {
                        bldName = object.buildingInterpolation;
                    }
                    geocodingResult = object.street.getName(lang, transliterate) + " " + bldName + ", " + object.city.getName(lang, transliterate);
                } else if (object.street != null) {
                    geocodingResult = object.street.getName(lang, transliterate) + ", " + object.city.getName(lang, transliterate);
                    relevantDistance = object.getDistanceP();
                } else if (object.city != null) {
                    geocodingResult = object.city.getName(lang, transliterate);
                } else if (object.point != null) {
                    RouteDataObject rd = object.point.getRoad();
                    String sname = rd.getName(lang, transliterate);
                    if (Algorithms.isEmpty(sname)) {
                        sname = "";
                    }
                    String ref = rd.getRef(lang, transliterate, true);
                    if (!Algorithms.isEmpty(ref)) {
                        if (!Algorithms.isEmpty(sname)) {
                            sname += ", ";
                        }
                        sname += ref;
                    }
                    geocodingResult = sname;
                    relevantDistance = object.getDistanceP();
                }
                result = geocodingResult;
                if (relevantDistance == -1) {
                    relevantDistance = object.getDistance();
                }
                if (!Algorithms.isEmpty(result) && relevantDistance > 100) {
                    result = app.getString("shared_string_near") + " " + result;
                }
            }
            lastFoundAddress = result;
            searchDone = true;
            return true;
        }

        @Override
        public boolean isCancelled() {
            return !hasAnyRequest(latLon);
        }
    });
}
Also used : GeocodingResult(net.osmand.binary.GeocodingUtilities.GeocodingResult) RouteDataObject(net.osmand.binary.RouteDataObject) Location(net.osmand.Location)

Example 4 with GeocodingResult

use of net.osmand.binary.GeocodingUtilities.GeocodingResult 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 5 with GeocodingResult

use of net.osmand.binary.GeocodingUtilities.GeocodingResult 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

GeocodingResult (net.osmand.binary.GeocodingUtilities.GeocodingResult)6 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)4 ArrayList (java.util.ArrayList)3 RouteRegion (net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion)3 GeocodingUtilities (net.osmand.binary.GeocodingUtilities)3 IOException (java.io.IOException)2 List (java.util.List)2 Location (net.osmand.Location)2 RouteDataObject (net.osmand.binary.RouteDataObject)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