use of net.osmand.binary.BinaryMapIndexReader in project OsmAnd-tools by osmandapp.
the class OsmAndMapsService method geocoding.
public synchronized List<GeocodingResult> geocoding(double lat, double lon) throws IOException, InterruptedException {
List<GeocodingResult> results = new ArrayList<>();
QuadRect points = points(null, new LatLon(lat, lon), new LatLon(lat, lon));
List<BinaryMapIndexReader> list = Arrays.asList(getObfReaders(points));
RoutePlannerFrontEnd router = new RoutePlannerFrontEnd();
RoutingContext ctx = prepareRouterContext("geocoding", points, router, null);
GeocodingUtilities su = new GeocodingUtilities();
double minBuildingDistance = 0;
List<GeocodingResult> complete = new ArrayList<GeocodingUtilities.GeocodingResult>();
List<GeocodingResult> res = su.reverseGeocodingSearch(ctx, lat, lon, false);
minBuildingDistance = justifyResults(list, su, complete, res);
// Collections.sort(complete, GeocodingUtilities.DISTANCE_COMPARATOR);
for (GeocodingResult r : complete) {
if (r.building != null && r.getDistance() > minBuildingDistance * GeocodingUtilities.THRESHOLD_MULTIPLIER_SKIP_BUILDINGS_AFTER) {
continue;
}
results.add(r);
}
return results;
}
use of net.osmand.binary.BinaryMapIndexReader in project OsmAnd-tools by osmandapp.
the class SearchDataCreator method merger.
public void merger(String[] args) throws IOException, SQLException {
if (args == null || args.length == 0) {
System.out.println(helpMessage);
return;
}
File outputFile = null;
String outputFilePath = null;
List<BinaryMapIndexReader> readers = new ArrayList<BinaryMapIndexReader>();
Set<Integer> combineParts = new HashSet<Integer>();
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("--")) {
combineParts.add(COMBINE_ARGS.get(args[i]));
} else if (outputFile == null) {
outputFilePath = args[i];
outputFile = new File(outputFilePath);
} else {
File file = new File(args[i]);
if (file.exists() && file.getName().endsWith(".json")) {
BinaryMapIndexReader reader = BinaryMapIndexTestReader.buildTestReader(file);
readers.add(reader);
}
}
}
if (combineParts.isEmpty()) {
combineParts.addAll(COMBINE_ARGS.values());
}
if (outputFile.exists()) {
if (!outputFile.delete()) {
throw new IOException("Cannot delete file " + outputFile);
}
}
if (readers.isEmpty()) {
throw new IOException("No data for merge");
}
combineParts(outputFile, null, readers, combineParts);
FileInputStream inputStream = new FileInputStream(outputFile);
File outFile = new File(outputFilePath + ".gz");
GZIPOutputStream outputStream = new GZIPOutputStream(new FileOutputStream(outFile));
Algorithms.streamCopy(inputStream, outputStream);
outputStream.close();
inputStream.close();
if (!outputFile.delete()) {
throw new IOException("Cannot delete file " + outputFile);
}
}
use of net.osmand.binary.BinaryMapIndexReader in project OsmAnd-tools by osmandapp.
the class BinaryMerger method mergeCitiesByNameDistance.
private void mergeCitiesByNameDistance(List<City> orderedCities, Map<City, List<City>> mergeGroup, Map<City, BinaryMapIndexReader> cityMap, boolean rename) {
for (int i = 0; i < orderedCities.size() - 1; i++) {
int j = i;
City oc = orderedCities.get(i);
City nc = orderedCities.get(j);
BinaryMapIndexReader ocIndexReader = cityMap.get(oc);
List<City> uniqueNamesakes = new ArrayList<City>();
boolean renameGroup = false;
while (MapObject.BY_NAME_COMPARATOR.areEqual(nc, oc)) {
boolean isUniqueCity = true;
for (ListIterator<City> uci = uniqueNamesakes.listIterator(); uci.hasNext(); ) {
City uc = uci.next();
if (isSameCity(uc, nc)) {
// Prefer cities with shortest names ("1101DL" instead of "1101 DL")
boolean shorter = nc.getName().length() < uc.getName().length();
if (shorter) {
mergeGroup.put(nc, mergeGroup.remove(uc));
uniqueNamesakes.remove(uc);
uniqueNamesakes.add(nc);
City tmp = uc;
uc = nc;
nc = tmp;
}
orderedCities.remove(nc);
mergeGroup.get(uc).add(nc);
isUniqueCity = false;
break;
}
}
if (isUniqueCity) {
uniqueNamesakes.add(nc);
mergeGroup.put(nc, new ArrayList<City>());
j++;
}
boolean areCitiesInSameRegion = ocIndexReader == cityMap.get(nc);
renameGroup = renameGroup || (rename && !areCitiesInSameRegion && uniqueNamesakes.size() > 1);
nc = (j < orderedCities.size()) ? orderedCities.get(j) : null;
}
if (uniqueNamesakes.size() == 1 && mergeGroup.get(uniqueNamesakes.get(0)).size() == 0) {
mergeGroup.remove(uniqueNamesakes.get(0));
} else {
if (renameGroup) {
for (City uc : uniqueNamesakes) {
for (City c : mergeGroup.get(uc)) {
addRegionToCityName(c, cityMap.get(c));
}
addRegionToCityName(uc, cityMap.get(uc));
}
}
}
}
}
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, List<BinaryMapIndexReader> readers, Set<Integer> combineParts) throws IOException, SQLException {
boolean combineFiles = files != null;
BinaryMapIndexReader[] indexes = combineFiles ? new BinaryMapIndexReader[files.size()] : readers.toArray(new BinaryMapIndexReader[readers.size()]);
RandomAccessFile[] rafs = combineFiles ? new RandomAccessFile[files.size()] : null;
long dateCreated = 0;
int version = -1;
// Go through all files and validate consistency
int c = 0;
if (combineFiles) {
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++;
}
} else {
for (BinaryMapIndexReader index : indexes) {
dateCreated = Math.max(dateCreated, index.getDateCreated());
if (version == -1) {
version = index.getVersion();
} else {
if (index.getVersion() != version) {
System.err.println("Error : Different input files has different input versions " + index.getVersion() + " != " + version);
return;
}
}
}
}
// 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[combineFiles ? files.size() : readers.size()];
PoiRegion[] poiRegions = new PoiRegion[combineFiles ? files.size() : readers.size()];
for (int k = 0; k < indexes.length; k++) {
BinaryMapIndexReader index = indexes[k];
RandomAccessFile raf = rafs != null ? rafs[k] : null;
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 if (raf != null) {
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();
}
use of net.osmand.binary.BinaryMapIndexReader 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;
IndexCreatorSettings settings = new IndexCreatorSettings();
settings.indexPOI = true;
final IndexPoiCreator indexPoiCreator = new IndexPoiCreator(settings, 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