Solution: Proximity and Sampling
Follow step-by-step instructions to perform proximity analysis and raster sampling.
We'll cover the following...
Let’s take a look at the solution for the proximity and sampling challenge and review the code:
Press + to interact
import geopandas as gpdimport rasterio as riofrom rasterio.plot import show# import the datasetscountries = gpd.read_file('countries.geojson')stations = gpd.read_file('ozone_stations.csv')cities = gpd.read_file('populated_places.geojson')rain = rio.open('rain.tif')# preprocess stationsstations['geometry'] = gpd.points_from_xy(x=stations['X'], y=stations['Y'])stations = stations.set_crs('epsg:4326')# select the cities that are in south americancontinents = countries.dissolve(by='continent')south_america = continents.query("continent == 'South America'")# filter major cities in south americasa_cities = cities.clip(south_america)major_cities = sa_cities.sort_values(by='pop_max', ascending=False).iloc[:10]# perform the spatial nearestmajor_cities = major_cities.to_crs('EPSG:32662')[['name', 'pop_max', 'geometry']]stations = stations.to_crs('EPSG:32662')[['platform_name', 'country', 'X', 'Y', 'geometry']]combined = major_cities.sjoin_nearest(stations, how='left')# create a list with the coordinates to be sampledcombined = combined.to_crs('EPSG:4326')cities_coords = [(x, y) for x, y in zip(combined['geometry'].x, combined['geometry'].y)]stations_coords = [(x, y) for x, y in zip(combined['X'].astype('float'), combined['Y'].astype('float'))]# perform the raster samplingcombined['cities_rain'] = list(rain.sample(cities_coords))combined['stations_rain'] = list(rain.sample(stations_coords))### display the results# plot South Americaax = south_america.plot(facecolor='none')show(rain, ax=ax)combined.plot(ax=ax, color='orange')selected_stations = gpd.gpd.points_from_xy(combined['X'], combined['Y'])gpd.GeoSeries(selected_stations).plot(ax=ax, color='red', marker='^')ax.set_xlabel('Longitude')ax.set_ylabel('Latitude')ax.figure.savefig('output/proximity_sampling.png', dpi=300)print(combined.to_html())
After loading all the necessary datasets, let's start by converting stations
to a GeoDataFrame (lines 12 and 13). Then, we are going to create a geometry to represent the South American continent by using the ...