Search in sources :

Example 1 with IndexAddressCreator

use of net.osmand.data.preparation.address.IndexAddressCreator in project OsmAnd-tools by osmandapp.

the class IndexCreator method generateIndexes.

public void generateIndexes(File[] readFile, IProgress progress, IOsmStorageFilter addFilter, MapZooms mapZooms, MapRenderingTypesEncoder renderingTypes, Log logMapDataWarn, boolean generateUniqueIds, boolean overwriteIds) throws IOException, SQLException, InterruptedException, XmlPullParserException {
    // }
    if (logMapDataWarn == null) {
        logMapDataWarn = log;
    }
    if (mapZooms == null) {
        mapZooms = MapZooms.getDefault();
    }
    // clear previous results and setting variables
    if (readFile != null && readFile.length > 0 && regionName == null) {
        int i = readFile[0].getName().indexOf('.');
        if (i > -1) {
            regionName = Algorithms.capitalizeFirstLetterAndLowercase(readFile[0].getName().substring(0, i));
        }
    }
    if (renderingTypes == null) {
        renderingTypes = new MapRenderingTypesEncoder(null, regionName);
    }
    this.indexTransportCreator = new IndexTransportCreator();
    this.indexPoiCreator = new IndexPoiCreator(renderingTypes, overwriteIds);
    this.indexAddressCreator = new IndexAddressCreator(logMapDataWarn);
    this.indexMapCreator = new IndexVectorMapCreator(logMapDataWarn, mapZooms, renderingTypes, zoomWaySmoothness);
    this.indexRouteCreator = new IndexRouteCreator(renderingTypes, logMapDataWarn, generateLowLevel);
    // init address
    indexAddressCreator.initSettings(cityAdminLevel);
    // Main generation method
    try {
        // do not create temp map file and rtree files
        if (recreateOnlyBinaryFile) {
            mapFile = new File(workingDir, getMapFileName());
            File tempDBMapFile = new File(workingDir, getTempMapDBFileName());
            mapConnection = (Connection) getDatabaseConnection(tempDBMapFile.getAbsolutePath(), mapIndexDBDialect);
            mapConnection.setAutoCommit(false);
            try {
                if (indexMap) {
                    indexMapCreator.createRTreeFiles(getRTreeMapIndexPackFileName());
                }
                if (indexRouting) {
                    indexRouteCreator.createRTreeFiles(getRTreeRouteIndexPackFileName());
                }
                if (indexTransport) {
                    indexTransportCreator.createRTreeFile(getRTreeTransportStopsPackFileName());
                }
            } catch (RTreeException e) {
                // $NON-NLS-1$
                log.error("Error flushing", e);
                throw new IOException(e);
            }
        } else {
            // 2. Create index connections and index structure
            createDatabaseIndexesStructure();
            OsmDbAccessor accessor = initDbAccessor(readFile, progress, addFilter, generateUniqueIds, overwriteIds, false);
            // 3. Processing all entries
            // 3.1 write all cities
            writeAllCities(accessor, progress);
            // 3.2 index address relations
            indexRelations(accessor, progress);
            // 3.3 MAIN iterate over all entities
            iterateMainEntities(accessor, progress);
            accessor.closeReadingConnection();
            // do not delete first db connection
            if (accessor.getDbConn() != null) {
                osmDBdialect.commitDatabase(accessor.getDbConn());
                osmDBdialect.closeDatabase(accessor.getDbConn());
            }
            if (deleteOsmDB) {
                osmDBdialect.removeDatabase(dbFile);
            }
            // 3.4 combine all low level ways and simplify them
            if (indexMap || indexRouting) {
                setGeneralProgress(progress, "[90 / 100]");
                if (indexMap) {
                    progress.startTask(Messages.getString("IndexCreator.INDEX_LO_LEVEL_WAYS"), indexMapCreator.getLowLevelWays());
                    indexMapCreator.processingLowLevelWays(progress);
                }
                if (indexRouting) {
                    progress.startTask(Messages.getString("IndexCreator.INDEX_LO_LEVEL_WAYS"), -1);
                    indexRouteCreator.processingLowLevelWays(progress);
                }
            }
            // 4. packing map rtree indexes
            if (indexMap) {
                // $NON-NLS-1$
                setGeneralProgress(progress, "[90 / 100]");
                // $NON-NLS-1$
                progress.startTask(Messages.getString("IndexCreator.PACK_RTREE_MAP"), -1);
                indexMapCreator.packRtreeFiles(getRTreeMapIndexNonPackFileName(), getRTreeMapIndexPackFileName());
            }
            if (indexRouting) {
                indexRouteCreator.packRtreeFiles(getRTreeRouteIndexNonPackFileName(), getRTreeRouteIndexPackFileName());
            }
            if (indexTransport) {
                // $NON-NLS-1$
                setGeneralProgress(progress, "[90 / 100]");
                // $NON-NLS-1$
                progress.startTask(Messages.getString("IndexCreator.PACK_RTREE_TRANSP"), -1);
                indexTransportCreator.packRTree(getRTreeTransportStopsFileName(), getRTreeTransportStopsPackFileName());
            }
        }
        // 5. Writing binary file
        if (indexMap || indexAddress || indexTransport || indexPOI || indexRouting) {
            if (mapFile.exists()) {
                mapFile.delete();
            }
            mapRAFile = new RandomAccessFile(mapFile, "rw");
            BinaryMapIndexWriter writer = new BinaryMapIndexWriter(mapRAFile, lastModifiedDate == null ? System.currentTimeMillis() : lastModifiedDate.longValue());
            if (indexMap) {
                setGeneralProgress(progress, "[95 of 100]");
                progress.startTask("Writing map index to binary file...", -1);
                indexMapCreator.writeBinaryMapIndex(writer, regionName);
            }
            if (indexRouting) {
                setGeneralProgress(progress, "[95 of 100]");
                progress.startTask("Writing route index to binary file...", -1);
                indexRouteCreator.writeBinaryRouteIndex(mapFile, writer, regionName, generateLowLevel);
            }
            if (indexAddress) {
                setGeneralProgress(progress, "[95 of 100]");
                progress.startTask("Writing address index to binary file...", -1);
                indexAddressCreator.writeBinaryAddressIndex(writer, regionName, progress);
            }
            if (indexPOI) {
                setGeneralProgress(progress, "[95 of 100]");
                progress.startTask("Writing poi index to binary file...", -1);
                indexPoiCreator.writeBinaryPoiIndex(writer, regionName, progress);
            }
            if (indexTransport) {
                setGeneralProgress(progress, "[95 of 100]");
                progress.startTask("Writing transport index to binary file...", -1);
                indexTransportCreator.writeBinaryTransportIndex(writer, regionName, mapConnection);
            }
            progress.finishTask();
            writer.close();
            mapRAFile.close();
            // $NON-NLS-1$
            log.info("Finish writing binary file");
        }
    } catch (RuntimeException e) {
        // $NON-NLS-1$
        log.error("Log exception", e);
        throw e;
    } catch (SQLException e) {
        // $NON-NLS-1$
        log.error("Log exception", e);
        throw e;
    } catch (IOException e) {
        // $NON-NLS-1$
        log.error("Log exception", e);
        throw e;
    } catch (XmlPullParserException e) {
        // $NON-NLS-1$
        log.error("Log exception", e);
        throw e;
    } finally {
        try {
            indexPoiCreator.commitAndClosePoiFile(lastModifiedDate);
            if (REMOVE_POI_DB) {
                indexPoiCreator.removePoiFile();
            }
            indexAddressCreator.closeAllPreparedStatements();
            indexTransportCreator.commitAndCloseFiles(getRTreeTransportStopsFileName(), getRTreeTransportStopsPackFileName(), deleteDatabaseIndexes);
            indexMapCreator.commitAndCloseFiles(getRTreeMapIndexNonPackFileName(), getRTreeMapIndexPackFileName(), deleteDatabaseIndexes);
            indexRouteCreator.commitAndCloseFiles(getRTreeRouteIndexNonPackFileName(), getRTreeRouteIndexPackFileName(), deleteDatabaseIndexes);
            if (mapConnection != null) {
                mapConnection.commit();
                mapConnection.close();
                mapConnection = null;
                File tempDBFile = new File(workingDir, getTempMapDBFileName());
                if (mapIndexDBDialect.databaseFileExists(tempDBFile) && deleteDatabaseIndexes) {
                    // do not delete it for now
                    mapIndexDBDialect.removeDatabase(tempDBFile);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }
}
Also used : RTreeException(rtree.RTreeException) SQLException(java.sql.SQLException) IOException(java.io.IOException) IndexAddressCreator(net.osmand.data.preparation.address.IndexAddressCreator) MapRenderingTypesEncoder(net.osmand.osm.MapRenderingTypesEncoder) RandomAccessFile(java.io.RandomAccessFile) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 SQLException (java.sql.SQLException)1 IndexAddressCreator (net.osmand.data.preparation.address.IndexAddressCreator)1 MapRenderingTypesEncoder (net.osmand.osm.MapRenderingTypesEncoder)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1 RTreeException (rtree.RTreeException)1