Getting Started with casaGeoTools

Introduction

The casaGeoTools library is designed to seamlessly integrate powerful location services into your Python data science workflow. This guide will walk you through the installation, secure authentication, and your first isochrone request.

1. Installation

Install the library directly via PyPI. Use your terminal or command prompt:

pip install casaGeoTools

For your first script, you should import the following packages:

Python Script

import os
from dotenv import load_dotenv

import pandas as pd
import shapely

# casaGeoTools
from casageo import coder as cc # Not required for this example
from casageo import spatial as cs
from casageo import tools as ct

pd.set_option("display.max_rows", None)
pd.set_option("display.max_columns", None)
pd.set_option("display.width", 999)

1.1 Explanations

  1. pd.set_option(): These settings ensure that all columns in the results are displayed in your output.

2. Access and API Key

  1. A valid access key is required to use the services. You can request your credentials here: https://www.casageo.de/en/here-connector-for-python.html
  2. New users can request a one-time trial access to test the functionality immediately.

3. Secure Setup (.env)

In professional projects, API keys should never be hardcoded. We recommend using a .env file.

  1. Create a file named .env in your project folder.
  2. Add your key: CASAGEOTOOLS_API_KEY= your_key_here
  3. Use python_dotenv to load the key.

Python Script

import os
from dotenv import load_dotenv
load_dotenv() # Lädt die Variable aus der .env-Datei


API_KEY = os.getenv("CASAGEOTOOLS_API_KEY")


if not API_KEY:
   raise ValueError(
      "No CASAGEOTOOLS_API_KEY found in .env file. Please set your casaGeoTools API key.")


cga = ct.CasaGeoClient(API_KEY)
cga.preferred_language = ["de", "en"]
account_info = cga.account_info().json()


account_info # Output in Jupyter Notebook
print(account_info) # Output in Python script

3.1 Explanations

  1. cga.preferred_language: Sets the language for the returned results.
  2. cga.account_info().json(): Provides user information such as expiration dates, package details, and a credit overview.

4. Your First Request

Calculating isochrones with casaGeoTools is efficient and delivers data directly in the GeoDataFrame format.

Python Script

import shapely


point_hh = shapely.Point(10.008223, 53.9580118) # Standort für den das Einzugsgebiet berechnet wird


cgs = cs.CasaGeoSpatial(cga)
cgs.transport_mode = "car"
cgs.routing_mode = "fast"


single_isolines = cgs.isolines(poin_hh, [3,9,15], range_type="time").dataframe(departure_info=False)


single_isolines # Output in Jupyter Notebook


print(single_isolines) # Output in Python script

4.1 Explanations

  1. cgs.transport_mode: Currently "car", "pedestrian", and "bicycle" are available..
  2. cgs.routing_mode: Choose between "fast" (quickest route) and "short" (shortest distance).
  3. cgs.isolines(...,[3,9,15],range_type="time"): [3,9,15] generates 3, 9, and 15-minute isochrones around the location. For a single 5-minute isochrone, use [5]. Time parameters are always in minutes, distance parameters in meters. For example, [3000, 9000, 15000] calculates areas for 3, 9, and 15 kilometers (change range_type from "time" to "distance").
  4. cgs.isolines().dataframe(departure_info=False): Hides the departure time in the result table. By default, traffic is not considered, making this info irrelevant in this case.

5. Downloads

To jumpstart your development, you can download this example as a Jupyter Notebook or a standalone Python script below