Skip to content

Instantly share code, notes, and snippets.

@jwalgran
Last active October 10, 2019 17:49
Show Gist options
  • Save jwalgran/0da9ee94079bc35d96ceeb8367f060f3 to your computer and use it in GitHub Desktop.
Save jwalgran/0da9ee94079bc35d96ceeb8367f060f3 to your computer and use it in GitHub Desktop.
Implementing the ST_AsMVT based tiler for the Open Apparel Registry

Implementing the ST_AsMVT based tiler for the Open Apparel Registry

ST_AsMVT and ST_AsMVTGeom

https://postgis.net/docs/ST_AsMVT.html https://postgis.net/docs/ST_AsMVTGeom.html

Animation showing how the process works https://info.crunchydata.com/hs-fs/hubfs/unnamed.gif

Available on RDS Postgres as of the 10.5 release which includes the required libprotobuf-c

ADR

https://github.com/open-apparel-registry/open-apparel-registry/blob/develop/doc/arch/adr-002-decide-how-to-display-more-facilities.md

Documentation of a group discussion about options

Heavily influenced by the Vector Tiles Recommentations document https://docs.google.com/document/d/1H-CDMLR_4cy5hyE5SomWtTInvqNhipRWGYay5gcuJJ4/

First step: Feature flagged alternate map with just points

opensupplyhub/open-apparel-registry#730

Next step: Grid layer

opensupplyhub/open-apparel-registry#755 Aggregating at low zooms by dividing each tile into subtiles 3 zoom layers down

Next step: Redesign after user feedback

opensupplyhub/open-apparel-registry#797 "Why is that map broken" -- Deb's husband

Discussed several options at RRP (listed in the PR)

Several PostGIS hex grid implementations available. I found this one to be the simplest to implement and understand. Arguments are box and hex size. https://gist.github.com/mjumbewu/1761802ea06fb78c596f9cf8c9b2e769

Last step: Fix distortion

opensupplyhub/open-apparel-registry#839 Putting 4326 geometries in the grid caused problems at low zooms Bug in the hex grid at 180. Work around.

The final tiler implementation https://github.com/open-apparel-registry/open-apparel-registry/blob/e84c3e1d123ded50093d8c7fdbe542410d9d0990/src/django/api/tiler.py

The supporting filter method https://github.com/open-apparel-registry/open-apparel-registry/blob/e84c3e1d123ded50093d8c7fdbe542410d9d0990/src/django/api/models.py#L964-L1039

Caching

Cache buster in the URL that can be incremented on demand Far-future expires headers

Gotchas

One desktop browser is going to make ~16 tile requests for an initial map load. Try to get query speed in the 10ms order of magnitude

In our filter case we are fortunate that Django is generating an IN clause with IDs rather than joining multiple tables. Simplifies SQL string manipulation.

Two different buffers. The MVT tile buffer and the buffer uses when querying features.

The future

PostGIS 3 has a performance boost https://info.crunchydata.com/blog/waiting-for-postgis-3-st_asmvt-performance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment