File size: 1,151 Bytes
9c23216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import json
import geopandas as gpd
from dotenv import load_dotenv 
from geopy.geocoders import Nominatim
from shapely.geometry import Point

load_dotenv()

file_rmqs ='data/soil_data/raw_data/rmqs.geojson'
df = gpd.read_file(file_rmqs)

def get_city_coordinates(city_name):
    """Get latitude and longitude of a city using Nominatim."""
    geolocator = Nominatim(user_agent="geo_finder")
    location = geolocator.geocode(city_name)
    if location:
        return (location.longitude, location.latitude)
    return None

def find_nearest_point(city_name):
    """Find the closest GPS point in the dataset to the given city."""
    city_coords = get_city_coordinates(city_name)
    print("city coords", city_coords)
    if not city_coords:
        return "City not found"

    # Iterate through GeoJSON features
    df["distance"] = df["geometry"].distance(Point(city_coords[0], city_coords[1]))
    closest_point = df.loc[df["distance"].idxmin()]


    return closest_point  # Returns the closest feature


if __name__ == "__main__":
    # Example usage
    city = "Paris"
    closest_feature = find_nearest_point(city)
    print(closest_feature)