Skip to content

Mapping

Optional Dependencies

To use this module, you need to install jetfuelburn with all optional dependencies:

pip install jetfuelburn[optionaldependencies]

jetfuelburn.utility.mapping

plot_ofp_1d

plot_ofp_1d(
    ofp_path_or_df,
    x_col="timestamp",
    y_col="alt",
    label_col=None,
    output_html=None,
)

Given an operational flight plan (OFP), plots a one-dimensional altitude profile using Plotly.

Parameters:

Name Type Description Default
ofp_path_or_df str or DataFrame

Path to the CSV file (or URL) containing trajectory data, OR a pandas DataFrame.

required
x_col str

Column name for the x-axis (default: 'timestamp').

'timestamp'
y_col str

Column name for the y-axis (default: 'alt').

'alt'
label_col str

Column name for the labels to display next to the points (default: None).

None
output_html str

Path to save the generated plot as an HTML file.

None

Returns:

Type Description
Figure

The generated Plotly figure object.

Example

Editor (session: default)Run
import jetfuelburn
from jetfuelburn.utility.mapping import plot_ofp_1d
fig = plot_ofp_1d(
    ofp_path_or_df="https://raw.githubusercontent.com/sustainableaviation/jetfuelburn/refs/heads/main/tests/data/ofp/ofp_with_altitude.csv",
)
OutputClear

Source code in jetfuelburn/utility/mapping.py
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def plot_ofp_1d(
    ofp_path_or_df: str | Path | pd.DataFrame,
    x_col: str = "timestamp",
    y_col: str = "alt",
    label_col: str | None = None,
    output_html: str | None = None,
) -> go.Figure:
    r"""
    Given an operational flight plan (OFP),
    plots a one-dimensional altitude profile using [Plotly](https://plotly.com/python/).

    Parameters
    ----------
    ofp_path_or_df : str or pandas.DataFrame
        Path to the CSV file (or URL) containing trajectory data, OR a pandas DataFrame.
    x_col : str, optional
        Column name for the x-axis (default: `'timestamp'`).
    y_col : str, optional
        Column name for the y-axis (default: `'alt'`).
    label_col : str, optional
        Column name for the labels to display next to the points (default: `None`).
    output_html : str, optional
        Path to save the generated plot as an HTML file.

    Returns
    -------
    plotly.graph_objects.Figure
        The generated Plotly figure object.

    Example
    -------
    ```pyodide install='jetfuelburn'
    import jetfuelburn
    from jetfuelburn.utility.mapping import plot_ofp_1d
    fig = plot_ofp_1d(
        ofp_path_or_df="https://raw.githubusercontent.com/sustainableaviation/jetfuelburn/refs/heads/main/tests/data/ofp/ofp_with_altitude.csv",
    )
    ```

    ```python exec="true" html="true"
    from jetfuelburn.utility.mapping import plot_ofp_1d
    fig = plot_ofp_1d(
        ofp_path_or_df="https://raw.githubusercontent.com/sustainableaviation/jetfuelburn/refs/heads/main/tests/data/ofp/ofp_with_altitude.csv",
    )
    print(fig.to_html(full_html=False, include_plotlyjs="cdn"))
    ```
    """
    if isinstance(ofp_path_or_df, (str, Path)):
        df = pd.read_csv(ofp_path_or_df)
    else:
        df = ofp_path_or_df.copy()

    mode = "lines+markers"
    text = None
    if label_col and label_col in df.columns:
        text = df[label_col]

    fig = go.Figure()

    if text is not None:
        hovertemplate = (
            "<b>%{text}</b><br>" + f"{x_col}: %{{x}}<br>{y_col}: %{{y}}<extra></extra>"
        )
    else:
        hovertemplate = f"{x_col}: %{{x}}<br>{y_col}: %{{y}}<extra></extra>"

    fig.add_trace(
        go.Scatter(
            x=df[x_col] if x_col in df.columns else df.index,
            y=df[y_col] if y_col in df.columns else [],
            mode=mode,
            text=text,
            textposition="top center",
            name="Trace",
            line=dict(color="blue"),
            marker=dict(size=6),
            hovertemplate=hovertemplate,
        )
    )

    fig.update_layout(
        template="plotly_white",
        hovermode="x unified",
        showlegend=False,
    )

    if output_html:
        fig.write_html(output_html)

    return fig

