import json

Загрузить упрощённый GeoJSON с границами стран (пример: countries_simple.geojson)

with open('countries.geojson', 'r', encoding='utf-8') as f:
countries_geojson = json.load(f)

def point_in_polygon(x, y, polygon):

Реализация алгоритма ray casting для проверки попадания точки в полигон

inside = False
for ring in polygon:
n = len(ring)
j = n - 1
for i in range(n):
xi, yi = ring[i]
xj, yj = ring[j]
intersect = ((yi > y) != (yj > y)) and \
(x < (xj - xi) * (y - yi) / (yj - yi + 1e-15) + xi)
if intersect:
inside = not inside
j = i
return inside

def get_country(lat, lon):
for feature in countries_geojson['features']:
geom = feature['geometry']
if geom['type'] == 'Polygon':
if point_in_polygon(lon, lat, geom['coordinates']):
return feature['properties']['name']
elif geom['type'] == 'MultiPolygon':
for polygon in geom['coordinates']:
if point_in_polygon(lon, lat, polygon):
return feature['properties']['name']
return None

Пример использования

lat, lon = 50.7558, 30.6173
country = get_country(lat, lon)
print(f"Страна: {country}")

[file-name 000127_2025-04-29_19-33-14.txt]