Search in sources :

Example 1 with OsmBaseStoragePbf

use of net.osmand.osm.io.OsmBaseStoragePbf in project OsmAnd-tools by osmandapp.

the class IndexCreator method extractOsmToNodesDB.

private OsmDbCreator extractOsmToNodesDB(OsmDbAccessor accessor, File readFile, IProgress progress, IOsmStorageFilter addFilter, int additionId, int shiftId, boolean ovewriteIds, boolean generateNewIds, boolean createTables, OsmDbCreator previous) throws IOException, SQLException, XmlPullParserException {
    boolean pbfFile = false;
    InputStream stream = new BufferedInputStream(new FileInputStream(readFile), 8192 * 4);
    InputStream streamFile = stream;
    long st = System.currentTimeMillis();
    if (readFile.getName().endsWith(".bz2")) {
        // $NON-NLS-1$
        if (stream.read() != 'B' || stream.read() != 'Z') {
        // throw new RuntimeException("The source stream must start with the characters BZ if it is to be read as a BZip2 stream."); //$NON-NLS-1$
        } else {
            stream = new CBZip2InputStream(stream);
        }
    } else if (readFile.getName().endsWith(".gz")) {
        // $NON-NLS-1$
        stream = new GZIPInputStream(stream);
    } else if (readFile.getName().endsWith(".pbf")) {
        // $NON-NLS-1$
        pbfFile = true;
    }
    OsmBaseStorage storage = pbfFile ? new OsmBaseStoragePbf() : new OsmBaseStorage();
    storage.setSupressWarnings(DataExtractionSettings.getSettings().isSupressWarningsForDuplicatedId());
    if (addFilter != null) {
        storage.getFilters().add(addFilter);
    }
    storage.getFilters().add(new IOsmStorageFilter() {

        @Override
        public boolean acceptEntityToLoad(OsmBaseStorage storage, EntityId entityId, Entity entity) {
            if (indexAddressCreator != null) {
                indexAddressCreator.registerCityIfNeeded(entity);
            }
            // accept to allow db creator parse it
            return true;
        }
    });
    // 1. Loading osm file
    OsmDbCreator dbCreator = new OsmDbCreator(additionId, shiftId, ovewriteIds, generateNewIds);
    if (previous != null) {
        dbCreator.setNodeIds(previous.getNodeIds());
        dbCreator.setWayIds(previous.getWayIds());
        dbCreator.setRelationIds(previous.getRelationIds());
    }
    dbCreator.setBackwardCompatibleIds(backwardCompatibleIds);
    try {
        // $NON-NLS-1$
        setGeneralProgress(progress, "[15 / 100]");
        // $NON-NLS-1$
        progress.startTask(Messages.getString("IndexCreator.LOADING_FILE") + readFile.getAbsolutePath(), -1);
        // 1 init database to store temporary data
        dbCreator.initDatabase(osmDBdialect, accessor.getDbConn(), createTables);
        storage.getFilters().add(dbCreator);
        if (pbfFile) {
            ((OsmBaseStoragePbf) storage).parseOSMPbf(stream, progress, false);
        } else {
            storage.parseOSM(stream, progress, streamFile, false);
        }
        dbCreator.finishLoading();
        osmDBdialect.commitDatabase(accessor.getDbConn());
        if (log.isInfoEnabled()) {
            // $NON-NLS-1$
            log.info("File parsed : " + (System.currentTimeMillis() - st));
        }
        progress.finishTask();
        return dbCreator;
    } finally {
        if (log.isInfoEnabled()) {
            // $NON-NLS-1$
            log.info("File indexed : " + (System.currentTimeMillis() - st));
        }
    }
}
Also used : Entity(net.osmand.osm.edit.Entity) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OsmBaseStoragePbf(net.osmand.osm.io.OsmBaseStoragePbf) IOsmStorageFilter(net.osmand.osm.io.IOsmStorageFilter) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) EntityId(net.osmand.osm.edit.Entity.EntityId) BufferedInputStream(java.io.BufferedInputStream) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 Entity (net.osmand.osm.edit.Entity)1 EntityId (net.osmand.osm.edit.Entity.EntityId)1 IOsmStorageFilter (net.osmand.osm.io.IOsmStorageFilter)1 OsmBaseStorage (net.osmand.osm.io.OsmBaseStorage)1 OsmBaseStoragePbf (net.osmand.osm.io.OsmBaseStoragePbf)1 CBZip2InputStream (org.apache.tools.bzip2.CBZip2InputStream)1