Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding filters to raster layers #9

Open
geohacker opened this issue Oct 26, 2020 · 1 comment
Open

Adding filters to raster layers #9

geohacker opened this issue Oct 26, 2020 · 1 comment

Comments

@geohacker
Copy link
Contributor

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
Copy link
Contributor Author

geohacker commented Oct 29, 2020

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

  • Run createdb catalyst to create a database called catalyst

Import raster data

  • We'll use rasterpgsql utility that comes with postgis
  • Run raster2pgsql -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.sql
  • Now import into the database using psql -d catalyst -f /tmp/kenya.sql. This will automatically create a table in the database and import the raster data.

2. Vectorize and export

Use ST_PixelAsPolygons

  • Open the db console by running psql -d catalyst
  • Then run:
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
  • Be sure to change the source table name 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.
  • Be sure to change band number to which band has the population density data. We weren't able to test this because the population data is missing from the tiff files.
  • The above step will create a table called kenya_polygons which is vector.

Export to GeoJSON

  • Use ogr2ogr (comes with GDAL): ogr2ogr -f GeoJSON /tmp/kenya.json "PG:host=localhost dbname=catalyst" -sql "select pop, geom FROM kenya_polygons"
  • This will write a GeoJSON feature collection to /tmp/kenya.json which can be uploaded to Mapbox.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant