def addGeoDataToAsuJp2File(inputJp2Path, inputHeaderPath, outputPath, keep=False):
    """Does the actual work of adding the geo data"""

    if not os.path.exists(inputJp2Path):
        raise Exception('Input file ' + inputJp2Path + ' does not exist!')
    if not os.path.exists(inputHeaderPath):
        raise Exception('Input file ' + inputHeaderPath + ' does not exist!')


    # Get the needed paths
    prjPath = replaceFileExtension(inputJp2Path, '.prj')
    vrtPath = replaceFileExtension(inputJp2Path, '.vrt')
    
    # The perl script works best from the input folder
    originalDirectory = os.getcwd()
    print originalDirectory
    inputFolder = os.path.dirname(inputJp2Path)
    print inputFolder
    if inputFolder != '':
        os.chdir(inputFolder)

    # Call perl script, then return to original directory
    cmd = 'isis3world.pl -J -prj ' + os.path.basename(inputHeaderPath)
    print cmd
    os.system(cmd)
    if inputFolder != '':
        os.chdir(originalDirectory)

    # If the first command is not called from the input folder, need to do this
    #mv .j2w <INPUT FOLDER>/B01_009838_2108_XI_30N319W.j2w
    #mv .prj <INPUT FOLDER>/B01_009838_2108_XI_30N319W.prj

    correctedProjPath = editProjFile(prjPath)

    if (not os.path.exists(correctedProjPath)):
        raise Exception('Failed to correct proj file!')
    
    # Determine the bounds of the image in projected coordinates
    projectedBounds = IrgGeoFunctions.getProjectedBoundsFromIsisLabel(inputHeaderPath)
    projectedBoundsString = (str(projectedBounds[0]) + ' ' +
                             str(projectedBounds[3]) + ' ' +
                             str(projectedBounds[1]) + ' ' +
                             str(projectedBounds[2]) )

    # Next conversion command
    #cmd = 'gdal_translate -of VRT -a_srs ESRI::"'+ prjPath +'" -a_nodata 0  '+ inputJp2Path +' '+ vrtPath

    ## Finish the conversion
    #cmd = 'gdal_translate -of JP2OpenJPEG '+ vrtPath +' '+ outputPath
    #print(cmd)
    #os.system(cmd)
    
    # Copy the input image to the output path if different
    if (outputPath != inputJp2Path):
        cmd = 'cp '+ inputJp2Path +'  '+ outputPath
        print(cmd)
        os.system(cmd)
    
    # Add metadata to the output image, specifying a projecting string and projected coordinate boundaries.
    f = open(correctedProjPath, 'r')
    prjText=f.read()
    f.close()
    cmd = 'gdal_edit.py -mo "AREA_OR_POINT=Area"  -a_ullr ' + projectedBoundsString + ' -a_srs "'+ prjText +'"  '+ outputPath
    print(cmd)
    os.system(cmd)

    # gdal_edit.py actually puts the metadata here, the input file is not touched!
    sidecarPath = outputPath + '.aux.xml'

    # Clean up temporary files
    if not keep:
        #os.remove(vrtPath)
        os.remove(prjPath)
        os.remove(correctedProjPath)

    return (outputPath, sidecarPath)
Exemple #2
0
def addGeoDataToAsuJp2File(inputJp2Path,
                           inputHeaderPath,
                           outputPath,
                           keep=False):
    """Does the actual work of adding the geo data"""

    if not os.path.exists(inputJp2Path):
        raise Exception('Input file ' + inputJp2Path + ' does not exist!')
    if not os.path.exists(inputHeaderPath):
        raise Exception('Input file ' + inputHeaderPath + ' does not exist!')

    # Get the needed paths
    prjPath = replaceFileExtension(inputJp2Path, '.prj')
    vrtPath = replaceFileExtension(inputJp2Path, '.vrt')

    # The perl script works best from the input folder
    originalDirectory = os.getcwd()
    print originalDirectory
    inputFolder = os.path.dirname(inputJp2Path)
    print inputFolder
    if inputFolder != '':
        os.chdir(inputFolder)

    # Call perl script, then return to original directory
    cmd = 'isis3world.pl -J -prj ' + os.path.basename(inputHeaderPath)
    print cmd
    os.system(cmd)
    if inputFolder != '':
        os.chdir(originalDirectory)

    # If the first command is not called from the input folder, need to do this
    #mv .j2w <INPUT FOLDER>/B01_009838_2108_XI_30N319W.j2w
    #mv .prj <INPUT FOLDER>/B01_009838_2108_XI_30N319W.prj

    correctedProjPath = editProjFile(prjPath)

    if (not os.path.exists(correctedProjPath)):
        raise Exception('Failed to correct proj file!')

    # Determine the bounds of the image in projected coordinates
    projectedBounds = IrgGeoFunctions.getProjectedBoundsFromIsisLabel(
        inputHeaderPath)
    projectedBoundsString = (str(projectedBounds[0]) + ' ' +
                             str(projectedBounds[3]) + ' ' +
                             str(projectedBounds[1]) + ' ' +
                             str(projectedBounds[2]))

    # Next conversion command
    #cmd = 'gdal_translate -of VRT -a_srs ESRI::"'+ prjPath +'" -a_nodata 0  '+ inputJp2Path +' '+ vrtPath

    ## Finish the conversion
    #cmd = 'gdal_translate -of JP2OpenJPEG '+ vrtPath +' '+ outputPath
    #print(cmd)
    #os.system(cmd)

    # Copy the input image to the output path if different
    if (outputPath != inputJp2Path):
        cmd = 'cp ' + inputJp2Path + '  ' + outputPath
        print(cmd)
        os.system(cmd)

    # Add metadata to the output image, specifying a projecting string and projected coordinate boundaries.
    f = open(correctedProjPath, 'r')
    prjText = f.read()
    f.close()
    cmd = 'gdal_edit.py -mo "AREA_OR_POINT=Area"  -a_ullr ' + projectedBoundsString + ' -a_srs "' + prjText + '"  ' + outputPath
    print(cmd)
    os.system(cmd)

    # gdal_edit.py actually puts the metadata here, the input file is not touched!
    sidecarPath = outputPath + '.aux.xml'

    # Clean up temporary files
    if not keep:
        #os.remove(vrtPath)
        os.remove(prjPath)
        os.remove(correctedProjPath)

    return (outputPath, sidecarPath)