use of net.osmand.plus.settings.enums.MetricsConstants in project Osmand by osmandapp.
the class TripHelper method getDistance.
static Distance getDistance(@NonNull OsmandApplication app, double meters) {
MetricsConstants mc = app.getSettings().METRIC_SYSTEM.get();
int displayUnit;
float mainUnitInMeters;
if (mc == net.osmand.plus.settings.enums.MetricsConstants.KILOMETERS_AND_METERS) {
displayUnit = Distance.UNIT_KILOMETERS;
mainUnitInMeters = METERS_IN_KILOMETER;
} else if (mc == net.osmand.plus.settings.enums.MetricsConstants.NAUTICAL_MILES) {
displayUnit = Distance.UNIT_MILES;
mainUnitInMeters = METERS_IN_ONE_NAUTICALMILE;
} else {
displayUnit = Distance.UNIT_MILES;
mainUnitInMeters = METERS_IN_ONE_MILE;
}
if (meters >= 100 * mainUnitInMeters) {
return Distance.create(meters / mainUnitInMeters, displayUnit);
} else if (meters > 9.99f * mainUnitInMeters) {
return Distance.create(meters / mainUnitInMeters, displayUnit);
} else if (meters > 0.999f * mainUnitInMeters) {
return Distance.create(meters / mainUnitInMeters, displayUnit);
} else if (mc == net.osmand.plus.settings.enums.MetricsConstants.MILES_AND_FEET && meters > 0.249f * mainUnitInMeters) {
return Distance.create(meters / mainUnitInMeters, displayUnit);
} else if (mc == net.osmand.plus.settings.enums.MetricsConstants.MILES_AND_METERS && meters > 0.249f * mainUnitInMeters) {
return Distance.create(meters / mainUnitInMeters, displayUnit);
} else if (mc == net.osmand.plus.settings.enums.MetricsConstants.MILES_AND_YARDS && meters > 0.249f * mainUnitInMeters) {
return Distance.create(meters / mainUnitInMeters, displayUnit);
} else if (mc == net.osmand.plus.settings.enums.MetricsConstants.NAUTICAL_MILES && meters > 0.99f * mainUnitInMeters) {
return Distance.create(meters / mainUnitInMeters, displayUnit);
} else {
if (mc == net.osmand.plus.settings.enums.MetricsConstants.KILOMETERS_AND_METERS || mc == net.osmand.plus.settings.enums.MetricsConstants.MILES_AND_METERS) {
return Distance.create(meters, Distance.UNIT_METERS);
} else if (mc == net.osmand.plus.settings.enums.MetricsConstants.MILES_AND_FEET) {
return Distance.create(meters * FEET_IN_ONE_METER, Distance.UNIT_FEET);
} else if (mc == net.osmand.plus.settings.enums.MetricsConstants.MILES_AND_YARDS) {
return Distance.create(meters * YARDS_IN_ONE_METER, Distance.UNIT_YARDS);
}
return Distance.create(meters, Distance.UNIT_METERS);
}
}
use of net.osmand.plus.settings.enums.MetricsConstants in project Osmand by osmandapp.
the class GpxUiHelper method createGPXSlopeDataSet.
public static OrderedLineDataSet createGPXSlopeDataSet(@NonNull OsmandApplication ctx, @NonNull LineChart mChart, @NonNull GPXTrackAnalysis analysis, @NonNull GPXDataSetAxisType axisType, @Nullable List<Entry> eleValues, boolean useRightAxis, boolean drawFilled, boolean calcWithoutGaps) {
if (axisType == GPXDataSetAxisType.TIME || axisType == GPXDataSetAxisType.TIMEOFDAY) {
return null;
}
OsmandSettings settings = ctx.getSettings();
boolean light = settings.isLightContent();
MetricsConstants mc = settings.METRIC_SYSTEM.get();
boolean useFeet = (mc == MetricsConstants.MILES_AND_FEET) || (mc == MetricsConstants.MILES_AND_YARDS);
final float convEle = useFeet ? 3.28084f : 1.0f;
final float totalDistance = calcWithoutGaps ? analysis.totalDistanceWithoutGaps : analysis.totalDistance;
XAxis xAxis = mChart.getXAxis();
float divX = setupAxisDistance(ctx, xAxis, calcWithoutGaps ? analysis.totalDistanceWithoutGaps : analysis.totalDistance);
final String mainUnitY = "%";
YAxis yAxis;
if (useRightAxis) {
yAxis = mChart.getAxisRight();
yAxis.setEnabled(true);
} else {
yAxis = mChart.getAxisLeft();
}
yAxis.setTextColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_green_label));
yAxis.setGranularity(1f);
yAxis.resetAxisMinimum();
yAxis.setValueFormatter((value, axis) -> OsmAndFormatter.formatInteger((int) value, mainUnitY, ctx));
List<Entry> values;
if (eleValues == null) {
values = calculateElevationArray(analysis, GPXDataSetAxisType.DISTANCE, 1f, 1f, false, calcWithoutGaps);
} else {
values = new ArrayList<>(eleValues.size());
for (Entry e : eleValues) {
values.add(new Entry(e.getX() * divX, e.getY() / convEle));
}
}
if (Algorithms.isEmpty(values)) {
if (useRightAxis) {
yAxis.setEnabled(false);
}
return null;
}
int lastIndex = values.size() - 1;
double STEP = 5;
int l = 10;
while (l > 0 && totalDistance / STEP > MAX_CHART_DATA_ITEMS) {
STEP = Math.max(STEP, totalDistance / (values.size() * l--));
}
double[] calculatedDist = new double[(int) (totalDistance / STEP) + 1];
double[] calculatedH = new double[(int) (totalDistance / STEP) + 1];
int nextW = 0;
for (int k = 0; k < calculatedDist.length; k++) {
if (k > 0) {
calculatedDist[k] = calculatedDist[k - 1] + STEP;
}
while (nextW < lastIndex && calculatedDist[k] > values.get(nextW).getX()) {
nextW++;
}
double pd = nextW == 0 ? 0 : values.get(nextW - 1).getX();
double ph = nextW == 0 ? values.get(0).getY() : values.get(nextW - 1).getY();
calculatedH[k] = ph + (values.get(nextW).getY() - ph) / (values.get(nextW).getX() - pd) * (calculatedDist[k] - pd);
}
double SLOPE_PROXIMITY = Math.max(100, STEP * 2);
if (totalDistance - SLOPE_PROXIMITY < 0) {
if (useRightAxis) {
yAxis.setEnabled(false);
}
return null;
}
double[] calculatedSlopeDist = new double[(int) ((totalDistance - SLOPE_PROXIMITY) / STEP) + 1];
double[] calculatedSlope = new double[(int) ((totalDistance - SLOPE_PROXIMITY) / STEP) + 1];
int index = (int) ((SLOPE_PROXIMITY / STEP) / 2);
for (int k = 0; k < calculatedSlopeDist.length; k++) {
calculatedSlopeDist[k] = calculatedDist[index + k];
calculatedSlope[k] = (calculatedH[2 * index + k] - calculatedH[k]) * 100 / SLOPE_PROXIMITY;
if (Double.isNaN(calculatedSlope[k])) {
calculatedSlope[k] = 0;
}
}
List<Entry> slopeValues = new ArrayList<>(calculatedSlopeDist.length);
float prevSlope = -80000;
float slope;
float x;
float lastXSameY = 0;
boolean hasSameY = false;
Entry lastEntry = null;
lastIndex = calculatedSlopeDist.length - 1;
for (int i = 0; i < calculatedSlopeDist.length; i++) {
x = (float) calculatedSlopeDist[i] / divX;
slope = (float) calculatedSlope[i];
if (prevSlope != -80000) {
if (prevSlope == slope && i < lastIndex) {
hasSameY = true;
lastXSameY = x;
continue;
}
if (hasSameY) {
slopeValues.add(new Entry(lastXSameY, lastEntry.getY()));
}
hasSameY = false;
}
prevSlope = slope;
lastEntry = new Entry(x, slope);
slopeValues.add(lastEntry);
}
OrderedLineDataSet dataSet = new OrderedLineDataSet(slopeValues, "", GPXDataSetType.SLOPE, axisType, !useRightAxis);
dataSet.divX = divX;
dataSet.units = mainUnitY;
dataSet.setColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_green));
dataSet.setLineWidth(1f);
if (drawFilled) {
dataSet.setFillAlpha(128);
dataSet.setFillColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_green));
dataSet.setDrawFilled(true);
} else {
dataSet.setDrawFilled(false);
}
dataSet.setDrawValues(false);
dataSet.setValueTextSize(9f);
dataSet.setFormLineWidth(1f);
dataSet.setFormSize(15.f);
dataSet.setDrawCircles(false);
dataSet.setDrawCircleHole(false);
dataSet.setHighlightEnabled(true);
dataSet.setDrawVerticalHighlightIndicator(true);
dataSet.setDrawHorizontalHighlightIndicator(false);
dataSet.setHighLightColor(ColorUtilities.getSecondaryTextColor(mChart.getContext(), !light));
/*
dataSet.setFillFormatter(new IFillFormatter() {
@Override
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
return dataProvider.getYChartMin();
}
});
*/
if (useRightAxis) {
dataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
}
return dataSet;
}
use of net.osmand.plus.settings.enums.MetricsConstants in project Osmand by osmandapp.
the class GpxSelectionHelper method formatSplitName.
private static String formatSplitName(double metricEnd, GpxDisplayGroup group, OsmandApplication app) {
if (group.isSplitDistance()) {
MetricsConstants mc = app.getSettings().METRIC_SYSTEM.get();
if (mc == MetricsConstants.KILOMETERS_AND_METERS) {
final double sd = group.getSplitDistance();
int digits = sd < 100 ? 2 : (sd < 1000 ? 1 : 0);
int rem1000 = (int) (metricEnd + 0.5) % 1000;
if (rem1000 > 1 && digits < 1) {
digits = 1;
}
int rem100 = (int) (metricEnd + 0.5) % 100;
if (rem100 > 1 && digits < 2) {
digits = 2;
}
return OsmAndFormatter.getFormattedRoundDistanceKm((float) metricEnd, digits, app);
} else {
return OsmAndFormatter.getFormattedDistance((float) metricEnd, app);
}
} else {
return Algorithms.formatDuration((int) metricEnd, app.accessibilityEnabled());
}
}
use of net.osmand.plus.settings.enums.MetricsConstants in project Osmand by osmandapp.
the class OsmandSettings method setPreference.
// TODO doesn't look correct
@SuppressWarnings("unchecked")
public boolean setPreference(String key, Object value, ApplicationMode mode) {
OsmandPreference<?> preference = registeredPreferences.get(key);
if (preference != null) {
if (preference == APPLICATION_MODE) {
if (value instanceof String) {
String appModeKey = (String) value;
ApplicationMode appMode = ApplicationMode.valueOfStringKey(appModeKey, null);
if (appMode != null) {
setApplicationMode(appMode);
return true;
}
}
} else if (preference == DEFAULT_APPLICATION_MODE) {
if (value instanceof String) {
String appModeKey = (String) value;
ApplicationMode appMode = ApplicationMode.valueOfStringKey(appModeKey, null);
if (appMode != null) {
DEFAULT_APPLICATION_MODE.set(appMode);
return true;
}
}
} else if (preference == METRIC_SYSTEM) {
MetricsConstants metricSystem = null;
if (value instanceof String) {
String metricSystemName = (String) value;
try {
metricSystem = MetricsConstants.valueOf(metricSystemName);
} catch (IllegalArgumentException e) {
return false;
}
} else if (value instanceof Integer) {
int index = (Integer) value;
if (index >= 0 && index < MetricsConstants.values().length) {
metricSystem = MetricsConstants.values()[index];
}
}
if (metricSystem != null) {
METRIC_SYSTEM.setModeValue(mode, metricSystem);
return true;
}
} else if (preference == SPEED_SYSTEM) {
SpeedConstants speedSystem = null;
if (value instanceof String) {
String speedSystemName = (String) value;
try {
speedSystem = SpeedConstants.valueOf(speedSystemName);
} catch (IllegalArgumentException e) {
return false;
}
} else if (value instanceof Integer) {
int index = (Integer) value;
if (index >= 0 && index < SpeedConstants.values().length) {
speedSystem = SpeedConstants.values()[index];
}
}
if (speedSystem != null) {
SPEED_SYSTEM.setModeValue(mode, speedSystem);
return true;
}
} else if (preference instanceof BooleanPreference) {
if (value instanceof Boolean) {
((BooleanPreference) preference).setModeValue(mode, (Boolean) value);
return true;
}
} else if (preference instanceof StringPreference) {
if (value instanceof String) {
((StringPreference) preference).setModeValue(mode, (String) value);
return true;
}
} else if (preference instanceof FloatPreference) {
if (value instanceof Float) {
((FloatPreference) preference).setModeValue(mode, (Float) value);
return true;
}
} else if (preference instanceof IntPreference) {
if (value instanceof Integer) {
((IntPreference) preference).setModeValue(mode, (Integer) value);
return true;
}
} else if (preference instanceof LongPreference) {
if (value instanceof Long) {
((LongPreference) preference).setModeValue(mode, (Long) value);
return true;
}
} else if (preference instanceof EnumStringPreference) {
EnumStringPreference enumPref = (EnumStringPreference) preference;
if (value instanceof String) {
Enum<?> enumValue = enumPref.parseString((String) value);
if (enumValue != null) {
return enumPref.setModeValue(mode, enumValue);
}
return false;
} else if (value instanceof Enum) {
return enumPref.setModeValue(mode, value);
} else if (value instanceof Integer) {
int newVal = (Integer) value;
if (enumPref.getValues().length > newVal) {
Enum<?> enumValue = enumPref.getValues()[newVal];
return enumPref.setModeValue(mode, enumValue);
}
return false;
}
} else if (preference instanceof ContextMenuItemsPreference) {
if (value instanceof ContextMenuItemsSettings) {
((ContextMenuItemsPreference) preference).setModeValue(mode, (ContextMenuItemsSettings) value);
}
}
}
return false;
}
use of net.osmand.plus.settings.enums.MetricsConstants in project Osmand by osmandapp.
the class RadiusRulerControlLayer method updateData.
private void updateData(RotatedTileBox tb, QuadPoint center) {
if (tb.getPixHeight() > 0 && tb.getPixWidth() > 0 && maxRadiusInDp > 0 && !Double.isNaN(tb.getLatitude()) && !Double.isNaN(tb.getLongitude())) {
if (cacheCenter.y != center.y || cacheCenter.x != center.x) {
cacheCenter = center;
updateCenter(tb, center);
}
MetricsConstants currentMetricSystem = app.getSettings().METRIC_SYSTEM.get();
float mapDensity = getMapDensity();
boolean updateCache = tb.getZoom() != cacheIntZoom || !tb.getCenterLatLon().equals(cacheCenterLatLon) || mapDensity != cacheMapDensity || cacheMetricSystem != currentMetricSystem;
if (!tb.isZoomAnimated() && updateCache) {
cacheMetricSystem = currentMetricSystem;
cacheIntZoom = tb.getZoom();
LatLon centerLatLon = tb.getCenterLatLon();
cacheCenterLatLon = new LatLon(centerLatLon.getLatitude(), centerLatLon.getLongitude());
cacheMapDensity = mapDensity;
updateDistance(tb);
}
}
}
Aggregations