Exemplo n.º 1
0
def getImageGeoInfo(imagePath, getStats=True):
    """Obtains some image geo information from gdalinfo in dictionary format"""
    
    if not os.path.exists(imagePath):
        raise Exception('Error: input file ' + imagePath + ' does not exist!')
    
    outputDict = {}
    
    # Call command line tool silently
    cmd = [asp_system_utils.which('gdalinfo'), imagePath, '-proj4']
    if getStats:
        cmd.append('-stats')
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
    textOutput, err = p.communicate()
    
    # Get the size in pixels
    imageSizeLine = asp_string_utils.getLineAfterText(textOutput, 'Size is ')
    sizeVals      = imageSizeLine.split(',')
    outputDict['image_size'] = (int(sizeVals[0]), int(sizeVals[1]))

    # Get origin location and pixel size    
    originLine    = asp_string_utils.getLineAfterText(textOutput, 'Origin = ')
    pixelSizeLine = asp_string_utils.getLineAfterText(textOutput, 'Pixel Size = ')    
    originVals    = asp_string_utils.getNumbersInParentheses(originLine)
    pixelSizeVals = asp_string_utils.getNumbersInParentheses(pixelSizeLine)
    outputDict['origin']     = originVals
    outputDict['pixel_size'] = pixelSizeVals

    # Get bounding box in projected coordinates
    upperLeftLine  = asp_string_utils.getLineAfterText(textOutput, 'Upper Left')
    lowerRightLine = asp_string_utils.getLineAfterText(textOutput, 'Lower Right')
    (minX, maxY)   = asp_string_utils.getNumbersInParentheses(upperLeftLine)
    (maxX, minY)   = asp_string_utils.getNumbersInParentheses(lowerRightLine)
    outputDict['projection_bounds'] = (minX, maxX, minY, maxY)
    outputDict['projection_center'] = ( (minX+maxX)/2.0, (minY+maxY)/2.0 )

    # Get some proj4 values
    outputDict['standard_parallel_1'] = getGdalInfoTagValue(textOutput, 'standard_parallel_1')
    outputDict['central_meridian']    = getGdalInfoTagValue(textOutput, 'central_meridian')

    # Get the projection type
    projStart = textOutput.find('PROJ.4 string is:')
    nextLine  = textOutput.find("'", projStart)+1
    endLine   = textOutput.find("'", nextLine)
    outputDict['proj_string'] = textOutput[nextLine:endLine]
    outputDict['projection'] = 'UNKNOWN'
    if '+proj=eqc' in textOutput:
        outputDict['projection'] = 'EQUIRECTANGULAR'
    elif '+proj=ster' in textOutput:
        outputDict['projection'] = 'POLAR STEREOGRAPHIC'
    
    # Extract this variable which ASP inserts into its point cloud files
    try:
        pointOffsetLine = asp_string_utils.getLineAfterText(textOutput, 'POINT_OFFSET=') # Tag name must be synced with C++ code
        offsetValues    = pointOffsetLine.split(' ')
        outputDict['point_offset'] =  (float(offsetValues[0]), float(offsetValues[1]), float(offsetValues[2]))        
    except:
        pass # In most cases this line will not be present

    # TODO: Currently this does not find much information, and there
    #       is another function in image_utils dedicated to returning statistics.
    if getStats:

        # List of dictionaries per band
        outputDict['band_info'] = []
    
        # Populate band information
        band = 1
        while (True): # Loop until we run out of bands
            bandString = 'Band ' + str(band) + ' Block='
            bandLoc = textOutput.find(bandString)
            if bandLoc < 0: # Ran out of bands
                break
        
            # Found the band, read pertinent information
            bandInfo = {}
        
            # Get the type string
            bandLine = asp_string_utils.getLineAfterText(textOutput, bandString)
            typePos  = bandLine.find('Type=')
            commaPos = bandLine.find(',')
            typeName = bandLine[typePos+5:commaPos-1]
            bandInfo['type'] = typeName
        
            outputDict['band_info'] = bandInfo
        
            band = band + 1 # Move on to the next band
        
    return outputDict
