Skip to content

Wrapper package for the open data of the city of Constance in for Python

License

Notifications You must be signed in to change notification settings

MisterXY89/konstanz-open-data-api

Repository files navigation

opencity

Project Status: Active – The project has reached a stable, usable state and is being actively developed. PyPI version shields.io

opencity is an interface for the open data portal of Constance. It allows you to directly inspect, download, and work with the available data. This package can be easily used by practitioners, members of the civil society, and academics. Technically, it relies on the DKAN API.
Install using pip. For potential problems see the the Installation and Problems part.

pip install opencity

Installation

pip install opencity

Potential problems

GeoPandas

When installing the opencity package on a Windows computer, you might run into trouble during the installation due to the package requirement geopandas. This package is necessary for reading in spatial data, which is available for some of the data sets. Please try this towardsdatascience article for assistance in installing geopandas and then try to install opencity again.

tk / tkinter

The show_data method has one option to show meta data of available data-sets in a popup window. If you want to use this feature and run into errors (e.g. There is an error with your Tkinter installation, use terminal=True to show the information anyway ) see this AskUbuntu Question or this StackOverflow Question.

However, you can always use the terminal=True parameter to display the same information in the terminal.

SSL: CERTIFICATE_VERIFY_FAILED

For later versions of Python on macOS, certificates are not pre-installed which seems to cause this error:

urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)

See this StackOverflow Question for a solution.

Capabilities

Generally, each functionality can be filtered by names and tags of the different data sets.

class opencity.OpenCity(cf = None)

class with several features described in the following

Properties :

  • formats
    list of Strings specifying formats
  • cf
    reference to configuration file containing all relevant information
  • (various) helper classes
    reference to various files containing helper classes

Functions :

  • show_data()
  • get_data()
  • save_data()

Each functionality is described in the following in detail.

OpenCity.show_data(data=[], tag=False, overview=False, meta=False, terminal=False)

displays an overview of the available and indicated data sets to the terminal or as a popup

Parameters:

  • data: list of Strings, default: empty list
    containing names or tags
  • tag: Boolean, default: False
    set to True if data list contains tags
  • overview: Boolean, default: False
    set to True if a short overview of the data sets (title, short name, tags) is desired in the console
  • meta: Boolean, default: False
    set to True if more detailed information on the data sets is desired depending on parameter 'terminal'
  • terminal: Boolean, default: False
    set to True if meta data should be printed in the console instead of a popup

Returns: void

OpenCity.get_data(data=[], tag=False, meta=False)

retrieves the indicated data from the open data portal of Constance

Parameters:

  • data: list of Strings, default: empty list
    containing names or tags
  • tag: Boolean, default: False
    set to True if data list contains tags
  • meta : Boolean, default: False
    set to True if meta data should be returned

Returns: pandas.DataFrame | dict containing pandas.DataFrames

OpenCity.save_data(data, tag=False, folder="", suppress=False)

saves the indicated data to the local disk

Parameters:

  • data: list of Strings
    containing names or tags
  • tag: Boolean, default: False
    set to True if data list contains tags
  • folder: String, default: empty
    If you want to save the data to a different folder than the one from which you are executing the python file, indicate the respective folder here (use either forward slashes '/' or double backward slashes '\')
  • suppress: Boolean, default: False
    set to True if you want to suppress the initial question asking whether you really want to download

Returns: void

Examples

In the following, we use <shortname> as a placeholder for the actual shortnames of each data set. To find out about the actual shortnames, you can use open_city.show_data(overview = True) and check the second column.

As a placeholder for the actual tags we use <Tag>. To find out about all the available tags, you can use open_city.show_data().

At first: create an instance of the class OpenCity

from opencity import opencity as oc
from opencity import config as conf

path = "<path>" # specify path here
cf = conf.Config(PKG_FOLDER=path)
open_city = oc.OpenCity(cf=cf)

Alternatively you can init an OpenCity object without specifying a path - the file holding all current datasets will then be stored in the current working directory:

from opencity import opencity as oc
open_city = oc.OpenCity()

show_data()

show the total number of available data sets and their tags

open_city.show_data()

show an overview

# of all available data sets:
open_city.show_data(overview = True)

# of all indicated data sets:
open_city.show_data(overview = True, data = ["<shortname>"]) #indicate one or several data sets as a list of Strings, using their shortname

# of all available data sets belonging to a certain tag:
open_city.show_data(overview = True, data = ["<Tag>"], tag = True) #indicate one or several tags as a list of Strings

show meta data in a popup table

# of all available data sets: 
open_city.show_data(meta = True)

# of all indicated data sets:
open_city.show_data(meta = True, data = ["<shortname>"]) #indicate one or several data sets as a list of Strings, using their shortname

# of all available data sets belonging to a certain tag:
open_city.show_data(meta = True, data = ["<Tag>"], tag = True) #you could also indicate several tags here

show meta data in the terminal

open_city.show_data(meta = True, terminal = True) #indicate one or several tags as a list of Strings

get_data()

get data of a data set

open_city.get_data(data = ["<shortname>"]) #indicate one or several data sets as a list of Strings, using their shortname

The output will look something like this:
Loading data
[+] Successfully loaded data set: radverkehr_kampagne_stadtradeln_konstanz_2018_csv
[+] Successfully loaded data set: radverkehr_kampagne_stadtradeln_konstanz_2019_csv
[+] Successfully loaded data set: radverkehr_kampagne_stadtradeln_konstanz_2020_csv

get data of a tag

open_city.get_data(data = ["<Tag>"], tag = True) #indicate one or several tags as a list of Strings

The output will look something like this:
Loading data
[+] Successfully loaded data set: einträge_im_mängelmelder_2017_csv
[-] External Link: Einträge im Mängelmelder 2017 Please visit https://konstanz.hub.arcgis.com/datasets/Konstanz::eintr%C3%A4ge-im-m%C3%A4ngelmelder-seit-dem-01-01-17
[+] Successfully loaded data set: außenwanderung_bei_stadtteil_von_2011_bis_2019_csv
[+] Successfully loaded data set: kindertagesbetreuung_einrichtungen_csv
[+] Successfully loaded data set: kindertagesbetreuung_einrichtungen_json

get meta data of a data set

open_city.get_data(data = ["<shortname>"], meta = True) #indicate one or several data sets as a list of Strings, using their shortname

The output will look something like this:
Loading data
[+] Successfully loaded meta data of 6 data sets

get meta data of a tag

open_city.get_data(data = ["<Tag>"], tag = True, meta = True) #indicate one or several tags as a list of Strings

The output will look something like this:
Loading data
[+] Successfully loaded meta data of 16 data sets

save_data()

save data of a data set

open_city.save_data(data = ["<shortname>"]) #indicate one or several data sets as a list of Strings, using their shortname

After you confirm the check-back with a y and the saving was successful, you will see a progress bar in your console, indicating the status of downloading the files. When the saving process is finished, the output on your console will look something like this:
[Saving Progress] : 100%|████████████████████████████████| 4/4 [00:15<00:00, 3.84s/it]
[+] Finished saving requested data to 'C:\Users\username\Downloads\test'.

save data of a tag

open_city.save_data(data = ["<Tag>"], tag = True) #indicate one or several tags as a list of Strings

save data to another folder than your project directory

path = "C:/Users/example_path" #important to use either forward slashes or double backward slashes!
open_city.save_data(data = ["<shortname>"], folder = path)

Found a bug?

Open an issue including OS, package- and python version, executed code and error message!

Author Information

Birke Pfeifle
Rahkakavee Baskaran
Tilman Kerl
Silke Husse

Email: [email protected]

About

Wrapper package for the open data of the city of Constance in for Python

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages