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
pip install opencity
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.
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.
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.
- get an overview of data sets via
show_data()
- load data directly into Python via
get_data()
- download data sets onto your local machine via
save_data()
Generally, each functionality can be filtered by names and tags of the different data sets.
class with several features described in the following
- 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
- show_data()
- get_data()
- save_data()
Each functionality is described in the following in detail.
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
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 containingpandas.DataFrame
s
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
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()
.
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()
open_city.show_data()
# 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
# 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
open_city.show_data(meta = True, terminal = True) #indicate one or several tags as a list of Strings
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
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
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
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
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'.
open_city.save_data(data = ["<Tag>"], tag = True) #indicate one or several tags as a list of Strings
path = "C:/Users/example_path" #important to use either forward slashes or double backward slashes!
open_city.save_data(data = ["<shortname>"], folder = path)
Open an issue including OS, package- and python version, executed code and error message!
Birke Pfeifle
Rahkakavee Baskaran
Tilman Kerl
Silke Husse
Email: konstanz@correlaid.org