Search in sources :

Example 61 with BinaryMapIndexReader

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

the class IndexUploader method extractRoadOnlyFile.

public static void extractRoadOnlyFile(File mainFile, File roadOnlyFile) throws IOException, RTreeException {
    RandomAccessFile raf = new RandomAccessFile(mainFile, "r");
    BinaryMapIndexReader index = new BinaryMapIndexReader(raf, mainFile);
    final RandomAccessFile routf = new RandomAccessFile(roadOnlyFile, "rw");
    routf.setLength(0);
    CodedOutputStream ous = CodedOutputStream.newInstance(new OutputStream() {

        @Override
        public void write(int b) throws IOException {
            routf.write(b);
        }

        @Override
        public void write(byte[] b) throws IOException {
            routf.write(b);
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            routf.write(b, off, len);
        }
    });
    byte[] BUFFER_TO_READ = new byte[BUFFER_SIZE];
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSION_FIELD_NUMBER, index.getVersion());
    ous.writeInt64(OsmandOdb.OsmAndStructure.DATECREATED_FIELD_NUMBER, index.getDateCreated());
    for (int i = 0; i < index.getIndexes().size(); i++) {
        BinaryIndexPart part = index.getIndexes().get(i);
        if (part instanceof MapIndex) {
            // copy only part of map index
            copyMapIndex(roadOnlyFile, (MapIndex) part, index, ous, raf, routf);
            continue;
        } else if (part instanceof AddressRegion) {
            ous.writeTag(OsmandOdb.OsmAndStructure.ADDRESSINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
        } else if (part instanceof TransportIndex) {
            ous.writeTag(OsmandOdb.OsmAndStructure.TRANSPORTINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
        } else if (part instanceof PoiRegion) {
            ous.writeTag(OsmandOdb.OsmAndStructure.POIINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
        } else if (part instanceof RouteRegion) {
            ous.writeTag(OsmandOdb.OsmAndStructure.ROUTINGINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
        } else {
            throw new UnsupportedOperationException();
        }
        BinaryMerger.writeInt(ous, part.getLength());
        BinaryMerger.copyBinaryPart(ous, BUFFER_TO_READ, raf, part.getFilePointer(), part.getLength());
    }
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSIONCONFIRM_FIELD_NUMBER, index.getVersion());
    ous.flush();
    routf.close();
    raf.close();
}
Also used : BinaryIndexPart(net.osmand.binary.BinaryIndexPart) CodedOutputStream(com.google.protobuf.CodedOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) CodedOutputStream(com.google.protobuf.CodedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex) AddressRegion(net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion) TransportIndex(net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex) PoiRegion(net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)

Example 62 with BinaryMapIndexReader

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

the class IndexUploader method checkfileAndGetDescription.

private String checkfileAndGetDescription(File mainFile) throws OneFileException, IOException, RTreeException {
    String fileName = mainFile.getName();
    boolean srtmFile = mainFile.getName().contains(".srtm") || mainFile.getName().contains(".srtmf");
    boolean roadFile = mainFile.getName().contains(".road");
    boolean wikiFile = mainFile.getName().contains(".wiki");
    boolean travelFile = mainFile.getName().contains(".travel");
    boolean worldFile = fileName.toLowerCase().contains("basemap") || fileName.toLowerCase().contains("world");
    boolean regionFile = !srtmFile && !roadFile && !wikiFile && !worldFile && !travelFile;
    if (srtmFile != this.srtmProcess) {
        return null;
    }
    if (roadFile != this.roadProcess) {
        return null;
    }
    if (wikiFile != this.wikiProcess) {
        return null;
    }
    if (travelFile != this.travelProcess) {
        return null;
    }
    if (regionFile && !fileName.contains("_ext_")) {
        extractRoadOnlyFile(mainFile, new File(mainFile.getParentFile(), fileName.replace(IndexConstants.BINARY_MAP_INDEX_EXT, IndexConstants.BINARY_ROAD_MAP_INDEX_EXT)));
    }
    if (fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT) || fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT_ZIP)) {
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile(mainFile, "r");
            BinaryMapIndexReader reader = new BinaryMapIndexReader(raf, mainFile);
            if (reader.getVersion() != IndexConstants.BINARY_MAP_VERSION) {
                throw new OneFileException("Uploader version is not compatible " + reader.getVersion() + " to current " + IndexConstants.BINARY_MAP_VERSION);
            }
            String summary = getDescription(reader, fileName);
            reader.close();
            mainFile.setLastModified(reader.getDateCreated());
            return summary;
        } catch (IOException e) {
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e1) {
                }
            }
            throw new OneFileException("Reader could not read the index: " + e.getMessage());
        }
    } else {
        throw new OneFileException("Not supported file format " + fileName);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 63 with BinaryMapIndexReader

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

the class WikivoyageDataGenerator method addCitiesData.

private void addCitiesData(File citiesObf, Connection conn) throws FileNotFoundException, IOException, SQLException {
    addColumns(conn);
    if (citiesObf != null) {
        RandomAccessFile raf = new RandomAccessFile(citiesObf, "r");
        BinaryMapIndexReader reader = new BinaryMapIndexReader(raf, citiesObf);
        PreparedStatement stat = conn.prepareStatement("SELECT title, lat, lon FROM travel_articles");
        int columns = getRowCount(conn);
        int count = 0;
        ResultSet rs = stat.executeQuery();
        Map<String, List<Amenity>> cities = fetchCities(reader);
        while (rs.next()) {
            String title = rs.getString(1);
            String searchTitle = title.replaceAll("\\(.*\\)", "").trim();
            long lat = rs.getLong(2);
            long lon = rs.getLong(3);
            LatLon ll = new LatLon(lat, lon);
            if (lat == 0 && lon == 0) {
                continue;
            }
            List<Amenity> results = cities.get(searchTitle);
            Amenity acceptedResult = (results != null && results.size() == 1) ? results.get(0) : getClosestMatch(results, ll);
            insertData(conn, title, acceptedResult, ll);
            if (count++ % BATCH_SIZE == 0) {
                System.out.format("%.2f", (((double) count / (double) columns) * 100d));
                System.out.println("%");
            }
        }
        stat.close();
        raf.close();
    }
}
Also used : LatLon(net.osmand.data.LatLon) Amenity(net.osmand.data.Amenity) RandomAccessFile(java.io.RandomAccessFile) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List)

