def random_ClipRaster(trainRaster,labelRaster,outDir,imgNums,imgHeight,imgWidth): #定义随机裁剪函数(训练原图,标签原图,输出路径,裁剪数量,图像行数,图像列数) Min_X = arcpy.GetRasterProperties_management(trainRaster,"LEFT") #训练原图的X的最小值 Min_Y = arcpy.GetRasterProperties_management(trainRaster,"BOTTOM") #训练原图的Y的最小值 Max_X = arcpy.GetRasterProperties_management(trainRaster,"RIGHT") #训练原图的X的最大值 Max_Y = arcpy.GetRasterProperties_management(trainRaster,"TOP") #训练原图的Y的最大值 CELLSIZEX = arcpy.GetRasterProperties_management(trainRaster,"CELLSIZEX") #获取训练原图单像素的宽度 CELLSIZEY = arcpy.GetRasterProperties_management(trainRaster,"CELLSIZEY") #获取训练原图单像素的高度 COLUMNCOUNT = arcpy.GetRasterProperties_management(trainRaster,"COLUMNCOUNT") #获取训练原图列数 ROWCOUNT =arcpy.GetRasterProperties_management(trainRaster,"ROWCOUNT") #获取训练原图行数 label_Min_X = arcpy.GetRasterProperties_management(labelRaster,"LEFT") #标签原图的X的最小值 label_Min_Y = arcpy.GetRasterProperties_management(labelRaster,"BOTTOM") #标签原图的Y的最小值 label_Max_X = arcpy.GetRasterProperties_management(labelRaster,"RIGHT") #标签原图的X的最大值 label_Max_Y = arcpy.GetRasterProperties_management(labelRaster,"TOP")#标签原图的Y的最大值 label_CELLSIZEX = arcpy.GetRasterProperties_management(labelRaster,"CELLSIZEX") #获取标签原图单像素的宽度 label_CELLSIZEY = arcpy.GetRasterProperties_management(labelRaster,"CELLSIZEY") #获取标签原图单像素的高度 label_COLUMNCOUNT = arcpy.GetRasterProperties_management(labelRaster,"COLUMNCOUNT") #获取标签原图列数 label_ROWCOUNT =arcpy.GetRasterProperties_management(labelRaster,"ROWCOUNT") #获取标签原图行数 rotate_list = ["90","180","270","mirror"] mkdir_dem = outDir + "train_dem" mkdir_label = outDir + "train_label" os.popen("mkdir "+mkdir_dem) #计算机先自动生成文件夹 os.popen("mkdir "+mkdir_label) if (str(label_Min_X) == str(Min_X)) and (str(label_Min_Y) == str(Min_Y))and ( str(Max_X) == str(label_Max_X))and( str(Max_Y) == str(label_Max_Y))and (str(label_CELLSIZEX) == str(CELLSIZEX))and(str(label_CELLSIZEY)== str(CELLSIZEY))and( str(COLUMNCOUNT) == str(label_COLUMNCOUNT))and(str(ROWCOUNT)== str(label_ROWCOUNT)): num =0 #print("test") while num < imgNums: random_x = random.randint(0,int(str(COLUMNCOUNT)) - imgWidth) #拟随机生成行列号来生成图像,(0,COLUMNCOUNT - imgWidth)为可选列号的范围 random_y = random.randint(0,int(str(ROWCOUNT))- imgHeight) #(0,ROWCOUNT- imgHeight)为可选行号的范围 temp_x = float(str(Min_X)) + random_x * float(str(CELLSIZEX)) #temp_x为所选图像左下角的横坐标 temp_y = float(str(Min_Y)) + random_y * float(str(CELLSIZEY)) #temp_y为所选图像左下角的纵坐标 cood_xy = str(temp_x) + " " +str(temp_y) + " " +str(temp_x + imgWidth*(float(str(CELLSIZEX)))) + " " + str(temp_y + imgHeight*(float(str(CELLSIZEY))))#所选图像的左下坐标和右上坐标 #print(cood_xy) out_demRaster = outDir+"train_dem\\"+"dem"+"_"+ str(num) + ".tif"#dem_1.tif dem_2.tif out_labelRaster = outDir+"train_label\\" +"label" +"_" +str(num) +".tif" #label_1.tif label_2.tif arcpy.Clip_management(trainRaster,cood_xy,out_demRaster,"#","#","NONE","MAINTAIN_EXTENT") #裁剪输出 arcpy.Clip_management(labelRaster,cood_xy,out_labelRaster,"#","#","NONE","MAINTAIN_EXTENT") #裁剪输出 #arcpy.Clip_management(trainRaster,cood_xy,out_demRaster,"#","#","NONE","NO_MAINTAIN_EXTENT") #裁剪输出 #arcpy.Clip_management(labelRaster,cood_xy,out_labelRaster,"#","#","NONE","NO_MAINTAIN_EXTENT") #裁剪输出 dem_numpy = arcpy.RasterToNumPyArray(out_demRaster,nodata_to_value=-9999) #栅格转成numpy,目的是统一设置nodata值为-9999,用于后面的判断 dem_numpy = numpy.hstack(dem_numpy) #把numpy数组元素变成一维,用于方便循环 for value in list(dem_numpy): if value == -9999: noDataValue = -9999 break else : noDataValue = "None" if noDataValue == "None": random_rotate = random.randint(0,3) out_demRotateRaster = outDir+"train_dem\\"+"dem"+"_"+ str(num) +"_"+rotate_list[random_rotate]+ ".tif" #dem_1.tif dem_2.tif out_labelRotateRaster = outDir+"train_label\\"+"label"+"_"+ str(num) +"_"+rotate_list[random_rotate]+ ".tif" #dem_1.tif dem_2.tif if rotate_list[random_rotate] == "mirror": arcpy.Mirror_management(out_demRaster,out_demRotateRaster) #镜像操作 arcpy.Mirror_management(out_labelRaster,out_labelRotateRaster) else : rotate_x = temp_x + (imgWidth*float(str(CELLSIZEX)))/2 #计算旋转中心 rotate_y = temp_y + (imgHeight*float(str(CELLSIZEY)))/2 arcpy.Rotate_management(out_demRaster,out_demRotateRaster,rotate_list[random_rotate],str(rotate_x)+" "+str(rotate_y)) #旋转操作 arcpy.Rotate_management(out_labelRaster,out_labelRotateRaster,rotate_list[random_rotate],str(rotate_x)+" "+str(rotate_y)) print("%dth and conversion %s images are generated"%(num,rotate_list[random_rotate])) #提示图片裁剪好了 num = num+1 #图片保留,继续循环 else: #删除生成的train和label图片 remove_dem = glob.glob(outDir + "train_dem\\" + "dem" + "_" + str(num) + ".*") #每一个图片有好几个文件,都需要删除 remove_label = glob.glob(outDir + "train_label\\" + "label" + "_" + str(num) + ".*") for tiff in remove_dem: #print(tiff) os.remove(tiff) print("Delete"+ " " + tiff) for label_tiff in remove_label: os.remove(label_tiff) print("Delete"+ " " + label_tiff) num = num else : Error = " Image matching failed " #两张源图片格式如果大小不匹配,无法运用此函数,直接error print(Error) return print("Successfully Done!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
def main(profile_line, in_raster, swath_width, output_csv, ncols=None, nrows=None): """ The main processing of the data and extraction of the profile data. Arguments: line feature class or feature set raster dem buffer distance as linar units e.g. ("5 Meters") outputfile csv name Optional arguments ncols numbger of rows default is the raster height nrows number of columns default is the raster width The main function buffers the profile line, then clips the raster and roates it so that it runs from north to south witth a bearing of 0 degrees. The raster is then found for each row and placed in a csv file """ # Set enviromntal variables arcpy.env.overwriteOutput = True # Collect data from our input layers spatail_reference = arcpy.Describe(profile_line).spatialReference arcpy.AddMessage("spatial reference is:{}".format(spatail_reference.name)) cursor = arcpy.da.SearchCursor(profile_line, ["SHAPE@"]) SHAPE_INDEX = 0 for feature in cursor: for part in feature[SHAPE_INDEX]: line = arcpy.Polyline(part, spatail_reference) arcpy.AddMessage("first X: {}".format(line.firstPoint.X)) arcpy.AddMessage("first Y: {}".format(line.firstPoint.Y)) arcpy.AddMessage("last X: {}".format(line.lastPoint.X)) arcpy.AddMessage("last Y: {}".format(line.lastPoint.Y)) # Buffer the profile line #buffer_fc = "{}\\buffer".format(arcpy.env.scratchGDB) buffer_fc = "in_memory\\buffer" # Need to halve the width to get the correct buffer distance buffer_distance = float(swath_width.split()[0]) / 2 buffer_linear_units = "{} {}".format(buffer_distance, swath_width.split()[1]) arcpy.AddMessage("Buffering the profile line by: {}".format( buffer_linear_units)) arcpy.Buffer_analysis(line, buffer_fc, buffer_linear_units, "FULL", "FLAT", "NONE", "", "GEODESIC") # Clip the dem to the buffer arcpy.AddMessage("Clipping the raster by the buffer") output_clip = "{}\\clip".format(arcpy.env.scratchGDB) arcpy.Clip_management(dem, "", output_clip, buffer_fc, "", "ClippingGeometry") #"MAINTAIN_EXTENT") # Roate the dem so that it is upright bearing = north_bearing(line.firstPoint, line.lastPoint) rotation = rotation_angle(bearing) if rotation <= 1: arcpy.AddMessage("No rotation needed") out_rotate = output_clip else: arcpy.AddMessage("Rotating raster by {}".format(rotation)) out_rotate = "{}\\rotate".format(arcpy.env.scratchGDB) arcpy.Rotate_management(output_clip, out_rotate, rotation) # Convert the dem to a numpy array and prepear it for the stats # need to skip nodata values in array # need to stip out lines of nodata # This needs to be done in bloks or else we run out of memory. arcpy.AddMessage("Converting the roated dem to a numpy array") dem_raster = arcpy.Raster(out_rotate) dem_cell_size = int(dem_raster.meanCellWidth) stats_result = None arcpy.AddMessage("Pixel Size: {} ".format(dem_cell_size)) dem_blocks = block_processing.EnumRasterToNumPyArray( out_rotate, num_rows=nrows) for dem_array in dem_blocks: dem_array_skip_nd = np.ma.masked_array(dem_array, dem_array == -9999) mask_nd_rows = np.all(np.isnan(dem_array_skip_nd), axis=1) dem_arr = dem_array_skip_nd[~mask_nd_rows] # Need to flip it as the array starts at the bottom # lefthand corner dem_arr_flip = np.flipud(dem_arr) try: stats = row_stats(dem_arr_flip) except ValueError: continue if stats_result is None: stats_result = stats else: stats_result = np.vstack((stats_result, stats)) number_rows = np.size(stats_result, axis=0) distance = np.arange(0, number_rows * dem_cell_size, dem_cell_size) distance_reshape = distance.reshape(number_rows, 1) distance_stats = np.hstack((distance_reshape, stats_result)) arcpy.AddMessage("Writing {} to disk.".format(output_csv)) np.savetxt( output_csv, distance_stats, header= "distance, max, min, mean, std, minus_1std, plus_1std, kurtosis", fmt='%1d, %1.3f, %1.3f, %1.3f, %1.3f, %1.3f, %1.3f, %1.3f', comments='') # Clean up file_list = [out_rotate, output_clip, buffer_fc] for file_item in file_list: if arcpy.Exists(file_item): arcpy.Delete_management(file_item)
bandExtent = str(band.extent) originalBottom, originalLeft, originalTop, originalRight, waste1, waste2, waste3, waste4 = bandExtent.split(" ") originalBottomFlt = float(originalBottom) originalLeftFlt = float(originalLeft) originalTopFlt = float(originalTop) originalRightFlt = float(originalRight) bandList.append([bandExtent]) # Fixes orientation, size, and location of acolite image arcpy.DefineProjection_management(acoRaster, proj) #rotates acolite image if applicable if rotateYN == 'true': arcpy.Rotate_management(acoRaster, "intermedRot" ,"180") arcpy.env.snapRaster = pathLandsat arcpy.CheckOutExtension("Spatial") #flips acolite image if appropriate if flipYN == 'true': if rotateYN == 'true': arcpy.Mirror_management("intermedRot", "intermedMir") else: arcpy.Mirror_management(acoRaster, "intermedMir") #rescales acolite image cellSizeXResult = arcpy.GetRasterProperties_management(pathLandsat, "CELLSIZEX") cellSizeYResult = arcpy.GetRasterProperties_management(pathLandsat, "CELLSIZEY")
def mosaic(dnight, sets): ''' This module creates the mosaic of the galactic model for each data set. ''' #set arcpy environment variables part 2/2 arcpy.env.workspace = filepath.rasters + 'scratch_galactic/' arcpy.env.scratchWorkspace = filepath.rasters + 'scratch_galactic' for s in sets: #file paths calsetp = filepath.calibdata + dnight + '/S_0%s/' % s[0] gridsetp = filepath.griddata + dnight + '/S_0%s/gal/' % s[0] if os.path.exists(gridsetp): shutil.rmtree(gridsetp) os.makedirs(gridsetp) #read in the galactic coordinates from coordinates_%s.txt file = filepath.calibdata + dnight + '/coordinates_%s.txt' % s[0] Gal_ang, Gal_l, Gal_b = n.loadtxt(file, usecols=(1, 2, 3)).T #read in the registered images coordinates file = filepath.calibdata + dnight + '/pointerr_%s.txt' % s[0] Obs_AZ, Obs_ALT = n.loadtxt(file, usecols=(3, 4)).T Obs_AZ[n.where(Obs_AZ > 180)] -= 360 Obs_AZ[35] %= 360 #loop through each file in the set for w in range(len(Obs_AZ) + 1): v = w + 1 if w == 45: w = 35 Obs_AZ[w] -= 360 get_galgn(Gal_l[w], Gal_b[w]) if v in range(0, 50, 5): print 'Generating galactic image %i/45' % v #rotate by galctic angle arcpy.Rotate_management('galgn.tif', 'rotaterasterg.tif', str(Gal_ang[w]), "0 0", "BILINEAR") #re-define projection to topocentric coordinates arcpy.DefineProjection_management('rotaterasterg.tif', tc(Obs_AZ[w], Obs_ALT[w])) #reproject into GCS arcpy.ProjectRaster_management('rotaterasterg.tif', 'gal%02d.tif' % v, geogcs, "BILINEAR", "0.05") #clip to image boundary rectangle = clip_envelope(Obs_AZ, Obs_ALT, w) arcpy.Clip_management("gal%02d.tif" % v, rectangle, "gali%02d" % v) #Mosaic to topocentric coordinate model; save in Griddata\ print "Mosaicking into all sky galactic model" R = ';'.join(['gali%02d' % i for i in range(1, 47)]) arcpy.MosaicToNewRaster_management(R, gridsetp, 'galtopmags', geogcs, "32_BIT_FLOAT", "0.05", "1", "BLEND", "FIRST") print "Creating layer files for galactic mosaic" layerfile = filepath.griddata + dnight + '/galtopmags%s.lyr' % s[0] arcpy.MakeRasterLayer_management(gridsetp + 'galtopmags', 'galtoplyr') arcpy.SaveToLayerFile_management('galtoplyr', layerfile, "ABSOLUTE") #Set layer symbology to magnitudes layer symbologyLayer = filepath.rasters + 'magnitudes.lyr' arcpy.ApplySymbologyFromLayer_management(layerfile, symbologyLayer) lyrFile = arcpy.mapping.Layer(layerfile) lyrFile.replaceDataSource(gridsetp, 'RASTER_WORKSPACE', 'galtopmags', 'FALSE') lyrFile.save() #Downscale the raster and save it as a fits file file = filepath.griddata + dnight + "/S_0" + s[0] + "/gal/galtopmags" arcpy_raster = arcpy.sa.Raster(file) A = arcpy.RasterToNumPyArray(arcpy_raster, "#", "#", "#", -9999) A_small = downscale_local_mean(A[:1800, :7200], (25, 25)) #72x288 fname = filepath.griddata + dnight + '/galtopmags%s.fits' % s[0] fits.writeto(fname, A_small, overwrite=True)
originalTopFlt = float(originalTop) originalRightFlt = float(originalRight) widthOriginal = originalRightFlt - originalLeftFlt heightOriginal = originalTopFlt - originalBottomFlt print widthOriginal, heightOriginal print "Done pulling original data" # Fixes orientation, size, and location of acolite image print proj print acoRaster arcpy.DefineProjection_management(acoRaster, proj) #rotates acolite image if applicable if rotateYN == 'true': arcpy.Rotate_management(acoRaster, "intermedRot", rotateAngle) print "rotate" #bring snapraster back here arcpy.CheckOutExtension("Spatial") #flips acolite image if appropriate if flipYN == 'true': if rotateYN == 'true': arcpy.Mirror_management("intermedRot", "intermedMir") print 'flip + rotate' else: arcpy.Mirror_management(acoRaster, "intermedMir") print 'flip'
arcpy.AddMessage('\n') # Rotate grid lines based on a pivot point - using the input fc centroid # Also requires us to convert to raster, rotate, and then convert back to vector # (As there is no built in vector rotation tool ...) # arcpy.AddMessage('>> Now running rotation process...') arcpy.AddMessage('\n') pivot_point = '{0} {1}'.format(bdy_fc_centroid[0], bdy_fc_centroid[1]) # X Y out_raster = os.path.join(tmp_gdb, bdy_name + '_raster') out_raster_rotated = os.path.join(tmp_gdb, bdy_name + '_raster_r') tmp_fishnet_rotated = os.path.join(tmp_gdb, bdy_name + '_fishnet_r') # Convert to raster, rotate, and convert back to polyline (use 10m to keep our raster cells separate) arcpy.PolylineToRaster_conversion(tmp_fishnet_path, 'OID', out_raster, 'MAXIMUM_LENGTH', 'NONE', 10) arcpy.Rotate_management(out_raster, out_raster_rotated, rotation_val, pivot_point, 'NEAREST') arcpy.RasterToPolyline_conversion(out_raster_rotated, tmp_fishnet_rotated, 'ZERO', 0, 'SIMPLIFY') arcpy.AddMessage( 'Rotated data by specified value: {0} degrees'.format(rotation_val)) # Perform a real simplification on the layer - to tidy up the lines tmp_fishnet_rotated_simpl = tmp_fishnet_rotated + '_s' arcpy.SimplifyLine_cartography(tmp_fishnet_rotated, tmp_fishnet_rotated_simpl, 'POINT_REMOVE', 10) time.sleep(5) arcpy.AddMessage('Simplified/cleaned up data') arcpy.AddMessage('\n') # Clip rotated lines to input boundary # tmp_fishnet_clip = os.path.join(tmp_gdb, bdy_name + '_fishnet_r_s_c')