Skip to content

Commit

Permalink
Refactor id propagate
Browse files Browse the repository at this point in the history
  • Loading branch information
vshcherb committed Oct 16, 2024
1 parent b13907d commit 380a20f
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 150 deletions.
16 changes: 12 additions & 4 deletions OsmAnd-java/src/main/java/net/osmand/NativeLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.Map;
import java.util.TreeMap;

import net.osmand.router.*;
import org.apache.commons.logging.Log;

import com.google.gson.JsonObject;
Expand All @@ -27,17 +26,26 @@
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion;
import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteSubregion;
import net.osmand.binary.ObfConstants;
import net.osmand.binary.RouteDataObject;
import net.osmand.data.LatLon;
import net.osmand.data.MapObject;
import net.osmand.data.QuadRect;
import net.osmand.render.RenderingRuleSearchRequest;
import net.osmand.render.RenderingRulesStorage;
import net.osmand.router.GeneralRouter;
import net.osmand.router.GpxRouteApproximation;
import net.osmand.router.HHRouteDataStructure.HHRoutingConfig;
import net.osmand.router.HHRoutePlanner;
import net.osmand.router.NativeTransportRoutingResult;
import net.osmand.router.RouteCalculationProgress;
import net.osmand.router.RoutePlannerFrontEnd.GpxPoint;
import net.osmand.router.RouteResultPreparation;
import net.osmand.router.RouteSegmentResult;
import net.osmand.router.RoutingContext;
import net.osmand.router.TransportRoutingConfiguration;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
import net.osmand.util.OsmUtils;

