Open geohacker opened 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.
createdb catalyst
to create a database called catalystrasterpgsql
utility that comes with postgisraster2pgsql -I -e -s 4326 -t auto ~/data/kenya_population.tiff | tee /tmp/kenya.sql
. (Replace ~/data/kenya_population.tiff
with the right path and filename). This will write import SQL into /tmp/kenya.sqlpsql -d catalyst -f /tmp/kenya.sql
. This will automatically create a table in the database and import the raster data.psql -d catalyst
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
kenya_population
based on what table the data was initially imported to. By default this will be the name of the tiff file used to import in step 1.kenya_polygons
which is vector.ogr2ogr -f GeoJSON /tmp/kenya.json "PG:host=localhost dbname=catalyst" -sql "select pop, geom FROM kenya_polygons"
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.
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:
I personally think we should use option 3. Could we use https://github.com/mapbox/rio-mbtiles?
cc @AliceR @sharkinsspatial @vincentsarago