r"""Finding the timezone of a given point or polygon geometry, assuming
    that the polygon is not crossing a border of a timezone. For a given point
    or polygon geometry not located within the timezone dataset (e.g. sea) the
    nearest timezone based on the bounding boxes of the geometries is returned.

    Parameters
    ----------
    connection : sqlalchemy connection object
        A valid connection to a postigs database containing the timezone table
    geometry : shapely geometry object
        A point or polygon object. The polygon should not cross a timezone.

    Returns
    -------
    string
        Timezone using the naming of the IANA time zone database

    References
    ----------
    http://postgis.net/docs/manual-2.2/geometry_distance_box.html

def tz_from_geom(connection, geometry):
    r"""Finding the timezone of a given point or polygon geometry, assuming
    that the polygon is not crossing a border of a timezone. For a given point
    or polygon geometry not located within the timezone dataset (e.g. sea) the
    nearest timezone based on the bounding boxes of the geometries is returned.

    Parameters
    ----------
    connection : sqlalchemy connection object
        A valid connection to a postigs database containing the timezone table
    geometry : shapely geometry object
        A point or polygon object. The polygon should not cross a timezone.

    Returns
    -------
    string
        Timezone using the naming of the IANA time zone database

    References
    ----------
    http://postgis.net/docs/manual-2.2/geometry_distance_box.html
    """

    # TODO@Günni
    if geometry.geom_type in ['Polygon', 'MultiPolygon']:
        coords = geometry.centroid
    else:
        coords = geometry
    sql = """
        SELECT tzid FROM oemof_test.tz_world
        WHERE st_contains(geom, ST_PointFromText('{wkt}', 4326));
        """.format(wkt=coords.wkt)

    if not connection.execute(sql).fetchone():
        sql = """
            SELECT tzid FROM oemof_test.tz_world
            ORDER BY ST_PointFromText('{wkt}', 4326) <#> geom LIMIT 1;
            """.format(wkt=coords.wkt)
    return connection.execute(sql).fetchone()[0]