Search in sources :

Example 1 with CachedOsmandIndexes

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

the class OsmExtractionUI method createUI.

public void createUI() {
    // $NON-NLS-1$
    frame = new JFrame(Messages.getString("OsmExtractionUI.OSMAND_MAP_CREATOR"));
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        // $NON-NLS-1$
        log.error("Can't set look and feel", e);
    }
    frame.addWindowListener(new ExitListener());
    Container content = frame.getContentPane();
    frame.setFocusable(true);
    mapPanel = new MapPanel(DataExtractionSettings.getSettings().getTilesDirectory());
    mapPanel.setFocusable(true);
    mapPanel.addMapLocationListener(this);
    statusBarLabel = new JLabel();
    osmandRegions = new OsmandRegions();
    try {
        File regions = new File("regions.ocbf");
        if (!regions.exists()) {
            InputStream is = OsmandRegions.class.getResourceAsStream("regions.ocbf");
            FileOutputStream fous = new FileOutputStream(regions);
            Algorithms.streamCopy(is, fous);
            fous.close();
        }
        osmandRegions.prepareFile(regions.getAbsolutePath());
    } catch (IOException e2) {
        e2.printStackTrace();
        log.error(e2.getMessage(), e2);
    }
    content.add(statusBarLabel, BorderLayout.SOUTH);
    File workingDir = DataExtractionSettings.getSettings().getDefaultWorkingDir();
    // $NON-NLS-1$ //$NON-NLS-2$
    statusBarLabel.setText(workingDir == null ? Messages.getString("OsmExtractionUI.WORKING_DIR_UNSPECIFIED") : Messages.getString("OsmExtractionUI.WORKING_DIRECTORY") + workingDir.getAbsolutePath());
    String bdir = DataExtractionSettings.getSettings().getBinaryFilesDir();
    List<BinaryMapIndexReader> files = new ArrayList<>();
    if (!Algorithms.isEmpty(bdir)) {
        try {
            File bdirFile = new File(bdir);
            File cacheFile = new File(bdirFile, "indexes.cache");
            CachedOsmandIndexes cache = new CachedOsmandIndexes();
            if (cacheFile.exists()) {
                cache.readFromFile(cacheFile, 2);
            }
            if (bdirFile.exists() && bdirFile.listFiles() != null) {
                List<File> asList = Arrays.asList(Algorithms.getSortedFilesVersions(bdirFile));
                ArrayList<File> sortedFiles = new ArrayList<>(asList);
                // Collections.reverse(sortedFiles);
                for (File obf : sortedFiles) {
                    if (!obf.isDirectory() && obf.getName().endsWith(".obf")) {
                        try {
                            // BinaryMapIndexReader bmir = new BinaryMapIndexReader(new RandomAccessFile(obf, "r"),
                            // obf);
                            BinaryMapIndexReader bmir = cache.getReader(obf);
                            RandomAccessFile raf = new RandomAccessFile(obf, "r");
                            BinaryMapIndexReader bmir2 = new BinaryMapIndexReader(raf, bmir);
                            files.add(bmir2);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }
            if (!files.isEmpty()) {
                cache.writeToFile(cacheFile);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    String loc = DataExtractionSettings.getSettings().getSearchLocale();
    if (loc.isEmpty()) {
        loc = null;
    }
    searchUICore = new SearchUICore(MapPoiTypes.getDefault(), loc, false);
    searchUICore.getSearchSettings().setOfflineIndexes(files);
    searchUICore.init();
    searchUICore.registerAPI(new SearchCoreFactory.SearchRegionByNameAPI());
    // treePlaces = new JTree();
    // treePlaces.setModel(new DefaultTreeModel(new DefaultMutableTreeNode(Messages.getString("OsmExtractionUI.REGION")), false)); 	     //$NON-NLS-1$
    // JSplitPane panelForTreeAndMap = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(treePlaces), mapPanel);
    // panelForTreeAndMap.setResizeWeight(0.2);
    // content.add(panelForTreeAndMap, BorderLayout.CENTER);
    content.add(mapPanel, BorderLayout.CENTER);
    JPanel bl = new JPanel();
    bl.setLayout(new BoxLayout(bl, BoxLayout.PAGE_AXIS));
    JPanel buttonsBar = createButtonsBar();
    bl.add(buttonsBar);
    final JTextField statusField = new JTextField();
    mapPanel.setStatusField(statusField);
    bl.add(statusField);
    updateStatusField(statusField);
    content.add(bl, BorderLayout.NORTH);
    JMenuBar bar = new JMenuBar();
    fillMenuWithActions(bar);
    frame.setJMenuBar(bar);
}
Also used : JPanel(javax.swing.JPanel) SearchCoreFactory(net.osmand.search.core.SearchCoreFactory) BoxLayout(javax.swing.BoxLayout) ArrayList(java.util.ArrayList) JTextField(javax.swing.JTextField) CachedOsmandIndexes(net.osmand.binary.CachedOsmandIndexes) Container(java.awt.Container) OsmandRegions(net.osmand.map.OsmandRegions) JFrame(javax.swing.JFrame) InputStream(java.io.InputStream) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) JLabel(javax.swing.JLabel) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) SAXException(org.xml.sax.SAXException) SQLException(java.sql.SQLException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) SearchUICore(net.osmand.search.SearchUICore) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) JMenuBar(javax.swing.JMenuBar)

Example 2 with CachedOsmandIndexes

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

the class ResourceManager method indexingMaps.

public List<String> indexingMaps(final IProgress progress) {
    long val = System.currentTimeMillis();
    ArrayList<File> files = new ArrayList<File>();
    File appPath = context.getAppPath(null);
    File roadsPath = context.getAppPath(IndexConstants.ROADS_INDEX_DIR);
    roadsPath.mkdirs();
    collectFiles(appPath, IndexConstants.BINARY_MAP_INDEX_EXT, files);
    renameRoadsFiles(files, roadsPath);
    collectFiles(roadsPath, IndexConstants.BINARY_MAP_INDEX_EXT, files);
    if (!Version.isFreeVersion(context) || context.getSettings().FULL_VERSION_PURCHASED.get()) {
        collectFiles(context.getAppPath(IndexConstants.WIKI_INDEX_DIR), IndexConstants.BINARY_MAP_INDEX_EXT, files);
    }
    if (OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) != null) {
        collectFiles(context.getAppPath(IndexConstants.SRTM_INDEX_DIR), IndexConstants.BINARY_MAP_INDEX_EXT, files);
    }
    changesManager.collectChangesFiles(context.getAppPath(IndexConstants.LIVE_INDEX_DIR), IndexConstants.BINARY_MAP_INDEX_EXT, files);
    Collections.sort(files, Algorithms.getFileVersionComparator());
    List<String> warnings = new ArrayList<String>();
    renderer.clearAllResources();
    CachedOsmandIndexes cachedOsmandIndexes = new CachedOsmandIndexes();
    File indCache = context.getAppPath(INDEXES_CACHE);
    if (indCache.exists()) {
        try {
            cachedOsmandIndexes.readFromFile(indCache, CachedOsmandIndexes.VERSION);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
    File liveDir = context.getAppPath(IndexConstants.LIVE_INDEX_DIR);
    depthContours = false;
    for (File f : files) {
        // $NON-NLS-1$
        progress.startTask(context.getString(R.string.indexing_map) + " " + f.getName(), -1);
        try {
            BinaryMapIndexReader mapReader = null;
            try {
                mapReader = cachedOsmandIndexes.getReader(f);
                if (mapReader.getVersion() != IndexConstants.BINARY_MAP_VERSION) {
                    mapReader = null;
                }
            } catch (IOException e) {
                log.error(String.format("File %s could not be read", f.getName()), e);
            }
            boolean wikiMap = (f.getName().contains("_wiki") || f.getName().contains(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT));
            boolean srtmMap = f.getName().contains(IndexConstants.BINARY_SRTM_MAP_INDEX_EXT);
            if (mapReader == null || (Version.isFreeVersion(context) && wikiMap && !context.getSettings().FULL_VERSION_PURCHASED.get())) {
                // $NON-NLS-1$
                warnings.add(MessageFormat.format(context.getString(R.string.version_index_is_not_supported), f.getName()));
            } else {
                if (mapReader.isBasemap()) {
                    basemapFileNames.put(f.getName(), f.getName());
                }
                long dateCreated = mapReader.getDateCreated();
                if (dateCreated == 0) {
                    dateCreated = f.lastModified();
                }
                if (f.getParentFile().getName().equals(liveDir.getName())) {
                    boolean toUse = changesManager.index(f, dateCreated, mapReader);
                    if (!toUse) {
                        try {
                            mapReader.close();
                        } catch (IOException e) {
                            log.error(e.getMessage(), e);
                        }
                        continue;
                    }
                } else if (!wikiMap && !srtmMap) {
                    changesManager.indexMainMap(f, dateCreated);
                }
                // $NON-NLS-1$
                indexFileNames.put(f.getName(), dateFormat.format(dateCreated));
                if (!depthContours && f.getName().toLowerCase().startsWith("depth_")) {
                    depthContours = true;
                }
                renderer.initializeNewResource(progress, f, mapReader);
                BinaryMapReaderResource resource = new BinaryMapReaderResource(f, mapReader);
                fileReaders.put(f.getName(), resource);
                if (!mapReader.getRegionNames().isEmpty()) {
                    RegionAddressRepositoryBinary rarb = new RegionAddressRepositoryBinary(this, resource);
                    addressMap.put(f.getName(), rarb);
                }
                if (mapReader.hasTransportData()) {
                    transportRepositories.put(f.getName(), new TransportIndexRepositoryBinary(resource));
                }
                // disable osmc for routing temporarily due to some bugs
                if (mapReader.containsRouteData() && (!f.getParentFile().equals(liveDir) || context.getSettings().USE_OSM_LIVE_FOR_ROUTING.get())) {
                    resource.setUseForRouting(true);
                }
                if (mapReader.containsPoiData()) {
                    try {
                        // $NON-NLS-1$
                        RandomAccessFile raf = new RandomAccessFile(f, "r");
                        amenityRepositories.put(f.getName(), new AmenityIndexRepositoryBinary(new BinaryMapIndexReader(raf, mapReader)));
                    } catch (IOException e) {
                        // $NON-NLS-1$
                        log.error("Exception reading " + f.getAbsolutePath(), e);
                        // $NON-NLS-1$
                        warnings.add(MessageFormat.format(context.getString(R.string.version_index_is_not_supported), f.getName()));
                    }
                }
            }
        } catch (SQLiteException e) {
            // $NON-NLS-1$
            log.error("Exception reading " + f.getAbsolutePath(), e);
            // $NON-NLS-1$
            warnings.add(MessageFormat.format(context.getString(R.string.version_index_is_not_supported), f.getName()));
        } catch (OutOfMemoryError oome) {
            // $NON-NLS-1$
            log.error("Exception reading " + f.getAbsolutePath(), oome);
            warnings.add(MessageFormat.format(context.getString(R.string.version_index_is_big_for_memory), f.getName()));
        }
    }
    log.debug("All map files initialized " + (System.currentTimeMillis() - val) + " ms");
    if (files.size() > 0 && (!indCache.exists() || indCache.canWrite())) {
        try {
            cachedOsmandIndexes.writeToFile(indCache);
        } catch (Exception e) {
            log.error("Index file could not be written", e);
        }
    }
    for (ResourceListener l : resourceListeners) {
        l.onMapsIndexed();
    }
    return warnings;
}
Also used : SRTMPlugin(net.osmand.plus.srtmplugin.SRTMPlugin) ArrayList(java.util.ArrayList) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) IOException(java.io.IOException) SQLiteException(android.database.sqlite.SQLiteException) SQLiteException(android.database.sqlite.SQLiteException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) CachedOsmandIndexes(net.osmand.binary.CachedOsmandIndexes) RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 RandomAccessFile (java.io.RandomAccessFile)2 ArrayList (java.util.ArrayList)2 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)2 CachedOsmandIndexes (net.osmand.binary.CachedOsmandIndexes)2 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2 SQLiteException (android.database.sqlite.SQLiteException)1 Container (java.awt.Container)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SQLException (java.sql.SQLException)1 BoxLayout (javax.swing.BoxLayout)1 JFrame (javax.swing.JFrame)1 JLabel (javax.swing.JLabel)1 JMenuBar (javax.swing.JMenuBar)1 JPanel (javax.swing.JPanel)1 JTextField (javax.swing.JTextField)1 XMLStreamException (javax.xml.stream.XMLStreamException)1