update MapUtils.decodeShortLinkString() to also parse zoom

This method is used to parse the custom OpenStreetMap shortlink algorithm
that encodes lat, lon, and zoom.  The previous version,
MapUtils.decodeShortLocString(), only parsed lat, lon from the shortlink.
This commit is contained in:
Hans-Christoph Steiner 2015-01-13 19:32:57 +01:00
parent 50abbb0d57
commit 2537d92a6a
4 changed files with 40 additions and 41 deletions

View file

@ -352,7 +352,7 @@ public class GeoPointParserUtil {
if (!expected.getQuery().equals(actual.getQuery())) if (!expected.getQuery().equals(actual.getQuery()))
throw new RuntimeException("Query param not equal"); throw new RuntimeException("Query param not equal");
} else { } else {
double aLat = actual.getLat(), eLat = expected.getLat(), aLon = actual.getLon(), eLon = expected.getLon(); double aLat = actual.getLatitude(), eLat = expected.getLatitude(), aLon = actual.getLongitude(), eLon = expected.getLongitude();
int aZoom = actual.getZoom(), eZoom = expected.getZoom(); int aZoom = actual.getZoom(), eZoom = expected.getZoom();
String aName = actual.getName(), eName = expected.getName(); String aName = actual.getName(), eName = expected.getName();
if (eName != null) { if (eName != null) {
@ -705,11 +705,11 @@ public class GeoPointParserUtil {
this.geoAddress = true; this.geoAddress = true;
} }
public double getLat() { public double getLatitude() {
return lat; return lat;
} }
public double getLon() { public double getLongitude() {
return lon; return lon;
} }

View file

@ -8,6 +8,7 @@ import java.util.List;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.MapObject; import net.osmand.data.MapObject;
import net.osmand.data.QuadPoint; import net.osmand.data.QuadPoint;
import net.osmand.util.GeoPointParserUtil.GeoParsedPoint;
/** /**
@ -286,10 +287,10 @@ public class MapUtils {
// System.out.println(buildShortOsmUrl(52.30103d, 4.862927d, 18)); // http://osm.org/go/0E4_JiVhs // System.out.println(buildShortOsmUrl(52.30103d, 4.862927d, 18)); // http://osm.org/go/0E4_JiVhs
// System.out.println(buildShortOsmUrl(40.59d, -115.213d, 9)); // http://osm.org/go/TelHTB-- // System.out.println(buildShortOsmUrl(40.59d, -115.213d, 9)); // http://osm.org/go/TelHTB--
public static String buildShortOsmUrl(double latitude, double longitude, int zoom){ public static String buildShortOsmUrl(double latitude, double longitude, int zoom){
return BASE_SHORT_OSM_URL + createShortLocString(latitude, longitude, zoom) + "?m"; return BASE_SHORT_OSM_URL + createShortLinkString(latitude, longitude, zoom) + "?m";
} }
public static String createShortLocString(double latitude, double longitude, int zoom) { public static String createShortLinkString(double latitude, double longitude, int zoom) {
long lat = (long) (((latitude + 90d)/180d)*(1L << 32)); long lat = (long) (((latitude + 90d)/180d)*(1L << 32));
long lon = (long) (((longitude + 180d)/360d)*(1L << 32)); long lon = (long) (((longitude + 180d)/360d)*(1L << 32));
long code = interleaveBits(lon, lat); long code = interleaveBits(lon, lat);
@ -306,44 +307,42 @@ public class MapUtils {
return str; return str;
} }
@SuppressWarnings("unused") public static GeoParsedPoint decodeShortLinkString(String s) {
public static LatLon decodeShortLocString(String s) { int i = 0;
long x = 0; long x = 0;
long y = 0; long y = 0;
int z = 0; int z = -8;
int z_offset = 0;
for (int i = 0; i < s.length(); i++) { for (i = 0; i < s.length(); i++) {
if (s.charAt(i) == '-') { int digit = -1;
z_offset--;
continue;
}
char c = s.charAt(i); char c = s.charAt(i);
for (int j = 0; j < intToBase64.length; j++) { for (int j = 0; j < intToBase64.length; j++)
if (c == intToBase64[j]) { if (c == intToBase64[j]) {
for (int k = 0; k < 3; k++) { digit = j;
x <<= 1;
if ((j & 32) != 0) {
x = x | 1;
}
j <<= 1;
y <<= 1;
if ((j & 32) != 0) {
y = y | 1;
}
j <<= 1;
}
z += 3;
break; break;
} }
if (digit < 0)
break;
if (digit < 0)
break;
// distribute 6 bits into x and y
x <<= 3;
y <<= 3;
for (int j = 2; j >= 0; j--) {
x |= ((digit & (1 << (j+j+1))) == 0 ? 0 : (1 << j));
y |= ((digit & (1 << (j+j))) == 0 ? 0 : (1 << j));
} }
z += 3;
} }
x <<= (32 - z); double lon = x * Math.pow(2, 2 - 3 * i) * 90. - 180;
y <<= (32 - z); double lat = y * Math.pow(2, 2 - 3 * i) * 45. - 90;
// int zoom = z - 8 - ((3 + z_offset) % 3); // adjust z
double dlat = (180d * (y) / ((double)(1L << 32))) - 90d; if(i < s.length() && s.charAt(i) == '-') {
double dlon = (360d * (x)/ ((double)(1L << 32))) - 180d; z -= 2;
return new LatLon(dlat, dlon); if(i + 1 < s.length() && s.charAt(i + 1) == '-')
z++;
}
return new GeoParsedPoint(lat, lon, z);
} }
/** /**

View file

@ -210,9 +210,9 @@ public class GeoIntentActivity extends OsmandListActivity {
GeoPointParserUtil.GeoParsedPoint p = GeoPointParserUtil.parse(uri.toString()); GeoPointParserUtil.GeoParsedPoint p = GeoPointParserUtil.parse(uri.toString());
if (p.isGeoPoint()) { if (p.isGeoPoint()) {
if (p.getName() != null) { if (p.getName() != null) {
return new GeoPointSearch(p.getLat(), p.getLon(), p.getName(), p.getZoom()); return new GeoPointSearch(p.getLatitude(), p.getLongitude(), p.getName(), p.getZoom());
} }
return new GeoPointSearch(p.getLat(), p.getLon(), p.getZoom()); return new GeoPointSearch(p.getLatitude(), p.getLongitude(), p.getZoom());
} else { } else {
return new GeoAddressSearch(p.getQuery()); return new GeoAddressSearch(p.getQuery());
} }

View file

@ -23,7 +23,6 @@ import net.osmand.PlatformUtil;
import net.osmand.access.AccessibleAlertBuilder; import net.osmand.access.AccessibleAlertBuilder;
import net.osmand.access.AccessibleToast; import net.osmand.access.AccessibleToast;
import net.osmand.data.DataTileManager; import net.osmand.data.DataTileManager;
import net.osmand.data.LatLon;
import net.osmand.plus.ApplicationMode; import net.osmand.plus.ApplicationMode;
import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.ContextMenuAdapter.OnContextMenuClick; import net.osmand.plus.ContextMenuAdapter.OnContextMenuClick;
@ -46,6 +45,7 @@ import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.plus.views.mapwidgets.StackWidgetView; import net.osmand.plus.views.mapwidgets.StackWidgetView;
import net.osmand.plus.views.mapwidgets.TextInfoWidget; import net.osmand.plus.views.mapwidgets.TextInfoWidget;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import net.osmand.util.GeoPointParserUtil.GeoParsedPoint;
import net.osmand.util.MapUtils; import net.osmand.util.MapUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -553,7 +553,7 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
} }
private File getBaseFileName(double lat, double lon, OsmandApplication app, String ext) { private File getBaseFileName(double lat, double lon, OsmandApplication app, String ext) {
String basename = MapUtils.createShortLocString(lat, lon, 15); String basename = MapUtils.createShortLinkString(lat, lon, 15);
int k = 1; int k = 1;
File f = app.getAppPath(IndexConstants.AV_INDEX_DIR); File f = app.getAppPath(IndexConstants.AV_INDEX_DIR);
f.mkdirs(); f.mkdirs();
@ -941,9 +941,9 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
encodeName = encodeName.substring(0, i); encodeName = encodeName.substring(0, i);
} }
r.file = f; r.file = f;
LatLon l = MapUtils.decodeShortLocString(encodeName); GeoParsedPoint geo = MapUtils.decodeShortLinkString(encodeName);
r.lat = l.getLatitude(); r.lat = geo.getLatitude();
r.lon = l.getLongitude(); r.lon = geo.getLongitude();
Float heading = app.getLocationProvider().getHeading(); Float heading = app.getLocationProvider().getHeading();
Location loc = app.getLocationProvider().getLastKnownLocation(); Location loc = app.getLocationProvider().getLastKnownLocation();
if (lastTakingPhoto != null && lastTakingPhoto.getName().equals(f.getName()) && heading != null) { if (lastTakingPhoto != null && lastTakingPhoto.getName().equals(f.getName()) && heading != null) {