How to find the top left extent of the rasters in a folder and its subfolders

When creating a raster dataset in a file or ArcSDE geodatabase, it is important to set the pyramid origin correctly if you want to take advantage of the partial pyramid updating feature. This document shows how to loop through all the rasters in a folder and its subfolders and find the top left point of the union extent. You can use this point to set the pyramid origin point when creating a new raster dataset in a file or ArcSDE geodatabase and mosaicking all the rasters into it.

Finding the top left extent of rasters in a folder and subfolders

You need to change the path in the code to your local path. The top left coordinates are printed out to the command window. See the following code:
static double minx = 100000000000d;
static double maxy =  - 100000000000d;

static void printTopLeft()throws Exception{
    String path = "C:\\Program Files\\ArcGIS\\java\\samples\\data\\3dmodel_texture";
    System.out.println("Left is:" + minx);
    System.out.println("Top is:" + maxy);

static void getTopLeft(String path)throws Exception{

    //Get top left most point from rasters in a folder and its subfolders.
    //Initialize geoprocessor.
    GeoProcessor geoprocessor = new GeoProcessor();
    GetRasterProperties getRasterProperties = new GetRasterProperties();
    GPUtilities gpUtil = new GPUtilities();

    //Get subdirectories.
    File dir = new File(path);

    FileFilter directoryFilter = new FileFilter(){
        public boolean accept(File file){
            return file.isDirectory();

    File[] subDirs = dir.listFiles(directoryFilter);

    //Set environment.
    geoprocessor.setEnvironmentValue("workspace", dir.getAbsolutePath());
    IGpEnumList files = geoprocessor.listRasters("", "");

    //Get first raster.
    String raster =;
    while (raster != null && !raster.trim().equals("")){
        System.out.println(dir.getAbsolutePath() + "\\" + raster);

        //Get xmin.
        geoprocessor.execute(getRasterProperties, null);
        double x = getRasterProperties.getProperty();

        if (x < minx)
            minx = x;

        //Get ymax.
        geoprocessor.execute(getRasterProperties, null);
        double y = getRasterProperties.getProperty();
        if (y > maxy)
            maxy = y;

        //Get next raster.
        raster =;

    if (subDirs != null && subDirs.length > 0){
        for (File file: subDirs)

Development licensingDeployment licensing
ArcGIS for Desktop BasicArcGIS for Desktop Basic
ArcGIS for Desktop StandardArcGIS for Desktop Standard
ArcGIS for Desktop AdvancedArcGIS for Desktop Advanced
Engine Developer KitEngine