use of net.osmand.binary.BinaryMapDataObject 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.BinaryMapDataObject in project OsmAnd-tools by osmandapp.
the class ObfFileInMemory method writeFile.
public void writeFile(File targetFile, boolean doNotSimplifyObjects) throws IOException, RTreeException, SQLException {
boolean gzip = targetFile.getName().endsWith(".gz");
File nonGzip = targetFile;
if (gzip) {
nonGzip = new File(targetFile.getParentFile(), targetFile.getName().substring(0, targetFile.getName().length() - 3));
}
final RandomAccessFile raf = new RandomAccessFile(nonGzip, "rw");
// write files
CodedOutputStream ous = CodedOutputStream.newInstance(new OutputStream() {
@Override
public void write(int b) throws IOException {
raf.write(b);
}
@Override
public void write(byte[] b) throws IOException {
raf.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
raf.write(b, off, len);
}
});
timestamp = timestamp == 0 ? System.currentTimeMillis() : timestamp;
int version = IndexConstants.BINARY_MAP_VERSION;
ous.writeInt32(OsmandOdb.OsmAndStructure.VERSION_FIELD_NUMBER, version);
ous.writeInt64(OsmandOdb.OsmAndStructure.DATECREATED_FIELD_NUMBER, timestamp);
BinaryMapIndexWriter writer = new BinaryMapIndexWriter(raf, ous);
String defName = targetFile.getName().substring(0, targetFile.getName().indexOf('.'));
if (mapObjects.size() > 0) {
String name = mapIndex.getName();
if (Algorithms.isEmpty(name)) {
name = defName;
}
writer.startWriteMapIndex(Algorithms.capitalizeFirstLetter(name));
writer.writeMapEncodingRules(mapIndex.decodingRules);
Iterator<Entry<MapZoomPair, TLongObjectHashMap<BinaryMapDataObject>>> it = mapObjects.entrySet().iterator();
while (it.hasNext()) {
Entry<MapZoomPair, TLongObjectHashMap<BinaryMapDataObject>> n = it.next();
writeMapData(writer, n.getKey(), n.getValue(), targetFile, doNotSimplifyObjects);
}
writer.endWriteMapIndex();
}
if (routeObjects.size() > 0) {
String name = mapIndex.getName();
if (Algorithms.isEmpty(name)) {
name = defName;
}
writer.startWriteRouteIndex(name);
writer.writeRouteRawEncodingRules(routeIndex.routeEncodingRules);
writeRouteData(writer, routeObjects, targetFile);
writer.endWriteRouteIndex();
}
if (poiObjects.size() > 0) {
String name = "";
boolean overwriteIds = false;
if (Algorithms.isEmpty(name)) {
name = defName;
}
MapRenderingTypesEncoder renderingTypes = new MapRenderingTypesEncoder(null, name);
final IndexPoiCreator indexPoiCreator = new IndexPoiCreator(renderingTypes, overwriteIds);
File poiFile = new File(targetFile.getParentFile(), IndexCreator.getPoiFileName(name));
indexPoiCreator.createDatabaseStructure(poiFile);
for (Map<String, Amenity> mp : poiObjects.valueCollection()) {
for (Amenity a : mp.values()) {
indexPoiCreator.insertAmenityIntoPoi(a);
}
}
indexPoiCreator.writeBinaryPoiIndex(writer, name, null);
indexPoiCreator.commitAndClosePoiFile(System.currentTimeMillis());
indexPoiCreator.removePoiFile();
}
// TODO Write Transport
ous.writeInt32(OsmandOdb.OsmAndStructure.VERSIONCONFIRM_FIELD_NUMBER, version);
ous.flush();
raf.close();
if (gzip) {
nonGzip.setLastModified(timestamp);
FileInputStream fis = new FileInputStream(nonGzip);
GZIPOutputStream gzout = new GZIPOutputStream(new FileOutputStream(targetFile));
Algorithms.streamCopy(fis, gzout);
fis.close();
gzout.close();
nonGzip.delete();
}
targetFile.setLastModified(timestamp);
}
use of net.osmand.binary.BinaryMapDataObject in project OsmAnd-tools by osmandapp.
the class ObfRegionSplitter method split.
private void split(String[] args) throws IOException {
File worldObf = new File(args[0]);
File ocbfFile = new File(args[2]);
File dir = new File(args[1]);
String subFolder = args.length > 3 ? args[3] : "";
String fileSuffix = args.length > 4 ? args[4] : "";
if (!worldObf.exists() || !ocbfFile.exists()) {
System.out.println("Incorrect file!");
System.exit(1);
}
if (!dir.exists()) {
dir.mkdir();
}
try {
ObfFileInMemory fl = new ObfFileInMemory();
fl.readObfFiles(Collections.singletonList(worldObf));
OsmandRegions osmandRegions = new OsmandRegions();
osmandRegions.prepareFile(ocbfFile.getAbsolutePath());
osmandRegions.cacheAllCountries();
Map<String, Map<MapZoomPair, TLongObjectHashMap<BinaryMapDataObject>>> regionsMapData = splitRegionMapData(fl, osmandRegions);
Map<String, TLongObjectHashMap<RouteDataObject>> regionsRouteData = splitRegionRouteData(fl, osmandRegions);
Map<String, TLongObjectHashMap<Map<String, Amenity>>> regionsPoiData = splitRegionPoiData(fl, osmandRegions);
TreeSet<String> regionNames = new TreeSet<>();
regionNames.addAll(regionsMapData.keySet());
regionNames.addAll(regionsRouteData.keySet());
for (String regionName : regionNames) {
File folder = new File(dir, regionName);
if (!Algorithms.isEmpty(subFolder)) {
folder = new File(folder, subFolder);
}
folder.mkdirs();
File result = new File(folder, Algorithms.capitalizeFirstLetter(regionName) + fileSuffix + ".obf.gz");
ObfFileInMemory obf = new ObfFileInMemory();
Map<MapZoomPair, TLongObjectHashMap<BinaryMapDataObject>> mp = regionsMapData.get(regionName);
if (mp != null) {
for (MapZoomPair mzPair : mp.keySet()) {
obf.putMapObjects(mzPair, mp.get(mzPair).valueCollection(), true);
}
}
TLongObjectHashMap<RouteDataObject> ro = regionsRouteData.get(regionName);
if (ro != null) {
obf.putRoutingData(ro, true);
}
TLongObjectHashMap<Map<String, Amenity>> poi = regionsPoiData.get(regionName);
if (poi != null) {
obf.putPoiData(poi, true);
}
// TODO split Transport
obf.updateTimestamp(fl.getTimestamp());
obf.writeFile(result, true);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
use of net.osmand.binary.BinaryMapDataObject in project OsmAnd-tools by osmandapp.
the class ObfRegionSplitter method splitRegionRouteData.
private Map<String, TLongObjectHashMap<RouteDataObject>> splitRegionRouteData(ObfFileInMemory fl, OsmandRegions osmandRegions) throws IOException {
Map<String, TLongObjectHashMap<RouteDataObject>> result = new HashMap<>();
TLongObjectHashMap<RouteDataObject> routingData = fl.getRoutingData();
for (RouteDataObject obj : routingData.valueCollection()) {
// if(obj.getPointsLength() == 0) {
// continue;
// }
int x = obj.getPoint31XTile(0);
int y = obj.getPoint31YTile(0);
List<BinaryMapDataObject> l = osmandRegions.query(x, y);
for (BinaryMapDataObject b : l) {
if (osmandRegions.contain(b, x, y)) {
String dw = osmandRegions.getDownloadName(b);
WorldRegion wr = osmandRegions.getRegionDataByDownloadName(dw);
if (dw == null || wr == null) {
continue;
}
if (!Algorithms.isEmpty(dw) && wr.isRegionMapDownload()) {
TLongObjectHashMap<RouteDataObject> mp = result.get(dw);
if (mp == null) {
mp = new TLongObjectHashMap<>();
result.put(dw, mp);
}
mp.put(obj.getId(), obj);
}
}
}
}
return result;
}
use of net.osmand.binary.BinaryMapDataObject in project OsmAnd-tools by osmandapp.
the class ObfRegionSplitter method splitRegionPoiData.
private Map<String, TLongObjectHashMap<Map<String, Amenity>>> splitRegionPoiData(ObfFileInMemory fl, OsmandRegions osmandRegions) throws IOException {
Map<String, TLongObjectHashMap<Map<String, Amenity>>> result = new HashMap<>();
TLongObjectHashMap<Map<String, Amenity>> poiData = fl.getPoiObjects();
for (Map<String, Amenity> objMap : poiData.valueCollection()) {
Amenity obj = objMap.values().iterator().next();
int x = MapUtils.get31TileNumberX(obj.getLocation().getLongitude());
int y = MapUtils.get31TileNumberY(obj.getLocation().getLatitude());
List<BinaryMapDataObject> l = osmandRegions.query(x, y);
for (BinaryMapDataObject b : l) {
if (osmandRegions.contain(b, x, y)) {
String dw = osmandRegions.getDownloadName(b);
WorldRegion wr = osmandRegions.getRegionDataByDownloadName(dw);
if (dw == null || wr == null) {
continue;
}
if (!Algorithms.isEmpty(dw) && wr.isRegionMapDownload()) {
TLongObjectHashMap<Map<String, Amenity>> mp = result.get(dw);
if (mp == null) {
mp = new TLongObjectHashMap<>();
result.put(dw, mp);
}
mp.put(obj.getId(), objMap);
}
}
}
}
return result;
}
Aggregations