mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 11:00:06 +00:00 
			
		
		
		
	Find nearest radar site
This commit is contained in:
		
							parent
							
								
									b739aad4bb
								
							
						
					
					
						commit
						4a31cf6d3e
					
				
					 3 changed files with 91 additions and 4 deletions
				
			
		|  | @ -8,6 +8,7 @@ | |||
| #include <unordered_map> | ||||
| 
 | ||||
| #include <boost/json.hpp> | ||||
| #include <GeographicLib/Geodesic.hpp> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
|  | @ -30,6 +31,9 @@ static std::unordered_map<std::string, std::shared_ptr<RadarSite>> | |||
| static std::unordered_map<std::string, std::string> siteIdMap_; | ||||
| static std::shared_mutex                            siteMutex_; | ||||
| 
 | ||||
| static GeographicLib::Geodesic geodesic_ {GeographicLib::Constants::WGS84_a(), | ||||
|                                           GeographicLib::Constants::WGS84_f()}; | ||||
| 
 | ||||
| static bool ValidateJsonEntry(const boost::json::object& o); | ||||
| 
 | ||||
| class RadarSiteImpl | ||||
|  | @ -162,6 +166,44 @@ std::vector<std::shared_ptr<RadarSite>> RadarSite::GetAll() | |||
|    return radarSites; | ||||
| } | ||||
| 
 | ||||
| std::shared_ptr<RadarSite> RadarSite::FindNearest( | ||||
|    double latitude, double longitude, std::optional<std::string> type) | ||||
| { | ||||
|    std::shared_lock lock(siteMutex_); | ||||
| 
 | ||||
|    double distanceInMeters; | ||||
| 
 | ||||
|    std::shared_ptr<RadarSite> nearestRadarSite = nullptr; | ||||
|    double                     nearestDistance  = 0.0; | ||||
| 
 | ||||
|    for (const auto& site : radarSiteMap_) | ||||
|    { | ||||
|       auto& radarSite = site.second; | ||||
| 
 | ||||
|       // If the type filter doesn't match, skip
 | ||||
|       if (type.has_value() && radarSite->type() != type) | ||||
|       { | ||||
|          continue; | ||||
|       } | ||||
| 
 | ||||
|       // Calculate distance to radar site
 | ||||
|       geodesic_.Inverse(latitude, | ||||
|                         longitude, | ||||
|                         radarSite->latitude(), | ||||
|                         radarSite->longitude(), | ||||
|                         distanceInMeters); | ||||
| 
 | ||||
|       // If the radar site is the closer, record it as the closest
 | ||||
|       if (nearestRadarSite == nullptr || distanceInMeters < nearestDistance) | ||||
|       { | ||||
|          nearestRadarSite = radarSite; | ||||
|          nearestDistance  = distanceInMeters; | ||||
|       } | ||||
|    } | ||||
| 
 | ||||
|    return nearestRadarSite; | ||||
| } | ||||
| 
 | ||||
| std::string GetRadarIdFromSiteId(const std::string& siteId) | ||||
| { | ||||
|    std::shared_lock lock(siteMutex_); | ||||
|  |  | |||
|  | @ -1,6 +1,7 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <memory> | ||||
| #include <optional> | ||||
| #include <string> | ||||
| #include <vector> | ||||
| 
 | ||||
|  | @ -38,6 +39,20 @@ public: | |||
|    static std::shared_ptr<RadarSite>              Get(const std::string& id); | ||||
|    static std::vector<std::shared_ptr<RadarSite>> GetAll(); | ||||
| 
 | ||||
|    /**
 | ||||
|     * Find the nearest radar site to the supplied location. | ||||
|     * | ||||
|     * @param latitude Latitude in degrees | ||||
|     * @param longitude Longitude in degrees | ||||
|     * @param type Restrict results to optional radar type | ||||
|     * | ||||
|     * @return Nearest radar site | ||||
|     */ | ||||
|    static std::shared_ptr<RadarSite> | ||||
|    FindNearest(double                     latitude, | ||||
|                double                     longitude, | ||||
|                std::optional<std::string> type = std::nullopt); | ||||
| 
 | ||||
|    static void   Initialize(); | ||||
|    static size_t ReadConfig(const std::string& path); | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat