Welcome to TERN Knowledge Base
Visualising site locations using GeoJSON data with Python
GeoJSON data can be parsed using python to create a plot that visualises the geographic range of your TERN EcoPlots dataset. The GeoJSON data that is included in TERN data package downloads can be parsed create a plot that visualises the geographic range of your TERN EcoPlots dataset. Below is an example of a basic plot that can been created.
Below is the script used to created the above plot. It contains code comments to give a basic explanation of each element of the plot creation process.
Setup
Before running the script in python, there are a number of setup steps needed.
Download some TERN GeoJSON data. This example uses a dataset acquired from data search results in Example Data Searches in EcoPlots | Example Two Plants in Western Australia.
Set your python working directory to an appropriate location on your device.
Copy your GeoJSON file to your python working directory and rename if desired.
Install the python packages containing the modules that are called in the first code chunk.
# python modules required
import geopandas as gpd
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.io.img_tiles import Stamen
# read in the data from the GeoJSON file
df_plants = gpd.read_file("geojson_PlantsInWA.json")
# some general exploration of our data
df_plants
# Some site names contain a comma then further information.
# This will make the plot site names look a bit messy.
# OPTIONAL - truncate the title to look neater on the plot
df_plants['siteName'] = df_plants['siteName'].str.split(',').str[0]
# initialize an axis
fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()}, figsize=(12, 9))
#customise grid
ax.set_xticks(range(114, 124, 1)) # Customize the x-axis grid
ax.set_yticks(range(-36, -29, 1)) # Customize the y-axis grid
ax.grid(which='both', linestyle='-', lw=0.3) # Customize grid style
#get naturalearth BG
countries = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
# External tile source as the background
tiles = Stamen('terrain-background')
ax.add_image(tiles, 6, interpolation='bilinear')
# Zoom in on a region of Australia
ax.set_xlim(114, 124) # Longitude (degrees)
ax.set_ylim(-36, -29) # Latitude (degrees)
# plot map on axis
countries[countries["name"] == "Australia"].plot(edgecolor="lightgrey", facecolor="none", linewidth=0, ax=ax)
df_plants.plot(color="green", ax=ax)
# Add labels to the points
for x, y, label in zip(df_plants.geometry.x, df_plants.geometry.y, df_plants['siteName']):
ax.annotate(label, (x, y), textcoords="offset points", xytext=(0,10), ha='left',
bbox=dict(boxstyle='round, pad=0.3', edgecolor='lightgrey', facecolor='white')
)
#Add map frame
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.title('TERN Plots with identified plant species data in Western Australia')
# export the plot.
plt.savefig('TERN_Sites_WA.png', dpi=300)
# gets rid of the annoying <Axis> output title
plt.show()
This is a basic example of one of the many ways that GeoJSON data can be visualised using Python. We recommend visiting sites such as The Python Graph Gallery for ideas on ways to customise the aesthetics of your plot.
Provide your feedback about the experience with Knowledge base