GeoJSON Module¶
This section documents the geoJSON components of the Nextmv Python SDK - Local experience.
geojson_handler
¶
GeoJSON visualization handler module.
This module provides functionality to handle GeoJSON visualizations by converting them to interactive HTML maps using Folium. It supports various GeoJSON formats and automatically calculates optimal map positioning and zoom levels.
FUNCTION | DESCRIPTION |
---|---|
handle_geojson_visual |
Handle and write GeoJSON visuals to HTML files. |
extract_coordinates |
Recursively extract coordinates from nested coordinate structures. |
calculate_map_center_and_zoom |
Calculate the optimal center and zoom level for a GeoJSON map. |
extract_geojson_fields |
Extract available fields for tooltip and popup from GeoJSON data. |
create_geojson_map |
Create a folium map with GeoJSON data. |
calculate_map_center_and_zoom
¶
calculate_map_center_and_zoom(
geojson_data: dict,
) -> tuple[float, float, int]
Calculate the optimal center and zoom level for a GeoJSON map.
This function analyzes the geographic extent of GeoJSON features to determine the best map center point and zoom level for visualization. It extracts all coordinates from the features, calculates the centroid, and determines an appropriate zoom level based on the data's geographic spread.
PARAMETER | DESCRIPTION |
---|---|
|
A GeoJSON object containing features with geometric data. Should follow the GeoJSON specification with a "features" key containing an array of feature objects.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[float, float, int]
|
A tuple containing (center_latitude, center_longitude, zoom_level). - center_latitude : float The latitude coordinate for the map center - center_longitude : float The longitude coordinate for the map center - zoom_level : int The recommended zoom level (typically 4-12) |
Notes
- Default center is New York City (40.7128, -74.0060) with zoom level 12
- Zoom levels are calculated based on coordinate range:
- Range > 10 degrees: zoom level 4 (continental view)
- Range > 1 degree: zoom level 8 (regional view)
- Range > 0.1 degree: zoom level 10 (city view)
- Smaller ranges: zoom level 12 (neighborhood view)
- Falls back to defaults if no valid coordinates are found or errors occur
Source code in nextmv/nextmv/local/geojson_handler.py
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 |
|
create_geojson_map
¶
create_geojson_map(
geojson_data: dict, layer_name: str = "GeoJSON Layer"
) -> Map
Create a folium map with GeoJSON data.
This function creates an interactive map using Folium with the provided GeoJSON data. It automatically calculates the optimal center point and zoom level, extracts relevant fields for tooltips and popups, and configures the map with appropriate interactive features.
PARAMETER | DESCRIPTION |
---|---|
|
A GeoJSON object containing the geographic data to display. Should follow the GeoJSON specification with features and geometries.
TYPE:
|
|
The name to assign to the GeoJSON layer in the map, by default "GeoJSON Layer". This name appears in the layer control widget.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Map
|
A configured Folium map object with the GeoJSON data added as a layer. The map includes tooltips, popups, and layer controls when applicable. |
Notes
- Map center and zoom are automatically calculated based on the data extent
- Tooltips are added if suitable fields are found in feature properties
- Popups are added if suitable fields are found in feature properties
- Layer control is always added to allow toggling of the GeoJSON layer
- The map uses default Folium styling and can be further customized
Source code in nextmv/nextmv/local/geojson_handler.py
extract_coordinates
¶
extract_coordinates(coords, all_coords) -> None
Recursively extract coordinates from nested coordinate structures.
This function traverses nested coordinate structures commonly found in GeoJSON geometries and extracts all coordinate pairs. It handles various geometry types by recursively processing nested arrays until it finds coordinate pairs in [longitude, latitude] format.
PARAMETER | DESCRIPTION |
---|---|
|
The coordinate structure to extract from. Can be a nested list/tuple containing coordinate pairs or other nested structures.
TYPE:
|
|
A list to accumulate all extracted coordinate pairs. This list is modified in-place to store [longitude, latitude] pairs.
TYPE:
|
Notes
- Coordinate pairs are identified as lists/tuples with exactly 2 numeric elements
- The function expects coordinates in [longitude, latitude] format as per GeoJSON specification
- Nested structures are recursively processed to handle complex geometries like Polygons and MultiPolygons
Source code in nextmv/nextmv/local/geojson_handler.py
extract_geojson_fields
¶
extract_geojson_fields(
geojson_data: dict,
) -> tuple[list[str], list[str]]
Extract available fields for tooltip and popup from GeoJSON data.
This function analyzes the properties of GeoJSON features to identify suitable fields for displaying in map tooltips and popups. It prioritizes common field names and limits the number of fields to maintain usability.
PARAMETER | DESCRIPTION |
---|---|
|
A GeoJSON object containing features with properties. Should follow the GeoJSON specification with features containing properties objects.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[list[str], list[str]]
|
A tuple containing (tooltip_fields, popup_fields). - tooltip_fields : list[str] List of field names suitable for tooltips (max 3 fields) - popup_fields : list[str] List of field names suitable for popups (max 5 fields) |
Notes
- Prioritizes common field names: "name", "title", "label", "id", "popupContent", "description"
- Tooltip fields are limited to 3 to prevent overcrowding
- Popup fields are limited to 5 to maintain readability
- Returns empty lists if no features or properties are found
- Gracefully handles malformed GeoJSON data by returning empty lists
Source code in nextmv/nextmv/local/geojson_handler.py
handle_geojson_visual
¶
handle_geojson_visual(
asset: Asset, visuals_dir: str
) -> None
Handle and write GeoJSON visuals to HTML files.
This function processes GeoJSON visualization assets and converts them to interactive HTML maps using Folium. It handles multiple content formats including dictionaries, lists, and JSON strings. Each visualization is converted to a map with appropriate positioning and saved as an HTML file.
PARAMETER | DESCRIPTION |
---|---|
|
The asset containing the GeoJSON visualization data. The content can be a dictionary (single GeoJSON), a list (multiple GeoJSONs), or a JSON string representation.
TYPE:
|
|
The directory path where the HTML files will be written.
TYPE:
|
Notes
- For list content, each GeoJSON is saved with an index suffix (e.g., "map_0.html", "map_1.html")
- For dict content, the GeoJSON is saved with the asset label (e.g., "map.html")
- String content is parsed as JSON before processing
- Invalid JSON strings or unsupported content types are ignored with appropriate logging