GeoParse
All About Points
Lines
and Polygons 
Website¶
Data Visualization¶
This notebook demonstrates how to display:
- Points, lines, and polygons.
- Their heatmaps and clusters.
- Their coverage areas using geospatial cells such as H3, S2, and Geohash.
- Geospatial cells on a map based on their indexes.
- OSM roads and buildings using their IDs.
import os
import sys
sys.path.append(os.path.abspath("../geoparse/"))
import warnings
import geopandas as gpd
import pandas as pd
from geoparse import plp
warnings.filterwarnings("ignore")
# Reload all changed modules every time before executing a new line
%load_ext autoreload
%autoreload 2
Read from Various File Formats¶
The plp (short for point, line, polygon) function in the Karta class (Swedish for "map") visualizes geometries using either a pandas DataFrame
or a GeoPandas GeoDataFrame
. When given a DataFrame
, plp
automatically detects columns containing "lat" and "lon" (case-insensitive) to use as latitude and longitude for plotting points on the map.
plp
creates interactive maps with multiple tile layers and supports data visualization from CSV
(as DataFrame) and GIS formats like Shapefile
, GPKG
, GeoJSON
, and GeoParquet
(as GeoDataFrame).
%%time
df = pd.read_csv("https://geoparse.io/tutorials/data/fatal_crash_great_britain_2023.csv")
df.head()
CPU times: user 45.5 ms, sys: 4.13 ms, total: 49.6 ms Wall time: 80.4 ms
date | time | latitude | longitude | number_of_vehicles | number_of_casualties | speed_limit | |
---|---|---|---|---|---|---|---|
0 | 03/01/2023 | 19:12 | 51.356551 | -0.097759 | 1 | 1 | 30 |
1 | 07/01/2023 | 10:05 | 51.593701 | 0.022379 | 1 | 1 | 30 |
2 | 14/01/2023 | 16:15 | 51.466689 | -0.011289 | 1 | 1 | 20 |
3 | 15/01/2023 | 19:51 | 51.671577 | -0.037543 | 1 | 1 | 30 |
4 | 16/01/2023 | 19:22 | 51.447944 | 0.117279 | 2 | 1 | 30 |
len(df)
1522
Available tile layers are:
- Light: A clean, minimalistic basemap that highlights overlaid data.
- Dark: A high-contrast, dark-themed map ideal for vibrant overlays and nighttime visualization.
- Outdoors: Designed for outdoor activities, featuring trails, elevation contours, and natural landmarks.
- Satellite: Displays high-resolution satellite imagery for real-world context and detailed analysis.
- OSM: A versatile map powered by OpenStreetMap, displaying roads, buildings, and points of interest.
You can switch between different tile layers using the options in the top right corner of the map.
plp(df)
The plp
function displays points in blue by default. However, you can change the point color using the point_color
argument.
plp(df, point_color="purple")
For a custom color, use an RGB
hex code like "#cc5500" for burnt orange.
plp(df, point_color="#cc5500")
The plp
function can group points by color based on their values, meaning points with the same value share the same color.
It assigns colors consistently by mapping input column values to a predefined color palette, ensuring a clear and structured visualization.
plp(df, point_color="speed_limit")
To enhance data interpretation, plp
allows tooltips that display attribute values when hovering over a point.
The point_popup
parameter is a dictionary where keys define tooltip labels, and values correspond to column names in the DataFrame.
plp(df, point_color="speed_limit", point_popup={"Date": "date", "Speed limit": "speed_limit"})
By passing the heatmap
argument as True
, plp
creates a heatmap layer for the points.
For better visualization, it's recommended to switch to Dark
mode and uncheck the Point
layer from the Layer Control menu in the top-right corner.
plp(df, heatmap=True)
plp
groups points together based on their proximity by setting the cluster
argument to True
.
plp(df, cluster=True)
We can also display both the heatmap
and cluster
simultaneously.
plp(df, heatmap=True, cluster=True)
TODO: buffer and rings
If no columns contain lat
and lon
keywords, or if more than two columns contain these keywords, you must explicitly specify the latitude and longitude using the y
and x
parameters, respectively, e.g., plp(df, x="easting", y="northing")
. Note that plp
assumes all data is in the EPSG:4326 projection.
For a GeoDataFrame
, the plp
function can render Shapely objects such as Point
, LineString
, Polygon
, and MultiPolygon
.
Furthermore, GeoParse can visualize geospatial grids (including geohash, S2, and H3) and OSM ways (lines and polygons) directly on the map.
Read a Polygon from Geo File¶
%%time
gdf = gpd.read_file("https://geoparse.io/tutorials/data/london.geojson")
gdf
CPU times: user 72.7 ms, sys: 26.1 ms, total: 98.8 ms Wall time: 238 ms
name | geometry | |
---|---|---|
0 | London | POLYGON ((-0.51035 51.46822, -0.51028 51.46837... |
plp(gdf)
plp(gdf, centroid=True)
plp(gdf, h3_res=6)
plp(gdf, h3_res=10, compact=True)
plp(gdf, s2_res=12)
plp(gdf, s2_res=14, compact=True)
plp(gdf, geohash_res=5)
plp(gdf, geohash_res=7, compact=True)