OpenEnergy PlatformPermalink

API tutorial 3 - Plot data and spatial dataPermalink


Please report bugs and improvements here:
How to get started with Jupyter Notebooks can be found here:
Please ensure you have read the Terms of use here:

In [1]:
__copyright__ = "Reiner Lemoine Institut, Zentrum für nachhaltige Energiesysteme Flensburg"
__license__   = "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__       = ""
__author__    = "wolfbunke, Ludee"


This is an important information!
This is an information!
This is your task!

This tutorial gives you an overview of the OpenEnergy Platform and how you can work with the REST-full-HTTP API in Python.
The full API documentaion can be found on

Part III - How to work with the OpenEnergy Platform (OEP)Permalink

0 Setup token
1 Select data
2 Make a pandas dataframe
3 Plot a dataframe (geo plot)

Part IIIPermalink

0. Setup tokenPermalink

The token is used to verify the API interaction for your OEP user.
Do not push your token to GitHub!
In [ ]:
import requests
import pandas as pd
from IPython.core.display import HTML

from token_config import oep_url, get_oep_token

# token
your_token = get_oep_token()

1. Select dataPermalink

In [ ]:
import geopandas as gpd
from shapely.geometry import Point
import shapely.wkt
from shapely import wkb
from geoalchemy2.shape import to_shape
In [ ]:
# select powerplant data
schema = 'supply'
table = 'ego_dp_conv_powerplant'
where = 'version=v0.2.10'

conv_powerplants = requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/?where='+where, )
Response [200] succesfully selected data!
Response [404] table doesn't exist!
In [ ]:
# select borders
schema = 'boundaries'
table = 'bkg_vg250_2_lan_mview'

vg = requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/')
Response [200] succesfully selected data!
Response [404] table doesn't exist!

2. Make a pandas dataframePermalink

Create pandas dataframes for each data set returned as API result!
In [ ]:
# Create dataframe from json format
df_pp = pd.DataFrame(conv_powerplants.json())
df_vg = pd.DataFrame(vg.json())
Let's take a look into our data!
In [ ]:
# Show metadata for a specific dataframe.
In [ ]:
# List all column names for a specific dataframe. 
In [ ]:
#Print the df_pp dataframe as table.

3. Plot a dataframe (geo plot)Permalink

In [ ]:
import geopandas as gpd
import shapely
import matplotlib.pyplot as plt
%matplotlib inline
If we want to apply a change to every entity in a column we can use Pandas apply function.
In [ ]:
# transform WKB to WKT / Geometry
df_pp['geom'] = df_pp['geom'].apply(lambda x:shapely.wkb.loads(x, hex=True))
df_vg['geom'] = df_vg['geom'].apply(lambda x:shapely.wkb.loads(x, hex=True))
Let's plot our data!
In [ ]:
# plot powerplants
crs = {'init' :'epsg:4326'}
gdf_pp = gpd.GeoDataFrame(df_pp, crs=crs, geometry=df_pp.geom)
base1 = gdf_pp.plot(color='white', edgecolor='black',figsize=(8, 8))
gdf_pp.plot(ax=base1, color='green')
In [ ]:
# plot borders
crs = {'init' :'epsg:4326'}
gdf_vg = gpd.GeoDataFrame(df_vg, geometry=df_vg.geom)
base2 = gdf_vg.plot(color='white', edgecolor='black',figsize=(8, 8))
Now we can create a map with two layers.
In [ ]:
# transform WKB to WKT / Geometry
crs1 = {'init' :'epsg:4326'}
crs2 = {'init' :'epsg:3035'}

gdf_pp = gpd.GeoDataFrame(df_pp, crs=crs1, geometry=df_pp.geom)
gdf_vg = gpd.GeoDataFrame(df_vg, crs=crs2, geometry=df_vg.geom)

base = gdf_vg.plot(color='white', edgecolor='black',figsize=(10, 10))

gdf_pp.plot(ax=base, marker='o', markersize=5)
# gdf_vg.plot(ax=base)
This plot does not display the data corretly. We need to change the Projections(crs) to something similar.
bug under ubuntu
In [ ]:
from shapely import geos
from geoalchemy2.shape import to_shape
from shapely.geometry import Point

# from ipywidgets import widgets
from IPython.display import display
from IPython.core.display import HTML
from geoalchemy2 import Geometry, WKTElement
import requests
import pandas as pd
# import mplleaflet
In [ ]:
plants_data = requests.get(oep_url+'/api/v0/schema/model_draft/tables/ego_dp_supply_conv_powerplant/rows/?where=scenario=Status+Quo&limit=910',)
regions  =  requests.get(oep_url+'/api/v0/schema/model_draft/tables/renpass_gis_parameter_region/rows/?where=stat_level=999',)
Response [200] succesfully selected data!
Response [404] table doesn't exist!
Let´s transform the crs and plot the data again.
In [ ]:
sq_plants = pd.DataFrame(plants_data.json())
renpass_region_df = pd.DataFrame(regions.json())

# transform WKB to WKT / Geometry
crs = {'init' :'epsg:4326'}

sq_plants['geom'] =sq_plants['geom'].apply(lambda x:shapely.wkb.loads(x, hex=True))
renpass_region_df['geom'] =renpass_region_df['geom'].apply(lambda x:shapely.wkb.loads(x, hex=True))

gdf_plants = gpd.GeoDataFrame(sq_plants, crs=crs, geometry=sq_plants.geom)
gdf_regions = gpd.GeoDataFrame(renpass_region_df, crs=crs, geometry=renpass_region_df.geom)

base = gdf_regions.plot(color='white', edgecolor='black',figsize=(10, 10))


Point PlotPermalink

In [ ]:
import folium
from folium import plugins
import matplotlib.pyplot as plt
%matplotlib inline
In [ ]:
# define map region
map = folium.Map(location=[51, 9], zoom_start=6)
In [ ]:
# Use column lon / lat in order to plot map
for name, row in gdf_pp.iloc[:1000].iterrows():
    folium.Marker([row["lat"], row["lon"]], popup=row["type"] ).add_to(map)

Heat plot for locationsPermalink

In [ ]:
stops_heatmap = folium.Map(location=[51, 9], zoom_start=6)
stops_heatmap.add_child(plugins.HeatMap([[row["lat"], row["lon"]] for capacity, row in df_pp.iloc[:1000].iterrows()]))"heatmap.html")

Make some statisticsPermalink

Make an interesting API-example you need!
In [ ]:

If you find bugs or if you have ideas to improve the Open Energy Platform, you are welcome to add your comments to the existing issues on GitHub.
You can also fork the project and get involved.

Please note that the platform is still under construction and therefore the design of this page is still highly volatile!


Back to tutorials