Skip to content

Commit

Permalink
Retreive amenity for rendering polygon
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanPyrohivskyi committed Sep 23, 2024
1 parent 37bcf08 commit 868d255
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 16 deletions.
2 changes: 1 addition & 1 deletion OsmAnd-java/src/main/java/net/osmand/NativeLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ public QuadRect getRectLatLon() {
left = Math.min(left, x);
right = Math.max(right, x);
top = Math.min(top, y);
bottom = Math.max(top, y);
bottom = Math.max(bottom, y);
}
return new QuadRect(MapUtils.get31LongitudeX(left), MapUtils.get31LatitudeY(top), MapUtils.get31LongitudeX(right), MapUtils.get31LatitudeY(bottom));
}
Expand Down
8 changes: 8 additions & 0 deletions OsmAnd-java/src/main/java/net/osmand/data/Amenity.java
Original file line number Diff line number Diff line change
Expand Up @@ -727,4 +727,12 @@ public List<LatLon> getPolygon() {
}
return res;
}

public void setX(TIntArrayList x) {
this.x = x;
}

public void setY(TIntArrayList y) {
this.y = y;
}
}
8 changes: 4 additions & 4 deletions OsmAnd-java/src/main/java/net/osmand/data/QuadRect.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public void expand(double left, double top, double right, double bottom) {
this.top = top;
this.bottom = bottom;
} else {
this.left = left < right ? Math.min(left, this.left) : Math.max(left, this.left);
this.right = left < right ? Math.max(right, this.right) : Math.min(right, this.right);
this.top = top < bottom ? Math.min(top, this.top) : Math.max(top, this.top);
this.bottom = top < bottom ? Math.max(bottom, this.bottom) : Math.min(bottom, this.bottom);
this.left = left <= right ? Math.min(left, this.left) : Math.max(left, this.left);
this.right = left <= right ? Math.max(right, this.right) : Math.min(right, this.right);
this.top = top <= bottom ? Math.min(top, this.top) : Math.max(top, this.top);
this.bottom = top <= bottom ? Math.max(bottom, this.bottom) : Math.min(bottom, this.bottom);
}
}

Expand Down
2 changes: 1 addition & 1 deletion OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,7 @@ private PoiUIFilter getPoiFilterForType(String nearestPoiType) {
return null;
}

private PoiUIFilter getPoiFilterForAmenity(Amenity amenity) {
protected PoiUIFilter getPoiFilterForAmenity(Amenity amenity) {
if (amenity != null) {
PoiCategory category = amenity.getType();
PoiType poiType = category.getPoiTypeByKeyName(amenity.getSubType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public class AmenityMenuBuilder extends MenuBuilder {

public static final Log LOG = PlatformUtil.getLog(AmenityMenuBuilder.class);

private final Amenity amenity;
protected Amenity amenity;
private AmenityUIHelper rowsBuilder;
private final Map<String, String> additionalInfo;
protected Map<String, String> additionalInfo;

public AmenityMenuBuilder(@NonNull MapActivity mapActivity, @NonNull Amenity amenity) {
super(mapActivity);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package net.osmand.plus.mapcontextmenu.builders;

import android.os.AsyncTask;

import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import net.osmand.NativeLibrary;
import net.osmand.data.Amenity;
import net.osmand.data.QuadRect;
import net.osmand.osm.MapPoiTypes;
import net.osmand.osm.PoiType;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.poi.PoiUIFilter;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
import net.osmand.util.OsmUtils;

import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RenderedObjectMenuBuilder extends AmenityMenuBuilder {

QuadRect bbox;
NativeLibrary.RenderedObject renderedObject;

public RenderedObjectMenuBuilder(@NonNull MapActivity mapActivity, @NonNull NativeLibrary.RenderedObject renderedObject) {
super(mapActivity, getSyntheticAmenity(mapActivity, renderedObject));
bbox = renderedObject.getBbox();
this.renderedObject = renderedObject;
}

private void searchAmenity(ViewGroup view, Object object) {
WeakReference<ViewGroup> viewGroupRef = new WeakReference<>(view);
PoiUIFilter filter = getPoiFilterForAmenity(amenity);
if (filter != null) {
execute(new SearchAmenitiesTask(filter, new SearchAmenityForRenderedObjectListener() {
@Override
public void onFinish(Amenity am) {
ViewGroup viewGroup = viewGroupRef.get();
if (viewGroup == null) {
return;
}
if (am != null) {
amenity = am;
amenity.setX(renderedObject.getX());
amenity.setY(renderedObject.getY());
additionalInfo = amenity.getAmenityExtensions(app.getPoiTypes(), false);
}
rebuild(viewGroup, object);
}
}));
} else {
rebuild(view, object);
}
}

@Override
public void build(@NonNull ViewGroup view, @Nullable Object object) {
searchAmenity(view, object);
}

private void rebuild(@NonNull ViewGroup view, @Nullable Object object) {
super.build(view, object);
}

private static Amenity getSyntheticAmenity(@NonNull MapActivity mapActivity, @NonNull NativeLibrary.RenderedObject renderedObject) {
Amenity am = new Amenity();
OsmandApplication app = mapActivity.getMyApplication();
MapPoiTypes mapPoiTypes = app.getPoiTypes();
am.setType(mapPoiTypes.getOtherPoiCategory());
am.setSubType("");
MapPoiTypes.PoiTranslator poiTranslator = mapPoiTypes.getPoiTranslator();
PoiType pt = null;
PoiType otherPt = null;
String subtype = null;
Map<String, String> additionalInfo = new HashMap<>();
for (Map.Entry<String, String> e : renderedObject.getTags().entrySet()) {
String tag = e.getKey();
String value = e.getValue();
if (tag.equals("name")) {
am.setName(value);
continue;
}
if (e.getKey().startsWith("name:")) {
am.setName(tag.substring("name:".length()), value);
continue;
}
if (tag.equals("amenity")) {
if (pt != null) {
otherPt = pt;
}
pt = mapPoiTypes.getPoiTypeByKey(value);
} else {
pt = mapPoiTypes.getPoiTypeByKey(e.getKey() + "_" + e.getValue());
}
if (pt != null) {
subtype = value;
continue;
}
if (Algorithms.isEmpty(value) && otherPt == null) {
otherPt = mapPoiTypes.getPoiTypeByKey(tag);
}
if (otherPt == null) {
PoiType poiType = mapPoiTypes.getPoiTypeByKey(value);
if (poiType != null && poiType.getOsmTag().equals(tag)) {
otherPt = poiType;
}
}
if (!Algorithms.isEmpty(value)) {
String translate = poiTranslator.getTranslation(tag + "_" + value);
String translate2 = poiTranslator.getTranslation(value);
if (translate != null && translate2 != null) {
additionalInfo.put(translate, translate2);
} else {
additionalInfo.put(tag, value);
}
}
}
if (pt != null) {
am.setType(pt.getCategory());
} else if(otherPt != null) {
am.setType(otherPt.getCategory());
am.setSubType(otherPt.getKeyName());
}
if (subtype != null) {
am.setSubType(subtype);
}
am.setId(renderedObject.getId());
am.setAdditionalInfo(additionalInfo);
am.setX(renderedObject.getX());
am.setY(renderedObject.getY());
return am;
}

private class SearchAmenitiesTask extends AsyncTask<Void, Void, Amenity> {

private final PoiUIFilter filter;
private final SearchAmenityForRenderedObjectListener listener;
private final QuadRect rect;
private final long osmId;

private SearchAmenitiesTask(PoiUIFilter filter, SearchAmenityForRenderedObjectListener listener) {
this.filter = filter;
this.listener = listener;
double l = MapUtils.get31LongitudeX((int)bbox.left);
double r = MapUtils.get31LongitudeX((int)bbox.right);
double t = MapUtils.get31LatitudeY((int)bbox.top);
double b = MapUtils.get31LatitudeY((int)bbox.bottom);
rect = new QuadRect(l, t, r, b);
osmId = OsmUtils.getOsmObjectId(renderedObject);
}

@Override
protected Amenity doInBackground(Void... params) {
List<Amenity> amenities = Collections.emptyList();
amenities = filter.searchAmenities(rect.top, rect.left, rect.bottom, rect.right, -1, null);
for (Amenity am : amenities) {
long id = OsmUtils.getOsmObjectId(am);
if (id == osmId) {
return am;
}
}
return null;
}

@Override
protected void onPostExecute(Amenity amenity) {
if (listener != null) {
listener.onFinish(amenity);
}
}
}

private interface SearchAmenityForRenderedObjectListener {
void onFinish(Amenity amenity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,24 @@
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapcontextmenu.MenuController;
import net.osmand.plus.mapcontextmenu.builders.RenderedObjectMenuBuilder;
import net.osmand.plus.render.RenderingIcons;
import net.osmand.plus.utils.AndroidUtils;
import net.osmand.util.Algorithms;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RenderedObjectMenuController extends MenuController {

private static final String POI_PREFIX = "poi";

private RenderedObject renderedObject;
private final List<String> DEFAULT_TAGS = new ArrayList<>() {{
add("amenity");
add("landuse");
}};

public RenderedObjectMenuController(@NonNull MapActivity mapActivity,
@NonNull PointDescription pointDescription,
@NonNull RenderedObject renderedObject) {
super(new MenuBuilder(mapActivity), pointDescription, mapActivity);
super(new RenderedObjectMenuBuilder(mapActivity, renderedObject), pointDescription, mapActivity);
builder.setShowNearestWiki(true);
setRenderedObject(renderedObject);
}
Expand Down

0 comments on commit 868d255

Please sign in to comment.