Search code examples
pythongeopandas

How to use geopandas to find the nearest value of a coordinate


I have a list of coordinates with temperatures at each coordinate. The data frame looks as follows: eg:

Lat Lon Temperature
51.23 4.234 23.3
51.29 4.211 26.4
51.25 4.238 24.3
51.26 4.221 28.4
51.30 4.244 19.3
51.40 4.231 20.4

Is there a way in geopandas to directly find the observations within 100m distance for every row and create a new column with the mean of nearest observations

eg:

Lat Lon Temperature Mean Temp
51.23 4.234 23.3 Mean temperature within 100m distance
51.29 4.211 26.4 Mean temperature within 100m distance
51.25 4.238 24.3 Mean temperature within 100m distance
51.26 4.221 28.4 Mean temperature within 100m distance
51.30 4.244 19.3 Mean temperature within 100m distance
51.40 4.231 20.4 Mean temperature within 100m distance

Ive tried using nearest_point:

def get_nearest_values(row, other_gdf, point_column='geometry', 
value_column="predictions_precipitation_type"):


    # Create an union of the other GeoDataFrame's geometries:
    other_points = other_gdf["geometry"].unary_union

    # Find the nearest points
    nearest_geoms = nearest_points(row[point_column], other_points)

    # Get corresponding values from the other df
    nearest_data = other_gdf.loc[other_gdf["geometry"] == 
    nearest_geoms[1]]

    nearest_value = nearest_data[value_column].values[0]

return nearest_value

but it finds the closest observation and its value.. I would like to find all the observations within 100m radius and then find the mean


Solution

  • Try this:

    import geopandas as gpd
    from shapely.geometry import Point
    
    s = """Lat  Lon Temperature
    51.23   4.234   23.3
    51.29   4.211   26.4
    51.25   4.238   24.3
    51.26   4.221   28.4
    51.30   4.244   19.3
    51.40   4.231   20.4"""
    
    n = 3 # Columns
    data =  [s.split()[i:i + n] for i in range(0, len(s.split()), n)]
    df = gpd.pd.DataFrame(data[1:], columns=data[0])
    
    for col in df.columns:
        df[col] = gpd.pd.to_numeric(df[col])
    
    geometry = [Point(xy) for xy in zip(df.Lon, df.Lat)]
    
    gdf = gpd.GeoDataFrame(df, geometry=geometry)
    
    for index, row in gdf.iterrows():
        buffer = row.geometry.buffer(0.1)
        points_inside_buffer = gdf[gdf.geometry.within(buffer)]
        points_temperatures = points_inside_buffer['Temperature'].tolist()
        mean_temp = sum(points_temperatures)/len(points_temperatures)
        gdf.at[index, "Mean Temp within 100m"] = mean_temp