Introduced SearchDataCreator
This commit is contained in:
parent
3910ab48f8
commit
5e103acc50
10 changed files with 379 additions and 7170 deletions
|
@ -221,7 +221,7 @@ public class BinaryMapAddressReaderAdapter {
|
|||
int fp = codedIS.getTotalBytesRead();
|
||||
int length = codedIS.readRawVarint32();
|
||||
int oldLimit = codedIS.pushLimit(length);
|
||||
City c = readCityHeader(new DefaultCityMatcher(matcher), fp, additionalTagsTable);
|
||||
City c = readCityHeader(resultMatcher, new DefaultCityMatcher(matcher), fp, additionalTagsTable);
|
||||
if (c != null) {
|
||||
if (resultMatcher == null || resultMatcher.publish(c)) {
|
||||
cities.add(c);
|
||||
|
@ -256,6 +256,7 @@ public class BinaryMapAddressReaderAdapter {
|
|||
int oldLimit = codedIS.pushLimit(length);
|
||||
readStreet(s, null, false, x >> 7, y >> 7, city.isPostcode() ? city.getName() : null,
|
||||
attributeTagsTable);
|
||||
publishRawData(resultMatcher, s);
|
||||
if (resultMatcher == null || resultMatcher.publish(s)) {
|
||||
city.registerStreet(s);
|
||||
}
|
||||
|
@ -303,7 +304,7 @@ public class BinaryMapAddressReaderAdapter {
|
|||
}
|
||||
}
|
||||
|
||||
protected City readCityHeader(CityMatcher matcher, int filePointer, List<String> additionalTagsTable) throws IOException {
|
||||
protected City readCityHeader(SearchRequest<? super City> resultMatcher, CityMatcher matcher, int filePointer, List<String> additionalTagsTable) throws IOException {
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
City c = null;
|
||||
|
@ -313,6 +314,7 @@ public class BinaryMapAddressReaderAdapter {
|
|||
int tag = WireFormat.getTagFieldNumber(t);
|
||||
switch (tag) {
|
||||
case 0:
|
||||
publishRawData(resultMatcher, c);
|
||||
return (matcher == null || matcher.matches(c)) ? c : null;
|
||||
case OsmandOdb.CityIndex.CITY_TYPE_FIELD_NUMBER:
|
||||
int type = codedIS.readUInt32();
|
||||
|
@ -445,6 +447,7 @@ public class BinaryMapAddressReaderAdapter {
|
|||
if (loadBuildingsAndIntersected) {
|
||||
int oldLimit = codedIS.pushLimit(length);
|
||||
Building b = readBuilding(offset, x, y, additionalTagsTable);
|
||||
publishRawData(buildingsMatcher, b);
|
||||
if (postcodeFilter == null || postcodeFilter.equalsIgnoreCase(b.getPostcode())) {
|
||||
if (buildingsMatcher == null || buildingsMatcher.publish(b)) {
|
||||
s.addBuilding(b);
|
||||
|
@ -688,7 +691,7 @@ public class BinaryMapAddressReaderAdapter {
|
|||
codedIS.seek(contOffset);
|
||||
int len = codedIS.readRawVarint32();
|
||||
int old = codedIS.pushLimit(len);
|
||||
obj = readCityHeader(null, contOffset, reg.attributeTagsTable);
|
||||
obj = readCityHeader(req, null, contOffset, reg.attributeTagsTable);
|
||||
codedIS.popLimit(old);
|
||||
}
|
||||
if (obj != null) {
|
||||
|
@ -701,6 +704,7 @@ public class BinaryMapAddressReaderAdapter {
|
|||
readStreet(s, null, false, MapUtils.get31TileNumberX(l.getLongitude()) >> 7,
|
||||
MapUtils.get31TileNumberY(l.getLatitude()) >> 7, obj.isPostcode() ? obj.getName() : null,
|
||||
reg.attributeTagsTable);
|
||||
publishRawData(req, s);
|
||||
boolean matches = stringMatcher.matches(s.getName());
|
||||
if (!matches) {
|
||||
for (String n : s.getAllNames()) {
|
||||
|
@ -727,7 +731,8 @@ public class BinaryMapAddressReaderAdapter {
|
|||
codedIS.seek(offset);
|
||||
int len = codedIS.readRawVarint32();
|
||||
int old = codedIS.pushLimit(len);
|
||||
City obj = readCityHeader(cityPostcodeMatcher, list.get(j), reg.attributeTagsTable);
|
||||
City obj = readCityHeader(req, cityPostcodeMatcher, list.get(j), reg.attributeTagsTable);
|
||||
publishRawData(req, obj);
|
||||
if (obj != null && !published.contains(offset)) {
|
||||
req.publish(obj);
|
||||
published.add(offset);
|
||||
|
@ -805,4 +810,9 @@ public class BinaryMapAddressReaderAdapter {
|
|||
}
|
||||
}
|
||||
|
||||
private <T> void publishRawData(SearchRequest<T> resultMatcher, T obj) {
|
||||
if (resultMatcher != null && obj != null) {
|
||||
resultMatcher.collectRawData(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1457,8 +1457,14 @@ public class BinaryMapIndexReader {
|
|||
|
||||
public static <T> SearchRequest<T> buildAddressByNameRequest(ResultMatcher<T> resultMatcher, String nameRequest,
|
||||
StringMatcherMode matcherMode) {
|
||||
return buildAddressByNameRequest(resultMatcher, null, nameRequest, matcherMode);
|
||||
}
|
||||
|
||||
public static <T> SearchRequest<T> buildAddressByNameRequest(ResultMatcher<T> resultMatcher, ResultMatcher<T> rawDataCollector, String nameRequest,
|
||||
StringMatcherMode matcherMode) {
|
||||
SearchRequest<T> request = new SearchRequest<T>();
|
||||
request.resultMatcher = resultMatcher;
|
||||
request.rawDataCollector = rawDataCollector;
|
||||
request.nameQuery = nameRequest.trim();
|
||||
request.matcherMode = matcherMode;
|
||||
return request;
|
||||
|
@ -1542,6 +1548,10 @@ public class BinaryMapIndexReader {
|
|||
|
||||
|
||||
public static SearchRequest<Amenity> buildSearchPoiRequest(int x, int y, String nameFilter, int sleft, int sright, int stop, int sbottom, ResultMatcher<Amenity> resultMatcher) {
|
||||
return buildSearchPoiRequest(x, y, nameFilter, sleft, sright, stop, sbottom, resultMatcher, null);
|
||||
}
|
||||
|
||||
public static SearchRequest<Amenity> buildSearchPoiRequest(int x, int y, String nameFilter, int sleft, int sright, int stop, int sbottom, ResultMatcher<Amenity> resultMatcher, ResultMatcher<Amenity> rawDataCollector) {
|
||||
SearchRequest<Amenity> request = new SearchRequest<Amenity>();
|
||||
request.x = x;
|
||||
request.y = y;
|
||||
|
@ -1550,6 +1560,7 @@ public class BinaryMapIndexReader {
|
|||
request.top = stop;
|
||||
request.bottom = sbottom;
|
||||
request.resultMatcher = resultMatcher;
|
||||
request.rawDataCollector = rawDataCollector;
|
||||
request.nameQuery = nameFilter.trim();
|
||||
return request;
|
||||
}
|
||||
|
@ -1634,6 +1645,7 @@ public class BinaryMapIndexReader {
|
|||
private boolean ocean = false;
|
||||
|
||||
private ResultMatcher<T> resultMatcher;
|
||||
private ResultMatcher<T> rawDataCollector;
|
||||
|
||||
// 31 zoom tiles
|
||||
// common variables
|
||||
|
@ -1711,6 +1723,12 @@ public class BinaryMapIndexReader {
|
|||
return false;
|
||||
}
|
||||
|
||||
public void collectRawData(T obj) {
|
||||
if (rawDataCollector != null) {
|
||||
rawDataCollector.publish(obj);
|
||||
}
|
||||
}
|
||||
|
||||
protected void publishOceanTile(boolean ocean) {
|
||||
if (ocean) {
|
||||
this.ocean = true;
|
||||
|
|
|
@ -0,0 +1,219 @@
|
|||
package net.osmand.binary;
|
||||
|
||||
import net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion;
|
||||
import net.osmand.data.Amenity;
|
||||
import net.osmand.data.Building;
|
||||
import net.osmand.data.City;
|
||||
import net.osmand.data.MapObject;
|
||||
import net.osmand.data.Street;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion;
|
||||
|
||||
public class BinaryMapIndexTestReader extends BinaryMapIndexReader {
|
||||
|
||||
private List<Amenity> amenities = Collections.emptyList();
|
||||
private List<City> cities = Collections.emptyList();
|
||||
private List<City> initCities = Collections.emptyList();
|
||||
private List<City> matchedCities = Collections.emptyList();
|
||||
private List<City> streetCities = Collections.emptyList();
|
||||
|
||||
private BinaryMapIndexTestReader() throws IOException {
|
||||
super(null, null, false);
|
||||
version = 2;
|
||||
dateCreated = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public static BinaryMapIndexReader buildTestReader(File jsonFile) throws IOException {
|
||||
String sourceJsonText = Algorithms.getFileAsString(jsonFile);
|
||||
JSONObject sourceJson = null;
|
||||
if (!Algorithms.isEmpty(sourceJsonText)) {
|
||||
sourceJson = new JSONObject(sourceJsonText);
|
||||
}
|
||||
if (sourceJson == null) {
|
||||
return null;
|
||||
}
|
||||
BinaryMapIndexTestReader reader = new BinaryMapIndexTestReader();
|
||||
if (sourceJson.has("amenities")) {
|
||||
JSONArray amenitiesArr = sourceJson.getJSONArray("amenities");
|
||||
List<Amenity> amenities = new ArrayList<>();
|
||||
for (int i = 0; i < amenitiesArr.length(); i++) {
|
||||
JSONObject amenityObj = amenitiesArr.getJSONObject(i);
|
||||
amenities.add(Amenity.parseJSON(amenityObj));
|
||||
}
|
||||
reader.amenities = amenities;
|
||||
PoiRegion region = new PoiRegion();
|
||||
region.name = Algorithms.getFileNameWithoutExtension(jsonFile);
|
||||
region.left31 = 0;
|
||||
region.top31 = 0;
|
||||
region.right31 = Integer.MAX_VALUE;
|
||||
region.bottom31 = Integer.MAX_VALUE;
|
||||
reader.poiIndexes.add(region);
|
||||
reader.indexes.add(region);
|
||||
}
|
||||
if (sourceJson.has("cities")) {
|
||||
JSONArray citiesArr = sourceJson.getJSONArray("cities");
|
||||
Set<String> attributeTagsTable = new HashSet<>();
|
||||
List<City> cities = new ArrayList<>();
|
||||
List<City> initCities = new ArrayList<>();
|
||||
List<City> matchedCities = new ArrayList<>();
|
||||
List<City> streetCities = new ArrayList<>();
|
||||
for (int i = 0; i < citiesArr.length(); i++) {
|
||||
JSONObject cityObj = citiesArr.getJSONObject(i);
|
||||
final City city = City.parseJSON(cityObj);
|
||||
cities.add(city);
|
||||
if (cityObj.has("init")) {
|
||||
initCities.add(city);
|
||||
}
|
||||
if (cityObj.has("matchCity")) {
|
||||
matchedCities.add(city);
|
||||
}
|
||||
if (cityObj.has("matchStreet")) {
|
||||
streetCities.add(city);
|
||||
}
|
||||
Set<String> names = city.getNamesMap(false).keySet();
|
||||
for (String name : names) {
|
||||
attributeTagsTable.add("name:" + name);
|
||||
}
|
||||
for (Street street : city.getStreets()) {
|
||||
names = street.getNamesMap(false).keySet();
|
||||
for (String name : names) {
|
||||
attributeTagsTable.add("name:" + name);
|
||||
}
|
||||
}
|
||||
// attributeTagsTable.add("name");
|
||||
// attributeTagsTable.add("name:en");
|
||||
}
|
||||
reader.cities = cities;
|
||||
reader.initCities = initCities;
|
||||
reader.matchedCities = matchedCities;
|
||||
reader.streetCities = streetCities;
|
||||
|
||||
AddressRegion region = new AddressRegion();
|
||||
region.name = Algorithms.getFileNameWithoutExtension(jsonFile);
|
||||
region.left31 = 0;
|
||||
region.top31 = 0;
|
||||
region.right31 = Integer.MAX_VALUE;
|
||||
region.bottom31 = Integer.MAX_VALUE;
|
||||
region.attributeTagsTable = new ArrayList<>(attributeTagsTable);
|
||||
reader.addressIndexes.add(region);
|
||||
reader.indexes.add(region);
|
||||
}
|
||||
return reader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Amenity> searchPoiByName(SearchRequest<Amenity> req) throws IOException {
|
||||
for (Amenity amenity : amenities) {
|
||||
req.publish(amenity);
|
||||
}
|
||||
return req.getSearchResults();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Amenity> searchPoi(SearchRequest<Amenity> req) throws IOException {
|
||||
for (Amenity amenity : amenities) {
|
||||
req.publish(amenity);
|
||||
}
|
||||
return req.getSearchResults();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<City> getCities(AddressRegion region, SearchRequest<City> resultMatcher, int cityType) throws IOException {
|
||||
return getCities(resultMatcher, cityType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<City> getCities(SearchRequest<City> resultMatcher, int cityType) throws IOException {
|
||||
for (City city : cities) {
|
||||
if (resultMatcher != null) {
|
||||
resultMatcher.publish(city);
|
||||
}
|
||||
}
|
||||
return cities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int preloadStreets(City c, SearchRequest<Street> resultMatcher) throws IOException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preloadBuildings(Street s, SearchRequest<Building> resultMatcher) throws IOException {
|
||||
// cities must be filled with streets and buildings
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MapObject> searchAddressDataByName(SearchRequest<MapObject> req) throws IOException {
|
||||
for (City city : streetCities) {
|
||||
for (Street street : city.getStreets()) {
|
||||
req.publish(street);
|
||||
}
|
||||
}
|
||||
for (City city : matchedCities) {
|
||||
req.publish(city);
|
||||
}
|
||||
return req.getSearchResults();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRegionName() {
|
||||
return "Test region";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsPoiData(int left31x, int top31y, int right31x, int bottom31y) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsMapData() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsPoiData() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsRouteData() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsRouteData(int left31x, int top31y, int right31x, int bottom31y, int zoom) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAddressData(int left31x, int top31y, int right31x, int bottom31y) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsMapData(int tile31x, int tile31y, int zoom) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsMapData(int left31x, int top31y, int right31x, int bottom31y, int zoom) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAddressData() {
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -26,7 +26,6 @@ import net.osmand.data.Amenity.AmenityRoutePoint;
|
|||
import net.osmand.data.LatLon;
|
||||
import net.osmand.osm.MapPoiTypes;
|
||||
import net.osmand.osm.PoiCategory;
|
||||
import net.osmand.osm.PoiType;
|
||||
import net.osmand.util.MapUtils;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
@ -591,6 +590,7 @@ public class BinaryMapPoiReaderAdapter {
|
|||
}
|
||||
}
|
||||
if (matches) {
|
||||
req.collectRawData(am);
|
||||
req.publish(am);
|
||||
}
|
||||
}
|
||||
|
@ -637,6 +637,7 @@ public class BinaryMapPoiReaderAdapter {
|
|||
int yp = (int) MapUtils.getTileNumberY(zSkip, am.getLocation().getLatitude());
|
||||
long valSkip = (((long) xp) << zSkip) | yp;
|
||||
if (!toSkip.contains(valSkip)) {
|
||||
req.collectRawData(am);
|
||||
boolean publish = req.publish(am);
|
||||
if (publish) {
|
||||
read = true;
|
||||
|
@ -647,6 +648,7 @@ public class BinaryMapPoiReaderAdapter {
|
|||
return read;
|
||||
}
|
||||
} else {
|
||||
req.collectRawData(am);
|
||||
if (req.publish(am)) {
|
||||
read = true;
|
||||
}
|
||||
|
|
|
@ -516,7 +516,7 @@ public class SearchUICore {
|
|||
}
|
||||
currentSearchResult = collection;
|
||||
if (phrase.getSettings().isExportObjects()) {
|
||||
//rm.createTestJSON(collection);
|
||||
rm.createTestJSON(collection);
|
||||
}
|
||||
rm.searchFinished(phrase);
|
||||
if (onResultsComplete != null) {
|
||||
|
|
|
@ -387,9 +387,6 @@ public class SearchCoreFactory {
|
|||
int limit = 0;
|
||||
@Override
|
||||
public boolean publish(MapObject object) {
|
||||
if (phrase.getSettings().isExportObjects()) {
|
||||
resultMatcher.exportObject(phrase, object);
|
||||
}
|
||||
if (isCancelled()) {
|
||||
return false;
|
||||
}
|
||||
|
@ -482,6 +479,23 @@ public class SearchCoreFactory {
|
|||
resultMatcher.isCancelled();
|
||||
}
|
||||
};
|
||||
|
||||
ResultMatcher<MapObject> rawDataCollector = null;
|
||||
if (phrase.getSettings().isExportObjects()) {
|
||||
rawDataCollector = new ResultMatcher<MapObject>() {
|
||||
@Override
|
||||
public boolean publish(MapObject object) {
|
||||
resultMatcher.exportObject(phrase, object);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Iterator<BinaryMapIndexReader> offlineIterator = phrase.getRadiusOfflineIndexes(DEFAULT_ADDRESS_BBOX_RADIUS * 5,
|
||||
SearchPhraseDataType.ADDRESS);
|
||||
String wordToSearch = phrase.getUnknownWordToSearch();
|
||||
|
@ -489,7 +503,7 @@ public class SearchCoreFactory {
|
|||
BinaryMapIndexReader r = offlineIterator.next();
|
||||
currentFile[0] = r;
|
||||
immediateResults.clear();
|
||||
SearchRequest<MapObject> req = BinaryMapIndexReader.buildAddressByNameRequest(rm, wordToSearch.toLowerCase(),
|
||||
SearchRequest<MapObject> req = BinaryMapIndexReader.buildAddressByNameRequest(rm, rawDataCollector, wordToSearch.toLowerCase(),
|
||||
phrase.isMainUnknownSearchWordComplete() ? StringMatcherMode.CHECK_EQUALS_FROM_SPACE
|
||||
: StringMatcherMode.CHECK_STARTS_FROM_SPACE);
|
||||
if (locSpecified) {
|
||||
|
@ -539,6 +553,22 @@ public class SearchCoreFactory {
|
|||
final NameStringMatcher nm = phrase.getMainUnknownNameStringMatcher();
|
||||
QuadRect bbox = phrase.getRadiusBBoxToSearch(BBOX_RADIUS_INSIDE);
|
||||
final Set<String> ids = new HashSet<String>();
|
||||
|
||||
ResultMatcher<Amenity> rawDataCollector = null;
|
||||
if (phrase.getSettings().isExportObjects()) {
|
||||
rawDataCollector = new ResultMatcher<Amenity>() {
|
||||
@Override
|
||||
public boolean publish(Amenity object) {
|
||||
resultMatcher.exportObject(phrase, object);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
SearchRequest<Amenity> req = BinaryMapIndexReader.buildSearchPoiRequest((int) bbox.centerX(),
|
||||
(int) bbox.centerY(), searchWord, (int) bbox.left, (int) bbox.right, (int) bbox.top,
|
||||
(int) bbox.bottom, new ResultMatcher<Amenity>() {
|
||||
|
@ -546,9 +576,6 @@ public class SearchCoreFactory {
|
|||
|
||||
@Override
|
||||
public boolean publish(Amenity object) {
|
||||
if (phrase.getSettings().isExportObjects()) {
|
||||
resultMatcher.exportObject(phrase, object);
|
||||
}
|
||||
if (limit++ > LIMIT) {
|
||||
return false;
|
||||
}
|
||||
|
@ -588,7 +615,7 @@ public class SearchCoreFactory {
|
|||
public boolean isCancelled() {
|
||||
return resultMatcher.isCancelled() && (limit < LIMIT);
|
||||
}
|
||||
});
|
||||
}, rawDataCollector);
|
||||
|
||||
while (offlineIterator.hasNext()) {
|
||||
BinaryMapIndexReader r = offlineIterator.next();
|
||||
|
|
|
@ -23,8 +23,7 @@ public class SearchSettings {
|
|||
private ObjectType[] searchTypes;
|
||||
private boolean emptyQueryAllowed;
|
||||
private boolean sortByName;
|
||||
private SearchExportSettings exportSettings;
|
||||
//private SearchExportSettings exportSettings = new SearchExportSettings(false, false, -1);
|
||||
private SearchExportSettings exportSettings; // = new SearchExportSettings(true, true, -1);
|
||||
|
||||
public SearchSettings(SearchSettings s) {
|
||||
if(s != null) {
|
||||
|
|
|
@ -1,13 +1,16 @@
|
|||
package net.osmand.search;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import net.osmand.ResultMatcher;
|
||||
import net.osmand.binary.BinaryMapIndexReader;
|
||||
import net.osmand.osm.AbstractPoiType;
|
||||
import net.osmand.osm.MapPoiTypes;
|
||||
import net.osmand.search.SearchUICore.SearchResultCollection;
|
||||
import net.osmand.search.SearchUICore.SearchResultMatcher;
|
||||
import net.osmand.search.core.SearchPhrase;
|
||||
import net.osmand.search.core.SearchResult;
|
||||
import net.osmand.search.core.SearchSettings;
|
||||
import net.osmand.util.Algorithms;
|
||||
import net.osmand.util.MapUtils;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
|
@ -19,23 +22,18 @@ import org.junit.runner.RunWith;
|
|||
import org.junit.runners.Parameterized;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import net.osmand.OsmAndCollator;
|
||||
import net.osmand.ResultMatcher;
|
||||
import net.osmand.binary.BinaryMapIndexReader;
|
||||
import net.osmand.data.Amenity;
|
||||
import net.osmand.data.Building;
|
||||
import net.osmand.data.City;
|
||||
import net.osmand.data.MapObject;
|
||||
import net.osmand.data.Street;
|
||||
import net.osmand.osm.AbstractPoiType;
|
||||
import net.osmand.osm.MapPoiTypes;
|
||||
import net.osmand.search.SearchUICore.SearchResultCollection;
|
||||
import net.osmand.search.SearchUICore.SearchResultMatcher;
|
||||
import net.osmand.search.core.SearchPhrase;
|
||||
import net.osmand.search.core.SearchResult;
|
||||
import net.osmand.search.core.SearchSettings;
|
||||
import net.osmand.util.Algorithms;
|
||||
import net.osmand.util.MapUtils;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class SearchUICoreTest {
|
||||
|
@ -94,11 +92,12 @@ public class SearchUICoreTest {
|
|||
@Test
|
||||
public void testSearch() throws IOException, JSONException {
|
||||
File jsonFile = testFile;
|
||||
File obfFile = new File(testFile.getParentFile(), testFile.getName().replace(".json", ".obf"));
|
||||
File obfZipFile = new File(testFile.getParentFile(), testFile.getName().replace(".json", ".obf.gz"));
|
||||
String sourceJsonText = Algorithms.getFileAsString(jsonFile);
|
||||
Assert.assertNotNull(sourceJsonText);
|
||||
Assert.assertTrue(sourceJsonText.length() > 0);
|
||||
|
||||
BinaryMapIndexReaderTest reader = new BinaryMapIndexReaderTest();
|
||||
JSONObject sourceJson = new JSONObject(sourceJsonText);
|
||||
JSONArray phrasesJson = sourceJson.optJSONArray("phrases");
|
||||
String singlePhrase = sourceJson.optString("phrase", null);
|
||||
|
@ -115,40 +114,21 @@ public class SearchUICoreTest {
|
|||
}
|
||||
}
|
||||
JSONObject settingsJson = sourceJson.getJSONObject("settings");
|
||||
if (sourceJson.has("amenities")) {
|
||||
JSONArray amenitiesArr = sourceJson.getJSONArray("amenities");
|
||||
List<Amenity> amenities = new ArrayList<>();
|
||||
for (int i = 0; i < amenitiesArr.length(); i++) {
|
||||
JSONObject amenityObj = amenitiesArr.getJSONObject(i);
|
||||
amenities.add(Amenity.parseJSON(amenityObj));
|
||||
}
|
||||
reader.amenities = amenities;
|
||||
}
|
||||
if (sourceJson.has("cities")) {
|
||||
JSONArray citiesArr = sourceJson.getJSONArray("cities");
|
||||
List<City> cities = new ArrayList<>();
|
||||
List<City> initCities = new ArrayList<>();
|
||||
List<City> matchedCities = new ArrayList<>();
|
||||
List<City> streetCities = new ArrayList<>();
|
||||
for (int i = 0; i < citiesArr.length(); i++) {
|
||||
JSONObject cityObj = citiesArr.getJSONObject(i);
|
||||
final City city = City.parseJSON(cityObj);
|
||||
cities.add(city);
|
||||
if (cityObj.has("init")) {
|
||||
initCities.add(city);
|
||||
}
|
||||
if (cityObj.has("matchCity")) {
|
||||
matchedCities.add(city);
|
||||
}
|
||||
if (cityObj.has("matchStreet")) {
|
||||
streetCities.add(city);
|
||||
}
|
||||
}
|
||||
reader.cities = cities;
|
||||
reader.initCities = initCities;
|
||||
reader.matchedCities = matchedCities;
|
||||
reader.streetCities = streetCities;
|
||||
|
||||
boolean obfZipFileExists = obfZipFile.exists();
|
||||
if (!obfZipFileExists) {
|
||||
System.out.println(String.format("Could not find obf file: %s", obfZipFile.getPath()));
|
||||
return;
|
||||
}
|
||||
//Assert.assertTrue(obfZipFileExists);
|
||||
|
||||
GZIPInputStream gzin = new GZIPInputStream(new FileInputStream(obfZipFile));
|
||||
FileOutputStream fous = new FileOutputStream(obfFile);
|
||||
Algorithms.streamCopy(gzin, fous);
|
||||
fous.close();
|
||||
gzin.close();
|
||||
|
||||
BinaryMapIndexReader reader = new BinaryMapIndexReader(new RandomAccessFile(obfFile.getPath(), "r"), obfFile);
|
||||
List<List<String>> results = new ArrayList<>();
|
||||
for (int i = 0; i < phrases.size(); i++) {
|
||||
results.add(new ArrayList<String>());
|
||||
|
@ -216,6 +196,8 @@ public class SearchUICoreTest {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
obfFile.delete();
|
||||
}
|
||||
|
||||
private void parseResults(JSONObject sourceJson, String tag, List<List<String>> results) {
|
||||
|
@ -329,116 +311,4 @@ public class SearchUICoreTest {
|
|||
return val;
|
||||
}
|
||||
};
|
||||
|
||||
private static class BinaryMapIndexReaderTest extends BinaryMapIndexReader {
|
||||
|
||||
List<Amenity> amenities = Collections.emptyList();
|
||||
List<City> cities = Collections.emptyList();
|
||||
List<City> initCities = Collections.emptyList();
|
||||
List<City> matchedCities = Collections.emptyList();
|
||||
List<City> streetCities = Collections.emptyList();
|
||||
|
||||
BinaryMapIndexReaderTest() throws IOException {
|
||||
super(null, null, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Amenity> searchPoiByName(SearchRequest<Amenity> req) throws IOException {
|
||||
for (Amenity amenity : amenities) {
|
||||
req.publish(amenity);
|
||||
}
|
||||
return req.getSearchResults();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Amenity> searchPoi(SearchRequest<Amenity> req) throws IOException {
|
||||
for (Amenity amenity : amenities) {
|
||||
req.publish(amenity);
|
||||
}
|
||||
return req.getSearchResults();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<City> getCities(SearchRequest<City> resultMatcher, int cityType) throws IOException {
|
||||
for (City city : initCities) {
|
||||
if (resultMatcher != null) {
|
||||
resultMatcher.publish(city);
|
||||
}
|
||||
}
|
||||
return initCities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int preloadStreets(City c, SearchRequest<Street> resultMatcher) throws IOException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preloadBuildings(Street s, SearchRequest<Building> resultMatcher) throws IOException {
|
||||
// cities must be filled with streets and buildings
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MapObject> searchAddressDataByName(SearchRequest<MapObject> req) throws IOException {
|
||||
for (City city : streetCities) {
|
||||
for (Street street : city.getStreets()) {
|
||||
req.publish(street);
|
||||
}
|
||||
}
|
||||
for (City city : matchedCities) {
|
||||
req.publish(city);
|
||||
}
|
||||
return req.getSearchResults();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRegionName() {
|
||||
return "Test region";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsPoiData(int left31x, int top31y, int right31x, int bottom31y) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsMapData() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsPoiData() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsRouteData() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsRouteData(int left31x, int top31y, int right31x, int bottom31y, int zoom) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAddressData(int left31x, int top31y, int right31x, int bottom31y) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsMapData(int tile31x, int tile31y, int zoom) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsMapData(int left31x, int top31y, int right31x, int bottom31y, int zoom) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAddressData() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
BIN
OsmAnd-java/src/test/resources/search/carrer_de_vic.obf.gz
Normal file
BIN
OsmAnd-java/src/test/resources/search/carrer_de_vic.obf.gz
Normal file
Binary file not shown.
Loading…
Reference in a new issue