[GIS] Cropping an image with a map extent

arcgis-serverclippythonraster

I have an raster file in my server space, which has the same projection as my webmap, and I would like to crop the image by passing the visible extent of a webmap. I think the cropping function is easily possible via a python script server side, but the process of converting the map envelope coordinates to a bounding box the script could use to cut the image to that exact extent is beyond me at the moment. Any ideas, methods, or general roadmap would be helpful.

In a nutshell, I am trying to reproduce the "export" functionality of an ESRI map service without using ArcServer.

Like this:
http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/export?bbox=-9562211.80152863,4584479.44244939,-9519931.21541649,4626760.02856153&size=1024,1024&format=jpg&imageSR=102100&bboxSR=102100&f=image

Am I crazy or what?

Best Answer

Clipping an image to the extents of a bounding box is very easy to do with the gdal_translate utility from GDAL. The documentation for it is here. You'd want to use the "-projwin" option like this,

gdal_translate -projwin ulx uly lrx lry infile outfile  

For the URL you gave, I would assume the following should work (Note that your ESRI command specifies LL and UR, while GDAL uses UL and LR)

$ gdal_translate -projwin -9562211.80152863 4626760.02856153 -9519931.21541649 4584479.44244939  <source_image> <output_image>

This would give you an output_image that is clipped to the extents of the bounding box. You should note that you cannot control the size of the resulting image when doing this. You get back as many pixels are needed to fill the extent. If you want to make the output_image a specific size, you can do that as a second step with gdalwarp and specify whatever resampling type you want to use.

GDAL has an excellent Python API, so you could combine both operations and glue the output into your processing setup, but I have to wonder whether this is really what you want? If all you need to do is display your image over a web map, it would be far simpler to setup Mapserver, point it at your image and either use WMS, or a tile request to bring the data into your mapping API.

Roger

Related Question