GeoParse Logo

GeoParse

All About Points No description has been provided for this image Lines No description has been provided for this image and Polygons No description has been provided for this image

Website¶


Data Visualization¶

Binder

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.

In [1]:
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")
In [2]:
# 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).

In [3]:
%%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
Out[3]:
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
In [4]:
len(df)
Out[4]:
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.

In [5]:
plp(df)
Out[5]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]:
 
In [ ]:
 

The plp function displays points in blue by default. However, you can change the point color using the point_color argument.

In [6]:
plp(df, point_color="purple")
Out[6]:
Make this Notebook Trusted to load map: File -> Trust Notebook

For a custom color, use an RGB hex code like "#cc5500" for burnt orange.

In [7]:
plp(df, point_color="#cc5500")
Out[7]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]:
 
In [ ]:
 

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.

In [8]:
plp(df, point_color="speed_limit")
Out[8]:
Make this Notebook Trusted to load map: File -> Trust Notebook

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.

In [9]:
plp(df, point_color="speed_limit", point_popup={"Date": "date", "Speed limit": "speed_limit"})
Out[9]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]:
 
In [ ]:
 

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.

In [10]:
plp(df, heatmap=True)
Out[10]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]:
 

plp groups points together based on their proximity by setting the cluster argument to True.

In [11]:
plp(df, cluster=True)
Out[11]:
Make this Notebook Trusted to load map: File -> Trust Notebook

We can also display both the heatmap and cluster simultaneously.

In [12]:
plp(df, heatmap=True, cluster=True)
Out[12]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]:
 

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.

In [ ]:
 

Read a Polygon from Geo File¶

In [13]:
%%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
Out[13]:
name geometry
0 London POLYGON ((-0.51035 51.46822, -0.51028 51.46837...
In [14]:
plp(gdf)
Out[14]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [15]:
plp(gdf, centroid=True)
Out[15]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [16]:
plp(gdf, h3_res=6)
Out[16]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [17]:
plp(gdf, h3_res=10, compact=True)
Out[17]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [18]:
plp(gdf, s2_res=12)
Out[18]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [19]:
plp(gdf, s2_res=14, compact=True)
Out[19]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [20]:
plp(gdf, geohash_res=5)
Out[20]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [21]:
plp(gdf, geohash_res=7, compact=True)
Out[21]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]:
 

Mutiple data frames¶

In [22]:
plp([df, gdf], h3_res=6)
Out[22]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Furthermore, GeoParse can visualize geospatial grids (including geohash, S2, and H3) and OSM ways (lines and polygons) directly on the map.

Geospatial Cells¶

In [23]:
import h3

lat, lon = 41.87, -87.78

# Get the H3 index at resolution 6 for the central point
h3_index = h3.latlng_to_cell(lat, lon, 6)

# Get adjacent H3 cells including the central cell itself
h3_cells = h3.grid_disk(h3_index, 1)  # k_ring with radius 1 returns the central cell + neighbors
h3_cells
Out[23]:
['862664c87ffffff',
 '862664cb7ffffff',
 '862664c97ffffff',
 '862664c9fffffff',
 '862664c8fffffff',
 '862664cafffffff',
 '862664ca7ffffff']
In [24]:
plp(cells=list(h3_cells), cell_type="h3")
Out[24]:
Make this Notebook Trusted to load map: File -> Trust Notebook

OSM Ways¶

In [25]:
plp(osm_ways=[335265936, 53820456, 1117218957], s2_res=20, compact=True)
Out[25]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [26]:
plp(osm_ways=[260909736, 54454413])
Out[26]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]: