Exemplo n.º 1
0
def _trackintel_model(gdf, set_names=None, geom_col=None, crs=None, tz_cols=None, tz=None):
    """Help function to assure the trackintel model on a GeoDataFrame.

    Parameters
    ----------
    gdf : GeoDataFrame
        Input GeoDataFrame

    set_names : dict, optional
        Renaming dictionary for the columns of the GeoDataFrame.

    set_geometry : str, optional
        Set geometry of GeoDataFrame.

    crs : pyproj.crs or str, optional
        Set coordinate reference system. The value can be anything accepted
        by pyproj.CRS.from_user_input(), such as an authority string
        (eg "EPSG:4326") or a WKT string.

    tz_cols : list, optional
        List of timezone aware datetime columns.

    tz : str, optional
        pytz compatible timezone string. If None UTC will be assumed

    Returns
    -------
    gdf : GeoDataFrame
        The input GeoDataFrame transformed to match the trackintel format.
    """
    if set_names is not None:
        gdf = gdf.rename(columns=set_names)

    if tz_cols is not None:
        for col in tz_cols:
            if not pd.api.types.is_datetime64tz_dtype(gdf[col]):
                try:
                    gdf[col] = _localize_timestamp(dt_series=gdf[col], pytz_tzinfo=tz, col_name=col)
                except ValueError:
                    # Taken if column contains datetimes with different timezone informations.
                    # Cast them to UTC in this case.
                    gdf[col] = pd.to_datetime(gdf[col], utc=True)

    # If is not GeoDataFrame and no geom_col is set end early.
    # That allows us to handle DataFrames and GeoDataFrames in one function.
    if not isinstance(gdf, gpd.GeoDataFrame) and geom_col is None:
        return gdf

    if geom_col is not None:
        gdf = gdf.set_geometry(geom_col)
    else:
        try:
            gdf.geometry
        except AttributeError:
            raise AttributeError("GeoDataFrame has no geometry, set it with keyword argument.")

    if crs is not None:
        gdf = gdf.set_crs(crs)

    return gdf
Exemplo n.º 2
0
def read_positionfixes_gpd(gdf,
                           tracked_at="tracked_at",
                           user_id="user_id",
                           geom="geom",
                           tz=None,
                           mapper={}):
    """
    Read positionfixes from GeoDataFrames.

    Warps the pd.rename function to simplify the import of GeoDataFrames.

    Parameters
    ----------
    gdf : GeoDataFrame
        GeoDataFrame with valid point geometry, containing the positionfixes to import

    tracked_at : str, default 'tracked_at'
        name of the column storing the timestamps.

    user_id : str, default 'user_id'
        name of the column storing the user_id.

    geom : str, default 'geom'
        name of the column storing the geometry.

    tz : str, optional
        pytz compatible timezone string. If None UTC will be assumed

    mapper : dict, optional
        further columns that should be renamed.

    Returns
    -------
    pfs : GeoDataFrame (as trackintel positionfixes)
        A GeoDataFrame containing the positionfixes.

    Examples
    --------
    >>> trackintel.read_positionfixes_gpd(gdf, user_id='User', geom='geometry', tz='utc')
    """
    columns = {tracked_at: "tracked_at", user_id: "user_id", geom: "geom"}
    columns.update(mapper)

    pfs = gdf.rename(columns=columns)
    pfs = pfs.set_geometry("geom")

    # check and/or set timezone
    for col in ["tracked_at"]:
        if not pd.api.types.is_datetime64tz_dtype(pfs[col]):
            pfs[col] = _localize_timestamp(dt_series=pfs[col],
                                           pytz_tzinfo=tz,
                                           col_name=col)

    # assert validity of positionfixes
    pfs.as_positionfixes
    return pfs
Exemplo n.º 3
0
def read_staypoints_gpd(gdf,
                        started_at="started_at",
                        finished_at="finished_at",
                        user_id="user_id",
                        geom="geom",
                        tz=None,
                        mapper={}):
    """
    Read staypoints from GeoDataFrames.

    Warps the pd.rename function to simplify the import of GeoDataFrames.

    Parameters
    ----------
    gdf : GeoDataFrame
        GeoDataFrame with valid point geometry, containing the staypoints to import

    started_at : str, default 'started_at'
        name of the column storing the starttime of the staypoints.

    finished_at : str, default 'finished_at'
        name of the column storing the endtime of the staypoints.

    user_id : str, default 'user_id'
        name of the column storing the user_id.

    geom : str, default 'geom'
        name of the column storing the geometry.

    tz : str, optional
        pytz compatible timezone string. If None UTC is assumed.

    mapper : dict, optional
        further columns that should be renamed.

    Returns
    -------
    stps : GeoDataFrame (as trackintel staypoints)
        A GeoDataFrame containing the staypoints

    Examples
    --------
    >>> trackintel.read_staypoints_gpd(gdf, started_at='start_time', finished_at='end_time', tz='utc')
    """
    columns = {
        started_at: "started_at",
        finished_at: "finished_at",
        user_id: "user_id",
        geom: "geom"
    }
    columns.update(mapper)

    stps = gdf.rename(columns=columns)
    stps = stps.set_geometry("geom")

    # check and/or set timezone
    for col in ["started_at", "finished_at"]:
        if not pd.api.types.is_datetime64tz_dtype(stps[col]):
            stps[col] = _localize_timestamp(dt_series=stps[col],
                                            pytz_tzinfo=tz,
                                            col_name=col)

    # assert validity of staypoints
    stps.as_staypoints
    return stps
Exemplo n.º 4
0
def read_trips_gpd(
    gdf,
    started_at="started_at",
    finished_at="finished_at",
    user_id="user_id",
    origin_staypoint_id="origin_staypoint_id",
    destination_staypoint_id="destination_staypoint_id",
    tz=None,
    mapper={},
):
    """
    Read trips from GeoDataFrames/DataFrames.

    Warps the pd.rename function to simplify the import of GeoDataFrames (DataFrames).

    Parameters
    ----------
    gdf : GeoDataFrame or DataFrame
        GeoDataFrame/DataFrame containing the trips to import.

    started_at : str, default 'started_at'
        name of the column storing the starttime of the staypoints.

    finished_at : str, default 'finished_at'
        name of the column storing the endtime of the staypoints.

    user_id : str, default 'user_id'
        name of the column storing the user_id.

    origin_staypoint_id : str, default 'origin_staypoint_id'
        name of the column storing the staypoint_id of the start of the tripleg

    destination_staypoint_id : str, default 'destination_staypoint_id'
        name of the column storing the staypoint_id of the end of the tripleg

    tz : str, optional
        pytz compatible timezone string. If None UTC is assumed.

    mapper : dict, optional
        further columns that should be renamed.

    Returns
    -------
    trips : GeoDataFrame/DataFrame (as trackintel trips)
        A GeoDataFrame/DataFrame containing the trips.

    Examples
    --------
    >>> trackintel.read_trips_gpd(df, tz='utc')
    """
    columns = {
        started_at: "started_at",
        finished_at: "finished_at",
        user_id: "user_id",
        origin_staypoint_id: "origin_staypoint_id",
        destination_staypoint_id: "destination_staypoint_id",
    }
    columns.update(mapper)

    trips = gdf.rename(columns=columns)

    # check and/or set timezone
    for col in ["started_at", "finished_at"]:
        if not pd.api.types.is_datetime64tz_dtype(trips[col]):
            trips[col] = _localize_timestamp(dt_series=trips[col],
                                             pytz_tzinfo=tz,
                                             col_name=col)

    # assert validity of trips
    trips.as_trips
    return trips