def mask_pixel_out_polygon(imagefile, shapefile, burnvalue, bkeepmidfile):
    """
    set other pixel outside the polygon as zero, will modify the original data
    Args:
        imagefile:the image need to be masked
        shapefile:the shapefile contains polygon
        burnvalue:masked value
        bkeepmidfile:whether keep middle file

    Returns:True if successful, False Otherwise

    """
    #inquiry imagefile srs
    target_srs = map_projection.get_raster_or_vector_srs_info_proj4(imagefile)
    if target_srs is False:
        return False
    polygon_srs = map_projection.get_raster_or_vector_srs_info_proj4(shapefile)
    if polygon_srs is False:
        return False
    polygon_path = shapefile
    #convert shapefile srs
    if target_srs != polygon_srs:
        # t_file = os.path.splitext(shapefile)[0] + '_trans.shp'
        t_file = os.path.basename(polygon_path).split('.')[0] + '_trans.shp'
        if os.path.isfile(t_file) is False:
            map_projection.transforms_vector_srs(shapefile, target_srs, t_file)
        else:
            basic.outputlogMessage('%s already exist' % t_file)
        polygon_path = t_file
    if os.path.isfile(polygon_path) is False:
        return False

    basic.outputlogMessage(
        'mask the pixel out the interesting region as value : ' +
        str(burnvalue))
    #mask the pixel outside polygon as zero
    layername = os.path.basename(polygon_path).split('.')[0]
    CommandString = 'gdal_rasterize -b 1  -at -i  -burn '+ str(burnvalue) + \
    ' -l '+ layername + ' '+polygon_path +' ' + imagefile
    (status, result) = basic.exec_command_string(CommandString)
    basic.outputlogMessage(result)

    if result.find('done'):
        return True
    else:
        return False
Beispiel #2
0
def get_tie_points_by_ZY3ImageMatch(basefile, warpfile, bkeepmidfile):
    workdir = os.getcwd()
    if not os.path.isfile(basefile):
        basic.outputlogMessage('not exist,file path: %s' % basefile)
        return False
    if not os.path.isfile(warpfile):
        basic.outputlogMessage('not exist ,file path: %s' % warpfile)
        return False

    taskID = time.strftime('%Y%m%d_%H%M%S', time.localtime()) + '_' + str(
        int(random.uniform(100, 999)))
    taskfile = taskID + '+filelist.txt'
    file_object = open(taskfile, 'w')
    file_object.writelines(str(2) + '\n')

    tempstr = os.path.abspath(basefile) + '\n'
    file_object.writelines(tempstr)
    tempstr = os.path.abspath(warpfile) + '\n'
    file_object.writelines(tempstr)
    file_object.close()

    io_function.mkdir(taskID)
    try:
        shutil.move(taskfile, taskID + '/.')
    except:
        basic.outputlogMessage("mv taskfile failed")
        basic.outputlogMessage('taskfile: ' + taskfile)
        basic.outputlogMessage('taskid: ' + taskID)
        return False

    taskfile = os.path.join(workdir, taskID, taskfile)
    resultdir = os.path.join(workdir, taskID)
    resultpath = os.path.join(resultdir, 'results.txt')

    exe_dir = parameters.get_exec_dir()
    if exe_dir is False:
        return False
    exepath = os.path.join(exe_dir, 'ImageMatchsiftGPU')
    CommandString = exepath + ' ' + taskfile + ' ' + resultpath + ' ' + str(2)
    # basic.outputlogMessage(CommandString)

    # (status, result) = commands.getstatusoutput(CommandString)
    basic.exec_command_string(CommandString)
    #syslog.outputlogMessage(result)

    if os.path.isfile('system.log'):
        io_function.copyfiletodir('system.log', taskID)
        shutil.move('system.log', 'matchbysiftgpu_systemlog.txt')

    #waiting and trying to get the tie points files
    result_tiepointfile = os.path.join(workdir, taskID, '0_1_after.pts')
    result_rms_files = os.path.join(workdir, taskID, '0_1_fs.txt')

    if os.path.isfile(result_tiepointfile):
        io_function.copyfiletodir(result_tiepointfile, '.')
        io_function.copyfiletodir(result_rms_files, '.')

    if bkeepmidfile is False:
        io_function.delete_file_or_dir(resultdir)

    tiepointfile = '0_1_after.pts'
    if os.path.isfile(tiepointfile):
        return os.path.abspath(tiepointfile)
    else:
        return False