35x
005597
2024-08-22

Web Queries (API) Geo-Zone Tool: Automation Using Python

How can I automate web requests (API) to the Geo-Zone Tool using Python?


Answer:

This is very easy. The following example shows you this.

Preparation

The web requests to the Geo-Zone Tool using Python require:

  1. Editor or IDE (Integrated Development Environment) for writing the script
  2. Python – python.org
  3. Python Library Requests
  4. Python Library - pandas (optional)

To run a web request of the Geo-Zone Tool, you need the information for the Geo-Zone Tool. This is explained in the following article using the example of the query URL structure:
Controlling WebService (API)

In this example, the following information is used, which you can replace with your own in the script:

  1. Language: en (English)
  2. Login: [email protected]
  3. Hash: 123456ABCD
  4. Map: wind-DIN-EN-1991-1-4 (wind load according to the German Annex EC1)
  5. Location: Dlubal, Tiefenbach (headquarters of Dlubal GmbH)
  6. Position: 49.4353975, 12.5894907 (Latitude, Longitude)

Executing Web Request and Reading Data

The following script queries the web service of the Geo-Zone Tool and documents the required times and content.

…
#%% Import
# Library for reading out timestamp (standard library, optional)
import datetime as dt
# Library to run web request
import requests

#%% Set parameters
# URL Webservice Geo-Zone Tool
urlgz = 'https://external-crm.dlubal.com/loadzones/data.aspx'

# Parameters for query (replace with your own values)
pargz = {
        'language': 'en',
        'login': '[email protected]',
        'hash': '123456ABCD',
        'map': 'wind-DIN-EN-1991-1-4',
        'place': 'Dlubal, Tiefenbach',
        'position': '49.4353975,12.5894907'
        }
# Set time for canceling the request
reto=10 # s

#%% Run query
# Timestamp before retrieval
cdt1 = dt.datetime.now()
# Web query via requests
rgz = requests.get(urlgz, params=pargz, timeout=reto)
# Timestamp after retrieval
cdt2 = dt.datetime.now()
# Query duration in seconds
dur=(cdt2-cdt1).total_seconds()
# HTTP status code of the request
sgz=rgz.status_code
# Contents description of the query
hgz=rgz.headers['content-type']
# Content of the web request as text
tgz = rgz.text

#%% Console output of the web request
txt=[]
txt.append(f"Timestamp: {cdt1}") # Time YYYY-MM-DD HH:MM:SS.SSSSSS
txt.append(f"Duration: {dur} s") # Duration of the query
txt.append(f"Status code: {rgz.status_code}") # HTTP status code (normal: 200)
txt.append(f"Header: {hgz}") # Content description (normal: text/html; charset=utf-8)
txt.append(f"Text output of request:\n{tgz}") # Output of Geo-Zone Tool
print('\n'.join(txt))
…

This leads to the following results, for example:

Timestamp: 2024-08-22 13:24:32.727006
Duration: 2.214527
Status code: 200
Header: text/html; charset=utf-8
Text output of request: 
Result 1,Result 2,Zone,Latitude,Longitude,Elevation,Street,ZIP,City,Standard,Annex,Note(s),Legal notice
22.5 m/s,0.32 kN/m²,1,49.4353975,12.5894907,520.69384765625,Am Zellweg 2,93464,Tiefenbach,EN 1991-1-4,DIN EN 1991-1-4,,All data without guarantee

Addition: Preparing Web Request Content

The following script converts the text obtained from the Geo-Zone Tool web service into a tabular form. Furthermore, the result values are separated from their units and finally saved as a CSV and an Excel file.

…
#%% Import
# String functions of standard library for import and export
from io import StringIO
# Library for data processing
import pandas as pd

#%% Functions
def rsep_val_unit(indf, cnstart='Result',):
    """
    Separates Dlubal Geo-Zone-Tool request Dataframe columns with results by value and unit.

    Parameters
    ----------
    indf : pandas.DataFrame
        Input data.
    cnstart: string, optional
        Identifier at start of column name containing results.

    Returns
    -------
    outdf : pandas.DataFrame
        Output data.

    """
    tmp2 = indf.loc(axis=1)[indf.columns.str.startswith(cnstart)]
    tmp3 = pd.DataFrame()
    for i in tmp2.columns:
        tmp3[[(i, 'value'), (i, 'unit')]] = tmp2[i].str.split(
            ' ', n=1, expand=True)
    outdf = pd.concat(
        [tmp3, indf.loc(axis=1)[~indf.columns.str.startswith(cnstart)]], axis=1)
    return outdf

#%% Run conversion
# Convert the output of Geo-Zone tool into "tabular" dataframe
dfgz=pd.read_csv(StringIO(rgz.text))
# DataFrame with results separated by value and unit
dfgz_rs=rsep_val_unit(dfgz)

#%% Saving
# as a CSV file
dfgz_rs.to_csv("Dlubal_GZT_request.csv")
# as an Excel file
dfgz_rs.to_excel("Dlubal_GZT_request.xlsx")

#%% Console output of conversion
print(f"Original Dataframe:\n{dfgz.to_string()}")
print(f"Manipulated Dataframe:\n{dfgz_rs.to_string()}")
print("Exemplary Output:\n"
      + f"   The first result has the value {dfgz_rs.iloc[0,0]}."
      + f" (in {dfgz_rs.iloc[0,1]})")
…

This results in the following, for example:

Original Dataframe:
   Result 1               Result 2  Zone   Latitude  Longitude   Elevation        Street    ZIP        City     Standard            Annex  Note(s)              Legal notice
0  22.5 m/s  0.32 kN/m²     1  49.435398  12.589491  520.693848  Am Zellweg 2  93464  Tiefenbach  EN 1991-1-4  DIN EN 1991-1-4      NaN  All data without guarantee
Manipulated Dataframe:
  (Result 1, value) (Result 1, unit) (Result 2, value)  (Result 2, unit)  Zone   Latitude  Longitude   Elevation        Street    ZIP        City     Standard            Annex  Note(s)              Legal notice
0              22.5              m/s              0.32  kN/m²     1  49.435398  12.589491  520.693848  Am Zellweg 2  93464  Tiefenbach  EN 1991-1-4  DIN EN 1991-1-4      NaN  All data without guarantee
Example output:
   The first result has the value 22.5. (in m/s)

Author

Mr. Gebhardt provides technical support for customers of Dlubal Software and takes care of their requests.

Links


;