Propagule pressure raster
In [1]:
Copied!
# Uncomment below to run in Google Collab
# pip install ecospat
# Uncomment below to run in Google Collab
# pip install ecospat
Creating and visualizing a propagule pressure raster¶
Step-by-step¶
In [2]:
Copied!
import ecospat.ecospat as ecospat_full
from ecospat.stand_alone_functions import (
process_species_historical_range,
analyze_species_distribution,
analyze_northward_shift,
calculate_rate_of_change_first_last,
merge_category_dataframes,
prepare_gdf_for_rasterization,
cat_int_mapping,
rasterize_multiband_gdf_match,
rasterize_multiband_gdf_world,
compute_propagule_pressure_range,
save_raster_to_downloads_range,
full_propagule_pressure_pipeline,
)
import ecospat.ecospat as ecospat_full
from ecospat.stand_alone_functions import (
process_species_historical_range,
analyze_species_distribution,
analyze_northward_shift,
calculate_rate_of_change_first_last,
merge_category_dataframes,
prepare_gdf_for_rasterization,
cat_int_mapping,
rasterize_multiband_gdf_match,
rasterize_multiband_gdf_world,
compute_propagule_pressure_range,
save_raster_to_downloads_range,
full_propagule_pressure_pipeline,
)
Classify historical range edges¶
In [3]:
Copied!
hist_pipeline = ecospat_full.Map()
hist_range = process_species_historical_range(
new_map=hist_pipeline, species_name="Populus angustifolia"
)
hist_pipeline = ecospat_full.Map()
hist_range = process_species_historical_range(
new_map=hist_pipeline, species_name="Populus angustifolia"
)
No overlapping polygons found — returning original classifications.
Classify modern range edges.¶
In [4]:
Copied!
classified_modern, classified_historic = analyze_species_distribution(
"Populus angustifolia", record_limit=1000, continent="north_america"
)
classified_modern, classified_historic = analyze_species_distribution(
"Populus angustifolia", record_limit=1000, continent="north_america"
)
Modern records (>= 1976): 1000 Historic records (< 1976): 254
Calculate the northward movement¶
In [5]:
Copied!
northward_rate_df = analyze_northward_shift(
gdf_hist=hist_range,
gdf_new=classified_modern,
species_name="Populus angustifolia",
)
northward_rate_df = northward_rate_df[
northward_rate_df["category"].isin(["leading", "core", "trailing"])
]
northward_rate_df["category"] = northward_rate_df["category"].str.title()
northward_rate_df = analyze_northward_shift(
gdf_hist=hist_range,
gdf_new=classified_modern,
species_name="Populus angustifolia",
)
northward_rate_df = northward_rate_df[
northward_rate_df["category"].isin(["leading", "core", "trailing"])
]
northward_rate_df["category"] = northward_rate_df["category"].str.title()
Calculate population density change¶
In [6]:
Copied!
change = calculate_rate_of_change_first_last(
classified_historic, classified_modern, "Populus angustifolia", custom_end_year=2025
)
change = change[change["collapsed_category"].isin(["leading", "core", "trailing"])]
change = change.rename(
columns={
"collapsed_category": "Category",
"rate_of_change_first_last": "Rate of Change",
"start_time_period": "Start Years",
"end_time_period": "End Years",
}
)
change["Category"] = change["Category"].str.title()
change = calculate_rate_of_change_first_last(
classified_historic, classified_modern, "Populus angustifolia", custom_end_year=2025
)
change = change[change["collapsed_category"].isin(["leading", "core", "trailing"])]
change = change.rename(
columns={
"collapsed_category": "Category",
"rate_of_change_first_last": "Rate of Change",
"start_time_period": "Start Years",
"end_time_period": "End Years",
}
)
change["Category"] = change["Category"].str.title()
Merge dataframes and prepare for rasterization¶
In [7]:
Copied!
merged = merge_category_dataframes(northward_rate_df, change)
preped_gdf = prepare_gdf_for_rasterization(classified_modern, merged)
preped_gdf_new = cat_int_mapping(preped_gdf)
preped_gdf_new.head()
merged = merge_category_dataframes(northward_rate_df, change)
preped_gdf = prepare_gdf_for_rasterization(classified_modern, merged)
preped_gdf_new = cat_int_mapping(preped_gdf)
preped_gdf_new.head()
Out[7]:
| geometry | category | density | northward_rate_km_per_year | Rate of Change | category_int | |
|---|---|---|---|---|---|---|
| 0 | POLYGON ((-112.16175 40.79402, -112.1358 40.81... | Core | 0.003597 | -1.736134 | 1.115629 | 1.0 |
| 188 | POLYGON ((-108.48788 37.47497, -107.24964 39.5... | Core | 0.001869 | -1.736134 | 1.115629 | 1.0 |
| 446 | POLYGON ((-112.38734 38.88478, -110.89519 38.3... | Trailing (0.05) | 0.001117 | 0.000000 | 0.000000 | NaN |
| 481 | POLYGON ((-111.90918 43.82033, -111.86789 43.8... | Leading (0.95) | 0.005273 | 0.000000 | 0.000000 | NaN |
| 515 | POLYGON ((-108.24095 32.90245, -108.271 33.225... | Relict (0.01 Latitude) | 0.010924 | 0.000000 | 0.000000 | NaN |
Rasterization¶
In [8]:
Copied!
value_columns = [
"density",
"northward_rate_km_per_year",
"Rate of Change",
"category_int",
]
raster_show, transform, show_bounds = rasterize_multiband_gdf_match(
preped_gdf_new, value_columns
)
value_columns = [
"density",
"northward_rate_km_per_year",
"Rate of Change",
"category_int",
]
raster_show, transform, show_bounds = rasterize_multiband_gdf_match(
preped_gdf_new, value_columns
)
In [9]:
Copied!
import matplotlib.pyplot as plt
# Plotting one of these bands (northward movement rate)
plt.imshow(raster_show[1], cmap="viridis", origin="upper")
plt.colorbar(label="Pressure")
plt.xlabel("Longitude")
plt.ylabel("Latitude")
plt.show()
import matplotlib.pyplot as plt
# Plotting one of these bands (northward movement rate)
plt.imshow(raster_show[1], cmap="viridis", origin="upper")
plt.colorbar(label="Pressure")
plt.xlabel("Longitude")
plt.ylabel("Latitude")
plt.show()
Construct propagule pressure raster.¶
In [10]:
Copied!
pressure_show = compute_propagule_pressure_range(raster_show)
pressure_show = compute_propagule_pressure_range(raster_show)
In [11]:
Copied!
import matplotlib.pyplot as plt
plt.imshow(pressure_show, cmap="viridis", origin="upper")
plt.colorbar(label="Pressure")
plt.xlabel("Longitude")
plt.ylabel("Latitude")
plt.show()
import matplotlib.pyplot as plt
plt.imshow(pressure_show, cmap="viridis", origin="upper")
plt.colorbar(label="Pressure")
plt.xlabel("Longitude")
plt.ylabel("Latitude")
plt.show()
(Optional) Save raster as a .tif¶
In [12]:
Copied!
# raster_download = save_raster_to_downloads_range(pressure_show, show_bounds, "Populus angustifolia")
# raster_download = save_raster_to_downloads_range(pressure_show, show_bounds, "Populus angustifolia")
View raster¶
In [13]:
Copied!
persistence_map = ecospat_full.Map()
persistence_map.add_basemap("GBIF.Classic")
persistence_map.add_raster(
"Populus_angustifolia_persistence_raster.tif",
colormap="viridis",
legend=True,
name="Persistence Raster",
)
# persistence_map.add_layer_control()
persistence_map
persistence_map = ecospat_full.Map()
persistence_map.add_basemap("GBIF.Classic")
persistence_map.add_raster(
"Populus_angustifolia_persistence_raster.tif",
colormap="viridis",
legend=True,
name="Persistence Raster",
)
# persistence_map.add_layer_control()
persistence_map
Out[13]:
Pipeline to generate persistence raster¶
In [14]:
Copied!
full_show, full_save, show_bounds, save_bounds, gdf_transform, world_transform = (
full_propagule_pressure_pipeline(classified_modern, northward_rate_df, change)
)
full_show, full_save, show_bounds, save_bounds, gdf_transform, world_transform = (
full_propagule_pressure_pipeline(classified_modern, northward_rate_df, change)
)
In [15]:
Copied!
import matplotlib.pyplot as plt
plt.imshow(full_show, cmap="viridis", origin="upper")
plt.colorbar(label="Pressure")
plt.xlabel("Longitude")
plt.ylabel("Latitude")
plt.show()
import matplotlib.pyplot as plt
plt.imshow(full_show, cmap="viridis", origin="upper")
plt.colorbar(label="Pressure")
plt.xlabel("Longitude")
plt.ylabel("Latitude")
plt.show()