0

Is it possible to retrieve the original width and height in pixels for a coverage in GeoServer using either the Python API or REST? I could not find anything in the documentation regarding how to retrieve height or width of a coverage in GeoServer.

Here is my current solution interfacing with GeoServer. It needs GDAL, and it will probably fail if the coverage is not a simple file readable by GDAL (i.e. a PostGIS raster)

import gdal
import os
from geoserver.catalog import Catalog

#...code

#store is a GeoServer store
def get_raster_dimensions(store):                                           
    """ Get the size of the raster from the catalog. """                    
    gdal.UseExceptions()        
    #this, of course will fail if the data isn't a regular file                                            
    base_url = store.url.replace('file:', '', 1)                            
    #what if the GEOSERVER_DATA_DIR is unset?  How to find it then?         
    coverage_path = os.path.join(os.environ['GEOSERVER_DATA_DIR'], base_url)
    #if the coverage store is a raster readable by GDAL,                    
    #we can get the dimensions with GDAL                                                      
    try:                                                                    
        dataset = gdal.Open(coverage_path)                                  
        return (dataset.RasterXSize, dataset.RasterYSize)                   
    except RuntimeError:                                                    
        return (None, None)                                                 
    finally:                                                                
        #no close method in GDAL docs, this was referenced as the way       
        #to clean up the file after finished                                
        dataset = None 

1 Answer 1

2

Found an easier method using Python via OWSLib when the workspace name, layer name, and WCS service endpoint are specified

from owslib.wcs import WebCoverageService

def get_raster_dimensions(workspace, name, server_url):                                      
    """ Get the size of the raster using OWSLib                            
        Returns a list of strings """                                            
    wcs = WebCoverageService(server_url + 'wcs', '1.0.0') 
    grid = wcs.contents[workspace + ':' + name].grid                             
    return [str(int(h) - int(l) + 1) for                                         
            h, l in zip(grid.highlimits, grid.lowlimits)]  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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