Batch export to raster using POLYGON_CROP

xmppxmpp Global Mapper UserPosts: 2
edited June 2015 in GM Script Language
Hi all,

I'm trying to batch export a bunch of GeoTIFFs based on a large number of square polygons from my polygon shapefile. My base raster data is a bunch of different topo sheets. Some of these tiles line up with others within my square polygons.

The script works 95% correctly - when the polygons overlap only a single tile, it exports the TIFs just how I want. When the polygons overlap 2 or more tiles, it exports multiple TIFs, with each of them only including their respective portion of the polygon, and the rest of the TIF is my GM background colour.

Is there a way to get them to merge automatically or export multiple tile portions into one TIF? All TIFs exported within a single polygon have a particular attribute in the filename, could this be a way to merge them?

My script is below:
GLOBAL_MAPPER_SCRIPT VERSION=1.00

// Hide all layers apart from the raster to be exported
LAYER_LOOP_START FILENAME="*" VAR_NAME_PREFIX="HIDE"
    SET_LAYER_OPTIONS FILENAME="%HIDE_FNAME_W_DIR%" HIDDEN=YES
LAYER_LOOP_END

// Loop over the loaded layers, doing a separate export for each
LAYER_LOOP_START FILENAME="*"    
    // Enable the current layer since we hid it above
    SET_LAYER_OPTIONS FILENAME="%LAYER_FNAME_W_DIR%" HIDDEN=NO
    
    // Export to GeoTIFF with defined polygon crop
    EXPORT_RASTER FILENAME="%LAYER_DIR%%LAYER_FNAME_WO_EXT%_.tif" \ 
    TYPE=GEOTIFF PALETTE=OPTIMIZED COMPRESSION=NONE POLYGON_CROP_FILE="C:\temp\shpfile.shp" \
    POLYGON_CROP_USE_EACH=YES POLYGON_CROP_NAME_ATTR=Name
    
    // Disable the current layer so it won't be involved in other operations
    SET_LAYER_OPTIONS FILENAME="%LAYER_FNAME_W_DIR%" HIDDEN=YES
LAYER_LOOP_END

// Unhide all layers
LAYER_LOOP_START FILENAME="*" VAR_NAME_PREFIX="HIDE"
    SET_LAYER_OPTIONS FILENAME="%HIDE_FNAME_W_DIR%" HIDDEN=NO
LAYER_LOOP_END

Thanks!
John

Comments

  • xmppxmpp Global Mapper User Posts: 2
    edited June 2015
    Managed to find a solution -- an already built in feature like I was trying to create!

    V.16:
    1. File>Export>Export Raster/Image Format>GeoTIFF
    2. Select 'Crop to Selected Area Feature(s)' in the Export Bounds tab
    3. Select 'Use Selected Area Feature(s) for Tiles' in the Tiling tab
    4. Select your preferred feature name in the grid naming dialog box
    5. Select 'Yes' to export specific boundary in the boundary dialog box
    6. Rest of the options as per your requirements
    7. Export as normal

    Thanks devs! :o
  • bmg_bobbmg_bob Global Mapper Programmer Posts: 2,233
    edited June 2015
    Hello,

    I'm glad to see that you found the solution in the UI. With respect to your script, if you simply leave all of your layers visible, instead of hiding them then making them visible one-by-one, you should get the result that you are looking for. (You probably will also need to modify the output file name.) In your current script, your loop causes the export to be run separately for each layer, which means GM will produce two output files when a polygon intersects two layers (one for each layer).

    Cheers,

    Bob
Sign In or Register to comment.