OpenEnergy Platform


In [ ]:
__copyright__ = "Reiner Lemoine Institut, Zentrum für nachhaltige Energiesysteme Flensburg"
__license__   = "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__       = "https://github.com/openego/data_processing/blob/master/LICENSE"
__author__    = "wolfbunke, Ludee"

Tutorial - How to work with the OpenEnergy Platform (OEP)


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 ReadtheDocs.io.

Part I

0 Get started - Sign-in and get your own token
0 Setup token
1 Create a table
2 Delete a table
3 Query table columns
4 Insert Data into a table
5 Insert data into a specific row
6 Alter data in a table

How to work with Python and Jupyter Notebooks
OPSD wiki
nbviewer example

Overview of Packages:

http://geopandas.org/install.html#installing-geopandas
http://docs.python-requests.org/en/master/

Part I

0. Get started - Sign-in and get your own token

The registration has to be done in the Openmod Wiki.
Login to the OpenEnergy Platform.

1. Click on the login button and sign in



2. Copy your token

0. Setup token


Do not push your token to GitHub!

In [ ]:
import requests
import pandas as pd
from IPython.core.display import HTML
# 
# 
# oedb
oep_url= 'http://oep.iks.cs.ovgu.de/'

# token
your_token = ''

0.1 About the Database and used Packages

Data are stored and ordered by topic under the "schema" tab: https://oep.iks.cs.ovgu.de/dataedit/schemas </div>

The data tables can be accessed by calling them via http request. </div>

The "request" package will handle the HTTP communication with the OEP database. It sends requests that insert (put, post, delete) or read (get) data in/from the databank. For either request the URL of a requested table has to be included. For all "put", "post" and "delete" requests one has to include the authentification token as a header, like in the "delete table " example.</div>

Example:

requests.delete(oep_url+'/api/v0/schema/'+schema+'/tables/'+table, headers=
                {'Authorization': 'Token %s'%your_token} )

About the table structure:

The tables are organized in rows and columns that can be adressed via:

requests.post(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/')
# and       
requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/columns')

In the following tutorial the schema will be

'model_draft'
and the table
'example_api_table_test'.

Inserted data must have the structure of a dictionary / json with a key for every column of the table and the respective value like in:

Random example from OpenStreetMap with freely chosen capacity.


In [ ]:
data = {"query": 
        {"name": "Windpark, Am Speller Sand", 
         "type":  "wind_onshore",
         "capacity": 20000,
         "lat":   52.40731,
         "lon":    7.46118
        }}

1. Create a table / Table Architecure

You installed all Python packages? Let's create our first database table:

The API is enabled for the following schmemas only: **'model_draft'** & **'sandbox'**
A table must have a column **'id'** of type **'bigserial'**!
Change the name of the table!

In [ ]:
# create table
schema = 'model_draft'
table = 'example_api_table_test'

data = { "query": 
        { "columns": [{ "name":"id", "data_type": "bigserial", "is_nullable": "NO" },
                      { "name":"name", "data_type": "varchar", "character_maximum_length": "50" },
                      { "name":"type", "data_type": "varchar", "character_maximum_length": "20" },
                      { "name":"capacity", "data_type": "decimal" },
                      { "name":"lat", "data_type": "numeric" },
                      { "name":"lon", "data_type": "numeric" } ],
         "constraints": [ { "constraint_type": "PRIMARY KEY", "constraint_parameter": "id" } ] 
        } }

requests.put(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/',
             json=data, headers={'Authorization': 'Token %s'%your_token} )
**Response [201]** succesfully created table!
**Response [500]** table already exists!

In [ ]:
# check if exists
schema = 'model_draft'
table = 'example_api_table_test'

requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table)
**Response [200]** table exists!
**Response [404]** table doesn't exist!

2. Delete a table


In [ ]:
# Delete your table
schema = 'model_draft'
table = 'example_api_table_test'

requests.delete(oep_url+'/api/v0/schema/'+schema+'/tables/'+table, headers={'Authorization': 'Token %s'%your_token} )
**Response [200]** succesfully deleted table!
**Response [404]** table doesn't exist and cannot be deleted!

In [ ]:
# check if exists
schema = 'model_draft'
table = 'example_api_table_test'

requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table)
**Response [404]** table does not exists!
Now create the table again!

3. Query table columns


In [ ]:
# table columns
schema = 'model_draft'
table = 'example_api_table_test'

result = requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/columns')
json_result = result.json()
json_result['capacity']

# all:
#json_result

4. Insert Data into a table

Now we insert a power plant with name, type and latitude longitude cooridinates.


In [ ]:
# insert data
schema = 'model_draft'
table = 'example_api_table_test'

data = {"query": 
        {"name": "Windpark, Am Speller Sand", 
         "type": "wind_onshore",
         "capacity": 20000,
         "lat":   52.40731,
         "lon":    7.46118
        }}

result = requests.post(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/new', 
                       json=data, headers={'Authorization': 'Token %s'%your_token} )
result.status_code
**201** succesfully inserted data!

In [ ]:
# show the id of the new row
json_result = result.json()
json_result['data']
It is row number **[1]**!

In [ ]:
# insert data
schema = 'model_draft'
table = 'example_api_table_test'

data = {"query": [{"name": "Anlage 2", 
                   "type": "photovoltaics",
                   "capacity": 10,
                   "lat":   51.804783,
                   "lon":    11.686346},
                  
                 {"name": "Anlage 3", 
                  "type": "photovoltaic", 
                   "capacity": 5.5,
                   "lat":   51.804783,
                   "lon":    11.6341573
                 }]}

result = requests.post(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/new', 
                       json=data, headers={'Authorization': 'Token %s'%your_token} )
result.status_code

If you want to insert multiple lines of data like two power plants in this case, you can insert a list of dictionaries with as many entries as the table has columns.

**201** succesfully inserted data!

In [ ]:
# show the id of the new rows
json_result = result.json()
json_result['data']
It is row number **[2]** and **[3]**!

5. Insert data into a specific row


In [ ]:
# insert data to row 11
schema = 'model_draft'
table = 'example_api_table_test'
rownumber = '11'

data = {"query": 
        {"name": "Anlage 11", 
                  "type": "photovoltaic", 
                   "capacity": 5.5,
                   "lat":   51.804783,
                   "lon":    10.41573
                 }}

result = requests.put(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/'+rownumber, 
                       json=data, headers={'Authorization': 'Token %s'%your_token} )
result.status_code
**201** succesfully inserted data!
**200** row already taken!
Insert some powerplants!

In [ ]:
# insert some data

6. Alter data in a table


In [ ]:
# alter data
schema = 'model_draft'
table = 'example_api_table_test'

result = requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/') # Load the names via GET
result.status_code
**200** table exists!

In [ ]:
row = "1"
data = {"query": {"capacity": "2", "type": "pv"}}

result = requests.post(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/{id}'.format(id=row), json=data, headers={'Authorization': 'Token %s'%your_token})
result.status_code
**200** succesfully changed data!