5

I have the following map of statistical regions of Belgium:

enter image description here

The blue dots correspond to coordinates for which I have temperature data. Surrounded by the red circle is Brussels. As it can be seen, there are no points inside Brussels and therefore I don't have temperature info for it.

I would like to assign the temperature in the closest blue point to Brussels. How could I find the closest point of the grid to the Brussels polygon using GeoPandas?

0
4

One of the solution would be like that:

import geopandas as gpd
points = gpd.read_file('/path/to/points.shp')
regions = gpd.read_file('/path/to/regions.shp')

# get Brussels' geometry by name
br_geom = regions[regions["name"]=="Brussels"].cascaded_union

# calculate distances between br_geom and the points
# {index: distance, index: distance, ...} / r[0]: index, r[1]: row in DataFrame
dist = {r[0]: r[1].geometry.distance(br_geom) for r in points.iterrows()}

# get the nearest point index
index = min(dist, key=dist.get)

#get the point by index
nearest_point = points.loc[index]

temp = nearest_point["temp"]
print(temp)

Sample data:

# points.shp

    id  temp    geometry
0   1   25.0    POINT (...
1   0   30.0    POINT (...
2   2   35.0    POINT (...
# regions.shp
    id  name      geometry
0   1   ...       POLYGON ((...
1   0   Brussels  POLYGON ((...
...
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.