Example 64 with BinaryMapIndexReader

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

the class OsmAndMapsService method getObfReaders.

public synchronized BinaryMapIndexReader[] getObfReaders(QuadRect quadRect) throws IOException {
    initObfReaders();
    List<BinaryMapIndexReader> files = new ArrayList<>();
    for (BinaryMapIndexReaderReference ref : obfFiles.values()) {
        boolean intersects = false;
        mainLoop: for (RoutingPart rp : ref.fileIndex.getRoutingIndexList()) {
            for (RoutingSubregion s : rp.getSubregionsList()) {
                intersects = quadRect.left <= s.getRight() && quadRect.right >= s.getLeft() && quadRect.top <= s.getBottom() && quadRect.bottom >= s.getTop();
                if (intersects) {
                    if (ref.reader == null) {
                        long val = System.currentTimeMillis();
                        // $NON-NLS-1$ //$NON-NLS-2$
                        RandomAccessFile raf = new RandomAccessFile(ref.file, "r");
                        ref.reader = cacheFiles.initReaderFromFileIndex(ref.fileIndex, raf, ref.file);
                        LOGGER.info("Initializing routing file " + ref.file.getName() + " " + (System.currentTimeMillis() - val) + " ms");
                    }
                    files.add(ref.reader);
                    break mainLoop;
                }
            }
        }
    }
    ;
    return files.toArray(new BinaryMapIndexReader[files.size()]);
}
Also used : RoutingPart(net.osmand.binary.OsmandIndex.RoutingPart) RoutingSubregion(net.osmand.binary.OsmandIndex.RoutingSubregion) RandomAccessFile(java.io.RandomAccessFile) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) ArrayList(java.util.ArrayList)

Example 65 with BinaryMapIndexReader

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

the class OsmAndMapsService 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);
        }
    }
    su.filterDuplicateRegionResults(complete);
    return minBuildingDistance;
}
Also used : GeocodingResult(net.osmand.binary.GeocodingUtilities.GeocodingResult) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader)

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