mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-15 10:04:44 +00:00
Add reverse geocoding for brewery street addresses (#245)
* Add geocoding to seed application, remove postal codes from generator Update comments
This commit is contained in:
@@ -13,12 +13,10 @@ BiergartenPipelineOrchestrator::BiergartenPipelineOrchestrator(
|
||||
std::unique_ptr<DataGenerator> generator,
|
||||
std::unique_ptr<IExportService> exporter,
|
||||
std::unique_ptr<ICuratedDataService> curated_data_service,
|
||||
std::unique_ptr<IPostalCodeService> postal_code_service,
|
||||
const ApplicationOptions& application_options)
|
||||
: logger_(std::move(logger)),
|
||||
context_service_(std::move(context_service)),
|
||||
generator_(std::move(generator)),
|
||||
exporter_(std::move(exporter)),
|
||||
curated_data_service_(std::move(curated_data_service)),
|
||||
postal_code_service_(std::move(postal_code_service)),
|
||||
application_options_(application_options) {}
|
||||
@@ -4,54 +4,16 @@
|
||||
*/
|
||||
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <format>
|
||||
#include <numbers>
|
||||
#include <optional>
|
||||
#include <random>
|
||||
|
||||
#include "biergarten_pipeline_orchestrator.h"
|
||||
#include "biergarten_pipeline_orchestrator/coords.h"
|
||||
#include "services/logging/logger.h"
|
||||
|
||||
struct Coords {
|
||||
double longitude;
|
||||
double latitude;
|
||||
};
|
||||
|
||||
// @todo find a better home for this
|
||||
namespace {
|
||||
constexpr double kEarthRadiusKm = 6371.0;
|
||||
|
||||
double DegreesToRadians(double degrees) {
|
||||
return degrees * std::numbers::pi / 180.0;
|
||||
}
|
||||
|
||||
double RadiansToDegrees(double radians) {
|
||||
return radians * 180.0 / std::numbers::pi;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
static Coords GetRandomCoordsWithinRange(const Coords& centre,
|
||||
const int distance_km,
|
||||
std::mt19937& rng) {
|
||||
std::uniform_real_distribution<double> angle_dist(0.0,
|
||||
2.0 * std::numbers::pi);
|
||||
std::uniform_real_distribution<double> unit_dist(0.0, 1.0);
|
||||
|
||||
const double angle_radians = angle_dist(rng);
|
||||
// sqrt() keeps points uniformly distributed over the disc's area rather
|
||||
// than clustering near the centre.
|
||||
const double radius_km = distance_km * std::sqrt(unit_dist(rng));
|
||||
|
||||
const double delta_latitude_degrees =
|
||||
RadiansToDegrees(radius_km * std::cos(angle_radians) / kEarthRadiusKm);
|
||||
const double delta_longitude_degrees = RadiansToDegrees(
|
||||
radius_km * std::sin(angle_radians) /
|
||||
(kEarthRadiusKm * std::cos(DegreesToRadians(centre.latitude))));
|
||||
|
||||
return Coords{.longitude = centre.longitude + delta_longitude_degrees,
|
||||
.latitude = centre.latitude + delta_latitude_degrees};
|
||||
}
|
||||
using biergarten_pipeline_orchestrator_internal::Coords;
|
||||
using biergarten_pipeline_orchestrator_internal::GetRandomCoordsWithinRange;
|
||||
|
||||
void BiergartenPipelineOrchestrator::GenerateBreweries(
|
||||
std::span<const EnrichedCity> cities) {
|
||||
@@ -70,8 +32,6 @@ void BiergartenPipelineOrchestrator::GenerateBreweries(
|
||||
try {
|
||||
const BreweryResult brewery =
|
||||
generator_->GenerateBrewery(enriched_city);
|
||||
const std::string postal_code =
|
||||
postal_code_service_->GeneratePostalCode(enriched_city.location);
|
||||
|
||||
constexpr int kMaxDistanceFromCentreKm = 5;
|
||||
const Coords city_centre{.longitude = enriched_city.location.longitude,
|
||||
@@ -81,7 +41,6 @@ void BiergartenPipelineOrchestrator::GenerateBreweries(
|
||||
|
||||
return BreweryRecord{
|
||||
.address = BreweryAddress{.city = enriched_city.location,
|
||||
.postal_code = postal_code,
|
||||
.longitude = brewery_coords.longitude,
|
||||
.latitude = brewery_coords.latitude},
|
||||
.brewery = brewery};
|
||||
|
||||
@@ -15,9 +15,13 @@
|
||||
#include <unordered_set>
|
||||
|
||||
#include "biergarten_pipeline_orchestrator.h"
|
||||
#include "biergarten_pipeline_orchestrator/coords.h"
|
||||
#include "services/curated_data/curated_json_data_service.h"
|
||||
#include "services/logging/logger.h"
|
||||
|
||||
using biergarten_pipeline_orchestrator_internal::Coords;
|
||||
using biergarten_pipeline_orchestrator_internal::GetRandomCoordsWithinRange;
|
||||
|
||||
namespace {
|
||||
std::string Sanitize(std::string_view value) {
|
||||
std::string out;
|
||||
@@ -135,12 +139,17 @@ void BiergartenPipelineOrchestrator::GenerateUsers(
|
||||
try {
|
||||
const UserResult user =
|
||||
generator_->GenerateUser(city, persona, sampled_name);
|
||||
const std::string postal_code =
|
||||
postal_code_service_->GeneratePostalCode(city.location);
|
||||
|
||||
constexpr int kMaxDistanceFromCentreKm = 5;
|
||||
const Coords city_centre{.longitude = city.location.longitude,
|
||||
.latitude = city.location.latitude};
|
||||
const Coords user_coords = GetRandomCoordsWithinRange(
|
||||
city_centre, kMaxDistanceFromCentreKm, rng);
|
||||
|
||||
return UserRecord{
|
||||
.address =
|
||||
UserAddress{.city = city.location, .postal_code = postal_code},
|
||||
.address = UserAddress{.city = city.location,
|
||||
.longitude = user_coords.longitude,
|
||||
.latitude = user_coords.latitude},
|
||||
.user = user,
|
||||
.email = BuildEmail(sampled_name, used_email_local_parts),
|
||||
.date_of_birth = GenerateDateOfBirth(rng),
|
||||
|
||||
@@ -26,7 +26,6 @@ void BiergartenPipelineOrchestrator::LogResults() const {
|
||||
{"country", city.country},
|
||||
{"state_province", city.state_province},
|
||||
{"iso3166_2", city.iso3166_2},
|
||||
{"postal_code", address.postal_code},
|
||||
{"longitude", address.longitude},
|
||||
{"latitude", address.latitude},
|
||||
}}});
|
||||
|
||||
Reference in New Issue
Block a user