Search in sources :

Example 66 with BinaryMapIndexReader

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

the class OsmAndMapsService method geocoding.

public synchronized List<GeocodingResult> geocoding(double lat, double lon) throws IOException, InterruptedException {
    List<GeocodingResult> results = new ArrayList<>();
    QuadRect points = points(null, new LatLon(lat, lon), new LatLon(lat, lon));
    List<BinaryMapIndexReader> list = Arrays.asList(getObfReaders(points));
    RoutePlannerFrontEnd router = new RoutePlannerFrontEnd();
    RoutingContext ctx = prepareRouterContext("geocoding", points, router, null);
    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);
    // Collections.sort(complete, GeocodingUtilities.DISTANCE_COMPARATOR);
    for (GeocodingResult r : complete) {
        if (r.building != null && r.getDistance() > minBuildingDistance * GeocodingUtilities.THRESHOLD_MULTIPLIER_SKIP_BUILDINGS_AFTER) {
            continue;
        }
        results.add(r);
    }
    return results;
}
Also used : LatLon(net.osmand.data.LatLon) RoutingContext(net.osmand.router.RoutingContext) GeocodingResult(net.osmand.binary.GeocodingUtilities.GeocodingResult) ArrayList(java.util.ArrayList) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) RoutePlannerFrontEnd(net.osmand.router.RoutePlannerFrontEnd) GeocodingUtilities(net.osmand.binary.GeocodingUtilities) QuadRect(net.osmand.data.QuadRect)

Example 67 with BinaryMapIndexReader

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

the class SearchDataCreator method merger.

public void merger(String[] args) throws IOException, SQLException {
    if (args == null || args.length == 0) {
        System.out.println(helpMessage);
        return;
    }
    File outputFile = null;
    String outputFilePath = null;
    List<BinaryMapIndexReader> readers = new ArrayList<BinaryMapIndexReader>();
    Set<Integer> combineParts = new HashSet<Integer>();
    for (int i = 0; i < args.length; i++) {
        if (args[i].startsWith("--")) {
            combineParts.add(COMBINE_ARGS.get(args[i]));
        } else if (outputFile == null) {
            outputFilePath = args[i];
            outputFile = new File(outputFilePath);
        } else {
            File file = new File(args[i]);
            if (file.exists() && file.getName().endsWith(".json")) {
                BinaryMapIndexReader reader = BinaryMapIndexTestReader.buildTestReader(file);
                readers.add(reader);
            }
        }
    }
    if (combineParts.isEmpty()) {
        combineParts.addAll(COMBINE_ARGS.values());
    }
    if (outputFile.exists()) {
        if (!outputFile.delete()) {
            throw new IOException("Cannot delete file " + outputFile);
        }
    }
    if (readers.isEmpty()) {
        throw new IOException("No data for merge");
    }
    combineParts(outputFile, null, readers, combineParts);
    FileInputStream inputStream = new FileInputStream(outputFile);
    File outFile = new File(outputFilePath + ".gz");
    GZIPOutputStream outputStream = new GZIPOutputStream(new FileOutputStream(outFile));
    Algorithms.streamCopy(inputStream, outputStream);
    outputStream.close();
    inputStream.close();
    if (!outputFile.delete()) {
        throw new IOException("Cannot delete file " + outputFile);
    }
}
Also used : BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) HashSet(java.util.HashSet)

Example 68 with BinaryMapIndexReader

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

the class BinaryMerger method mergeCitiesByNameDistance.

private void mergeCitiesByNameDistance(List<City> orderedCities, Map<City, List<City>> mergeGroup, Map<City, BinaryMapIndexReader> cityMap, boolean rename) {
    for (int i = 0; i < orderedCities.size() - 1; i++) {
        int j = i;
        City oc = orderedCities.get(i);
        City nc = orderedCities.get(j);
        BinaryMapIndexReader ocIndexReader = cityMap.get(oc);
        List<City> uniqueNamesakes = new ArrayList<City>();
        boolean renameGroup = false;
        while (MapObject.BY_NAME_COMPARATOR.areEqual(nc, oc)) {
            boolean isUniqueCity = true;
            for (ListIterator<City> uci = uniqueNamesakes.listIterator(); uci.hasNext(); ) {
                City uc = uci.next();
                if (isSameCity(uc, nc)) {
                    // Prefer cities with shortest names ("1101DL" instead of "1101 DL")
                    boolean shorter = nc.getName().length() < uc.getName().length();
                    if (shorter) {
                        mergeGroup.put(nc, mergeGroup.remove(uc));
                        uniqueNamesakes.remove(uc);
                        uniqueNamesakes.add(nc);
                        City tmp = uc;
                        uc = nc;
                        nc = tmp;
                    }
                    orderedCities.remove(nc);
                    mergeGroup.get(uc).add(nc);
                    isUniqueCity = false;
                    break;
                }
            }
            if (isUniqueCity) {
                uniqueNamesakes.add(nc);
                mergeGroup.put(nc, new ArrayList<City>());
                j++;
            }
            boolean areCitiesInSameRegion = ocIndexReader == cityMap.get(nc);
            renameGroup = renameGroup || (rename && !areCitiesInSameRegion && uniqueNamesakes.size() > 1);
            nc = (j < orderedCities.size()) ? orderedCities.get(j) : null;
        }
        if (uniqueNamesakes.size() == 1 && mergeGroup.get(uniqueNamesakes.get(0)).size() == 0) {
            mergeGroup.remove(uniqueNamesakes.get(0));
        } else {
            if (renameGroup) {
                for (City uc : uniqueNamesakes) {
                    for (City c : mergeGroup.get(uc)) {
                        addRegionToCityName(c, cityMap.get(c));
                    }
                    addRegionToCityName(uc, cityMap.get(uc));
                }
            }
        }
    }
}
Also used : BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) ArrayList(java.util.ArrayList) City(net.osmand.data.City)