public class NativeLibrary {

Expand Down Expand Up @@ -707,8 +715,8 @@ public String getGpxFileName() {
@Override
public String toString() {
String s = getClass().getSimpleName() + " " + name;
String link = OsmUtils.getOsmUrlForId(this);
String tags = OsmUtils.getPrintTags(this);
String link = ObfConstants.getOsmUrlForId(this);
String tags = ObfConstants.getPrintTags(this);
s += s.contains(link) ? "" : " " + link;
s += s.contains(tags) ? "" : " " + tags;
return s;
Expand Down
126 changes: 125 additions & 1 deletion OsmAnd-java/src/main/java/net/osmand/binary/ObfConstants.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,135 @@
package net.osmand.binary;

import static net.osmand.data.MapObject.AMENITY_ID_RIGHT_SHIFT;
import static net.osmand.router.RouteResultPreparation.SHIFT_ID;

import java.util.Locale;
import java.util.Map;

import net.osmand.NativeLibrary.RenderedObject;
import net.osmand.data.Amenity;
import net.osmand.data.MapObject;
import net.osmand.osm.edit.Entity.EntityType;
import net.osmand.util.Algorithms;

public class ObfConstants {

public static final int SHIFT_MULTIPOLYGON_IDS = 43;
public static final int SHIFT_NON_SPLIT_EXISTING_IDS = 41;

public static final int SHIFT_PROPAGATED_NODE_IDS = 50;
public static final int SHIFT_PROPAGATED_NODES_BITS = 11;
public static final long MAX_COUNT_PROPAGATED_NODES = (1L << SHIFT_PROPAGATED_NODES_BITS) - 1;//2047
public static final long MAX_ID_PROPAGATED_NODES = (1L << SHIFT_PROPAGATED_NODES_BITS) - 1;//2047


public static final long RELATION_BIT = 1L << (ObfConstants.SHIFT_MULTIPOLYGON_IDS - 1); // 1L << 42
public static final long PROPAGATE_NODE_BIT = 1L << (ObfConstants.SHIFT_PROPAGATED_NODE_IDS - 1); // 1L << 41
public static final long SPLIT_BIT = 1L << (ObfConstants.SHIFT_NON_SPLIT_EXISTING_IDS - 1); // 1L << 40

public static final int DUPLICATE_SPLIT = 5; //According IndexPoiCreator DUPLICATE_SPLIT


public static String getOsmUrlForId(MapObject mapObject) {
EntityType type = getOsmEntityType(mapObject);
if (type != null) {
long osmId = getOsmObjectId(mapObject);
return "https://www.openstreetmap.org/" + type.name().toLowerCase(Locale.US) + "/" + osmId;
}
return "";
}

public static long getOsmObjectId(MapObject object) {
long originalId = -1;
Long id = object.getId();
if (id != null) {
if (object instanceof RenderedObject) {
id >>= 1;
}
if (isIdFromPropagatedNode(id)) {
long shifted = id & ~PROPAGATE_NODE_BIT;
originalId = shifted >> ObfConstants.SHIFT_PROPAGATED_NODES_BITS;
} else {
if (isShiftedID(id)) {
originalId = getOsmId(id);
} else {
int shift = object instanceof Amenity ? AMENITY_ID_RIGHT_SHIFT : SHIFT_ID;
originalId = id >> shift;
}
}
}
return originalId;
}

public static EntityType getOsmEntityType(MapObject object) {
if (isOsmUrlAvailable(object)) {
Long id = object.getId();
long originalId = id >> 1;
if (object instanceof RenderedObject && isIdFromPropagatedNode(originalId)) {
return EntityType.WAY;
}
if (isIdFromPropagatedNode(id)) {
return EntityType.WAY;
}
long relationShift = 1L << 41;
if (originalId > relationShift) {
return EntityType.RELATION;
} else {
return id % 2 == MapObject.WAY_MODULO_REMAINDER ? EntityType.WAY : EntityType.NODE;
}
}
return null;
}

public static String getPrintTags(RenderedObject renderedObject) {
StringBuilder s = new StringBuilder();
for (Map.Entry<String, String> entry : renderedObject.getTags().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
boolean keyEmpty = Algorithms.isEmpty(key);
boolean valueEmpty = Algorithms.isEmpty(value);
boolean bothPresent = !keyEmpty && !valueEmpty;
boolean anyPresent = !keyEmpty || !valueEmpty;
if (!keyEmpty) {
s.append(key);
}
if (bothPresent) {
s.append(":");
}
if (!valueEmpty) {
s.append(value);
}
if (anyPresent) {
s.append(" ");
}
}
return s.toString().trim();
}

public static boolean isOsmUrlAvailable(MapObject object) {
Long id = object.getId();
return id != null && id > 0;
}

public static long getOsmId(long id) {
//According methods assignIdForMultipolygon and genId in IndexPoiCreator
long clearBits = RELATION_BIT | SPLIT_BIT;
id = isShiftedID(id) ? (id & ~clearBits) >> DUPLICATE_SPLIT : id;
return id >> SHIFT_ID;
}

public static boolean isShiftedID(long id) {
return isIdFromRelation(id) || isIdFromSplit(id);
}

public static boolean isIdFromRelation(long id) {
return id > 0 && (id & RELATION_BIT) == RELATION_BIT;
}

public static boolean isIdFromPropagatedNode(long id) {
return id > 0 && (id & PROPAGATE_NODE_BIT) == PROPAGATE_NODE_BIT;
}

public static boolean isIdFromSplit(long id) {
return id > 0 && (id & SPLIT_BIT) == SPLIT_BIT;
}
}
126 changes: 0 additions & 126 deletions OsmAnd-java/src/main/java/net/osmand/util/OsmUtils.java

This file was deleted.

1 change: 0 additions & 1 deletion OsmAnd/src/net/osmand/core/android/MapRendererContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import net.osmand.render.RenderingRulesStorage;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
import net.osmand.util.OsmUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.osmand.CallbackWithObject;
import net.osmand.ResultMatcher;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.binary.ObfConstants;
import net.osmand.data.Amenity;
import net.osmand.data.QuadRect;
import net.osmand.osm.MapPoiTypes;
Expand All @@ -19,7 +20,6 @@
import net.osmand.plus.activities.MapActivity;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
import net.osmand.util.OsmUtils;

import java.lang.ref.WeakReference;
import java.util.HashMap;
Expand Down Expand Up @@ -147,7 +147,7 @@ private SearchAmenitiesTask(OsmandApplication application, RenderedObject render
double lat = MapUtils.get31LatitudeY(renderedObject.getLabelY());
double lon = MapUtils.get31LongitudeX(renderedObject.getLabelX());
rect = MapUtils.calculateLatLonBbox(lat, lon, SEARCH_POI_RADIUS);
osmId = OsmUtils.getOsmObjectId(renderedObject);
osmId = ObfConstants.getOsmObjectId(renderedObject);
app = application;
}

Expand All @@ -160,7 +160,7 @@ protected Amenity doInBackground(Void... params) {
new ResultMatcher<>() {
@Override
public boolean publish(Amenity amenity) {
long id = OsmUtils.getOsmObjectId(amenity);
long id = ObfConstants.getOsmObjectId(amenity);
return id == osmId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import net.osmand.NativeLibrary.RenderedObject;
import net.osmand.PlatformUtil;
import net.osmand.binary.ObfConstants;
import net.osmand.plus.plugins.osmedit.quickactions.ShowHideOSMEditsAction;
import net.osmand.plus.shared.SharedUtil;
import net.osmand.data.Amenity;
Expand Down Expand Up @@ -88,7 +89,6 @@
import net.osmand.render.RenderingRuleProperty;
import net.osmand.shared.io.KFile;
import net.osmand.util.Algorithms;
import net.osmand.util.OsmUtils;

import org.apache.commons.logging.Log;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -346,7 +346,7 @@ public void registerMapContextMenuActions(@NonNull MapActivity mapActivity,
PoiType poiType = amenity.getType().getPoiTypeByKeyName(amenity.getSubType());
isEditable = !amenity.getType().isWiki() && poiType != null && !poiType.isNotEditableOsm();
} else if (selectedObj instanceof MapObject) {
isEditable = OsmUtils.isOsmUrlAvailable((MapObject) selectedObj);
isEditable = ObfConstants.isOsmUrlAvailable((MapObject) selectedObj);
}
if (isEditable) {
adapter.addItem(new ContextMenuItem(MAP_CONTEXT_MENU_CREATE_POI)
Expand Down Expand Up @@ -602,13 +602,13 @@ public boolean isMenuControllerSupported(Class<? extends MenuController> menuCon
@Override
public void buildContextMenuRows(@NonNull MenuBuilder menuBuilder, @NonNull View view, Object object) {
if (object instanceof Amenity amenity) {
String link = OsmUtils.getOsmUrlForId(amenity);
String link = ObfConstants.getOsmUrlForId(amenity);
if (!Algorithms.isEmpty(link)) {
menuBuilder.buildRow(view, R.drawable.ic_action_openstreetmap_logo, null, link,
0, false, null, true, 0, true, null, false);
}
} else if (object instanceof RenderedObject renderedObject) {
String link = OsmUtils.getOsmUrlForId(renderedObject);
String link = ObfConstants.getOsmUrlForId(renderedObject);
if (!Algorithms.isEmpty(link)) {
menuBuilder.buildRow(view, R.drawable.ic_action_info_dark, null, link, 0, false,
null, true, 0, true, null, false);
Expand Down
Loading

0 comments on commit 380a20f

Please sign in to comment.