use of net.osmand.ResultMatcher in project Osmand by osmandapp.
the class SearchUICore method search.
public SearchResultCollection search(final String text, final boolean delayedExecution, final ResultMatcher<SearchResult> matcher) {
final int request = requestNumber.incrementAndGet();
final SearchPhrase phrase = this.phrase.generateNewPhrase(text, searchSettings);
this.phrase = phrase;
if (debugMode) {
LOG.info("Prepare search <" + phrase + ">");
}
singleThreadedExecutor.submit(new Runnable() {
@Override
public void run() {
try {
if (onSearchStart != null) {
onSearchStart.run();
}
final SearchResultMatcher rm = new SearchResultMatcher(matcher, phrase, request, requestNumber, totalLimit);
if (debugMode) {
LOG.info("Starting search <" + phrase.toString() + ">");
}
rm.searchStarted(phrase);
if (debugMode) {
LOG.info("Search started <" + phrase.toString() + ">");
}
if (delayedExecution) {
long startTime = System.currentTimeMillis();
if (debugMode) {
LOG.info("Wait for next char <" + phrase.toString() + ">");
}
boolean filtered = false;
while (System.currentTimeMillis() - startTime <= TIMEOUT_BETWEEN_CHARS) {
if (rm.isCancelled()) {
if (debugMode) {
LOG.info("Search cancelled <" + phrase + ">");
}
return;
}
Thread.sleep(TIMEOUT_BEFORE_FILTER);
if (!filtered) {
final SearchResultCollection quickRes = new SearchResultCollection(phrase);
if (debugMode) {
LOG.info("Filtering current data <" + phrase + "> Results=" + currentSearchResult.searchResults.size());
}
filterCurrentResults(phrase, new ResultMatcher<SearchResult>() {
@Override
public boolean publish(SearchResult object) {
quickRes.searchResults.add(object);
return true;
}
@Override
public boolean isCancelled() {
return rm.isCancelled();
}
});
if (debugMode) {
LOG.info("Current data filtered <" + phrase + "> Results=" + quickRes.searchResults.size());
}
if (!rm.isCancelled()) {
currentSearchResult = quickRes;
rm.filterFinished(phrase);
}
filtered = true;
}
}
} else {
Thread.sleep(TIMEOUT_BEFORE_SEARCH);
}
if (rm.isCancelled()) {
if (debugMode) {
LOG.info("Search cancelled <" + phrase + ">");
}
return;
}
searchInBackground(phrase, rm);
if (!rm.isCancelled()) {
SearchResultCollection collection = new SearchResultCollection(phrase);
if (debugMode) {
LOG.info("Processing search results <" + phrase + ">");
}
collection.addSearchResults(rm.getRequestResults(), true, true);
if (debugMode) {
LOG.info("Finishing search <" + phrase + "> Results=" + rm.getRequestResults().size());
}
currentSearchResult = collection;
rm.searchFinished(phrase);
if (onResultsComplete != null) {
onResultsComplete.run();
}
if (debugMode) {
LOG.info("Search finished <" + phrase + "> Results=" + rm.getRequestResults().size());
}
} else {
if (debugMode) {
LOG.info("Search cancelled <" + phrase + ">");
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
return null;
}
use of net.osmand.ResultMatcher in project Osmand by osmandapp.
the class BinaryMapIndexFilter method process.
private Stat process(final int zoom) throws IOException {
final Stat stat = new Stat();
final Map<TagValuePair, Integer> map = new HashMap<TagValuePair, Integer>();
SearchFilter sf = new SearchFilter() {
@Override
public boolean accept(TIntArrayList types, MapIndex index) {
boolean polygon = false;
boolean polyline = false;
for (int j = 0; j < types.size(); j++) {
int wholeType = types.get(j);
TagValuePair pair = index.decodeType(wholeType);
if (pair != null) {
int t = wholeType & 3;
if (t == RenderingRulesStorage.POINT_RULES) {
stat.pointCount++;
} else if (t == RenderingRulesStorage.LINE_RULES) {
stat.wayCount++;
polyline = true;
} else {
polygon = true;
stat.polygonCount++;
if (!map.containsKey(pair)) {
map.put(pair, 0);
}
map.put(pair, map.get(pair) + 1);
}
}
}
stat.totalCount++;
return polyline;
}
};
ResultMatcher<BinaryMapDataObject> matcher = new ResultMatcher<BinaryMapDataObject>() {
TIntHashSet set = new TIntHashSet();
@Override
public boolean isCancelled() {
return false;
}
@Override
public boolean publish(BinaryMapDataObject object) {
// double area = calculateArea(object, zoom);
double len = calculateLength(object, zoom);
if (/*tilesCovers(object, zoom, set) >= 2 && */
len > 100) {
stat.polygonBigSize++;
if (stat.polygonBigSize % 10000 == 0) {
return true;
}
}
return false;
}
};
SearchRequest<BinaryMapDataObject> req = BinaryMapIndexReader.buildSearchRequest(0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, zoom, sf, matcher);
List<BinaryMapDataObject> result = reader.searchMapIndex(req);
ArrayList<TagValuePair> list = new ArrayList<TagValuePair>(map.keySet());
Collections.sort(list, new Comparator<TagValuePair>() {
@Override
public int compare(TagValuePair o1, TagValuePair o2) {
return -map.get(o1) + map.get(o2);
}
});
for (TagValuePair tp : list) {
Integer i = map.get(tp);
if (i > 10) {
// System.out.println(tp.toString() + " " + i);
}
}
for (BinaryMapDataObject obj : result) {
System.out.println("id " + (obj.getId() >> 3) + " " + calculateArea(obj, zoom));
}
return stat;
}
use of net.osmand.ResultMatcher in project Osmand by osmandapp.
the class BinaryMapIndexReader method testAddressSearch.
private static void testAddressSearch(BinaryMapIndexReader reader) throws IOException {
// test address index search
final Map<String, Integer> streetFreq = new HashMap<String, Integer>();
List<City> cs = reader.getCities(null, BinaryMapAddressReaderAdapter.CITY_TOWN_TYPE);
for (City c : cs) {
int buildings = 0;
reader.preloadStreets(c, null);
for (Street s : c.getStreets()) {
updateFrequence(streetFreq, s.getName());
reader.preloadBuildings(s, buildAddressRequest((ResultMatcher<Building>) null));
buildings += s.getBuildings().size();
println(s.getName() + " " + s.getName("ru"));
}
println(c.getName() + " " + c.getLocation() + " " + c.getStreets().size() + " " + buildings + " " + c.getEnName(true) + " " + c.getName("ru"));
}
// int[] count = new int[1];
List<City> villages = reader.getCities(buildAddressRequest((ResultMatcher<City>) null), BinaryMapAddressReaderAdapter.VILLAGES_TYPE);
for (City v : villages) {
reader.preloadStreets(v, null);
for (Street s : v.getStreets()) {
updateFrequence(streetFreq, s.getName());
}
}
System.out.println("Villages " + villages.size());
List<String> sorted = new ArrayList<String>(streetFreq.keySet());
Collections.sort(sorted, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return -streetFreq.get(o1) + streetFreq.get(o2);
}
});
System.out.println(streetFreq.size());
for (String s : sorted) {
System.out.println(s + " " + streetFreq.get(s));
if (streetFreq.get(s) < 10) {
break;
}
}
}
use of net.osmand.ResultMatcher in project Osmand by osmandapp.
the class OsmandRegions method prepareFile.
public BinaryMapIndexReader prepareFile(String fileName) throws IOException {
reader = new BinaryMapIndexReader(new RandomAccessFile(fileName, "r"), new File(fileName));
// final Collator clt = OsmAndCollator.primaryCollator();
final Map<String, String> parentRelations = new LinkedHashMap<String, String>();
final ResultMatcher<BinaryMapDataObject> resultMatcher = new ResultMatcher<BinaryMapDataObject>() {
@Override
public boolean publish(BinaryMapDataObject object) {
initTypes(object);
int[] types = object.getTypes();
for (int i = 0; i < types.length; i++) {
TagValuePair tp = object.getMapIndex().decodeType(types[i]);
if ("boundary".equals(tp.value)) {
return false;
}
}
WorldRegion rd = initRegionData(parentRelations, object);
if (rd == null) {
return false;
}
if (rd.regionDownloadName != null) {
downloadNamesToFullNames.put(rd.regionDownloadName, rd.regionFullName);
}
fullNamesToRegionData.put(rd.regionFullName, rd);
return false;
}
@Override
public boolean isCancelled() {
return false;
}
};
iterateOverAllObjects(resultMatcher);
// post process download names
for (Map.Entry<String, String> e : parentRelations.entrySet()) {
String fullName = e.getKey();
String parentFullName = e.getValue();
// String parentParentFulName = parentRelations.get(parentFullName); // could be used for japan/russia
WorldRegion rd = fullNamesToRegionData.get(fullName);
WorldRegion parent = fullNamesToRegionData.get(parentFullName);
if (parent != null && rd != null) {
parent.addSubregion(rd);
}
}
structureWorldRegions(new ArrayList<WorldRegion>(fullNamesToRegionData.values()));
return reader;
}
use of net.osmand.ResultMatcher in project Osmand by osmandapp.
the class OsmandRegions method cacheAllCountries.
public Map<String, LinkedList<BinaryMapDataObject>> cacheAllCountries() throws IOException {
quadTree = new QuadTree<String>(new QuadRect(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE), 8, 0.55f);
final ResultMatcher<BinaryMapDataObject> resultMatcher = new ResultMatcher<BinaryMapDataObject>() {
// int c = 0;
@Override
public boolean publish(BinaryMapDataObject object) {
if (object.getPointsLength() < 1) {
return false;
}
initTypes(object);
String nm = mapIndexFields.get(mapIndexFields.downloadNameType, object);
if (!countriesByDownloadName.containsKey(nm)) {
LinkedList<BinaryMapDataObject> ls = new LinkedList<BinaryMapDataObject>();
countriesByDownloadName.put(nm, ls);
ls.add(object);
} else {
countriesByDownloadName.get(nm).add(object);
}
int maxx = object.getPoint31XTile(0);
int maxy = object.getPoint31YTile(0);
int minx = maxx;
int miny = maxy;
for (int i = 1; i < object.getPointsLength(); i++) {
int x = object.getPoint31XTile(i);
int y = object.getPoint31YTile(i);
if (y < miny) {
miny = y;
} else if (y > maxy) {
maxy = y;
}
if (x < minx) {
minx = x;
} else if (x > maxx) {
maxx = x;
}
}
quadTree.insert(nm, new QuadRect(minx, miny, maxx, maxy));
return false;
}
@Override
public boolean isCancelled() {
return false;
}
};
iterateOverAllObjects(resultMatcher);
return countriesByDownloadName;
}
Aggregations