use of net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion in project Osmand by osmandapp.
the class CachedOsmandIndexes method initFileIndex.
private BinaryMapIndexReader initFileIndex(FileIndex found, RandomAccessFile mf, File f) throws IOException {
BinaryMapIndexReader reader = new BinaryMapIndexReader(mf, f, false);
reader.version = found.getVersion();
reader.dateCreated = found.getDateModified();
for (MapPart index : found.getMapIndexList()) {
MapIndex mi = new MapIndex();
mi.length = (int) index.getSize();
mi.filePointer = (int) index.getOffset();
mi.name = index.getName();
for (MapLevel mr : index.getLevelsList()) {
MapRoot root = new MapRoot();
root.length = (int) mr.getSize();
root.filePointer = (int) mr.getOffset();
root.left = mr.getLeft();
root.right = mr.getRight();
root.top = mr.getTop();
root.bottom = mr.getBottom();
root.minZoom = mr.getMinzoom();
root.maxZoom = mr.getMaxzoom();
mi.roots.add(root);
}
reader.mapIndexes.add(mi);
reader.indexes.add(mi);
reader.basemap = reader.basemap || mi.isBaseMap();
}
for (AddressPart index : found.getAddressIndexList()) {
AddressRegion mi = new AddressRegion();
mi.length = (int) index.getSize();
mi.filePointer = (int) index.getOffset();
mi.name = index.getName();
mi.enName = index.getNameEn();
mi.indexNameOffset = index.getIndexNameOffset();
for (CityBlock mr : index.getCitiesList()) {
CitiesBlock cblock = new CitiesBlock();
cblock.length = (int) mr.getSize();
cblock.filePointer = (int) mr.getOffset();
cblock.type = mr.getType();
mi.cities.add(cblock);
}
for (String s : index.getAdditionalTagsList()) {
mi.attributeTagsTable.add(s);
}
reader.addressIndexes.add(mi);
reader.indexes.add(mi);
}
for (PoiPart index : found.getPoiIndexList()) {
PoiRegion mi = new PoiRegion();
mi.length = (int) index.getSize();
mi.filePointer = (int) index.getOffset();
mi.name = index.getName();
mi.left31 = index.getLeft();
mi.right31 = index.getRight();
mi.top31 = index.getTop();
mi.bottom31 = index.getBottom();
reader.poiIndexes.add(mi);
reader.indexes.add(mi);
}
for (TransportPart index : found.getTransportIndexList()) {
TransportIndex mi = new TransportIndex();
mi.length = (int) index.getSize();
mi.filePointer = (int) index.getOffset();
mi.name = index.getName();
mi.left = index.getLeft();
mi.right = index.getRight();
mi.top = index.getTop();
mi.bottom = index.getBottom();
mi.stopsFileLength = index.getStopsTableLength();
mi.stopsFileOffset = index.getStopsTableOffset();
mi.stringTable = new IndexStringTable();
mi.stringTable.fileOffset = index.getStringTableOffset();
mi.stringTable.length = index.getStringTableLength();
reader.transportIndexes.add(mi);
reader.indexes.add(mi);
}
for (RoutingPart index : found.getRoutingIndexList()) {
RouteRegion mi = new RouteRegion();
mi.length = (int) index.getSize();
mi.filePointer = (int) index.getOffset();
mi.name = index.getName();
for (RoutingSubregion mr : index.getSubregionsList()) {
RouteSubregion sub = new RouteSubregion(mi);
sub.length = (int) mr.getSize();
sub.filePointer = (int) mr.getOffset();
sub.left = mr.getLeft();
sub.right = mr.getRight();
sub.top = mr.getTop();
sub.bottom = mr.getBottom();
sub.shiftToData = mr.getShifToData();
if (mr.getBasemap()) {
mi.basesubregions.add(sub);
} else {
mi.subregions.add(sub);
}
}
reader.routingIndexes.add(mi);
reader.indexes.add(mi);
}
return reader;
}
use of net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion in project OsmAnd-tools by osmandapp.
the class BinaryMerger method combineAddressIndex.
private void combineAddressIndex(String name, BinaryMapIndexWriter writer, AddressRegion[] addressRegions, BinaryMapIndexReader[] indexes) throws IOException {
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>();
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));
if (cityMap.containsKey(city)) {
cityMap.remove(city);
}
cityMap.put(city, index);
}
}
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());
if (!city.isPostcode()) {
for (Street s : city.getStreets()) {
IndexAddressCreator.putNamedMapObject(namesIndex, s, s.getFileOffset());
}
}
city.getStreets().clear();
namesakesStreetNodes.clear();
}
writer.endCityBlockIndex();
}
writer.writeAddressNameIndex(namesIndex);
writer.endWriteAddressIndex();
}
use of net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion in project Osmand by osmandapp.
the class BinaryInspector method combineParts.
@SuppressWarnings("unchecked")
public static List<Float> combineParts(File fileToExtract, Map<File, String> partsToExtractFrom) throws IOException {
BinaryMapIndexReader[] indexes = new BinaryMapIndexReader[partsToExtractFrom.size()];
RandomAccessFile[] rafs = new RandomAccessFile[partsToExtractFrom.size()];
LinkedHashSet<Float>[] partsSet = new LinkedHashSet[partsToExtractFrom.size()];
int c = 0;
Set<String> addressNames = new LinkedHashSet<String>();
int version = -1;
// Go through all files and validate conistency
for (File f : partsToExtractFrom.keySet()) {
if (f.getAbsolutePath().equals(fileToExtract.getAbsolutePath())) {
System.err.println("Error : Input file is equal to output file " + f.getAbsolutePath());
return null;
}
rafs[c] = new RandomAccessFile(f.getAbsolutePath(), "r");
indexes[c] = new BinaryMapIndexReader(rafs[c], f);
partsSet[c] = new LinkedHashSet<Float>();
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 null;
}
}
LinkedHashSet<Float> temp = new LinkedHashSet<Float>();
String pattern = partsToExtractFrom.get(f);
boolean minus = true;
for (int i = 0; i < indexes[c].getIndexes().size(); i++) {
partsSet[c].add(i + 1f);
BinaryIndexPart part = indexes[c].getIndexes().get(i);
if (part instanceof MapIndex) {
List<MapRoot> roots = ((MapIndex) part).getRoots();
int rsize = roots.size();
for (int j = 0; j < rsize; j++) {
partsSet[c].add((i + 1f) + (j + 1) / 10f);
}
}
}
if (pattern != null) {
minus = pattern.startsWith("-");
String[] split = pattern.substring(1).split(",");
for (String s : split) {
temp.add(Float.valueOf(s));
}
}
Iterator<Float> p = partsSet[c].iterator();
while (p.hasNext()) {
Float part = p.next();
if (minus) {
if (temp.contains(part)) {
p.remove();
}
} else {
if (!temp.contains(part)) {
p.remove();
}
}
}
c++;
}
// write files
FileOutputStream fout = new FileOutputStream(fileToExtract);
CodedOutputStream ous = CodedOutputStream.newInstance(fout, BUFFER_SIZE);
List<Float> list = new ArrayList<Float>();
byte[] BUFFER_TO_READ = new byte[BUFFER_SIZE];
ous.writeInt32(OsmandOdb.OsmAndStructure.VERSION_FIELD_NUMBER, version);
ous.writeInt64(OsmandOdb.OsmAndStructure.DATECREATED_FIELD_NUMBER, System.currentTimeMillis());
for (int k = 0; k < indexes.length; k++) {
LinkedHashSet<Float> partSet = partsSet[k];
BinaryMapIndexReader index = indexes[k];
RandomAccessFile raf = rafs[k];
for (int i = 0; i < index.getIndexes().size(); i++) {
if (!partSet.contains(Float.valueOf(i + 1f))) {
continue;
}
list.add(i + 1f);
BinaryIndexPart part = index.getIndexes().get(i);
String map;
if (part instanceof MapIndex) {
ous.writeTag(OsmandOdb.OsmAndStructure.MAPINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
map = "Map";
} else if (part instanceof AddressRegion) {
ous.writeTag(OsmandOdb.OsmAndStructure.ADDRESSINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
map = "Address";
if (addressNames.contains(part.getName())) {
System.err.println("Error : going to merge 2 addresses with same names. Skip " + part.getName());
continue;
}
addressNames.add(part.getName());
} else if (part instanceof TransportIndex) {
ous.writeTag(OsmandOdb.OsmAndStructure.TRANSPORTINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
map = "Transport";
} else if (part instanceof PoiRegion) {
ous.writeTag(OsmandOdb.OsmAndStructure.POIINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
map = "POI";
} else if (part instanceof RouteRegion) {
ous.writeTag(OsmandOdb.OsmAndStructure.ROUTINGINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
map = "Routing";
} else {
throw new UnsupportedOperationException();
}
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(), map }));
}
}
ous.writeInt32(OsmandOdb.OsmAndStructure.VERSIONCONFIRM_FIELD_NUMBER, version);
ous.flush();
fout.close();
return list;
}
use of net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion in project Osmand by osmandapp.
the class BinaryMapIndexReader method preloadBuildings.
public void preloadBuildings(Street s, SearchRequest<Building> resultMatcher) throws IOException {
AddressRegion reg = checkAddressIndex(s.getFileOffset());
codedIS.seek(s.getFileOffset());
int size = codedIS.readRawVarint32();
int old = codedIS.pushLimit(size);
City city = s.getCity();
addressAdapter.readStreet(s, resultMatcher, true, 0, 0, city != null && city.isPostcode() ? city.getName() : null, reg.attributeTagsTable);
codedIS.popLimit(old);
}
use of net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion in project Osmand by osmandapp.
the class BinaryMapIndexReader method preloadStreets.
public int preloadStreets(City c, SearchRequest<Street> resultMatcher) throws IOException {
AddressRegion reg;
try {
reg = checkAddressIndex(c.getFileOffset());
} catch (IllegalArgumentException e) {
throw new IOException(e.getMessage() + " while reading " + c + " (id: " + c.getId() + ")");
}
codedIS.seek(c.getFileOffset());
int size = codedIS.readRawVarint32();
int old = codedIS.pushLimit(size);
addressAdapter.readCityStreets(resultMatcher, c, reg.attributeTagsTable);
codedIS.popLimit(old);
return size;
}
Aggregations