use of net.osmand.binary.BinaryMapIndexReader in project OsmAnd-tools by osmandapp.
the class MapRouterLayer method selfRoute.
public List<Way> selfRoute(LatLon start, LatLon end, List<LatLon> intermediates, List<RouteSegmentResult> previousRoute, RouteCalculationMode rm) {
List<Way> res = new ArrayList<Way>();
long time = System.currentTimeMillis();
List<File> files = new ArrayList<File>();
for (File f : Algorithms.getSortedFilesVersions(new File(DataExtractionSettings.getSettings().getBinaryFilesDir()))) {
if (f.getName().endsWith(".obf")) {
files.add(f);
}
}
final boolean animateRoutingCalculation = DataExtractionSettings.getSettings().isAnimateRouting();
if (animateRoutingCalculation) {
nextTurn.setVisible(true);
playPauseButton.setVisible(true);
stopButton.setVisible(true);
pause = true;
playPauseButton.setText("Play");
}
stop = false;
if (files.isEmpty()) {
JOptionPane.showMessageDialog(OsmExtractionUI.MAIN_APP.getFrame(), "Please specify obf file in settings", "Obf file not found", JOptionPane.ERROR_MESSAGE);
return null;
}
System.out.println("Self made route from " + start + " to " + end);
if (start != null && end != null) {
try {
BinaryMapIndexReader[] rs = new BinaryMapIndexReader[files.size()];
int it = 0;
for (File f : files) {
// $NON-NLS-1$ //$NON-NLS-2$
RandomAccessFile raf = new RandomAccessFile(f, "r");
rs[it++] = new BinaryMapIndexReader(raf, f);
}
String m = DataExtractionSettings.getSettings().getRouteMode();
String[] props = m.split("\\,");
RoutePlannerFrontEnd router = new RoutePlannerFrontEnd(USE_OLD_ROUTING);
Map<String, String> paramsR = new LinkedHashMap<String, String>();
for (String p : props) {
if (p.contains("=")) {
paramsR.put(p.split("=")[0], p.split("=")[1]);
} else {
paramsR.put(p, "true");
}
}
RoutingConfiguration config = DataExtractionSettings.getSettings().getRoutingConfig().build(props[0], /*RoutingConfiguration.DEFAULT_MEMORY_LIMIT*/
1000, paramsR);
PrecalculatedRouteDirection precalculatedRouteDirection = null;
// Test gpx precalculation
// LatLon[] lts = parseGPXDocument("/home/victor/projects/osmand/temp/esya.gpx");
// start = lts[0];
// end = lts[lts.length - 1];
// System.out.println("Start " + start + " end " + end);
// precalculatedRouteDirection = PrecalculatedRouteDirection.build(lts, config.router.getMaxDefaultSpeed());
// precalculatedRouteDirection.setFollowNext(true);
// config.planRoadDirection = 1;
// Test initial direction
// config.initialDirection = 90d / 180d * Math.PI; // EAST
// config.initialDirection = 180d / 180d * Math.PI; // SOUTH
// config.initialDirection = -90d / 180d * Math.PI; // WEST
// config.initialDirection = 0 / 180d * Math.PI; // NORTH
// config.NUMBER_OF_DESIRABLE_TILES_IN_MEMORY = 300;
// config.ZOOM_TO_LOAD_TILES = 14;
final RoutingContext ctx = router.buildRoutingContext(config, DataExtractionSettings.getSettings().useNativeRouting() ? NativeSwingRendering.getDefaultFromSettings() : null, rs, rm);
ctx.leftSideNavigation = false;
ctx.previouslyCalculatedRoute = previousRoute;
log.info("Use " + config.routerName + "mode for routing");
final DataTileManager<Entity> points = new DataTileManager<Entity>(11);
map.setPoints(points);
ctx.setVisitor(createSegmentVisitor(animateRoutingCalculation, points));
// Choose native or not native
long nt = System.nanoTime();
startProgressThread(ctx);
try {
List<RouteSegmentResult> searchRoute = router.searchRoute(ctx, start, end, intermediates, precalculatedRouteDirection);
throwExceptionIfRouteNotFound(ctx, searchRoute);
System.out.println("External native time " + (System.nanoTime() - nt) / 1.0e9f);
if (animateRoutingCalculation) {
playPauseButton.setVisible(false);
nextTurn.setText("FINISH");
waitNextPress();
nextTurn.setText(">>");
}
this.previousRoute = searchRoute;
calculateResult(res, searchRoute);
} finally {
ctx.calculationProgress.isCancelled = true;
}
} catch (Exception e) {
ExceptionHandler.handle(e);
} finally {
playPauseButton.setVisible(false);
nextTurn.setVisible(false);
stopButton.setVisible(false);
if (map.getPoints() != null) {
map.getPoints().clear();
}
}
System.out.println("Finding self routes " + res.size() + " " + (System.currentTimeMillis() - time) + " ms");
}
return res;
}
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) {
// skip map part
copyMapIndex(roadOnlyFile, (MapIndex) part, index, ous, 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();
}
use of net.osmand.binary.BinaryMapIndexReader in project OsmAnd-tools by osmandapp.
the class CheckRoadConnectivity method main.
public static void main(String[] args) throws IOException {
CheckRoadConnectivity crc = new CheckRoadConnectivity();
File fl = new File("/home/victor/projects/osmand/osm-gen/Brazil_southamerica_2.obf");
// $NON-NLS-1$ //$NON-NLS-2$
RandomAccessFile raf = new RandomAccessFile(fl, "r");
crc.collectDisconnectedRoads(new BinaryMapIndexReader(raf, fl));
// ClusteringContext ctx = new ClusteringContext();
// crc.clustering(ctx, new BinaryMapIndexReader(raf));
}
use of net.osmand.binary.BinaryMapIndexReader in project OsmAnd-tools by osmandapp.
the class ObfFileInMemory method readObfFiles.
public void readObfFiles(List<File> files) throws IOException {
for (int i = 0; i < files.size(); i++) {
File inputFile = files.get(i);
File nonGzip = inputFile;
boolean gzip = false;
if (inputFile.getName().endsWith(".gz")) {
nonGzip = new File(inputFile.getParentFile(), inputFile.getName().substring(0, inputFile.getName().length() - 3));
GZIPInputStream gzin = new GZIPInputStream(new FileInputStream(inputFile));
FileOutputStream fous = new FileOutputStream(nonGzip);
Algorithms.streamCopy(gzin, fous);
fous.close();
gzin.close();
gzip = true;
}
RandomAccessFile raf = new RandomAccessFile(nonGzip, "r");
BinaryMapIndexReader indexReader = new BinaryMapIndexReader(raf, nonGzip);
for (BinaryIndexPart p : indexReader.getIndexes()) {
if (p instanceof MapIndex) {
MapIndex mi = (MapIndex) p;
for (MapRoot mr : mi.getRoots()) {
MapZooms.MapZoomPair pair = new MapZooms.MapZoomPair(mr.getMinZoom(), mr.getMaxZoom());
TLongObjectHashMap<BinaryMapDataObject> objects = readBinaryMapData(indexReader, mi, mr.getMinZoom());
putMapObjects(pair, objects.valueCollection(), true);
}
} else if (p instanceof RouteRegion) {
RouteRegion rr = (RouteRegion) p;
readRoutingData(indexReader, rr, ZOOM_LEVEL_ROUTING, true);
} else if (p instanceof PoiRegion) {
PoiRegion pr = (PoiRegion) p;
TLongObjectHashMap<Map<String, Amenity>> rr = readPoiData(indexReader, pr, ZOOM_LEVEL_POI, true);
putPoiData(rr, true);
} else if (p instanceof TransportIndex) {
// read all data later
}
}
readTransportData(indexReader, true);
updateTimestamp(indexReader.getDateCreated());
indexReader.close();
raf.close();
if (gzip) {
nonGzip.delete();
}
}
}
use of net.osmand.binary.BinaryMapIndexReader in project OsmAnd-tools by osmandapp.
the class BinaryMerger method combineParts.
public void combineParts(File fileToExtract, List<File> files, Set<Integer> combineParts) throws IOException, SQLException {
BinaryMapIndexReader[] indexes = new BinaryMapIndexReader[files.size()];
RandomAccessFile[] rafs = new RandomAccessFile[files.size()];
long dateCreated = 0;
int version = -1;
// Go through all files and validate consistency
int c = 0;
for (File f : files) {
if (f.getAbsolutePath().equals(fileToExtract.getAbsolutePath())) {
System.err.println("Error : Input file is equal to output file " + f.getAbsolutePath());
return;
}
rafs[c] = new RandomAccessFile(f.getAbsolutePath(), "r");
indexes[c] = new BinaryMapIndexReader(rafs[c], f);
dateCreated = Math.max(dateCreated, indexes[c].getDateCreated());
if (version == -1) {
version = indexes[c].getVersion();
} else {
if (indexes[c].getVersion() != version) {
System.err.println("Error : Different input files has different input versions " + indexes[c].getVersion() + " != " + version);
return;
}
}
c++;
}
// write files
RandomAccessFile rafToExtract = new RandomAccessFile(fileToExtract, "rw");
BinaryMapIndexWriter writer = new BinaryMapIndexWriter(rafToExtract, dateCreated);
CodedOutputStream ous = writer.getCodedOutStream();
byte[] BUFFER_TO_READ = new byte[BUFFER_SIZE];
AddressRegion[] addressRegions = new AddressRegion[files.size()];
PoiRegion[] poiRegions = new PoiRegion[files.size()];
for (int k = 0; k < indexes.length; k++) {
BinaryMapIndexReader index = indexes[k];
RandomAccessFile raf = rafs[k];
for (int i = 0; i < index.getIndexes().size(); i++) {
BinaryIndexPart part = index.getIndexes().get(i);
if (combineParts.contains(part.getFieldNumber())) {
if (part.getFieldNumber() == OsmandOdb.OsmAndStructure.ADDRESSINDEX_FIELD_NUMBER) {
addressRegions[k] = (AddressRegion) part;
} else if (part.getFieldNumber() == OsmandOdb.OsmAndStructure.POIINDEX_FIELD_NUMBER) {
poiRegions[k] = (PoiRegion) part;
}
} else {
ous.writeTag(part.getFieldNumber(), WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
writeInt(ous, part.getLength());
copyBinaryPart(ous, BUFFER_TO_READ, raf, part.getFilePointer(), part.getLength());
System.out.println(MessageFormat.format("{2} part {0} is extracted {1} bytes", new Object[] { part.getName(), part.getLength(), part.getPartName() }));
}
}
}
String nm = fileToExtract.getName();
int i = nm.indexOf('_');
if (i > 0) {
nm = nm.substring(0, i);
}
if (combineParts.contains(OsmandOdb.OsmAndStructure.ADDRESSINDEX_FIELD_NUMBER)) {
combineAddressIndex(nm, writer, addressRegions, indexes);
}
if (combineParts.contains(OsmandOdb.OsmAndStructure.POIINDEX_FIELD_NUMBER)) {
combinePoiIndex(nm, writer, dateCreated, poiRegions, indexes);
}
ous.writeInt32(OsmandOdb.OsmAndStructure.VERSIONCONFIRM_FIELD_NUMBER, version);
ous.flush();
}
Aggregations