use of net.osmand.data.preparation.IndexPoiCreator 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.data.preparation.IndexPoiCreator in project OsmAnd-tools by osmandapp.
the class BinaryMerger method combinePoiIndex.
private void combinePoiIndex(String name, BinaryMapIndexWriter writer, long dateCreated, PoiRegion[] poiRegions, BinaryMapIndexReader[] indexes) throws IOException, SQLException {
final int[] writtenPoiCount = { 0 };
MapRenderingTypesEncoder renderingTypes = new MapRenderingTypesEncoder(null, name);
boolean overwriteIds = false;
final IndexPoiCreator indexPoiCreator = new IndexPoiCreator(renderingTypes, overwriteIds);
indexPoiCreator.createDatabaseStructure(new File(new File(System.getProperty("user.dir")), IndexCreator.getPoiFileName(name)));
final Map<Long, List<Amenity>> amenityRelations = new HashMap<Long, List<Amenity>>();
final TLongHashSet set = new TLongHashSet();
final long[] generatedRelationId = { -1 };
for (int i = 0; i < poiRegions.length; i++) {
BinaryMapIndexReader index = indexes[i];
final TLongHashSet file = new TLongHashSet();
log.info("Region: " + extractRegionName(index));
index.searchPoi(BinaryMapIndexReader.buildSearchPoiRequest(0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, -1, BinaryMapIndexReader.ACCEPT_ALL_POI_TYPE_FILTER, new ResultMatcher<Amenity>() {
@Override
public boolean publish(Amenity amenity) {
try {
boolean isRelation = amenity.getId() < 0;
if (isRelation) {
long j = latlon(amenity);
List<Amenity> list;
if (!amenityRelations.containsKey(j)) {
list = new ArrayList<Amenity>(1);
amenityRelations.put(j, list);
} else {
list = amenityRelations.get(j);
}
boolean unique = true;
for (Amenity a : list) {
if (a.getType() == amenity.getType() && Algorithms.objectEquals(a.getSubType(), amenity.getSubType())) {
unique = false;
break;
}
}
if (unique) {
amenity.setId(generatedRelationId[0]--);
amenityRelations.get(j).add(amenity);
indexPoiCreator.insertAmenityIntoPoi(amenity);
writtenPoiCount[0]++;
}
} else {
if (!set.contains(amenity.getId())) {
file.add(amenity.getId());
indexPoiCreator.insertAmenityIntoPoi(amenity);
writtenPoiCount[0]++;
}
}
return false;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean isCancelled() {
return false;
}
}));
set.addAll(file);
}
indexPoiCreator.writeBinaryPoiIndex(writer, name, null);
indexPoiCreator.commitAndClosePoiFile(dateCreated);
// REMOVE_POI_DB = false;
if (REMOVE_POI_DB) {
indexPoiCreator.removePoiFile();
}
log.info("Written " + writtenPoiCount[0] + " POI.");
}
Aggregations