I've tried just about every single TileMill compositing operation on the vector and raster layers in question, and I have to admit that I can't figure it out: How the heck can I mask a raster with a vector in TileMill/CartoCSS?

Basically, I have a raster, generated by QGIS (and masked in QGIS with a vector shapefile of the U.S.). I ran it through GDAL to colorize it and re-project it into web mercator for TileMill. Somewhere in the process, everything outside the QGIS mask became a solid white pixel, and I just can't seem to trim that white off. I've brought the same shapefile into TileMill, and that's what I'm trying to use to trim the raster.

As usual, I feel like there's something basic I didn't do—maybe in the QGIS GeoTIFF export process?—that would explain TileMill's stubborn refusal to do away with the gross white square around my map, but Google is failing me. Any ideas?

2 Answers 2


If I understand your problem correctly, there may be another workaround to this as well by using the comp-op operations (which can be quite confusing).

Let's say you had your #raster layer and a #shp poly layer. The shp layer is polys defining areas that you WANT to see from the raster. Anything outside the shp polys will be masked with the following method:

#shp{polygon-comp-op: src-out;} #raster{raster-comp-op: dst-over;}

So now, the raster will only show through your shp poly and will be completely transparent everywhere else (in your case, any area outside the US shp poly)


Turns out I was asking the wrong question—this wasn't a problem for Tilemill to solve. I used the dstalpha command in GDAL, and it seems to have taken care of the issue. The command in question is:

gdalwarp -dstalpha -srcnodata 255 -dstnodata 0 SOURCE.tif DESTINATION.tif 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.