Exemplo n.º 2
0
def getImageGeoInfo(imagePath, getStats=True):
    """Obtains some image geo information from gdalinfo in dictionary format"""
    
    if not os.path.exists(imagePath):
        raise Exception('Error: input file ' + imagePath + ' does not exist!')
    
    outputDict = {}
    
    # Call command line tool silently
    cmd = [asp_system_utils.which('gdalinfo'), imagePath, '-proj4']
    if getStats:
        cmd.append('-stats')
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
    textOutput, err = p.communicate()
    
    # Get the size in pixels
    imageSizeLine = asp_string_utils.getLineAfterText(textOutput, 'Size is ')
    sizeVals      = imageSizeLine.split(',')
    outputDict['image_size'] = (int(sizeVals[0]), int(sizeVals[1]))

    # Get origin location and pixel size    
    originLine    = asp_string_utils.getLineAfterText(textOutput, 'Origin = ')
    pixelSizeLine = asp_string_utils.getLineAfterText(textOutput, 'Pixel Size = ')    
    originVals    = asp_string_utils.getNumbersInParentheses(originLine)
    pixelSizeVals = asp_string_utils.getNumbersInParentheses(pixelSizeLine)
    outputDict['origin']     = originVals
    outputDict['pixel_size'] = pixelSizeVals

    # Get bounding box in projected coordinates
    upperLeftLine  = asp_string_utils.getLineAfterText(textOutput, 'Upper Left')
    lowerRightLine = asp_string_utils.getLineAfterText(textOutput, 'Lower Right')
    (minX, maxY)   = asp_string_utils.getNumbersInParentheses(upperLeftLine)
    (maxX, minY)   = asp_string_utils.getNumbersInParentheses(lowerRightLine)
    outputDict['projection_bounds'] = (minX, maxX, minY, maxY)
    outputDict['projection_center'] = ( (minX+maxX)/2.0, (minY+maxY)/2.0 )

    # Get some proj4 values
    outputDict['standard_parallel_1'] = getGdalInfoTagValue(textOutput, 'standard_parallel_1')
    outputDict['central_meridian']    = getGdalInfoTagValue(textOutput, 'central_meridian')

    # Get the projection type
    projStart = textOutput.find('PROJ.4 string is:')
    nextLine  = textOutput.find("'", projStart)+1
    endLine   = textOutput.find("'", nextLine)
    outputDict['proj_string'] = textOutput[nextLine:endLine]
    outputDict['projection'] = 'UNKNOWN'
    if '+proj=eqc' in textOutput:
        outputDict['projection'] = 'EQUIRECTANGULAR'
    elif ('+proj=ster' in textOutput) or ('+proj=stere' in textOutput):
        outputDict['projection'] = 'POLAR STEREOGRAPHIC'

    outputDict['lonlat_bounds'] = outputDict['projection_bounds']
    if '+proj=longlat' not in outputDict['proj_string']:
        longlatString = getLonLatProjString(outputDict['proj_string'])
        ul = convertCoordinate(outputDict['proj_string'], longlatString, minX, maxY)
        br = convertCoordinate(outputDict['proj_string'], longlatString, maxX, minY)
        outputDict['lonlat_bounds'] = (ul[0], br[0], br[1], ul[1])

    # Extract this variable which ASP inserts into its point cloud files
    try:
        pointOffsetLine = asp_string_utils.getLineAfterText(textOutput, 'POINT_OFFSET=') # Tag name must be synced with C++ code
        offsetValues    = pointOffsetLine.split(' ')
        outputDict['point_offset'] =  (float(offsetValues[0]), float(offsetValues[1]), float(offsetValues[2]))        
    except:
        pass # In most cases this line will not be present

    # TODO: Currently this does not find much information, and there
    #       is another function in image_utils dedicated to returning statistics.
    if getStats:

        # List of dictionaries per band
        outputDict['band_info'] = []
    
        # Populate band information
        band = 1
        while (True): # Loop until we run out of bands
            bandString = 'Band ' + str(band) + ' Block='
            bandLoc = textOutput.find(bandString)
            if bandLoc < 0: # Ran out of bands
                break
        
            # Found the band, read pertinent information
            bandInfo = {}
        
            # Get the type string
            bandLine = asp_string_utils.getLineAfterText(textOutput, bandString)
            typePos  = bandLine.find('Type=')
            commaPos = bandLine.find(',')
            typeName = bandLine[typePos+5:commaPos-1]
            bandInfo['type'] = typeName
        
            outputDict['band_info'] = bandInfo
        
            band = band + 1 # Move on to the next band
        
    return outputDict