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) {
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);
}

View file

@ -71,8 +71,8 @@ public class LocationTextView extends TextView {
text.append("<b>").append(location.name).append("</b>");
if (text.length() == 0 && location.hasLocation())
text.append(getContext().getString(R.string.directions_location_view_coordinate)).append(":<br/>")
.append(String.format(Locale.ENGLISH, "%1$.6f, %2$.6f", location.lat / 1E6,
location.lon / 1E6));
.append(String.format(Locale.ENGLISH, "%1$.6f, %2$.6f", location.getLatAsDouble(),
location.getLonAsDouble()));
setText(Html.fromHtml(text.toString()));
setCompoundDrawablesWithIntrinsicBounds(
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) {
locationType = location.type;
id = location.id;
lat = location.lat / 1E6;
lon = location.lon / 1E6;
lat = location.getLatAsDouble();
lon = location.getLonAsDouble();
place = location.place;
setText(location.uniqueShortName());
updateAppearance();
@ -370,8 +370,7 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
else if (locationType == LocationType.ANY && Strings.isNullOrEmpty(name))
return null;
else
return new Location(locationType, id, (int) (lat * 1E6), (int) (lon * 1E6), name != null ? place : null,
name);
return new Location(locationType, id, Point.fromDouble(lat, lon), name != null ? place : null, name);
}
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) {
final Point coord;
if (address.hasLatitude() && address.hasLongitude())
coord = new Point((int) Math.round(address.getLatitude() * 1E6),
(int) Math.round(address.getLongitude() * 1E6));
coord = Point.fromDouble(address.getLatitude(), address.getLongitude());
else
coord = null;
@ -469,8 +467,8 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
location = new Location(LocationType.ADDRESS, null, coord, address.getAddressLine(1),
address.getAddressLine(0));
else if (address.getThoroughfare() != null)
location = new Location(LocationType.ADDRESS, null, coord, address.getLocality(),
address.getThoroughfare() + (address.getFeatureName() != null ? " " + address.getFeatureName() : ""));
location = new Location(LocationType.ADDRESS, null, coord, address.getLocality(), address.getThoroughfare()
+ (address.getFeatureName() != null ? " " + address.getFeatureName() : ""));
else
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;
if (publicLeg.departure.hasLocation()) {
android.location.Location.distanceBetween(publicLeg.departure.lat / 1E6,
publicLeg.departure.lon / 1E6, location.lat / 1E6, location.lon / 1E6,
distanceBetweenResults);
android.location.Location.distanceBetween(publicLeg.departure.getLatAsDouble(),
publicLeg.departure.getLonAsDouble(), location.getLatAsDouble(),
location.getLonAsDouble(), distanceBetweenResults);
final float distance = distanceBetweenResults[0];
if (distance < minDistance) {
minDistance = distance;
@ -448,9 +448,9 @@ public class TripDetailsActivity extends OeffiActivity implements LocationListen
if (intermediateStops != null) {
for (final Stop stop : intermediateStops) {
if (stop.location.hasLocation()) {
android.location.Location.distanceBetween(stop.location.lat / 1E6,
stop.location.lon / 1E6, location.lat / 1E6, location.lon / 1E6,
distanceBetweenResults);
android.location.Location.distanceBetween(stop.location.getLatAsDouble(),
stop.location.getLonAsDouble(), location.getLatAsDouble(),
location.getLonAsDouble(), distanceBetweenResults);
final float distance = distanceBetweenResults[0];
if (distance < minDistance) {
minDistance = distance;
@ -461,9 +461,9 @@ public class TripDetailsActivity extends OeffiActivity implements LocationListen
}
if (publicLeg.arrival.hasLocation()) {
android.location.Location.distanceBetween(publicLeg.arrival.lat / 1E6,
publicLeg.arrival.lon / 1E6, location.lat / 1E6, location.lon / 1E6,
distanceBetweenResults);
android.location.Location.distanceBetween(publicLeg.arrival.getLatAsDouble(),
publicLeg.arrival.getLonAsDouble(), location.getLatAsDouble(),
location.getLonAsDouble(), distanceBetweenResults);
final float distance = distanceBetweenResults[0];
if (distance < minDistance) {
minDistance = distance;

View file

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

View file

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

View file

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

View file

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