Example 69 with BinaryMapIndexReader

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

the class BinaryMerger method combineParts.

public void combineParts(File fileToExtract, List<File> files, List<BinaryMapIndexReader> readers, Set<Integer> combineParts) throws IOException, SQLException {
    boolean combineFiles = files != null;
    BinaryMapIndexReader[] indexes = combineFiles ? new BinaryMapIndexReader[files.size()] : readers.toArray(new BinaryMapIndexReader[readers.size()]);
    RandomAccessFile[] rafs = combineFiles ? new RandomAccessFile[files.size()] : null;
    long dateCreated = 0;
    int version = -1;
    // Go through all files and validate consistency
    int c = 0;
    if (combineFiles) {
        for (File f : files) {
            if (f.getAbsolutePath().equals(fileToExtract.getAbsolutePath())) {
                System.err.println("Error : Input file is equal to output file " + f.getAbsolutePath());
                return;
            }
            rafs[c] = new RandomAccessFile(f.getAbsolutePath(), "r");
            indexes[c] = new BinaryMapIndexReader(rafs[c], f);
            dateCreated = Math.max(dateCreated, indexes[c].getDateCreated());
            if (version == -1) {
                version = indexes[c].getVersion();
            } else {
                if (indexes[c].getVersion() != version) {
                    System.err.println("Error : Different input files has different input versions " + indexes[c].getVersion() + " != " + version);
                    return;
                }
            }
            c++;
        }
    } else {
        for (BinaryMapIndexReader index : indexes) {
            dateCreated = Math.max(dateCreated, index.getDateCreated());
            if (version == -1) {
                version = index.getVersion();
            } else {
                if (index.getVersion() != version) {
                    System.err.println("Error : Different input files has different input versions " + index.getVersion() + " != " + version);
                    return;
                }
            }
        }
    }
    // write files
    RandomAccessFile rafToExtract = new RandomAccessFile(fileToExtract, "rw");
    BinaryMapIndexWriter writer = new BinaryMapIndexWriter(rafToExtract, dateCreated);
    CodedOutputStream ous = writer.getCodedOutStream();
    byte[] BUFFER_TO_READ = new byte[BUFFER_SIZE];
    AddressRegion[] addressRegions = new AddressRegion[combineFiles ? files.size() : readers.size()];
    PoiRegion[] poiRegions = new PoiRegion[combineFiles ? files.size() : readers.size()];
    for (int k = 0; k < indexes.length; k++) {
        BinaryMapIndexReader index = indexes[k];
        RandomAccessFile raf = rafs != null ? rafs[k] : null;
        for (int i = 0; i < index.getIndexes().size(); i++) {
            BinaryIndexPart part = index.getIndexes().get(i);
            if (combineParts.contains(part.getFieldNumber())) {
                if (part.getFieldNumber() == OsmandOdb.OsmAndStructure.ADDRESSINDEX_FIELD_NUMBER) {
                    addressRegions[k] = (AddressRegion) part;
                } else if (part.getFieldNumber() == OsmandOdb.OsmAndStructure.POIINDEX_FIELD_NUMBER) {
                    poiRegions[k] = (PoiRegion) part;
                }
            } else if (raf != null) {
                ous.writeTag(part.getFieldNumber(), WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
                writeInt(ous, part.getLength());
                copyBinaryPart(ous, BUFFER_TO_READ, raf, part.getFilePointer(), part.getLength());
                System.out.println(MessageFormat.format("{2} part {0} is extracted {1} bytes", new Object[] { part.getName(), part.getLength(), part.getPartName() }));
            }
        }
    }
    String nm = fileToExtract.getName();
    int i = nm.indexOf('_');
    if (i > 0) {
        nm = nm.substring(0, i);
    }
    if (combineParts.contains(OsmandOdb.OsmAndStructure.ADDRESSINDEX_FIELD_NUMBER)) {
        combineAddressIndex(nm, writer, addressRegions, indexes);
    }
    if (combineParts.contains(OsmandOdb.OsmAndStructure.POIINDEX_FIELD_NUMBER)) {
        combinePoiIndex(nm, writer, dateCreated, poiRegions, indexes);
    }
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSIONCONFIRM_FIELD_NUMBER, version);
    ous.flush();
}
Also used : BinaryIndexPart(net.osmand.binary.BinaryIndexPart) CodedOutputStream(com.google.protobuf.CodedOutputStream) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) RandomAccessFile(java.io.RandomAccessFile) BinaryMapIndexWriter(net.osmand.obf.preparation.BinaryMapIndexWriter) AddressRegion(net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) PoiRegion(net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)

