catalyst-offgrid / catalyst-aip

Catalyst Access Insights Platform (AIP)
https://accessinsights.org/
MIT License
2 stars 4 forks source link

Adding filters to raster layers #9

Open geohacker opened 4 years ago

geohacker commented 4 years ago

Filtering raster layers using expressions in Mapbox GL is currently unsupported. There's a discussion on Mapbox GL repo about raster-colorize but that hasn't really found a place on their roadmap.

I'm capturing some of the options we discussed so far:

  1. If we want to use the raster files as is, we might want to serve them as COGs but this means setting up a separate tile server.
  2. Try something like https://github.com/kylebarron/deck.gl-raster — but this is really experimental
  3. Turn the rasters into vectors like a 50m - 100m tile grid that can visualize the population density, for example like: https://sedac.ciesin.columbia.edu/data/set/gpw-v3-population-density/maps

I personally think we should use option 3. Could we use https://github.com/mapbox/rio-mbtiles?

cc @AliceR @sharkinsspatial @vincentsarago

geohacker commented 4 years ago

The approach we're taking here is to vectorize the rasters and then using that to style and filter on the map. This gives us the best control on styling as well as dynamically filter.

Outlining here the steps to vectorize the raster file. Thanks @ingalls for helping me figure this out.

1. Import raster into a PostgreSQL database with PostGIS.

Install PostgreSQL + PostGIS and GDAL

Create a database

Import raster data

2. Vectorize and export

Use ST_PixelAsPolygons

CREATE TABLE kenya_polygons AS
            SELECT
                gv.rid AS rid,
                (gv.geom).val AS pop,
                (gv.geom).geom AS geom
            FROM (
                SELECT
                    rid,
                    ST_PixelAsPolygons(rast, <band_number>, TRUE) AS geom
                FROM
                    kenya_population
                ) gv
            WHERE
                (gv.geom).val > 0

Export to GeoJSON

Zoom levels and resolution

The population data is at a much higher resolution. By default Mapbox would make the population vector tileset only visible from a higher zoom level, like 9. To have them show up at a lower zoom, use the Tippecanoe method and upload the mbtiles. More here.