Generate vector tiles

This notebook takes the spatial signature geometries we have saved as a Parquet file and convert them to the vector tiles that can be used in a webmap.

Clip by coastline

We first clip the signatures by the coastline of GB using the coastline layer we initially used to delimit enclosures.

import os
import geopandas as gpd
from sqlalchemy import create_engine
user = os.environ.get('DB_USER')
pwd = os.environ.get('DB_PWD')
host = os.environ.get('DB_HOST')
port = os.environ.get('DB_PORT')

db_connection_url = f"postgres+psycopg2://{user}:{pwd}@{host}:{port}/built_env"
engine = create_engine(db_connection_url)
sql = f'SELECT * FROM gb_coastline_2016'
coastline = gpd.read_postgis(sql, engine, geom_col='geometry')
from shapely.ops import polygonize

polygons = polygonize(coastline.geometry)
polygons = gpd.GeoSeries([poly for poly in polygons], crs=coastline.crs)
polygons
0       POLYGON ((85970.000 14510.000, 85940.000 14530...
1       POLYGON ((86170.000 14440.000, 86200.000 14430...
2       POLYGON ((86330.000 13970.000, 86350.000 13980...
3       POLYGON ((85950.000 13950.000, 85950.000 13940...
4       POLYGON ((86080.000 15470.000, 86080.000 15450...
                              ...                        
2394    POLYGON ((166426.000 225405.000, 166426.000 22...
2395    POLYGON ((167080.000 225526.000, 167064.000 22...
2396    POLYGON ((171825.000 226293.000, 171839.000 22...
2397    POLYGON ((517572.000 424703.000, 517594.000 42...
2398    POLYGON ((327888.000 562274.000, 327971.000 56...
Length: 2399, dtype: geometry
df = gpd.read_parquet("../../urbangrammar_samba/spatial_signatures/signatures/signatures_combined_levels_orig.pq")
%%time
clipped = gpd.clip(df, polygons)
CPU times: user 26min 54s, sys: 465 ms, total: 26min 55s
Wall time: 26min 55s
# clipped = clipped.explode()
clipped = clipped.reset_index(drop=True)
clipped_polys = clipped[clipped.geom_type=="Polygon"]
clipped_polys.geom_type.value_counts()
Polygon    96936
dtype: int64

Save to GeoJSON

Clipped and cleaned signature geometry is now ready to be convereted to vector tiles. We use tippecanoe to do that. tippecanoe is a command-line toolkit that requires GeoJSON in lat/lon as an input.

clipped_polys.reset_index(drop=True)[["signature_type", "geometry"]].to_crs(4326).to_file("../../urbangrammar_samba/spatial_signatures/signatures/signatures_combined_levels_clipped_4326.geojson", driver="GeoJSON")

Generate tiles

Finally, we run tippecanoe and create tiles.

! tippecanoe -z15 \
             --no-tile-compression \
             --output-to-directory=../../great-britain/tiles/ \
             --drop-densest-as-needed \
             --coalesce-smallest-as-needed --extend-zooms-if-still-dropping --detect-shared-borders --coalesce --reorder --hilbert \
             --force \
             ../../urbangrammar_samba/spatial_signatures/signatures/signatures_combined_levels_clipped_4326.geojson
For layer 0, using name "signatures_combined_levels_clipped_4326"
96936 features, 168540234 bytes of geometry, 13651 bytes of separate metadata, 116 bytes of string pool
tile 5/15/10 size is 765139 with detail 12, >500000    
Going to try keeping the sparsest 58.81% of the features to make it fit
tile 5/15/10 size is 731072 with detail 12, >500000    
Going to try keeping the sparsest 36.20% of the features to make it fit
tile 5/15/10 size is 646126 with detail 12, >500000    
Going to try keeping the sparsest 25.21% of the features to make it fit
tile 5/15/10 size is 563230 with detail 12, >500000    
Going to try keeping the sparsest 20.14% of the features to make it fit
tile 6/31/20 size is 812368 with detail 12, >500000    
Going to try keeping the sparsest 55.39% of the features to make it fit
tile 6/31/21 size is 695267 with detail 12, >500000    
Going to try keeping the sparsest 64.72% of the features to make it fit
tile 6/31/20 size is 735976 with detail 12, >500000    
Going to try keeping the sparsest 33.87% of the features to make it fit
tile 6/31/21 size is 669677 with detail 12, >500000    
Going to try keeping the sparsest 43.49% of the features to make it fit
tile 6/31/20 size is 605705 with detail 12, >500000    
Going to try keeping the sparsest 25.16% of the features to make it fit
tile 6/31/21 size is 559870 with detail 12, >500000    
Going to try keeping the sparsest 34.96% of the features to make it fit
tile 6/31/20 size is 518924 with detail 12, >500000    
Going to try keeping the sparsest 21.82% of the features to make it fit
tile 6/31/21 size is 508719 with detail 12, >500000    
Going to try keeping the sparsest 30.92% of the features to make it fit
tile 7/63/41 size is 930588 with detail 12, >500000    
Going to try keeping the sparsest 48.36% of the features to make it fit
tile 7/63/42 size is 888442 with detail 12, >500000    
Going to try keeping the sparsest 50.65% of the features to make it fit
tile 7/63/41 size is 776312 with detail 12, >500000    
Going to try keeping the sparsest 28.03% of the features to make it fit
tile 7/63/42 size is 699392 with detail 12, >500000    
Going to try keeping the sparsest 32.59% of the features to make it fit
tile 7/63/41 size is 542427 with detail 12, >500000    
Going to try keeping the sparsest 23.25% of the features to make it fit
tile 7/63/42 size is 555598 with detail 12, >500000    
Going to try keeping the sparsest 26.40% of the features to make it fit
tile 8/127/85 size is 611913 with detail 12, >500000    
Going to try keeping the sparsest 73.54% of the features to make it fit
tile 8/126/82 size is 659545 with detail 12, >500000    
Going to try keeping the sparsest 68.23% of the features to make it fit
tile 8/127/85 size is 581803 with detail 12, >500000    
Going to try keeping the sparsest 56.88% of the features to make it fit
tile 8/126/82 size is 561495 with detail 12, >500000    
Going to try keeping the sparsest 54.68% of the features to make it fit
tile 8/126/82 size is 509050 with detail 12, >500000    
Going to try keeping the sparsest 48.34% of the features to make it fit
tile 8/127/85 size is 542002 with detail 12, >500000    
Going to try keeping the sparsest 47.22% of the features to make it fit
  99.9%  15/16275/10435