Visualize API

visualize.geojsonio_contents(**kwargs) str

Generate a GeoJSON string for visualization with geojson.io from input trajectory data.

Parameters:

kwargs (dict) –

Keyword arguments to specify trajectory data. Must include one of the following:

  • trajectories (list): A list of trajectories, where each trajectory is a list of coordinates (each coordinate is a 2-element list of floats).

  • trajectory (list): A single trajectory, represented as a list of coordinates (each coordinate is a tuple of two floats).

  • swapcoords (bool): A flag to swap latitudes with longitudes.

Returns:

A GeoJSON string formatted as either a FeatureCollection (for multiple trajectories) or a LineString (for a single trajectory).

Return type:

str

Raises:

ValueError – If no arguments are provided or if the provided arguments do not match the expected format.

Examples

get_geojsonio_contents(
    trajectories=[[[19.603914, -99.01801], [19.60482, -99.016198]]]
)
# Output:
# '{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": 
# {"type": "LineString", "coordinates": [[-99.01801, 19.603914], [-99.016198, 
# 19.60482]]}, "properties": {}}]}'

get_geojsonio_contents(
    trajectory=[(19.603914, -99.01801), (19.60482, -99.016198)]
)
# Output:
# '{"type": "LineString", "coordinates": [[-99.01801, 19.603914], [-99.016198, 
# 19.60482]]}'

Note

This function expects coordinates to be in the format [latitude, longitude] for trajectories or (latitude, longitude) for trajectory. The _swap_coordinates helper function is used to adjust coordinates to the required [longitude, latitude] order for GeoJSON output.

visualize.get_geojsonio_trajectory(trajectory_data, swapcoords=True)

Convert a single trajectory to a GeoJSON LineString format.

This function converts a single trajectory, represented as a dictionary with trajectory details, into the GeoJSON format. The resulting GeoJSON is in the form of a LineString, with coordinates swapped to (longitude, latitude) format.

Parameters:

trajectory_data (dict) – A dictionary with trajectory details, including ‘coordinates’.

Returns:

GeoJSON dictionary for the trajectory in LineString format.

Return type:

dict

visualize.get_geojson_trajectories(geojson_obj)

Convert all trajectories to GeoJSON FeatureCollection format.

This function takes a GeoJSON object containing multiple trajectories and converts them into a GeoJSON FeatureCollection format. Each trajectory is represented as a Feature with a LineString geometry and properties such as the trajectory’s ‘id’ and ‘timestamp’.

Parameters:

geojson_obj (GeoJSON) – GeoJSON object containing multiple trajectories.

Returns:

GeoJSON FeatureCollection for all trajectories.

Return type:

dict

visualize._swap_coordinates(coords)

Swap latitude and longitude for each coordinate tuple in a trajectory.

This function takes a list of coordinates, where each coordinate is represented as a tuple of (latitude, longitude), and returns the list with the order swapped to (longitude, latitude). This is often required for proper visualization in some mapping libraries that expect coordinates in the (longitude, latitude) order.

Parameters:

coords (list[list[float, float]]) – List of coordinate tuples in (latitude, longitude) format.

Returns:

List of coordinates in (longitude, latitude) format.

Return type:

list[list[float, float]]