Static Maps
staticmaps
¶
Assembly: Mappy
Filename: staticmaps.py
Author: Terry D. Eppler
Created: 05-31-2022
Last Modified By: Terry D. Eppler
Last Modified On: 05-01-2025
Mappy is a python framework encapsulating the Google Maps functionality.
Copyright © 2022 Terry Eppler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
You can contact me at: terryeppler@gmail.com or eppler.terry@epa.gov
Classes¶
StaticMap
¶
Generate Google Static Maps URLs.
Purpose
Builds embeddable Google Static Maps URLs for single-point previews, multi-marker previews, polyline paths, and rectangular bounding boxes. The class stores map URL parameters, marker state, coordinate state, map type, path styling, and bounding-box values so Streamlit workflows and reporting pages can render or inspect the latest generated static map request.
Attributes:
| Name | Type | Description |
|---|---|---|
api_key |
Optional[str]
|
Google Maps Platform API key read from configuration. |
zoom |
Optional[int]
|
Zoom level used by the latest generated map URL. |
size |
Optional[str]
|
Static map image dimensions in |
latitude |
Optional[float]
|
Latitude from the latest validated coordinate. |
longitude |
Optional[float]
|
Longitude from the latest validated coordinate. |
params |
Optional[Dict]
|
Query parameters used to build the latest Static Maps URL. |
markers |
Optional[str]
|
Marker parameter value used by the latest single-pin URL. |
base_url |
Optional[str]
|
Google Static Maps endpoint URL. |
points |
Optional[list]
|
Validated coordinate points used by multi-pin, path, or box maps. |
maptype |
Optional[str]
|
Static map type such as |
color |
Optional[str]
|
Marker or path color used by the latest generated URL. |
weight |
Optional[int]
|
Path stroke weight used by the latest generated URL. |
west |
Optional[float]
|
Western longitude of the latest bounding box. |
south |
Optional[float]
|
Southern latitude of the latest bounding box. |
east |
Optional[float]
|
Eastern longitude of the latest bounding box. |
north |
Optional[float]
|
Northern latitude of the latest bounding box. |
Initialize the Static Maps URL builder.
Purpose
Reads the Google Maps API key from configuration and initializes runtime state used by coordinate validation, marker generation, URL construction, path rendering, and bounding-box previews. The constructor performs local assignment only and prepares the object for later URL-building methods.
Source code in staticmaps.py
Methods:¶
validate_coordinate
¶
Validate and normalize a coordinate pair.
Purpose
Converts latitude and longitude inputs to floats and enforces valid geographic coordinate ranges before Static Maps URL parameters are built. The validated coordinate is stored on the instance for later map generation and inspection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
float
|
Latitude in decimal degrees. |
required |
lng
|
float
|
Longitude in decimal degrees. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple
|
Validated latitude and longitude. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after logging when validation or conversion fails. |
Source code in staticmaps.py
normalize_points
¶
Normalize supported point shapes into coordinate tuples.
Purpose
Accepts a list of coordinate tuples, coordinate lists, or dictionaries with
lat/lng or latitude/longitude fields and converts each item
into a validated coordinate tuple. The normalized point list is stored on
the instance for multi-pin, path, and bounding-box URL generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
list
|
Coordinate items to normalize. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
Validated coordinate tuples. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after logging when validation or normalization fails. |
Source code in staticmaps.py
build_url
¶
Build a Google Static Maps URL.
Purpose
Copies caller-supplied Static Maps query parameters, injects the configured Google Maps API key, stores the final parameter dictionary on the instance, and returns a fully qualified URL suitable for embedding in Streamlit, Markdown, reports, or browser links.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Dict
|
Google Static Maps query parameters. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: Fully qualified Google Static Maps URL. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after logging when validation or URL construction fails. |
Source code in staticmaps.py
pin
¶
Build a single-marker Static Maps URL.
Purpose
Validates a center coordinate, clamps the zoom level to the supported Static Maps range, stores marker and map state on the instance, and returns a URL centered on the coordinate with one marker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
float
|
Latitude for the marker and map center. |
required |
lng
|
float
|
Longitude for the marker and map center. |
required |
zoom
|
int
|
Zoom level from 1 through 20. |
12
|
size
|
int
|
Static map image dimensions in |
'400x300'
|
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: Fully qualified Static Maps URL. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after logging when validation or URL construction fails. |
Source code in staticmaps.py
pins
¶
pins(
points: list,
zoom: int = 0,
size: int = "600x400",
maptype: int = "roadmap",
color: int = "red",
) -> str | None
Build a multi-marker Static Maps URL.
Purpose
Normalizes one or more coordinate points, validates the requested map type,
builds marker parameters for each point, and returns a Static Maps URL. When
zoom is greater than zero, the map is centered on the average coordinate
and the zoom is clamped. When zoom is zero, Google is allowed to fit the
visible area to the supplied points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
list
|
Coordinate tuples, coordinate lists, or coordinate dictionaries. |
required |
zoom
|
int
|
Zoom level. Use zero to omit explicit zoom and let Google fit bounds. |
0
|
size
|
int
|
Static map image dimensions in |
'600x400'
|
maptype
|
int
|
Map type: |
'roadmap'
|
color
|
int
|
Marker color. |
'red'
|
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: Fully qualified Static Maps URL. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after logging when validation or URL construction fails. |
Source code in staticmaps.py
path
¶
path(
points: list,
size: int = "600x400",
maptype: int = "roadmap",
color: int = "0x0000ff",
weight: int = 5,
) -> str | None
Build a Static Maps URL showing a path.
Purpose
Normalizes two or more coordinate points, validates the requested map type, builds a Static Maps path parameter with configured color and stroke weight, adds start and end markers, and includes the path points as visible bounds so the generated image frames the route.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
list
|
Coordinate tuples, coordinate lists, or coordinate dictionaries. |
required |
size
|
int
|
Static map image dimensions in |
'600x400'
|
maptype
|
int
|
Map type: |
'roadmap'
|
color
|
int
|
Path color. |
'0x0000ff'
|
weight
|
int
|
Path stroke weight. |
5
|
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: Fully qualified Static Maps URL. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after logging when validation or URL construction fails. |
Source code in staticmaps.py
bbox
¶
bbox(
west: float,
south: float,
east: float,
north: float,
size: int = "600x400",
maptype: int = "roadmap",
) -> str | None
Build a Static Maps URL showing a bounding box.
Purpose
Validates bounding-box edges, converts them into southwest, northwest,
northeast, southeast, and closing southwest coordinates, stores the box
state on the instance, and delegates to path to render the rectangle
as a red outline on a Static Maps image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
west
|
float
|
Western longitude. |
required |
south
|
float
|
Southern latitude. |
required |
east
|
float
|
Eastern longitude. |
required |
north
|
float
|
Northern latitude. |
required |
size
|
int
|
Static map image dimensions in |
'600x400'
|
maptype
|
int
|
Map type: |
'roadmap'
|
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: Fully qualified Static Maps URL. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after logging when validation or URL construction fails. |
Source code in staticmaps.py
Functions:¶
throw_if
¶
Validate that a required static-map value is present.
Purpose
Provides a lightweight guard for required Google Static Maps inputs before coordinate validation, point normalization, URL construction, and bounding-box generation. The function rejects missing objects and blank strings so map URL workflows fail before producing incomplete or ambiguous query parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the argument being validated. |
required |
value
|
object
|
Runtime value to validate. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
Raised when |