Skip to content

Commit

Permalink
Get country by coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
iwatkot authored Feb 12, 2025
1 parent 501d8a7 commit e65727c
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions maps4fs/generator/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import shutil
from typing import Any, Generator

from geopy.geocoders import Nominatim

from maps4fs.generator.component import Background, Component, Layer, Texture
from maps4fs.generator.dtm.dtm import DTMProvider, DTMProviderSettings
from maps4fs.generator.game import Game
Expand Down Expand Up @@ -80,6 +82,7 @@ def __init__(
"game": game.code,
"latitude": coordinates[0],
"longitude": coordinates[1],
"country": self.get_country_by_coordinates(),
"size": size,
"rotation": rotation,
"dtm_provider": dtm_provider.name(),
Expand Down Expand Up @@ -328,3 +331,19 @@ def pack(self, archive_path: str, remove_source: bool = True) -> str:
except Exception as e:
self.logger.debug("Error removing map directory %s: %s", self.map_directory, e)
return archive_path

def get_country_by_coordinates(self) -> str:
"""Get country name by coordinates.
Returns:
str: Country name.
"""
try:
geolocator = Nominatim(user_agent="maps4fs")
location = geolocator.reverse(self.coordinates, language="en")
if location and "country" in location.raw["address"]:
return location.raw["address"]["country"]
except Exception as e:
self.logger.error("Error getting country name by coordinates: %s", e)
return "Unknown"
return "Unknown"

0 comments on commit e65727c

Please sign in to comment.