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);
}
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;
}
Aggregations