Example 70 with BinaryMapIndexReader

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

the class BinaryMerger method combinePoiIndex.

private void combinePoiIndex(String name, BinaryMapIndexWriter writer, long dateCreated, PoiRegion[] poiRegions, BinaryMapIndexReader[] indexes) throws IOException, SQLException {
    final int[] writtenPoiCount = { 0 };
    MapRenderingTypesEncoder renderingTypes = new MapRenderingTypesEncoder(null, name);
    boolean overwriteIds = false;
    IndexCreatorSettings settings = new IndexCreatorSettings();
    settings.indexPOI = true;
    final IndexPoiCreator indexPoiCreator = new IndexPoiCreator(settings, renderingTypes, overwriteIds);
    indexPoiCreator.createDatabaseStructure(new File(new File(System.getProperty("user.dir")), IndexCreator.getPoiFileName(name)));
    final Map<Long, List<Amenity>> amenityRelations = new HashMap<Long, List<Amenity>>();
    final TLongHashSet set = new TLongHashSet();
    final long[] generatedRelationId = { -1 };
    for (int i = 0; i < poiRegions.length; i++) {
        BinaryMapIndexReader index = indexes[i];
        final TLongHashSet file = new TLongHashSet();
        log.info("Region: " + extractRegionName(index));
        index.searchPoi(BinaryMapIndexReader.buildSearchPoiRequest(0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, -1, BinaryMapIndexReader.ACCEPT_ALL_POI_TYPE_FILTER, new ResultMatcher<Amenity>() {

            @Override
            public boolean publish(Amenity amenity) {
                try {
                    boolean isRelation = amenity.getId() < 0;
                    if (isRelation) {
                        long j = latlon(amenity);
                        List<Amenity> list;
                        if (!amenityRelations.containsKey(j)) {
                            list = new ArrayList<Amenity>(1);
                            amenityRelations.put(j, list);
                        } else {
                            list = amenityRelations.get(j);
                        }
                        boolean unique = true;
                        for (Amenity a : list) {
                            if (a.getType() == amenity.getType() && Algorithms.objectEquals(a.getSubType(), amenity.getSubType())) {
                                unique = false;
                                break;
                            }
                        }
                        if (unique) {
                            amenity.setId(generatedRelationId[0]--);
                            amenityRelations.get(j).add(amenity);
                            indexPoiCreator.insertAmenityIntoPoi(amenity);
                            writtenPoiCount[0]++;
                        }
                    } else {
                        if (!set.contains(amenity.getId())) {
                            file.add(amenity.getId());
                            indexPoiCreator.insertAmenityIntoPoi(amenity);
                            writtenPoiCount[0]++;
                        }
                    }
                    return false;
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public boolean isCancelled() {
                return false;
            }
        }));
        set.addAll(file);
    }
    indexPoiCreator.writeBinaryPoiIndex(writer, name, null);
    indexPoiCreator.commitAndClosePoiFile(dateCreated);
    // REMOVE_POI_DB = false;
    if (REMOVE_POI_DB) {
        indexPoiCreator.removePoiFile();
    }
    log.info("Written " + writtenPoiCount[0] + " POI.");
}
Also used : Amenity(net.osmand.data.Amenity) IndexCreatorSettings(net.osmand.obf.preparation.IndexCreatorSettings) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) IndexPoiCreator(net.osmand.obf.preparation.IndexPoiCreator) ResultMatcher(net.osmand.ResultMatcher) MapRenderingTypesEncoder(net.osmand.osm.MapRenderingTypesEncoder) TLongHashSet(gnu.trove.set.hash.TLongHashSet) List(java.util.List) ArrayList(java.util.ArrayList) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

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