Skip to content

Instantly share code, notes, and snippets.

Created August 5, 2014 18:23
Show Gist options
  • Save anonymous/2ebebb679a56284efc68 to your computer and use it in GitHub Desktop.
Save anonymous/2ebebb679a56284efc68 to your computer and use it in GitHub Desktop.
Coding the Details Screen
public static String getFormattedWind(Context context, float windSpeed, float degrees) {
int windFormat;
if (Utility.isMetric(context)) {
windFormat = R.string.format_wind_kmh;
} else {
windFormat = R.string.format_wind_mph;
windSpeed = .621371192237334f * windSpeed;
}
// From wind direction in degrees, determine compass direction as a string (e.g NW)
// You know what's fun, writing really long if/else statements with tons of possible
// conditions. Seriously, try it!
String direction = "Unknown";
if (degrees >= 337.5 || degrees < 22.5) {
direction = "N";
} else if (degrees >= 22.5 && degrees < 67.5) {
direction = "NE";
} else if (degrees >= 67.5 && degrees < 112.5) {
direction = "E";
} else if (degrees >= 112.5 && degrees < 157.5) {
direction = "SE";
} else if (degrees >= 157.5 && degrees < 202.5) {
direction = "S";
} else if (degrees >= 202.5 && degrees < 247.5) {
direction = "SW";
} else if (degrees >= 247.5 && degrees < 292.5) {
direction = "W";
} else if (degrees >= 292.5 || degrees < 22.5) {
direction = "NW";
}
return String.format(context.getString(windFormat), windSpeed, direction);
}
<!-- Strings for formatting weather-related data -->
<!-- Temperature format [CHAR LIMIT=5] -->
<string name="format_temperature"><xliff:g id="temp">%1.0f</xliff:g>\u00B0</string>
<!-- Windspeed formats -->
<!-- Wind in mph [CHAR LIMIT=25] -->
<string name="format_wind_mph">
Wind: <xliff:g id="speed">%1$1.0f</xliff:g> mph <xliff:g id="direction">%2$s</xliff:g>
</string>
<!-- Wind in kph [CHAR LIMIT=25] -->
<string name="format_wind_kmh">
Wind: <xliff:g id="speed">%1$1.0f</xliff:g> km/h <xliff:g id="direction">%2$s</xliff:g>
</string>
<!-- Pressure format CHAR LIMIT=25] -->
<string name="format_pressure">Pressure: <xliff:g id="pressure">%1.0f</xliff:g> hPa</string>
<!-- Humidity format CHAR LIMIT=25]-->
<string name="format_humidity">Humidity: <xliff:g id="humidity">%1.0f</xliff:g> %%</string>
@metaflow
Copy link

why not

double step = (double)(360 * 3) / 16;
StringBuilder direction = new StringBuilder();
if (degrees >= 360 - step || degrees < step) direction.append('N');
if (degrees >= 180 - step && degrees < 180 + step) direction.append('S');
if (degrees >= 90 - step && degrees < 90 + step) direction.append('E');
if (degrees >= 270 - step && degrees < 270 + step) direction.append('W');

if you don't like if's so much?
And by the way there is an error in statements - last degrees < 22.5 will not be hit as we have same condition in the very first if. I believe it should be plain else branch and direction will never have a unknown value (should we care about NaN? I bet no)

@amberwine
Copy link

I think the last 'else if' should have its condition changed to:
else if (degrees >= 292.5 && degrees < 337.5) { direction = "NW"; }

@AlanCowap
Copy link

I issued a Pull Request on github to optimise the if-statement...
https://github.com/AlanCowap/Sunshine-Version-2/pull/1/files

@pgnewell
Copy link

I think you are mistaken about the metric wind speed. It is delivered in meters/sec not kmh. (see http://openweathermap.org/weather-data#current) I think you will find your numbers are wrong. The correct factor should be something like 3.6 * 0.62.

@ereinecke
Copy link

Agree that the windspeed is wrong, but I see it a bit differently.

To convert m/sec to km/hr, we have to multiply by 1000/360, or 2.77777778

To convert m/sec to mi/hr, the conversion is 1609.34/360, or 4.47038888889

@VinQbator
Copy link

@ereinecke
1 m/s = 1 m / 1 s = 0.001 km / (1/3600) h = 3.6 km/h
1 m/s = (1/1609) mi / (1/3600) h = 3600 / 1609 mph = 2.2374... mph

@Katalune
Copy link

Explain, please, isn't this ambiguous?
String.format(context.getString(windFormat), windSpeed, direction)
if we can write it just as:
context.getString(windFormat, windSpeed, direction)

@HNeukermans
Copy link

why not create a string indexed array to map the degrees to directions

private String getWindDirection(int degree){

    int directionStepSize = 45;

    double degreeNormalized = degree + 360 - (directionStepSize/2d);

    int directionIndex = (int) Math.ceil(degreeNormalized / 45d);

    directionIndex = directionIndex%8; /*modulo = cyclic range*/

    String[] directionTable = new String[]{"N", "NE", "E", "SE", "S", "SW", "W", "NW"};
    String direction = directionTable[directionIndex];
    return direction;
}

    assertEquals("N", getWindDirection(0));
    assertEquals("N", getWindDirection(10));
    assertEquals("S", getWindDirection(180));
    assertEquals("NE", getWindDirection(45));
    assertEquals("E", getWindDirection(90));
    assertEquals("W", getWindDirection(270));
    assertEquals("N", getWindDirection(350));

@triarius
Copy link

Following @HNeukermans, I think this two line function does it

    private static String getWindDirection(float degrees) {
        String[] directions = new String[] {"N", "NE", "E", "SE", "S", "SW", "W", "NW"};
        return directions[(int) Math.round(degrees / 45) % 8];
    }

as evidenced by this unit test: http://pastebin.com/BCbFfxaF giving me this output: http://pastebin.com/sVA2wCT3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment