Skip to content

Commit

Permalink
Merge branch 'cieslarmichal:main' into feature/anytime-func
Browse files Browse the repository at this point in the history
  • Loading branch information
tonykcao authored Oct 4, 2024
2 parents e33d900 + 37ee841 commit ee1d39f
Show file tree
Hide file tree
Showing 7 changed files with 1,309 additions and 0 deletions.
23 changes: 23 additions & 0 deletions include/faker-cxx/location.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <string>
#include <string_view>
#include <limits>

#include "faker-cxx/export.h"
#include "faker-cxx/types/locale.h"
Expand Down Expand Up @@ -150,6 +151,28 @@ FAKER_CXX_EXPORT std::string latitude(Precision precision = Precision::FourDp);
*/
FAKER_CXX_EXPORT std::string longitude(Precision precision = Precision::FourDp);

/**
* @brief Generates a random GPS coordinate within the specified radius from the given coordinate.
*
* @param precision The number of decimal points of precision for the latitude and longitude. Defaults to `Precision::FourDp`.
* @param origin The origin GPS coordinate. Defaults to a random GPS coordinate.
* @param radius The radius in kilometers or miles. Defaults to 10.
* @param isMetric The unit of radius. Defaults to false which means miles.
*
* @returns GPS coordinate within the specified radius from the given coordinate.
*
* @code
* faker::location::nearbyGPSCoordinate() // "48.8566", "2.3522"
* faker::location::nearbyGPSCoordinate(Precision::FourDp, {33, -170}) // "33.0165", "-170.0636"
* faker::location::nearbyGPSCoordinate(Precision::FourDp, {33, -170}, 1000, true) // "37.9163", "-179.2408"
* @endcode
*/
FAKER_CXX_EXPORT std::tuple<std::string, std::string> nearbyGPSCoordinate(
Precision precision = Precision::FourDp,
const std::tuple<double, double>& origin = { std::numeric_limits<double>::max(), std::numeric_limits<double>::max() },
double radius = 10,
bool isMetric = false);

/**
* @brief Generates a random direction from cardinal and ordinal directions.
*
Expand Down
29 changes: 29 additions & 0 deletions include/faker-cxx/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,35 @@ FAKER_CXX_EXPORT std::string sample(unsigned length = 10);
*/
FAKER_CXX_EXPORT std::string sample(GuaranteeMap&& guarantee, unsigned length = 10);

/**
* @brief Returns a string containing "~`!@#$%^&*()_-+={[}]|:;\"'<,>.?/".
*
* @param length The number of characters to generate. Defaults to `10`.
*
* @returns Sample string.
*
* @code
* faker::string::sample() // "#$%^&#$%^&"
* faker::string::sample(5) // "#$%^&"
* @endcode
*/
FAKER_CXX_EXPORT std::string symbol(unsigned length = 10);

/**
* @brief Returns a string containing "~`!@#$%^&*()_-+={[}]|:;\"'<,>.?/".
*
* @param minlength The number of minimum characters to generate. Defaults to `1`.
* @param maxLength The number of maximum characters to generate. Defaults to `10`.
*
* @returns Sample string.
*
* @code
* faker::string::sample() // "#$%^&#$%^&"
* faker::string::sample(1,5) // "#$%^&"
* @endcode
*/
FAKER_CXX_EXPORT std::string symbol(unsigned int minLength, unsigned int maxLength);

/**
* @brief Generates a string consisting of given characters.
*
Expand Down
48 changes: 48 additions & 0 deletions src/modules/location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include "faker-cxx/types/precision.h"
#include "location_data.h"

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

namespace faker::location
{
namespace
Expand Down Expand Up @@ -68,6 +72,8 @@ CountryAddressesInfo getAddresses(const Locale& locale)
return estoniaAddresses;
case Locale::en_GB:
return unitedkingdomAddresses;
case Locale::sk_SK:
return slovakiaAddresses;
default:
return usaAddresses;
}
Expand Down Expand Up @@ -211,4 +217,46 @@ std::string_view timeZone()
return helper::randomElement(timeZones);
}

std::tuple<std::string, std::string> nearbyGPSCoordinate(
Precision precision,
const std::tuple<double, double>& origin,
const double radius,
const bool isMetric)
{
// If origin is not provided, generate a random GPS coordinate.
if (std::get<0>(origin) == std::numeric_limits<double>::max() &&
std::get<1>(origin) == std::numeric_limits<double>::max())
{
return { latitude(precision), longitude(precision) };
}

const auto angleRadians = number::decimal<double>(2 * M_PI);

const auto radiusMetric = isMetric ? radius : radius * 1.60934;
const auto distanceInKm = number::decimal<double>(radiusMetric);

constexpr auto kmPerDegree = 40000 / 360; //The distance in km per degree for earth.
const auto distanceInDegree = distanceInKm / kmPerDegree;

auto coordinateLatitude = std::get<0>(origin) + distanceInDegree * std::sin(angleRadians);
auto coordinateLongitude = std::get<1>(origin) + distanceInDegree * std::cos(angleRadians);

// Box the latitude [-90, 90]
coordinateLatitude = std::fmod(coordinateLatitude, 180.0);
if (coordinateLatitude < -90.0 || coordinateLatitude > 90.0)
{
coordinateLatitude = std::copysign(180.0, coordinateLatitude) - coordinateLatitude;
coordinateLongitude += 180.0;
}

// Box the longitude [-180, 180]
coordinateLongitude = std::fmod(std::fmod(coordinateLongitude, 360.0) + 540.0, 360.0) - 180.0;

return
{
common::precisionFormat(precision, coordinateLatitude),
common::precisionFormat(precision, coordinateLongitude)
};
}

}
Loading

0 comments on commit ee1d39f

Please sign in to comment.