plot_ofp_2d

plot_ofp_2d(
    ofp_path_or_df,
    label_col="waypoint",
    lat_col="lat",
    lon_col="lon",
    output_html=None,
)

Plots flight trajectory data from operational flight plans (OFP) on an interactive Plotly Map map.

Parameters:

Name Type Description Default
ofp_path_or_df str or DataFrame

Path to the CSV file (or URL) containing trajectory data, OR a pandas DataFrame already containing the data. The data must contain at least the following columns:

Column Type Description
lat float Latitude of the waypoint (decimal degrees).
lon float Longitude of the waypoint (decimal degrees).
waypoint str Short name or designator for the waypoint.
required
label_col str

The name of the CSV column to use for labeling the waypoints on the map (default: 'waypoint').

'waypoint'
lat_col str

The name of the CSV column containing latitude values (default: 'lat').

'lat'
lon_col str

The name of the CSV column containing longitude values (default: 'lon').

'lon'
output_html str

Path to save the generated map as an HTML file.

None

Returns:

Type Description
Figure

The generated Plotly Figure object.

Warnings

Rows with missing or invalid lat/lon values are automatically skipped.

Notes

The map uses the OpenStreetMap style by default and is centered at the mean position of all valid waypoints.

Example

Editor (session: default)Run
import jetfuelburn
from jetfuelburn.utility.mapping import plot_ofp_2d
fig = plot_ofp_2d(
    ofp_path_or_df="https://raw.githubusercontent.com/sustainableaviation/jetfuelburn/refs/heads/main/tests/data/ofp/ofp_basic.csv",
    label_col="waypoint",
    lat_col="lat",
    lon_col="lon",
)
fig.show()
OutputClear

Source code in jetfuelburn/utility/mapping.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
def plot_ofp_2d(
    ofp_path_or_df: str | Path | pd.DataFrame,
    label_col: str = "waypoint",
    lat_col: str = "lat",
    lon_col: str = "lon",
    output_html: str | None = None,
) -> go.Figure:
    r"""
    Plots flight trajectory data from operational flight plans (OFP) on an
    interactive [Plotly Map](https://plotly.com/python/map-layers/) map.

    Parameters
    ----------
    ofp_path_or_df : str or pandas.DataFrame
        Path to the CSV file (or URL) containing trajectory data, OR a pandas DataFrame
        already containing the data.
        The data must contain at least the following columns:

        | Column     | Type    | Description                                      |
        |------------|---------|--------------------------------------------------|
        | `lat`      | `float` | Latitude of the waypoint (decimal degrees).      |
        | `lon`      | `float` | Longitude of the waypoint (decimal degrees).     |
        | `waypoint` | `str`   | Short name or designator for the waypoint.       |

    label_col : str, optional
        The name of the CSV column to use for labeling the waypoints on the map (default: `'waypoint'`).

    lat_col : str, optional
        The name of the CSV column containing latitude values (default: `'lat'`).

    lon_col : str, optional
        The name of the CSV column containing longitude values (default: `'lon'`).

    output_html : str, optional
        Path to save the generated map as an HTML file.

    Returns
    -------
    plotly.graph_objects.Figure
        The generated Plotly Figure object.

    Warnings
    -----
    Rows with missing or invalid lat/lon values are automatically skipped.

    Notes
    -----
    The map uses the OpenStreetMap style by default and is centered at the mean position
    of all valid waypoints.

    Example
    -------
    ```pyodide install='jetfuelburn'
    import jetfuelburn
    from jetfuelburn.utility.mapping import plot_ofp_2d
    fig = plot_ofp_2d(
        ofp_path_or_df="https://raw.githubusercontent.com/sustainableaviation/jetfuelburn/refs/heads/main/tests/data/ofp/ofp_basic.csv",
        label_col="waypoint",
        lat_col="lat",
        lon_col="lon",
    )
    fig.show()
    ```

    ```python exec="true" html="true"
    from jetfuelburn.utility.mapping import plot_ofp_2d
    fig = plot_ofp_2d(
        ofp_path_or_df="https://raw.githubusercontent.com/sustainableaviation/jetfuelburn/refs/heads/main/tests/data/ofp/ofp_basic.csv",
    )
    print(fig.to_html(full_html=False, include_plotlyjs="cdn"))
    ```
    """
    if isinstance(ofp_path_or_df, (str, Path)):
        df = pd.read_csv(ofp_path_or_df)
    else:
        df = ofp_path_or_df.copy()

    if lat_col in df.columns and lon_col in df.columns:
        df[lat_col] = pd.to_numeric(df[lat_col], errors="coerce")
        df[lon_col] = pd.to_numeric(df[lon_col], errors="coerce")
        df = df.dropna(subset=[lat_col, lon_col])
    else:
        df = pd.DataFrame(columns=[lat_col, lon_col])

    if df.empty:
        center = dict(lat=0, lon=0)
        zoom = 1
        bounds = None
    else:
        min_lat, max_lat = df[lat_col].min(), df[lat_col].max()
        min_lon, max_lon = df[lon_col].min(), df[lon_col].max()

        center = dict(lat=(min_lat + max_lat) / 2, lon=(min_lon + max_lon) / 2)

        lat_span = max_lat - min_lat
        lon_span = max_lon - min_lon
        max_span = max(lat_span, lon_span, 0.1)

        zoom = max(0, min(18, math.log2(360 / max_span) - 0.5))

        bounds = dict(
            west=min_lon - lon_span * 0.2,
            east=max_lon + lon_span * 0.2,
            south=min_lat - lat_span * 0.2,
            north=max_lat + lat_span * 0.2,
        )

    fig = go.Figure()

    # 1. Background Path Line
    fig.add_trace(
        go.Scattermap(
            lat=df[lat_col],
            lon=df[lon_col],
            mode="lines",
            line=dict(width=3, color="blue"),
            hoverinfo="none",
            showlegend=False,
        )
    )

    if not df.empty:
        # 2. Intermediate Waypoints
        if len(df) > 2:
            intermediate = df.iloc[1:-1]
            fig.add_trace(
                go.Scattermap(
                    lat=intermediate[lat_col],
                    lon=intermediate[lon_col],
                    mode="markers",
                    marker=dict(size=8, color="blue"),
                    text=intermediate[label_col] if label_col in df.columns else None,
                    textposition="top right",
                    name="Waypoints",
                    hoverinfo="text",
                    showlegend=False,
                )
            )

        # 3. Start Point
        fig.add_trace(
            go.Scattermap(
                lat=[df.iloc[0][lat_col]],
                lon=[df.iloc[0][lon_col]],
                mode="markers",
                marker=dict(size=14, color="green", symbol="circle"),
                text=(
                    [str(df.iloc[0][label_col])]
                    if label_col in df.columns
                    else ["Start"]
                ),
                textposition="top right",
                name="Start",
                hoverinfo="text",
                showlegend=False,
            )
        )

        # 4. End Point
        if len(df) > 1:
            fig.add_trace(
                go.Scattermap(
                    lat=[df.iloc[-1][lat_col]],
                    lon=[df.iloc[-1][lon_col]],
                    mode="markers",
                    marker=dict(size=14, color="red", symbol="circle"),
                    text=(
                        [str(df.iloc[-1][label_col])]
                        if label_col in df.columns
                        else ["End"]
                    ),
                    textposition="top right",
                    name="End",
                    hoverinfo="text",
                    showlegend=False,
                )
            )

    fig.update_layout(
        map=dict(
            style="open-street-map",
            center=center,
            zoom=zoom,
            bounds=bounds,
        ),
        margin={"r": 0, "t": 30, "l": 0, "b": 0},
        showlegend=False,
    )

    if output_html:
        fig.write_html(output_html)

    return fig