Search in sources :

Example 51 with BinaryMapIndexReader

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

the class OsmandRegions method prepareFile.

public BinaryMapIndexReader prepareFile(String fileName) throws IOException {
    reader = new BinaryMapIndexReader(new RandomAccessFile(fileName, "r"), new File(fileName));
    // final Collator clt = OsmAndCollator.primaryCollator();
    final Map<String, String> parentRelations = new LinkedHashMap<String, String>();
    final ResultMatcher<BinaryMapDataObject> resultMatcher = new ResultMatcher<BinaryMapDataObject>() {

        @Override
        public boolean publish(BinaryMapDataObject object) {
            initTypes(object);
            int[] types = object.getTypes();
            for (int i = 0; i < types.length; i++) {
                TagValuePair tp = object.getMapIndex().decodeType(types[i]);
                if ("boundary".equals(tp.value)) {
                    return false;
                }
            }
            WorldRegion rd = initRegionData(parentRelations, object);
            if (rd == null) {
                return false;
            }
            if (rd.regionDownloadName != null) {
                downloadNamesToFullNames.put(rd.regionDownloadName, rd.regionFullName);
            }
            fullNamesToRegionData.put(rd.regionFullName, rd);
            return false;
        }

        @Override
        public boolean isCancelled() {
            return false;
        }
    };
    iterateOverAllObjects(resultMatcher);
    // post process download names
    for (Map.Entry<String, String> e : parentRelations.entrySet()) {
        String fullName = e.getKey();
        String parentFullName = e.getValue();
        // String parentParentFulName = parentRelations.get(parentFullName); // could be used for japan/russia
        WorldRegion rd = fullNamesToRegionData.get(fullName);
        WorldRegion parent = fullNamesToRegionData.get(parentFullName);
        if (parent != null && rd != null) {
            parent.addSubregion(rd);
        }
    }
    structureWorldRegions(new ArrayList<WorldRegion>(fullNamesToRegionData.values()));
    return reader;
}
Also used : BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) ResultMatcher(net.osmand.ResultMatcher) LinkedHashMap(java.util.LinkedHashMap) RandomAccessFile(java.io.RandomAccessFile) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) TagValuePair(net.osmand.binary.BinaryMapIndexReader.TagValuePair) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 52 with BinaryMapIndexReader

use of net.osmand.binary.BinaryMapIndexReader 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)

Example 53 with BinaryMapIndexReader

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

the class CurrentPositionHelper method initCtx.

private void initCtx(OsmandApplication app, List<BinaryMapReaderResource> checkReaders, @NonNull ApplicationMode appMode) {
    am = appMode;
    String p;
    if (am.isDerivedRoutingFrom(ApplicationMode.BICYCLE)) {
        p = GeneralRouterProfile.BICYCLE.name().toLowerCase();
    } else if (am.isDerivedRoutingFrom(ApplicationMode.PEDESTRIAN)) {
        p = GeneralRouterProfile.PEDESTRIAN.name().toLowerCase();
    } else if (am.isDerivedRoutingFrom(ApplicationMode.CAR)) {
        p = GeneralRouterProfile.CAR.name().toLowerCase();
    } else {
        p = "geocoding";
    }
    BinaryMapIndexReader[] rs = new BinaryMapIndexReader[checkReaders.size()];
    if (rs.length > 0) {
        int i = 0;
        for (BinaryMapReaderResource rep : checkReaders) {
            rs[i++] = rep.getReader(BinaryMapReaderResourceType.STREET_LOOKUP);
        }
        RoutingConfiguration cfg = app.getDefaultRoutingConfig().build(p, 10, new HashMap<String, String>());
        ctx = new RoutePlannerFrontEnd(false).buildRoutingContext(cfg, null, rs);
        RoutingConfiguration defCfg = app.getDefaultRoutingConfig().build("geocoding", 10, new HashMap<String, String>());
        defCtx = new RoutePlannerFrontEnd(false).buildRoutingContext(defCfg, null, rs);
    } else {
        ctx = null;
        defCtx = null;
    }
    usedReaders = checkReaders;
}
Also used : BinaryMapReaderResource(net.osmand.plus.resources.ResourceManager.BinaryMapReaderResource) RoutingConfiguration(net.osmand.router.RoutingConfiguration) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) RoutePlannerFrontEnd(net.osmand.router.RoutePlannerFrontEnd)

Example 54 with BinaryMapIndexReader

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

the class TestRouting method runAllTests.

public static boolean runAllTests(Parameters params, NativeLibrary lib) throws FileNotFoundException, IOException, Exception {
    BinaryMapIndexReader[] rs = collectFiles(params.obfDir.getAbsolutePath());
    boolean allSuccess = true;
    for (File f : params.tests) {
        System.out.println("Before test " + f.getAbsolutePath());
        System.out.flush();
        allSuccess &= test(lib, new FileInputStream(f), rs, params.configBuilder);
    }
    return allSuccess;
}
Also used : BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 55 with BinaryMapIndexReader

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

the class SearchPhrase method getDiffsByRegion.

private Map<String, List<BinaryMapIndexReader>> getDiffsByRegion() {
    Map<String, List<BinaryMapIndexReader>> result = new HashMap<>();
    Iterator<BinaryMapIndexReader> it = indexes.iterator();
    while (it.hasNext()) {
        BinaryMapIndexReader r = it.next();
        if (r == null || r.getFile() == null) {
            continue;
        }
        String filename = r.getFile().getName();
        if (filename.matches("([a-zA-Z-]+_)+([0-9]+_){2}[0-9]+\\.obf")) {
            String currRegionName = r.getRegionName();
            if (result.containsKey(currRegionName)) {
                result.get(currRegionName).add(r);
            } else {
                result.put(currRegionName, new ArrayList<>(Arrays.asList(r)));
            }
            it.remove();
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)74 File (java.io.File)41 RandomAccessFile (java.io.RandomAccessFile)39 ArrayList (java.util.ArrayList)35 IOException (java.io.IOException)23 LinkedHashMap (java.util.LinkedHashMap)11 MapIndex (net.osmand.binary.BinaryMapIndexReader.MapIndex)11 List (java.util.List)10 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)10 RouteRegion (net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion)10 RoutePlannerFrontEnd (net.osmand.router.RoutePlannerFrontEnd)10 LatLon (net.osmand.data.LatLon)9 RoutingConfiguration (net.osmand.router.RoutingConfiguration)9 FileOutputStream (java.io.FileOutputStream)8 BinaryIndexPart (net.osmand.binary.BinaryIndexPart)8 AddressRegion (net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion)8 PoiRegion (net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)8 HashMap (java.util.HashMap)7 City (net.osmand.data.City)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)6