Get rid of 1E6 factor lat/lon conversions.

This commit is contained in:
Andreas Schildbach 2018-11-07 15:53:44 +01:00
parent 1ede41110c
commit 4e3cd9656e
11 changed files with 64 additions and 66 deletions

View file

@ -197,7 +197,7 @@ public class DirectionsShortcutActivity extends OeffiActivity
} }
private void query(final Point here) { private void query(final Point here) {
final String hereName = String.format(Locale.ENGLISH, "%.6f, %.6f", here.lat / 1E6, here.lon / 1E6); final String hereName = String.format(Locale.ENGLISH, "%.6f, %.6f", here.getLatAsDouble(), here.getLonAsDouble());
query(here, hereName); query(here, hereName);
} }

View file

@ -71,8 +71,8 @@ public class LocationTextView extends TextView {
text.append("<b>").append(location.name).append("</b>"); text.append("<b>").append(location.name).append("</b>");
if (text.length() == 0 && location.hasLocation()) if (text.length() == 0 && location.hasLocation())
text.append(getContext().getString(R.string.directions_location_view_coordinate)).append(":<br/>") text.append(getContext().getString(R.string.directions_location_view_coordinate)).append(":<br/>")
.append(String.format(Locale.ENGLISH, "%1$.6f, %2$.6f", location.lat / 1E6, .append(String.format(Locale.ENGLISH, "%1$.6f, %2$.6f", location.getLatAsDouble(),
location.lon / 1E6)); location.getLonAsDouble()));
setText(Html.fromHtml(text.toString())); setText(Html.fromHtml(text.toString()));
setCompoundDrawablesWithIntrinsicBounds( setCompoundDrawablesWithIntrinsicBounds(
showLocationType ? LocationView.locationTypeIconRes(location.type) : 0, 0, 0, 0); showLocationType ? LocationView.locationTypeIconRes(location.type) : 0, 0, 0, 0);

View file

@ -331,8 +331,8 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
public void setLocation(final Location location) { public void setLocation(final Location location) {
locationType = location.type; locationType = location.type;
id = location.id; id = location.id;
lat = location.lat / 1E6; lat = location.getLatAsDouble();
lon = location.lon / 1E6; lon = location.getLonAsDouble();
place = location.place; place = location.place;
setText(location.uniqueShortName()); setText(location.uniqueShortName());
updateAppearance(); updateAppearance();
@ -370,8 +370,7 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
else if (locationType == LocationType.ANY && Strings.isNullOrEmpty(name)) else if (locationType == LocationType.ANY && Strings.isNullOrEmpty(name))
return null; return null;
else else
return new Location(locationType, id, (int) (lat * 1E6), (int) (lon * 1E6), name != null ? place : null, return new Location(locationType, id, Point.fromDouble(lat, lon), name != null ? place : null, name);
name);
} }
private final OnClickListener contextButtonClickListener = new OnClickListener() { private final OnClickListener contextButtonClickListener = new OnClickListener() {
@ -458,8 +457,7 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
public static Location addressToLocation(final Address address) { public static Location addressToLocation(final Address address) {
final Point coord; final Point coord;
if (address.hasLatitude() && address.hasLongitude()) if (address.hasLatitude() && address.hasLongitude())
coord = new Point((int) Math.round(address.getLatitude() * 1E6), coord = Point.fromDouble(address.getLatitude(), address.getLongitude());
(int) Math.round(address.getLongitude() * 1E6));
else else
coord = null; coord = null;
@ -469,8 +467,8 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
location = new Location(LocationType.ADDRESS, null, coord, address.getAddressLine(1), location = new Location(LocationType.ADDRESS, null, coord, address.getAddressLine(1),
address.getAddressLine(0)); address.getAddressLine(0));
else if (address.getThoroughfare() != null) else if (address.getThoroughfare() != null)
location = new Location(LocationType.ADDRESS, null, coord, address.getLocality(), location = new Location(LocationType.ADDRESS, null, coord, address.getLocality(), address.getThoroughfare()
address.getThoroughfare() + (address.getFeatureName() != null ? " " + address.getFeatureName() : "")); + (address.getFeatureName() != null ? " " + address.getFeatureName() : ""));
else else
location = new Location(LocationType.ADDRESS, null, coord); location = new Location(LocationType.ADDRESS, null, coord);

View file

@ -434,9 +434,9 @@ public class TripDetailsActivity extends OeffiActivity implements LocationListen
final Trip.Public publicLeg = (Trip.Public) leg; final Trip.Public publicLeg = (Trip.Public) leg;
if (publicLeg.departure.hasLocation()) { if (publicLeg.departure.hasLocation()) {
android.location.Location.distanceBetween(publicLeg.departure.lat / 1E6, android.location.Location.distanceBetween(publicLeg.departure.getLatAsDouble(),
publicLeg.departure.lon / 1E6, location.lat / 1E6, location.lon / 1E6, publicLeg.departure.getLonAsDouble(), location.getLatAsDouble(),
distanceBetweenResults); location.getLonAsDouble(), distanceBetweenResults);
final float distance = distanceBetweenResults[0]; final float distance = distanceBetweenResults[0];
if (distance < minDistance) { if (distance < minDistance) {
minDistance = distance; minDistance = distance;
@ -448,9 +448,9 @@ public class TripDetailsActivity extends OeffiActivity implements LocationListen
if (intermediateStops != null) { if (intermediateStops != null) {
for (final Stop stop : intermediateStops) { for (final Stop stop : intermediateStops) {
if (stop.location.hasLocation()) { if (stop.location.hasLocation()) {
android.location.Location.distanceBetween(stop.location.lat / 1E6, android.location.Location.distanceBetween(stop.location.getLatAsDouble(),
stop.location.lon / 1E6, location.lat / 1E6, location.lon / 1E6, stop.location.getLonAsDouble(), location.getLatAsDouble(),
distanceBetweenResults); location.getLonAsDouble(), distanceBetweenResults);
final float distance = distanceBetweenResults[0]; final float distance = distanceBetweenResults[0];
if (distance < minDistance) { if (distance < minDistance) {
minDistance = distance; minDistance = distance;
@ -461,9 +461,9 @@ public class TripDetailsActivity extends OeffiActivity implements LocationListen
} }
if (publicLeg.arrival.hasLocation()) { if (publicLeg.arrival.hasLocation()) {
android.location.Location.distanceBetween(publicLeg.arrival.lat / 1E6, android.location.Location.distanceBetween(publicLeg.arrival.getLatAsDouble(),
publicLeg.arrival.lon / 1E6, location.lat / 1E6, location.lon / 1E6, publicLeg.arrival.getLonAsDouble(), location.getLatAsDouble(),
distanceBetweenResults); location.getLonAsDouble(), distanceBetweenResults);
final float distance = distanceBetweenResults[0]; final float distance = distanceBetweenResults[0];
if (distance < minDistance) { if (distance < minDistance) {
minDistance = distance; minDistance = distance;

View file

@ -53,6 +53,7 @@ import com.google.common.util.concurrent.MoreExecutors;
import de.schildbach.oeffi.Constants; import de.schildbach.oeffi.Constants;
import de.schildbach.oeffi.util.Downloader; import de.schildbach.oeffi.util.Downloader;
import de.schildbach.pte.NetworkId; import de.schildbach.pte.NetworkId;
import de.schildbach.pte.dto.Point;
import android.app.SearchManager; import android.app.SearchManager;
import android.content.ContentProvider; import android.content.ContentProvider;
@ -217,8 +218,7 @@ public class PlanContentProvider extends ContentProvider {
final String planId = fields[0]; final String planId = fields[0];
final int rowId = planId.hashCode(); // FIXME colliding hashcodes final int rowId = planId.hashCode(); // FIXME colliding hashcodes
final String[] coords = fields[1].split(","); final String[] coords = fields[1].split(",");
final int lat = (int) (Double.parseDouble(coords[0]) * 1E6); final Point p = Point.fromDouble(Double.parseDouble(coords[0]), Double.parseDouble(coords[1]));
final int lon = (int) (Double.parseDouble(coords[1]) * 1E6);
final Date planValidFrom = parse(fields[2], dateFormat); final Date planValidFrom = parse(fields[2], dateFormat);
final String planName = fields[3]; final String planName = fields[3];
final String planDisclaimer = numFields > 4 ? Strings.emptyToNull(fields[4]) : null; final String planDisclaimer = numFields > 4 ? Strings.emptyToNull(fields[4]) : null;
@ -234,7 +234,7 @@ public class PlanContentProvider extends ContentProvider {
filterMatch = false; filterMatch = false;
if (filterMatch) { if (filterMatch) {
cursor.newRow().add(rowId).add(planId).add(planName).add(lat).add(lon) cursor.newRow().add(rowId).add(planId).add(planName).add(p.lat).add(p.lon)
.add(planValidFrom != null ? planValidFrom.getTime() : 0).add(planDisclaimer).add(planUrl) .add(planValidFrom != null ? planValidFrom.getTime() : 0).add(planDisclaimer).add(planUrl)
.add(planNetworkLogo); .add(planNetworkLogo);
} }
@ -373,13 +373,15 @@ public class PlanContentProvider extends ContentProvider {
public int compare(final Integer index1, final Integer index2) { public int compare(final Integer index1, final Integer index2) {
cursor.moveToPosition(index1); cursor.moveToPosition(index1);
android.location.Location.distanceBetween(lat, lon, cursor.getInt(latColumn) / 1E6, final Point p1 = new Point(cursor.getInt(latColumn), cursor.getInt(lonColumn));
cursor.getInt(lonColumn) / 1E6, distanceBetweenResults); android.location.Location.distanceBetween(lat, lon, p1.getLatAsDouble(), p1.getLonAsDouble(),
distanceBetweenResults);
final float dist1 = distanceBetweenResults[0]; final float dist1 = distanceBetweenResults[0];
cursor.moveToPosition(index2); cursor.moveToPosition(index2);
android.location.Location.distanceBetween(lat, lon, cursor.getInt(latColumn) / 1E6, final Point p2 = new Point(cursor.getInt(latColumn), cursor.getInt(lonColumn));
cursor.getInt(lonColumn) / 1E6, distanceBetweenResults); android.location.Location.distanceBetween(lat, lon, p2.getLatAsDouble(), p2.getLonAsDouble(),
distanceBetweenResults);
final float dist2 = distanceBetweenResults[0]; final float dist2 = distanceBetweenResults[0];
return Float.compare(dist1, dist2); return Float.compare(dist1, dist2);
} }

View file

@ -256,7 +256,7 @@ public class PlansPickerActivity extends OeffiMainActivity implements ActivityCo
private void requery() { private void requery() {
final String sortOrder = location != null final String sortOrder = location != null
? Double.toString(location.lat / 1E6) + "," + Double.toString(location.lon / 1E6) : null; ? Double.toString(location.getLatAsDouble()) + "," + Double.toString(location.getLonAsDouble()) : null;
final Uri.Builder uri = PlanContentProvider.CONTENT_URI.buildUpon(); final Uri.Builder uri = PlanContentProvider.CONTENT_URI.buildUpon();
if (filter != null) if (filter != null)
uri.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY).appendPath(filter); uri.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY).appendPath(filter);

View file

@ -39,6 +39,7 @@ import de.schildbach.oeffi.util.Objects;
import de.schildbach.pte.NetworkId; import de.schildbach.pte.NetworkId;
import de.schildbach.pte.NetworkProvider; import de.schildbach.pte.NetworkProvider;
import de.schildbach.pte.dto.Departure; import de.schildbach.pte.dto.Departure;
import de.schildbach.pte.dto.Point;
import de.schildbach.pte.dto.QueryDeparturesResult; import de.schildbach.pte.dto.QueryDeparturesResult;
import de.schildbach.pte.dto.StationDepartures; import de.schildbach.pte.dto.StationDepartures;
import de.schildbach.pte.exception.BlockedException; import de.schildbach.pte.exception.BlockedException;
@ -194,8 +195,7 @@ public class NearestFavoriteStationWidgetService extends JobIntentService {
final String stationId = favCursor.getString(stationIdCol); final String stationId = favCursor.getString(stationIdCol);
String stationPlace = favCursor.getString(stationPlaceCol); String stationPlace = favCursor.getString(stationPlaceCol);
String stationName = favCursor.getString(stationNameCol); String stationName = favCursor.getString(stationNameCol);
double stationLat = favCursor.getInt(stationLatCol) / 1E6; Point stationPoint = new Point(favCursor.getInt(stationLatCol), favCursor.getInt(stationLonCol));
double stationLon = favCursor.getInt(stationLonCol) / 1E6;
try { try {
final NetworkId networkId = NetworkId.valueOf(network); final NetworkId networkId = NetworkId.valueOf(network);
@ -214,17 +214,16 @@ public class NearestFavoriteStationWidgetService extends JobIntentService {
if (placeCol != -1) if (placeCol != -1)
stationPlace = stationCursor.getString(placeCol); stationPlace = stationCursor.getString(placeCol);
stationName = stationCursor.getString(nameCol); stationName = stationCursor.getString(nameCol);
stationLat = stationCursor.getInt(latCol) / 1E6; stationPoint = new Point(stationCursor.getInt(latCol), stationCursor.getInt(lonCol));
stationLon = stationCursor.getInt(lonCol) / 1E6;
} }
stationCursor.close(); stationCursor.close();
} }
if (stationLat > 0 || stationLon > 0) { if (stationPoint.lat > 0 || stationPoint.lon > 0) {
final float[] distanceBetweenResults = new float[1]; final float[] distanceBetweenResults = new float[1];
android.location.Location.distanceBetween(here.getLatitude(), here.getLongitude(), stationLat, android.location.Location.distanceBetween(here.getLatitude(), here.getLongitude(),
stationLon, distanceBetweenResults); stationPoint.getLatAsDouble(), stationPoint.getLonAsDouble(), distanceBetweenResults);
final float distance = distanceBetweenResults[0]; final float distance = distanceBetweenResults[0];
final Favorite favorite = new Favorite(networkId, stationId, stationPlace, stationName, final Favorite favorite = new Favorite(networkId, stationId, stationPlace, stationName,
distance); distance);

View file

@ -122,8 +122,8 @@ public class StationContextMenu extends PopupMenu {
new MenuInflater(context).inflate(R.menu.station_map_context, menu); new MenuInflater(context).inflate(R.menu.station_map_context, menu);
final float lat = location.lat / 1E6f; final double lat = location.getLatAsDouble();
final float lon = location.lon / 1E6f; final double lon = location.getLonAsDouble();
final String name = location.name; final String name = location.name;
final MenuItem googleMapsItem = menu.findItem(R.id.station_map_context_google_maps); final MenuItem googleMapsItem = menu.findItem(R.id.station_map_context_google_maps);

View file

@ -579,9 +579,9 @@ public class StationsActivity extends OeffiMainActivity implements StationsAware
i.remove(); i.remove();
stationsMap.remove(station.location.id); stationsMap.remove(station.location.id);
} else if (station.location.hasLocation()) { } else if (station.location.hasLocation()) {
android.location.Location.distanceBetween(deviceLocation.lat / 1E6, android.location.Location.distanceBetween(deviceLocation.getLatAsDouble(),
deviceLocation.lon / 1E6, station.location.lat / 1E6, deviceLocation.getLonAsDouble(), station.location.getLatAsDouble(),
station.location.lon / 1E6, distanceBetweenResults); station.location.getLonAsDouble(), distanceBetweenResults);
station.setDistanceAndBearing(distanceBetweenResults[0], distanceBetweenResults[1]); station.setDistanceAndBearing(distanceBetweenResults[0], distanceBetweenResults[1]);
} }
} }
@ -674,9 +674,9 @@ public class StationsActivity extends OeffiMainActivity implements StationsAware
// location box // location box
findViewById(R.id.stations_location_box).setVisibility(fixedLocation != null ? View.VISIBLE : View.GONE); findViewById(R.id.stations_location_box).setVisibility(fixedLocation != null ? View.VISIBLE : View.GONE);
if (fixedLocation != null) if (fixedLocation != null)
((TextView) findViewById(R.id.stations_location_text)).setText(fixedLocation.name != null ((TextView) findViewById(R.id.stations_location_text))
? fixedLocation.name .setText(fixedLocation.name != null ? fixedLocation.name : String.format(Locale.ENGLISH,
: String.format(Locale.ENGLISH, "%.6f, %.6f", fixedLocation.lat / 1E6, fixedLocation.lon / 1E6)); "%.6f, %.6f", fixedLocation.getLatAsDouble(), fixedLocation.getLonAsDouble()));
// search box // search box
findViewById(R.id.stations_search_box).setVisibility(searchQuery != null ? View.VISIBLE : View.GONE); findViewById(R.id.stations_search_box).setVisibility(searchQuery != null ? View.VISIBLE : View.GONE);
@ -780,8 +780,8 @@ public class StationsActivity extends OeffiMainActivity implements StationsAware
if (referenceLocation != null) { if (referenceLocation != null) {
final MyActionBar actionBar = getMyActionBar(); final MyActionBar actionBar = getMyActionBar();
final double referenceLat = referenceLocation.lat / 1E6; final double referenceLat = referenceLocation.getLatAsDouble();
final double referenceLon = referenceLocation.lon / 1E6; final double referenceLon = referenceLocation.getLonAsDouble();
final StringBuilder favoriteIds = new StringBuilder(); final StringBuilder favoriteIds = new StringBuilder();
for (final Map.Entry<String, Integer> entry : favorites.entrySet()) for (final Map.Entry<String, Integer> entry : favorites.entrySet())
@ -840,13 +840,13 @@ public class StationsActivity extends OeffiMainActivity implements StationsAware
: cursor.getString(nativeIdColumnIndex); : cursor.getString(nativeIdColumnIndex);
final String place = placeColumnIndex != -1 ? cursor.getString(placeColumnIndex) : null; final String place = placeColumnIndex != -1 ? cursor.getString(placeColumnIndex) : null;
final String name = cursor.getString(nameColumnIndex); final String name = cursor.getString(nameColumnIndex);
final int lat = cursor.getInt(latColumnIndex); final Point p = new Point(cursor.getInt(latColumnIndex), cursor.getInt(lonColumnIndex));
final int lon = cursor.getInt(lonColumnIndex); final Station station = new Station(network,
final Station station = new Station(network, new de.schildbach.pte.dto.Location( new de.schildbach.pte.dto.Location(LocationType.STATION, id, p, place, name),
LocationType.STATION, id, lat, lon, place, name), lineDestinations); lineDestinations);
if (deviceLocation != null) { if (deviceLocation != null) {
android.location.Location.distanceBetween(referenceLat, referenceLon, lat / 1E6, android.location.Location.distanceBetween(referenceLat, referenceLon,
lon / 1E6, distanceBetweenResults); p.getLatAsDouble(), p.getLonAsDouble(), distanceBetweenResults);
station.setDistanceAndBearing(distanceBetweenResults[0], distanceBetweenResults[1]); station.setDistanceAndBearing(distanceBetweenResults[0], distanceBetweenResults[1]);
} }
freshStations.add(station); freshStations.add(station);
@ -887,8 +887,9 @@ public class StationsActivity extends OeffiMainActivity implements StationsAware
if (favType == FavoriteStationsProvider.TYPE_FAVORITE) { if (favType == FavoriteStationsProvider.TYPE_FAVORITE) {
final Station station = new Station(network, location, null); final Station station = new Station(network, location, null);
if (deviceLocation != null && location.hasLocation()) { if (deviceLocation != null && location.hasLocation()) {
android.location.Location.distanceBetween(deviceLocation.lat / 1E6, deviceLocation.lon / 1E6, android.location.Location.distanceBetween(deviceLocation.getLatAsDouble(),
location.lat / 1E6, location.lon / 1E6, distanceBetweenResults); deviceLocation.getLonAsDouble(), location.getLatAsDouble(), location.getLonAsDouble(),
distanceBetweenResults);
station.setDistanceAndBearing(distanceBetweenResults[0], distanceBetweenResults[1]); station.setDistanceAndBearing(distanceBetweenResults[0], distanceBetweenResults[1]);
} }
freshStations.add(station); freshStations.add(station);
@ -906,15 +907,15 @@ public class StationsActivity extends OeffiMainActivity implements StationsAware
final Location referenceLocation = getReferenceLocation(); final Location referenceLocation = getReferenceLocation();
if (referenceLocation != null) { if (referenceLocation != null) {
final double referenceLat = referenceLocation.lat / 1E6; final double referenceLat = referenceLocation.getLatAsDouble();
final double referenceLon = referenceLocation.lon / 1E6; final double referenceLon = referenceLocation.getLonAsDouble();
final float[] distanceBetweenResults = new float[2]; final float[] distanceBetweenResults = new float[2];
for (final Station freshStation : freshStations) { for (final Station freshStation : freshStations) {
if (freshStation.location.hasLocation()) { if (freshStation.location.hasLocation()) {
android.location.Location.distanceBetween(referenceLat, referenceLon, android.location.Location.distanceBetween(referenceLat, referenceLon,
freshStation.location.lat / 1E6, freshStation.location.lon / 1E6, freshStation.location.getLatAsDouble(), freshStation.location.getLonAsDouble(),
distanceBetweenResults); distanceBetweenResults);
freshStation.setDistanceAndBearing(distanceBetweenResults[0], freshStation.setDistanceAndBearing(distanceBetweenResults[0],
distanceBetweenResults[1]); distanceBetweenResults[1]);
@ -1441,8 +1442,8 @@ public class StationsActivity extends OeffiMainActivity implements StationsAware
for (final Station station : stations) { for (final Station station : stations) {
if (station.location.hasLocation()) { if (station.location.hasLocation()) {
android.location.Location.distanceBetween(hereLat, hereLon, station.location.lat / 1E6, android.location.Location.distanceBetween(hereLat, hereLon, station.location.getLatAsDouble(),
station.location.lon / 1E6, distanceBetweenResults); station.location.getLonAsDouble(), distanceBetweenResults);
station.setDistanceAndBearing(distanceBetweenResults[0], distanceBetweenResults[1]); station.setDistanceAndBearing(distanceBetweenResults[0], distanceBetweenResults[1]);
} }
} }

View file

@ -51,7 +51,7 @@ public class GeocoderThread extends Thread {
private static final Logger log = LoggerFactory.getLogger(GeocoderThread.class); private static final Logger log = LoggerFactory.getLogger(GeocoderThread.class);
public GeocoderThread(final Context context, final Point coord, final Callback callback) { public GeocoderThread(final Context context, final Point coord, final Callback callback) {
this(context, coord.lat / 1E6, coord.lon / 1E6, callback); this(context, coord.getLatAsDouble(), coord.getLonAsDouble(), callback);
} }
public GeocoderThread(final Context context, final double latitude, final double longitude, public GeocoderThread(final Context context, final double latitude, final double longitude,

View file

@ -173,18 +173,16 @@ public class LocationUriParser {
if (!m.matches()) if (!m.matches())
return null; return null;
final int lat = (int) Math.round(Double.parseDouble(m.group(2)) * 1E6); final Point p = Point.fromDouble(Double.parseDouble(m.group(2)), Double.parseDouble(m.group(3)));
final int lon = (int) Math.round(Double.parseDouble(m.group(3)) * 1E6);
if (title != null) if (title != null)
return new Location(LocationType.ADDRESS, null, lat, lon, null, title); return new Location(LocationType.ADDRESS, null, p, null, title);
else if (m.group(1) != null) else if (m.group(1) != null)
return new Location(LocationType.ADDRESS, null, lat, lon, null, return new Location(LocationType.ADDRESS, null, p, null, m.group(1).length() > 0 ? m.group(1) : null);
m.group(1).length() > 0 ? m.group(1) : null);
else if (m.group(4) != null) else if (m.group(4) != null)
return new Location(LocationType.ADDRESS, null, lat, lon, null, m.group(4)); return new Location(LocationType.ADDRESS, null, p, null, m.group(4));
else else
return Location.coord(lat, lon); return Location.coord(p);
} }
private static String normalizeDecodeParam(final String raw) { private static String normalizeDecodeParam(final String raw) {