I need to create a script, probably JS or Python, to perform the following:

I have a polygonal region -- no "holes", and always fully convex -- for which I need to extract the pixel data from one of a set of map layers. Each of the layers in question will consist of either (a) standard quadtree-based map tiles, or (b) a set of non-overlapping rectangular GeoTIFF files (or JPEG/PNG files with lat/lon for the four corners).

So, the intended input of this script/function/method will be a polygon (specified in lat/lon coordinates), and a map layer. The output will be a rectangular png image (or some matrix representation of pixels that'll be used to generate a png), where all pixels that either fall outside the polygon or don't have any images in the map layer that overlap them will be transparent, and the rest will contain the pixel value taken from the map layer.

This feels like something someone's probably done before, but I'm not finding any modules that do it.

Does anyone know if one exists?


Sounds like a job for gdalwarp: http://www.gdal.org/gdalwarp.html

Each polygonal region would be a "cutline". The input imagery could be a single raster, or you could create a VRT from multiple images to act as a virtual mosaic.

The docs I've linked are for the pre-built command-line utilities. You could use those as-is if this is a one-time job, but those utilities also have matching bindings that you can use through Python.

  • 1
    This is exactly what I was looking for. Many thanks!
    – DanM
    Jul 26 '18 at 14:25
  • Also worth noting that there's a GDAL module for JavaScript: npmjs.com/package/gdal
    – DanM
    Jul 27 '18 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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