Example #1
0
def subset_image_by_shapefile(imagefile, shapefile, bkeepmidfile):
    """
    subset an image by polygons contained in the shapefile
    Args:
        imagefile:input image file path
        shapefile:input shapefile contains polygon
        bkeepmidfile:indicate whether keep middle file

    Returns:output file name if succussful, False Otherwise

    """
    if io_function.is_file_exist(imagefile) is False:
        return False
    if io_function.is_file_exist(shapefile) is False:
        return False

    Outfilename = io_function.get_name_by_adding_tail(imagefile, 'vsub')

    # ds = ogr.Open(shapefile)
    # lyr = ds.GetLayer(0)
    # lyr.ResetReading()
    # ft = lyr.GetNextFeature()

    # subprocess.call(['gdalwarp', imagefile, Outfilename, '-cutline', shapefile,\
    #                       '-crop_to_cutline'])

    orgimg_obj = RSImageclass()
    if orgimg_obj.open(imagefile) is False:
        return False
    x_res = abs(orgimg_obj.GetXresolution())
    y_res = abs(orgimg_obj.GetYresolution())

    CommandString = 'gdalwarp ' + ' -tr ' + str(x_res) + '  ' + str(
        y_res
    ) + ' ' + imagefile + ' ' + Outfilename + ' -cutline ' + shapefile + ' -crop_to_cutline ' + ' -overwrite '
    if basic.exec_command_string_one_file(CommandString, Outfilename) is False:
        return False

    # while ft:
    #     country_name = ft.GetFieldAsString('admin')
    #     outraster = imagefile.replace('.tif', '_%s.tif' % country_name.replace(' ', '_'))
    #     subprocess.call(['gdalwarp', imagefile, Outfilename, '-cutline', shapefile,
    #                      '-crop_to_cutline', '-cwhere', "'admin'='%s'" % country_name])
    #
    #     ft = lyr.GetNextFeature()

    if not bkeepmidfile:
        io_function.delete_file_or_dir(imagefile)
        os.remove(imagefile)

    if io_function.is_file_exist(Outfilename):
        return Outfilename
    else:
        # basic.outputlogMessage(result)
        basic.outputlogMessage(
            'The version of GDAL must be great than 2.0 in order to use the r option '
        )
        return False
Example #2
0
def subset_image_by_shapefile(imagefile,
                              shapefile,
                              bkeepmidfile=True,
                              overwrite=False,
                              format='GTiff',
                              save_path=None,
                              resample_m='bilinear',
                              src_nodata=None,
                              dst_nondata=None,
                              xres=None,
                              yres=None,
                              compress=None,
                              tiled=None,
                              bigtiff=None,
                              thread_num=None):
    """
    subset an image by polygons contained in the shapefile
    the shapefile and imagefile may have different projections, the gdalwarp can handle
    Args:
        imagefile:input image file path
        shapefile:input shapefile contains polygon
        bkeepmidfile:indicate whether keep middle file
        format: output format,  default is GTiff, GeoTIFF File Format. Use "VRT": GDAL Virtual Format to save disk storage

    Returns:output file name if succussful, False Otherwise

    """
    if io_function.is_file_exist(imagefile) is False:
        return False
    if io_function.is_file_exist(shapefile) is False:
        return False

    if save_path is None:
        Outfilename = io_function.get_name_by_adding_tail(imagefile, 'vsub')
    else:
        Outfilename = save_path

    # ds = ogr.Open(shapefile)
    # lyr = ds.GetLayer(0)
    # lyr.ResetReading()
    # ft = lyr.GetNextFeature()

    # subprocess.call(['gdalwarp', imagefile, Outfilename, '-cutline', shapefile,\
    #                       '-crop_to_cutline'])

    if overwrite is False and os.path.isfile(Outfilename):
        basic.outputlogMessage('warning, crop file: %s already exist, skip' %
                               Outfilename)
        return Outfilename

    orgimg_obj = RSImageclass()
    if orgimg_obj.open(imagefile) is False:
        return False
    if xres is None or yres is None:
        x_res = abs(orgimg_obj.GetXresolution())
        y_res = abs(orgimg_obj.GetYresolution())
    else:
        x_res = xres
        y_res = yres


    CommandString = 'gdalwarp -r %s '% resample_m+' -tr ' + str(x_res) + '  '+ str(y_res)+ ' -of ' + format + ' ' + \
                    imagefile +' ' + Outfilename +' -cutline ' +shapefile +' -crop_to_cutline ' + ' -overwrite '

    if src_nodata != None:
        CommandString += ' -srcnodata %d ' % src_nodata
    if dst_nondata != None:
        CommandString += ' -dstnodata %d ' % dst_nondata

    if compress != None:
        CommandString += ' -co ' + 'compress=%s' % compress  # lzw
    if tiled != None:
        CommandString += ' -co ' + 'TILED=%s' % tiled  # yes
    if bigtiff != None:
        CommandString += ' -co ' + 'bigtiff=%s' % bigtiff  # IF_SAFER

    if thread_num != None:
        CommandString += ' -multi -wo NUM_THREADS=%d ' % thread_num

    if basic.exec_command_string_one_file(CommandString, Outfilename) is False:
        return False

    # while ft:
    #     country_name = ft.GetFieldAsString('admin')
    #     outraster = imagefile.replace('.tif', '_%s.tif' % country_name.replace(' ', '_'))
    #     subprocess.call(['gdalwarp', imagefile, Outfilename, '-cutline', shapefile,
    #                      '-crop_to_cutline', '-cwhere', "'admin'='%s'" % country_name])
    #
    #     ft = lyr.GetNextFeature()

    if not bkeepmidfile:
        io_function.delete_file_or_dir(imagefile)
        os.remove(imagefile)

    if io_function.is_file_exist(Outfilename):
        return Outfilename
    else:
        # basic.outputlogMessage(result)
        basic.outputlogMessage(
            'The version of GDAL must be great than 2.0 in order to use the r option '
        )
        return False
Example #3
0
def calculate_terrain_offset(output, dem_file, image_file, exec_dir,
                             bkeepmidfile):
    if io_function.is_file_exist(
            image_file) is False or io_function.is_file_exist(
                dem_file, ) is False:
        return False
    exefile = os.path.join(exec_dir, 'geometry_pro')
    nodata = parameters.get_nodata_value()

    (centre_lat, centre_lon) = RSImage.get_image_latlon_centre(image_file)
    if centre_lat is False or centre_lon is False:
        return False

    image_obj = RSImageclass()
    if image_obj.open(image_file) is False:
        return False
    dem_obj = RSImageclass()
    if dem_obj.open(dem_file) is False:
        return False

    x_res = image_obj.GetXresolution()
    y_res = image_obj.GetYresolution()
    x_res_dem = dem_obj.GetXresolution()
    y_res_dem = dem_obj.GetYresolution()

    image_prj = image_obj.GetProjection()
    dem_prj = dem_obj.GetProjection()

    #check projection and resolution, and convert it if need
    #use orthometricH_file as base image
    dem_convertedfile = io_function.get_name_by_adding_tail(dem_file, 'tran')
    if x_res != x_res_dem or y_res != y_res_dem or image_prj != dem_prj:
        if os.path.isfile(dem_convertedfile) is False:
            if map_projection.transforms_raster_srs(
                    dem_file, image_prj, dem_convertedfile, abs(x_res),
                    abs(y_res)) is False:
                return False
    if os.path.isfile(dem_convertedfile):
        dem_file = dem_convertedfile

    #sub  dem file
    (ulx, uly, lrx, lry) = RSImage.get_image_proj_extent(image_file)
    if ulx is False:
        return False
    tail = os.path.splitext(os.path.basename(image_file))[0]
    dem_file_sub = io_function.get_name_by_adding_tail(dem_file, tail)
    if os.path.isfile(dem_file_sub) is False:
        if RSImageProcess.subset_image_projwin(dem_file_sub, dem_file, ulx,
                                               uly, lrx, lry) is False:
            return False

    #calculateing terrain contains a lot of I/O operations, the parallel computing will slow down it
    nblockwidth = 8000
    nblcckheight = 8000
    njobs = 1

    logfile = 'cal_terrain_offset_log.txt'

    CommandString = exefile \
                    + ' -i ' + image_file + ' -d ' + dem_file_sub \
                    + ' -o '  + output  + ' -n ' + str(nodata)\
                    + ' -w ' + str(nblockwidth) + ' -h ' + str(nblcckheight) + ' -j ' +str(njobs) \
                    + ' --centre_lat=' + str(centre_lat)  \
                    + ' --logfile=' + logfile

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

    if bkeepmidfile is False:
        # if os.path.isfile(dem_convertedfile):
        #     io_function.delete_file_or_dir(dem_convertedfile)
        if os.path.isfile(dem_file_sub):
            io_function.delete_file_or_dir(dem_file_sub)

    if os.path.isfile(output):
        if os.path.getsize(output) > 0:
            return output
        else:
            basic.outputlogMessage('error: the size of file %s is 0' %
                                   os.path.basename(output))
            return False
    else:
        return False
Example #4
0
def get_geoimage_range_geoid_height(outputfile, ref_image):
    #convert srs
    ref_img_obj = RSImageclass()
    if not ref_img_obj.open(ref_image):
        return False
    # x_res = ref_img_obj.GetXresolution()
    # y_res = ref_img_obj.GetYresolution()
    width = ref_img_obj.GetWidth()
    height = ref_img_obj.GetHeight()

    img_pro = RSImgProclass()
    ref_image_data = img_pro.Read_Image_band_data_to_numpy_array_all_pixel(
        1, ref_image)
    if ref_image_data is False:
        return False

    nodata = parameters.get_nodata_value()
    Image_array = ref_image_data.reshape(height, width)
    start_x = ref_img_obj.GetStartX()
    start_y = ref_img_obj.GetStartY()
    resolution_x = ref_img_obj.GetXresolution()
    resolution_y = ref_img_obj.GetYresolution()
    ref_img_WKT = ref_img_obj.GetProjection()
    # ref_img_WKT = RSImageProcess.get_raster_or_vector_srs_info_wkt(ref_image,syslog)

    (i, j) = numpy.where(Image_array != nodata)
    input_x = start_x + j * resolution_x
    input_y = start_y + i * resolution_y

    # srs_longlat_prj4 = '\'+proj=longlat +datum=WGS84 +no_defs\''
    # intput_proj4 = RSImage.wkt_to_proj4(ref_img_WKT,syslog)
    # intput_proj4 = RSImageProcess.get_raster_or_vector_srs_info_proj4(ref_image,syslog)
    # map_projection.convert_points_coordinate_proj4(input_x,input_y,intput_proj4,srs_longlat_prj4,syslog)

    srs_longlat_wkt = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.01745329251994328]]"
    map_projection.convert_points_coordinate(input_x, input_y, ref_img_WKT,
                                             srs_longlat_wkt)

    tempsave_str = []
    save_point_txt_file = 'TXTgeoid_' + os.path.splitext(
        os.path.basename(ref_image))[0] + '.txt'
    if os.path.isfile(save_point_txt_file):
        file_object = open(save_point_txt_file, 'r')
        savepoints = file_object.readlines()
        for point in savepoints:
            tempsave_str.append(point)
        file_object.close()
        io_function.delete_file_or_dir(save_point_txt_file)
    file_object = open(save_point_txt_file, 'a')

    nsize = Image_array.size
    Image_array = Image_array.astype(numpy.float32)
    for index in range(0, nsize):
        lon_deg = input_x[index]
        lat_deg = input_y[index]
        if index < len(tempsave_str):
            temp_point = tempsave_str[index].split()
            value = float(temp_point[2])
        else:
            (LongitudeDeg, LongitudeMin, LongitudeSec) = degree_to_dms(lon_deg)
            (LatitudeDeg, LatitudeMin, LatitudeSec) = degree_to_dms(lat_deg)
            value = get_geoid_height(LatitudeDeg, LatitudeMin, LatitudeSec,
                                     LongitudeDeg, LongitudeMin, LongitudeSec,
                                     nodata)
            if value is False:
                break
        saved_point = ('%f  %f  %f' % (lon_deg, lat_deg, value))
        print saved_point
        # tempsave_str.append(saved_point)
        file_object.writelines(saved_point + '\n')
        file_object.flush()
        basic.outputlogMessage('Longitude=%f, Latitude=%f, geoid = %f' %
                               (lon_deg, lat_deg, value))
        # Image_array[index] = value
        Image_array[i[index], j[index]] = value
        print(i[index], j[index], Image_array[i[index], j[index]])

    file_object.close()
    if index != (nsize - 1):
        return False
    #save geoid height
    RSImageProcess.save_numpy_2d_array_to_image_tif(outputfile,Image_array,\
        6,ref_img_obj.GetGeoTransform(),ref_img_WKT,nodata)

    return True
Example #5
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