How to create a shape-file from raster to exactly match irregular image boundary.
In my case I want to get rid of the extra (black) area (fig. shown at the end of this post) to reduce the file dimensions, to be able to use images in PhotoMod (demo) software for mosaicing tiles together. As the demo software is limited to 20 Mega pixels per image and rectified image exceeds that limit if flight line is (rotated) as rotation increases the bounding rectangle, which results in increased image X-Y size.
(Entire process can be done in ArcMap too, but I like to use Gdal (free software) as much as possible.)
Here I will explain how to create the above mentioned shape-file using Gdal & ArcMap.
First step is to convert all non-zero digital numbers of a raster to “0 & 1″
For this conversion I use Gdal Batch Commander and enter the following command in parameters field…
“-co compress=lzw -b 1 -ot byte 0 -a_no-data 0 -scale 1 1″.
This will create a new raster with all non-zero (except no-data,0) values converted to “1” (You can use any number in place of 1).
Command line parameters explained :-
- -co compress=lzw it to compress the generated tiffs with losseless LZW compression.
- -b 1 option is for using only first band, just to save time by not reading all the bands in MS imagery.
- -ot byte will create 8-bit raster.
- -a_nodata 0 specifies nodata value for the input band. This one is not shown is figure below, so don’t skip it. As this parameter helps ArcMap to ignore outer rectangle (black area) for its shapefile generation routine.
- -scale 1 1 will convert all non-zero pixel values to 1.
Second step is to convert “0 & 1″ image to shape file.
In ArcMap you can choose “Raster to Polygon” tool to create shapefile. Use ArcMap’s model builder to create a shape file from all the newly created tiffs (with 0 & 1 as pixel values for no-data and data areas respectively). Below is a screenshot of model builder that I used to convert 0&1 tiff to shape file.
Note: Check simplify polygon option in “Raster to Polygon” window.
Resulting shape file (a yellow polygon) is produced after running Gdal_translate & ArcMap.
Now I can use the shape-file to crop the raster to the newly generated polygon and get rid of the extra black area.