use of net.osmand.obf.preparation.BinaryFileReference in project OsmAnd-tools by osmandapp.
the class BinaryMerger method combineAddressIndex.
private void combineAddressIndex(String name, BinaryMapIndexWriter writer, AddressRegion[] addressRegions, BinaryMapIndexReader[] indexes) throws IOException {
IndexCreatorSettings settings = new IndexCreatorSettings();
Set<String> attributeTagsTableSet = new TreeSet<String>();
for (int i = 0; i != addressRegions.length; i++) {
AddressRegion region = addressRegions[i];
attributeTagsTableSet.addAll(region.getAttributeTagsTable());
}
writer.startWriteAddressIndex(name, attributeTagsTableSet);
List<String> attributeTagsTable = new ArrayList<String>();
attributeTagsTable.addAll(attributeTagsTableSet);
Map<String, Integer> tagRules = new HashMap<String, Integer>();
Map<String, List<MapObject>> namesIndex = new TreeMap<String, List<MapObject>>(Collator.getInstance());
ListIterator<String> it = attributeTagsTable.listIterator();
while (it.hasNext()) {
tagRules.put(it.next(), it.previousIndex());
}
for (int type : BinaryMapAddressReaderAdapter.CITY_TYPES) {
Map<City, BinaryMapIndexReader> cityMap = new HashMap<City, BinaryMapIndexReader>();
Map<Long, City> cityIds = new HashMap<Long, City>();
for (int i = 0; i < addressRegions.length; i++) {
AddressRegion region = addressRegions[i];
final BinaryMapIndexReader index = indexes[i];
for (City city : index.getCities(region, null, type)) {
normalizePostcode(city, extractCountryName(index));
// probably code to merge cities below is not needed (it called mostly for postcodes)
if (cityIds.containsKey(city.getId())) {
index.preloadStreets(city, null);
City city2 = cityIds.get(city.getId());
cityMap.get(city2).preloadStreets(city2, null);
if (city.getStreets().size() > city2.getStreets().size()) {
cityMap.remove(city2);
cityIds.put(city.getId(), city);
cityMap.put(city, index);
}
} else {
cityMap.put(city, index);
cityIds.put(city.getId(), city);
}
}
}
List<City> cities = new ArrayList<City>(cityMap.keySet());
Map<City, List<City>> mergeCityGroup = new HashMap<City, List<City>>();
Collections.sort(cities, MapObject.BY_NAME_COMPARATOR);
mergeCitiesByNameDistance(cities, mergeCityGroup, cityMap, type == BinaryMapAddressReaderAdapter.CITY_TOWN_TYPE);
List<BinaryFileReference> refs = new ArrayList<BinaryFileReference>();
// 1. write cities
writer.startCityBlockIndex(type);
Map<City, Map<Street, List<Node>>> namesakesStreetNodes = new HashMap<City, Map<Street, List<Node>>>();
for (int i = 0; i < cities.size(); i++) {
City city = cities.get(i);
BinaryMapIndexReader rindex = cityMap.get(city);
preloadStreetsAndBuildings(rindex, city, namesakesStreetNodes);
List<City> namesakes = mergeCityGroup.get(city);
if (namesakes != null) {
for (City namesake : namesakes) {
preloadStreetsAndBuildings(cityMap.get(namesake), namesake, namesakesStreetNodes);
city = mergeCities(city, namesake, namesakesStreetNodes);
}
}
int cityType = city.isPostcode() ? -1 : city.getType().ordinal();
BinaryFileReference ref = writer.writeCityHeader(city, cityType, tagRules);
refs.add(ref);
writer.writeCityIndex(city, city.getStreets(), namesakesStreetNodes.get(city), ref, tagRules);
IndexAddressCreator.putNamedMapObject(namesIndex, city, ref.getStartPointer(), settings);
if (!city.isPostcode()) {
for (Street s : city.getStreets()) {
IndexAddressCreator.putNamedMapObject(namesIndex, s, s.getFileOffset(), settings);
}
}
city.getStreets().clear();
namesakesStreetNodes.clear();
}
writer.endCityBlockIndex();
}
writer.writeAddressNameIndex(namesIndex);
writer.endWriteAddressIndex();
}
use of net.osmand.obf.preparation.BinaryFileReference in project OsmAnd-tools by osmandapp.
the class IndexUploader method copyMapIndex.
private static void copyMapIndex(File roadOnlyFile, MapIndex part, BinaryMapIndexReader index, CodedOutputStream ous, RandomAccessFile raf, RandomAccessFile routf) throws IOException, RTreeException {
final List<MapRoot> rts = part.getRoots();
BinaryMapIndexWriter writer = new BinaryMapIndexWriter(routf, ous);
writer.startWriteMapIndex(part.getName());
boolean first = true;
for (MapRoot r : rts) {
if (r.getMaxZoom() <= 10) {
if (first) {
throw new UnsupportedOperationException("Can't write top level zoom");
}
ous.writeTag(OsmandOdb.OsmAndMapIndex.LEVELS_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
BinaryMerger.writeInt(ous, r.getLength());
BinaryMerger.copyBinaryPart(ous, new byte[BUFFER_SIZE], raf, r.getFilePointer(), r.getLength());
continue;
}
final TLongObjectHashMap<BinaryMapDataObject> objects = new TLongObjectHashMap<BinaryMapDataObject>();
File nonpackRtree = new File(roadOnlyFile.getParentFile(), "nonpack" + r.getMinZoom() + "." + roadOnlyFile.getName() + ".rtree");
File packRtree = new File(roadOnlyFile.getParentFile(), "pack" + r.getMinZoom() + "." + roadOnlyFile.getName() + ".rtree");
RTree rtree = null;
try {
rtree = new RTree(nonpackRtree.getAbsolutePath());
final SearchRequest<BinaryMapDataObject> req = buildSearchRequest(r, objects, rtree);
index.searchMapIndex(req, part);
if (first) {
first = false;
writer.writeMapEncodingRules(part.decodingRules);
}
rtree = AbstractIndexPartCreator.packRtreeFile(rtree, nonpackRtree.getAbsolutePath(), packRtree.getAbsolutePath());
TLongObjectHashMap<BinaryFileReference> treeHeader = new TLongObjectHashMap<BinaryFileReference>();
long rootIndex = rtree.getFileHdr().getRootIndex();
rtree.Node root = rtree.getReadNode(rootIndex);
Rect rootBounds = IndexVectorMapCreator.calcBounds(root);
if (rootBounds != null) {
writer.startWriteMapLevelIndex(r.getMinZoom(), r.getMaxZoom(), rootBounds.getMinX(), rootBounds.getMaxX(), rootBounds.getMinY(), rootBounds.getMaxY());
IndexVectorMapCreator.writeBinaryMapTree(root, rootBounds, rtree, writer, treeHeader);
IndexVectorMapCreator.writeBinaryMapBlock(root, rootBounds, rtree, writer, treeHeader, objects, r.getMapZoom(), false);
writer.endWriteMapLevelIndex();
}
} finally {
if (rtree != null) {
RandomAccessFile file = rtree.getFileHdr().getFile();
file.close();
}
nonpackRtree.delete();
packRtree.delete();
RTree.clearCache();
}
}
writer.endWriteMapIndex();
}
Aggregations