Skip to content

Commit

Permalink
Dynamic tag groups (city) filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanPyrohivskyi committed Jan 17, 2025
1 parent fb963fe commit c6839f8
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import net.osmand.router.HHRouteDataStructure.HHRouteRegionPointsCtx;
import net.osmand.router.HHRouteDataStructure.HHRoutingContext;
import net.osmand.router.HHRouteDataStructure.NetworkDBPoint;
import net.osmand.search.core.DynamicTagGroupFilter;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;

Expand Down Expand Up @@ -1672,14 +1673,20 @@ public static SearchRequest<RouteDataObject> buildSearchRouteRequest(int sleft,


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);
return buildSearchPoiRequest(x, y, nameFilter, sleft, sright, stop, sbottom, null, resultMatcher, null, 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) {
return buildSearchPoiRequest(x, y, nameFilter, sleft, sright, stop, sbottom, null, resultMatcher, null);
public static SearchRequest<Amenity> buildSearchPoiRequest(int x, int y, String nameFilter, int sleft, int sright, int stop, int sbottom,
SearchPoiTypeFilter poiTypeFilter, ResultMatcher<Amenity> resultMatcher, ResultMatcher<Amenity> rawDataCollector) {
return buildSearchPoiRequest(x, y, nameFilter, sleft, sright, stop, sbottom, poiTypeFilter, resultMatcher, null, null);
}

public static SearchRequest<Amenity> buildSearchPoiRequest(int x, int y, String nameFilter, int sleft, int sright, int stop, int sbottom, SearchPoiTypeFilter poiTypeFilter, ResultMatcher<Amenity> resultMatcher, ResultMatcher<Amenity> rawDataCollector) {
public static SearchRequest<Amenity> buildSearchPoiRequest(int x, int y, String nameFilter,
int sleft, int sright, int stop, int sbottom,
SearchPoiTypeFilter poiTypeFilter,
ResultMatcher<Amenity> resultMatcher,
ResultMatcher<Amenity> rawDataCollector,
ResultMatcher<List<TagValuePair>> dynamicTagGroupsMatcher) {
SearchRequest<Amenity> request = new SearchRequest<Amenity>();
request.x = x;
request.y = y;
Expand All @@ -1691,6 +1698,7 @@ public static SearchRequest<Amenity> buildSearchPoiRequest(int x, int y, String
request.resultMatcher = resultMatcher;
request.rawDataCollector = rawDataCollector;
request.nameQuery = nameFilter.trim();
request.dynamicTagGroupsMatcher = dynamicTagGroupsMatcher;
return request;
}

Expand Down Expand Up @@ -1781,6 +1789,7 @@ public static class SearchRequest<T> {

private ResultMatcher<T> resultMatcher;
private ResultMatcher<T> rawDataCollector;
public ResultMatcher<List<TagValuePair>> dynamicTagGroupsMatcher;

// 31 zoom tiles
// common variables
Expand All @@ -1806,6 +1815,7 @@ public static class SearchRequest<T> {

SearchPoiTypeFilter poiTypeFilter = null;
SearchPoiAdditionalFilter poiAdditionalFilter;
DynamicTagGroupFilter tagGroupFilter = null;

// cache information
TIntArrayList cacheCoordinates = new TIntArrayList();
Expand Down Expand Up @@ -1859,6 +1869,21 @@ public boolean publish(T obj) {
return false;
}

public boolean matchTagGroups(List<TagValuePair> tagValuePairs) {
if (dynamicTagGroupsMatcher == null) {
return false;
}
return dynamicTagGroupsMatcher.publish(tagValuePairs);
}

public void addTagGroupIdFilter(int tagGroupId) {
if (tagGroupFilter == null) {
tagGroupFilter = new DynamicTagGroupFilter(tagGroupId);
} else {
tagGroupFilter.addTagGroup(tagGroupId);
}
}

public void collectRawData(T obj) {
if (rawDataCollector != null) {
rawDataCollector.publish(obj);
Expand Down Expand Up @@ -2463,7 +2488,7 @@ public boolean accept(PoiCategory type, String subcategory) {
public boolean isEmpty() {
return false;
}
}, null, null);
}, null, null, null);

reader.searchPoi(req);
for (Amenity a : req.getSearchResults()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,9 @@ private Amenity readPoiPoint(int left31, int right31, int top31, int bottom31,
switch (tag) {
case 0:
req.numberOfAcceptedObjects++;
if (req.tagGroupFilter != null && am.hasTagGroups() && !req.tagGroupFilter.isAccept(am.getTagGroups().keySet())) {
return null;
}
if (hasLocation) {
if (precisionXY != 0) {
int[] xy = MapUtils.calculateFinalXYFromBaseAndPrecisionXY(BASE_POI_ZOOM, FINAL_POI_ZOOM, precisionXY, x >> BASE_POI_SHIFT, y >> BASE_POI_SHIFT, true);
Expand Down Expand Up @@ -1152,6 +1155,9 @@ private void readTagGroup(Map<Integer, List<TagValuePair>> tagGroups, SearchRequ
tagValuePairs.add(new TagValuePair(tagValues.get(i), tagValues.get(i + 1), -1));
}
tagGroups.put(id, tagValuePairs);
if (req.matchTagGroups(tagValuePairs)) {
req.addTagGroupIdFilter(id);
}
}
return;
case OsmandOdb.OsmAndPoiTagGroup.ID_FIELD_NUMBER:
Expand Down
18 changes: 17 additions & 1 deletion OsmAnd-java/src/main/java/net/osmand/data/Amenity.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public void setTagGroups(Map<Integer, List<TagValuePair>> tagGroups) {
this.tagGroups = tagGroups;
}

public boolean hasTagGroups() {
return tagGroups != null && !tagGroups.isEmpty();
}

public void setRegionName(String regionName) {
this.regionName = regionName;
}
Expand Down Expand Up @@ -734,6 +738,15 @@ public String getCityFromTagGroups(String lang) {
return null;
}
String result = null;
Map<City.CityType, String> places = new TreeMap<>(new Comparator<City.CityType>() {
@Override
public int compare(City.CityType o1, City.CityType o2) {
if (o1 == o2) {
return 0;
}
return o1.getPopulation() > o2.getPopulation() ? -1 : 1;
}
});
for (Map.Entry<Integer, List<TagValuePair>> entry : tagGroups.entrySet()) {
String translated = "";
String nonTranslated = "";
Expand All @@ -751,9 +764,12 @@ public String getCityFromTagGroups(String lang) {
}
String name = translated.isEmpty() ? nonTranslated : translated;
if (!name.isEmpty() && isCityTypeAccept(type)) {
result = result == null ? name : result + ", " + name;
places.put(type, name);
}
}
for (Map.Entry<City.CityType, String> e : places.entrySet()) {
result = result == null ? e.getValue() : result + ", " + e.getValue();
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.osmand.search.core;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class DynamicTagGroupFilter {

public DynamicTagGroupFilter(int tagGroupId) {
addTagGroup(tagGroupId);
}

private HashSet<Integer> tagGroups = null;

public boolean isAccept(Set<Integer> tagGroupIds) {
if (tagGroups == null) {
return true;
}
for (int id : tagGroupIds) {
if (tagGroups.contains(id)) {
return true;
}
}
return false;
}
public boolean isAccept(int tagGroupId) {
if (tagGroups == null) {
return true;
}
return tagGroups.contains(tagGroupId);
}

public void addTagGroup(int tagGroupId) {
if (tagGroups == null) {
tagGroups = new HashSet<>();
}
tagGroups.add(tagGroupId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -681,15 +681,34 @@ public boolean isCancelled() {
}
};

ResultMatcher<List<BinaryMapIndexReader.TagValuePair>> dynamicTagGroupsMatcher = new ResultMatcher<>() {
@Override
public boolean publish(List<BinaryMapIndexReader.TagValuePair> object) {
for (BinaryMapIndexReader.TagValuePair tagValue : object) {
if (tagValue.tag.startsWith("name")) {
if (nm.matches(tagValue.value)) {
return true;
}
}
}
return false;
}

@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,
matcher, rawDataCollector);
(int) bbox.left, (int) bbox.right, (int) bbox.top, (int) bbox.bottom, null,
matcher, rawDataCollector, dynamicTagGroupsMatcher);

SearchRequest<Amenity> reqUnlimited = BinaryMapIndexReader.buildSearchPoiRequest(
(int) bbox.centerX(), (int) bbox.centerY(), searchWord,
0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE,
matcher, rawDataCollector);
0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, null,
matcher, rawDataCollector, dynamicTagGroupsMatcher);

BinaryMapIndexReader fileRequest = phrase.getFileRequest();
if (fileRequest != null) {
Expand Down

0 comments on commit c6839f8

Please sign in to comment.