def resample_and_clip(cellsize, feature_path, raster_dir): temp_raster = raster_dir + "\\temp.tif" if arcpy.Exists(temp_raster): arcpy.Delete_management(temp_raster) for file in os.listdir(raster_dir): if file.endswith(".tif"): raster = raster_dir + "\\" + file resampled_raster = "in_memory\\resampled" resampled_cellsize = str(cellsize) + ' ' + str(cellsize) arcpy.Resample_management(raster, resampled_raster, resampled_cellsize, "BILINEAR") envelope = "in_memory\\envelope" buffer = "in_memory\\buffer" envelope = arcpy.FeatureEnvelopeToPolygon_management( feature_path, envelope, "SINGLEPART") buffer = arcpy.Buffer_analysis(envelope, buffer, 500, "FULL", "ROUND", "NONE", "", "PLANAR") rows = arcpy.SearchCursor(buffer) shapeName = arcpy.Describe(buffer).shapeFieldName for row in rows: feat = row.getValue(shapeName) extent = feat.extent envelope = str(extent.XMin) + ' ' + str(extent.YMin) + ' ' + str( extent.XMax) + ' ' + str(extent.YMax) arcpy.Clip_management(resampled_raster, envelope, temp_raster) arcpy.Delete_management("in_memory") return
def clip(feature_path, raster_dir): '''function for clipping raster to shapefile extent''' temp_raster = raster_dir + "\\temp.tif" if arcpy.Exists(temp_raster): arcpy.Delete_management(temp_raster) for file in os.listdir(raster_dir): if file.endswith(".tif"): raster = raster_dir + "\\" + file envelope = "in_memory\\envelope" buffer = "in_memory\\buffer" envelope = arcpy.FeatureEnvelopeToPolygon_management( feature_path, envelope, "SINGLEPART") buffer = arcpy.Buffer_analysis(envelope, buffer, 500, "FULL", "ROUND", "NONE", "", "PLANAR") rows = arcpy.SearchCursor(buffer) shapeName = arcpy.Describe(buffer).shapeFieldName for row in rows: feat = row.getValue(shapeName) extent = feat.extent bounds = str(extent.XMin) + ' ' + str(extent.YMin) + ' ' + str( extent.XMax) + ' ' + str(extent.YMax) arcpy.Clip_management(raster, bounds, temp_raster) arcpy.Delete_management("in_memory") return
xxCombine = arcpy.sa.Con(xxplus, "50", xxplus, "Value > 50") ##maxval = arcpy.GetRasterProperties_management (xxplus, "MAXIMUM") ##maxval = float(str(maxval)) ##maxcost = maxval * 10 costRaster = arcpy.sa.Con(linkLayer, "50", xxplus, "Value = 0") costRaster.save(tWorkspace + os.sep + "costraster1") # Create source and destination polygons (NSEW) arcpy.AddMessage("Creating corridor destination polygons...") maxval = arcpy.GetRasterProperties_management (linkLayer, "MAXIMUM") xxblank = arcpy.sa.Reclassify(linkLayer, "VALUE", "0 " + str(maxval) + " 1", "DATA") arcpy.RasterToPolygon_conversion(xxblank, tWorkspace + os.sep + "xxpoly", "SIMPLIFY", "VALUE") # Added feature envelope to polygon to clean unwanted vertices from output arcpy.FeatureEnvelopeToPolygon_management(tWorkspace + os.sep + "xxpoly.shp", tWorkspace + os.sep + "xxpoly2.shp", "SINGLEPART") arcpy.FeatureToLine_management(tWorkspace + os.sep + "xxpoly2.shp", tWorkspace + os.sep + "xxline.shp", "", "ATTRIBUTES") arcpy.SplitLine_management(tWorkspace + os.sep + "xxline.shp", tWorkspace + os.sep + "xxsplit.shp") fc = arcpy.Buffer_analysis(tWorkspace + os.sep + "xxsplit.shp", tWorkspace + os.sep + "buf.shp", CellSize + " Meters", "RIGHT", "FLAT", "NONE", "") fc = arcpy.MakeFeatureLayer_management(fc, "pLayer") rows = arcpy.SearchCursor(fc) for row in rows: p = row.getValue("FID") arcpy.MakeFeatureLayer_management(fc, "fLayer") arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", "\"FID\" = " + str(p)) arcpy.MakeFeatureLayer_management(fc, "pLayer_" + str(p))
arcpy.Dissolve_management(citrus, path + r"\citrus_dissolved") # create random points outName = "random_points" conFC = path + r"\citrus_dissolved.shp" numPoints = 40 arcpy.CreateRandomPoints_management(path, outName, conFC, "", numPoints, "100 Meters", "", "") # draw a circle around points using buffer tool arcpy.Buffer_analysis(path + r"\random_points.shp", path + r"\random_circles", "20 Meters") # draw polygons around the circles arcpy.FeatureEnvelopeToPolygon_management(path + r"\random_circles.shp", path + r"\random_polygons") # add random polygons layer to map document # create a new layer randomPoly = arcpy.mapping.Layer(path + r"\random_polygons.shp") # add layers to the map arcpy.mapping.AddLayer(df, randomPoly, "AUTO_ARRANGE") # save the map document mxd.saveACopy(path + "\\" + merged_raster_name + "_pre_sig.mxd") # open the map document os.startfile(path + "\\" + merged_raster_name + "_pre_sig.mxd")
# Set raster as basis for coordinate system sr = arcpy.CreateSpatialReference_management("", raster_location + raster_file) # Process: Project arcpy.Project_management( mask_location + mask_file, mask_scratch + "\\GEOG490_maskscratch.gdb\\" + mask_file[:-4] + "_proj", sr) # Process: Buffer arcpy.Buffer_analysis( mask_scratch + "\\GEOG490_maskscratch.gdb\\" + mask_file[:-4] + "_proj", mask_scratch + "\\GEOG490_maskscratch.gdb\\" + mask_file[:-4] + "_buffer", buffer_size, "FULL", "ROUND", "NONE", "", "PLANAR") # Process: Feature Envelope To Polygon arcpy.FeatureEnvelopeToPolygon_management( mask_scratch + "\\GEOG490_maskscratch.gdb\\" + mask_file[:-4] + "_buffer", mask_scratch + "\\GEOG490_maskscratch.gdb\\" + mask_file[:-4] + "_masktemp", "SINGLEPART") # Select Layers by Location, then output layer to scratch # Process: Raster Domain arcpy.RasterDomain_3d( raster_location + raster_file, mask_scratch + "\\GEOG490_maskscratch.gdb\\" + mask_file[:-4] + "_rasterExtent", "POLYGON") # Make temp layer with polygon mask and raster extent arcpy.MakeFeatureLayer_management( mask_scratch + "\\GEOG490_maskscratch.gdb\\" + mask_file[:-4] + "_masktemp", 'temp_mask_within') arcpy.MakeFeatureLayer_management( mask_scratch + "\\GEOG490_maskscratch.gdb\\" + mask_file[:-4] + "_rasterExtent", 'temp_rasterExtent')
# projection arcpy.Project_management(in_dataset="CENTER_TMP", out_dataset="CENTER_PROJ", out_coor_system=spatialReference_new, transform_method="", in_coor_system=spatialReference, preserve_shape="NO_PRESERVE_SHAPE", max_deviation="", vertical="NO_VERTICAL") # buffer creation arcpy.Buffer_analysis("CENTER_PROJ", out, bufferField, sideType, endType, dissolveType) # run feature to envelope tool arcpy.FeatureEnvelopeToPolygon_management( "miniarea_TMP", "miniarea_square", "SINGLEPART") #reproject in normal (basic for SLDEM2013) #arcpy.Project_management(in_dataset="miniarea_square", out_dataset="miniarea_square2", out_coor_system=spatialReference_DTM_first, transform_method="", in_coor_system=spatialReference_new, preserve_shape="NO_PRESERVE_SHAPE", max_deviation="", vertical="NO_VERTICAL") # get the extent of miniarea_square (but this is in the new coordinates! So there is a problem in the next steps desc_extent = arcpy.Describe("miniarea_square") extent = desc_extent.extent top = extent.YMax bottom = extent.YMin left = extent.XMin right = extent.XMax ExtStr = "{} {} {} {}".format(left, bottom, right, top) # 11.06.2019 convert from local ref to lat/lon with the help of arcgis
arcpy.env.overwriteOutput = True #no changes are necessary after this line #create temporary file path buffer_shape = os.path.join(dump_folder, "temp.shp") poly_shape = os.path.join(Environment, "temp2") temp_raster = os.path.join(Environment, "ts") #csv and image save path point_data = os.path.join(path, str(point+"_pt_based2_s.xls")) writer = pd.ExcelWriter(point_data) image = os.path.join(path, str(point+"_R_Square_s.png")) #creating polygon from the point. If necessary adjust the buffer_distance_or_field for change in the polygon size arcpy.Buffer_analysis(in_features=point, out_feature_class=buffer_shape, buffer_distance_or_field="3 Meters", line_side="FULL", line_end_type="ROUND", dissolve_option="NONE", dissolve_field="", method="PLANAR") arcpy.FeatureEnvelopeToPolygon_management(in_features=buffer_shape, out_feature_class=poly_shape, single_envelope="SINGLEPART") satellite = {"*Intel*": ["IntelinAir - 0.16m","ko"], "*PSC*":["PlanetScope - 3m","gv"], "*Sent*":["Sentinal - 10m", "mp"], "*RE*":["Rapid-Eye - 5m", "rd"]} fig, ax = plt.subplots() for item in satellite: #two dataframe will be created here df = pd.DataFrame() point_id = [i[0] for i in arcpy.da.SearchCursor(point, "Point_ID")] df["Point_ID"] = point_id df1 = pd.DataFrame() df1["Date"] = ["STD_Curve" , "Slope" , "Intercept" , "SAT", "R value"] #run each satellite data sources at once
def area_cut(roads_in, widths_in, area_in, output_dir_in, output_name_in): """ This function cuts the input lines feature class (roads, streets, anything) into a square shape around the median point of the input lines feature class. It saves the polygon (the square itself) to the specified output location, the cut streets and intersections are saved to the in_memory workspace as they will be further processed afterwords in the graph analysis. :param roads_in: line feature class that will be cut :param widths_in: the size of the area in meters :param area_in: the area in km^2 as in the procedural area generation :param output_dir_in: path, where to save the results :param output_name_in: :return: paths to the resulting feature classes - polygon square, lines, intersections """ arcpy.overwriteOutput = 1 # Transform roads (lines) into a polygon area_path = os.path.join('in_memory', 'area') check_exists(area_path) area = arcpy.FeatureToPolygon_management(roads_in, area_path) # Dissolve a multipart polygon area_dissolved_path = os.path.join('in_memory', 'area_dissolved') check_exists(area_dissolved_path) arcpy.Dissolve_management(area, area_dissolved_path) # Get central point centroid_path = os.path.join('in_memory', 'centroid') check_exists(centroid_path) arcpy.arcpy.FeatureToPoint_management(area_dissolved_path, centroid_path) # Create a circular buffer around with the radius of half of the needed width buffer_out = os.path.join('in_memory', 'buffer') check_exists(buffer_out) width = str(widths_in) + ' Meters' arcpy.Buffer_analysis(centroid_path, buffer_out, width) # Create a square from the circular buffer square_path = os.path.join( output_dir_in, '{0}_area{1}_ply'.format(output_name_in, area_in)) check_exists(square_path) arcpy.FeatureEnvelopeToPolygon_management(buffer_out, square_path) # Cut roads name_streets_out = os.path.join( output_dir_in, '{0}_area{1}_streets'.format(output_name_in, area_in)) check_exists(name_streets_out) arcpy.Clip_analysis(roads_in, square_path, name_streets_out) arcpy.TrimLine_edit(name_streets_out) arcpy.TrimLine_edit(name_streets_out) arcpy.TrimLine_edit(name_streets_out) # Get intersections out_feature_class = os.path.join('in_memory', 'intersections_tmp') arcpy.Intersect_analysis(name_streets_out, out_feature_class, output_type='POINT') arcpy.DeleteIdentical_management(out_feature_class, 'Shape') name_intersections_out = os.path.join( output_dir_in, '{0}_area{1}_intersections'.format(output_name_in, area_in)) check_exists(name_intersections_out) arcpy.FeatureToPoint_management(out_feature_class, name_intersections_out) return square_path, name_streets_out, name_intersections_out
#The below code allows one to select a square or round buffer #Change the below code such that specific file name and indexes are altered to be own usInpt = raw_input( "enter R for Round buffer zones or S for square buffers zones: ") for i in range(0, len(pointsList)): if usInpt == 'R': print "Now buffering each point by 500 meters" arcpy.Buffer_analysis(pointsList[i], pointsBuff[i], "500 meters") print "Now clipping sample zone (gridbuff.shp) to IA_shp" arcpy.Clip_analysis(pointsBuff[i], IA_shp, pointClip[i]) elif usInpt == 'S': print "Now buffering each point by 500 meters" arcpy.Buffer_analysis(pointsList[i], pointsBuff[i], "500 meters") arcpy.FeatureEnvelopeToPolygon_management(pointsBuff[i], squareBuff[i], "MULTIPART") arcpy.Delete_management(pointsBuff[i]) print "Now clipping sample zone (gridbuff.shp) to IA_shp" arcpy.Clip_analysis(squareBuff[i], IA_shp, squareClip[i]) print "Executing Zonal Statistics" if usInpt == 'R': for i in range(0, len(AgrLand)): for j in range(0, len(pointClip)): for k in range(0, len(DBF_stats)): arcpy.sa.ZonalStatisticsAsTable(pointClip[j], 'id', AgrLand[i], DBF_stats[k], 'DATA', 'MEAN') sCur = arcpy.SearchCursor(DBF_stats[k]) zrow = sCur.next() mean = zrow.getValue("MEAN")
def create_building_area(self): ctos_buffer = arcpy.Buffer_analysis(self.ly_taps, "in_memory\\ctos_buffer", "10 Meters", "FULL", "ROUND") edificio_pol = arcpy.FeatureEnvelopeToPolygon_management(ctos_buffer, "in_memory\\edificio_pol", "SINGLEPART") self.edificio_area = arcpy.Dissolve_management(edificio_pol, os.path.join(self.pathgdb, "edificio_area"), '''"ITEM_PLAN"''')
def main(pathf, pathdab, infile, restart, ixstop, fname): ''' pathf = "Y:/nilscp/GeologyMoon/FRESH_IMPACT_WILLIAMS_2018/" infile = pathf + 'XYdownloadSupplement.shp' pathdab = "Y:/nilscp/GeologyMoon/FRESH_IMPACT_WILLIAMS_2018/database.gdb/" fname = '0_200' ''' bufferField = "BUFFER_TXT" sideType = "FULL" endType = "ROUND" dissolveType = "NONE" # path to folder os.chdir(pathf) # Set overwrite option arcpy.env.overwriteOutput = True arcpy.CheckOutExtension("3D") arcpy.CheckOutExtension("Spatial") # define paths and workspace (I need to create the gdb at some points) env.workspace = env.scratchWorkspace = pathdab # extract the centers of craters (OK regardless of the projection) arcpy.FeatureToPoint_management(infile, 'CENTER', "CENTROID") infile2 = pathdab + 'CENTER' # crater name and buffer extent fieldname1 = arcpy.ValidateFieldName("CRATER_ID") fieldname2 = arcpy.ValidateFieldName("BUFFER_TXT") # add fields arcpy.AddField_management(infile, fieldname1, "TEXT","","",30) # get the number of rows in infile n = int(arcpy.GetCount_management(infile2)[0]) # prepare empty arrays diam = np.ones(n) crater_id = np.chararray(n, itemsize=30) buffer_txt = np.chararray(n, itemsize=30) ## some constant variables to define if restart: areac = np.loadtxt(pathf + "numbers" + fname + ".txt", delimiter=";") areaa_20_40 = areac[:,0]#np.concatenate((areac[:,0], np.zeros(1781))) # work around (only need to do that once) areaa_40_55 = areac[:,1] #np.concatenate((areac[:,1], np.zeros(1781))) areaa_55_80 = areac[:,2] #np.concatenate((areac[:,2], np.zeros(1781))) NAC_selected_tmp_20_40 = np.genfromtxt(pathf + "NAC_i20_40_" + fname + ".txt", dtype='str') NAC_selected_tmp_40_55 = np.genfromtxt(pathf + "NAC_i40_55_" + fname + ".txt", dtype='str') NAC_selected_tmp_55_80 = np.genfromtxt(pathf + "NAC_i55_80_" + fname + ".txt", dtype='str') edr_selected_tmp_20_40 = np.genfromtxt(pathf + "EDR_i20_40_" + fname + ".txt", dtype='str') edr_selected_tmp_40_55 = np.genfromtxt(pathf + "EDR_i40_55_" + fname + ".txt", dtype='str') edr_selected_tmp_55_80 = np.genfromtxt(pathf + "EDR_i55_80_" + fname + ".txt", dtype='str') NAC_selected_all_20_40 = [] edr_selected_all_20_40 = [] NAC_selected_all_40_55 = [] edr_selected_all_40_55 = [] NAC_selected_all_55_80 = [] edr_selected_all_55_80 = [] # needs to be list for ip, var in np.ndenumerate(NAC_selected_tmp_20_40): NAC_selected_all_20_40.append(NAC_selected_tmp_20_40[ip]) edr_selected_all_20_40.append(edr_selected_tmp_20_40[ip]) for ip, var in np.ndenumerate(NAC_selected_tmp_40_55): NAC_selected_all_40_55.append(NAC_selected_tmp_40_55[ip]) edr_selected_all_40_55.append(edr_selected_tmp_40_55[ip]) for ip, var in np.ndenumerate(NAC_selected_tmp_55_80): NAC_selected_all_55_80.append(NAC_selected_tmp_55_80[ip]) edr_selected_all_55_80.append(edr_selected_tmp_55_80[ip]) else: NAC_selected_all_20_40 = [] edr_selected_all_20_40 = [] NAC_selected_all_40_55 = [] edr_selected_all_40_55 = [] NAC_selected_all_55_80 = [] edr_selected_all_55_80 = [] areaa_20_40 = np.zeros(n) areaa_40_55 = np.zeros(n) areaa_55_80 = np.zeros(n) # we add info about the name of the craters here with arcpy.da.UpdateCursor(infile, ["Diameter", "CRATER_ID"]) as cursor: ix = 0 for row in cursor: a = 'crater' + str(int(ix)).zfill(4) buffer_value = np.round((row[0]) * 10.0, decimals=4) b = str(buffer_value) + ' Meters' row[1] = a cursor.updateRow(row) diam[ix] = row[0] crater_id[ix] = a buffer_txt[ix] = b ix = ix + 1 # add two fields arcpy.AddField_management(infile2, fieldname1, "TEXT","","",30) arcpy.AddField_management(infile2, fieldname2, "TEXT","","",30) with arcpy.da.UpdateCursor(infile2, ["CRATER_ID", "BUFFER_TXT"]) as cursor: ix = 0 for row in cursor: row[0] = crater_id[ix] row[1] = buffer_txt[ix] cursor.updateRow(row) ix = ix + 1 #arcpy.AddField_management(infile2, fieldname3, "DOUBLE") #arcpy.AddField_management(infile2, fieldname4, "DOUBLE") #arcpy.AddField_management(infile2, fieldname5, "DOUBLE") # Make a layer from the feature class arcpy.MakeFeatureLayer_management("CENTER", "CENTER_lyr") with arcpy.da.UpdateCursor("CENTER_lyr", ["Shape@", "Lon", "Lat"]) as cursor: ix = 0 for row in cursor: #print index #print (ix) # could reset here too (just to be sure) if ix > 0: arcpy.SelectLayerByAttribute_management("CENTER_lyr", "CLEAR_SELECTION") else: None # if first time or timesteps over last restarted if ((restart & (ix > ixstop)) | (restart == False)): #query selection CENTER query = "CRATER_ID = '" + crater_id[ix] + "'" print (query) arcpy.SelectLayerByAttribute_management("CENTER_lyr", "NEW_SELECTION", query) #print ("YESx2") # make a layer of the selection arcpy.CopyFeatures_management("CENTER_lyr", "CENTER_TMP") # old coordinate systems desc = arcpy.Describe("CENTER_TMP") spatialReference = desc.spatialReference # project to the right coordinate systems # central meridian should be replaced by the longitude # standard parallel_1 by the latitude cent_med = np.round(row[1],decimals=0) std_parall = np.round(row[2],decimals=0) str_bef = "PROJCS['Equirectangular_Moon',GEOGCS['GCS_Moon',DATUM['D_Moon',SPHEROID['Moon_localRadius',1737400.0,0.0]],PRIMEM['Reference_Meridian',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Equidistant_Cylindrical'],PARAMETER['false_easting',0.0],PARAMETER['false_northing',0.0]," str_cent_med = "PARAMETER['central_meridian'," + str(cent_med) + "]," str_parall = "PARAMETER['standard_parallel_1'," + str(std_parall) + "]," str_after = "UNIT['Meter',1.0]]" # the whole string is spatialReference_new = str_bef + str_cent_med + str_parall + str_after # projection arcpy.Project_management(in_dataset="CENTER_TMP", out_dataset="CENTER_PROJ", out_coor_system= spatialReference_new, transform_method="", in_coor_system=spatialReference, preserve_shape="NO_PRESERVE_SHAPE", max_deviation="", vertical="NO_VERTICAL") # buffer creation arcpy.Buffer_analysis("CENTER_PROJ", "miniarea_TMP", bufferField, sideType, endType, dissolveType) # run feature to envelope tool arcpy.FeatureEnvelopeToPolygon_management(pathdab + "miniarea_TMP", pathdab + "miniarea_square", "SINGLEPART") # get area of the 10 diameters squared polygon area_bnd = "miniarea_square" with arcpy.da.SearchCursor(area_bnd, ["SHAPE@"]) as rows: # get the area of the 10 diameters squared polygon for row in rows: feat = row[0] fresh_crater_area = feat.area # select nac images (left and right satellite images) between x and y degrees of incidence select_NAC("nac_all", area_bnd, "nac_selection_20_40", 20., 40.) select_NAC("nac_all", area_bnd, "nac_selection_40_55", 40., 55.) select_NAC("nac_all", area_bnd, "nac_selection_55_80", 55., 80.) # define covered area here! fieldname_area = arcpy.ValidateFieldName("COVERED_AREA") for shpfile in ["nac_selection_20_40", "nac_selection_40_55", "nac_selection_55_80"]: if len(arcpy.ListFields(shpfile,"COVERED_AREA"))>0: None else: arcpy.AddField_management(shpfile, fieldname_area, "DOUBLE") #I could project the nac_selection here arcpy.Project_management(in_dataset="nac_selection_20_40", out_dataset="nac_selection_20_40_proj", out_coor_system= spatialReference_new, transform_method="", in_coor_system=spatialReference, preserve_shape="NO_PRESERVE_SHAPE", max_deviation="", vertical="NO_VERTICAL") arcpy.Project_management(in_dataset="nac_selection_40_55", out_dataset="nac_selection_40_55_proj", out_coor_system= spatialReference_new, transform_method="", in_coor_system=spatialReference, preserve_shape="NO_PRESERVE_SHAPE", max_deviation="", vertical="NO_VERTICAL") arcpy.Project_management(in_dataset="nac_selection_55_80", out_dataset="nac_selection_55_80_proj", out_coor_system= spatialReference_new, transform_method="", in_coor_system=spatialReference, preserve_shape="NO_PRESERVE_SHAPE", max_deviation="", vertical="NO_VERTICAL") # selected NAC + EDR + Area covered #print ("last function") (NAC_selected_20_40, edr_source_selected_20_40, areac_20_40) = calculate_newly_added_area(pathdab, "nac_selection_20_40_proj", area_bnd, fresh_crater_area, spatialReference, spatialReference_new) #print ("2") (NAC_selected_40_55, edr_source_selected_40_55, areac_40_55) = calculate_newly_added_area(pathdab, "nac_selection_40_55_proj", area_bnd, fresh_crater_area, spatialReference, spatialReference_new) #print ("3") (NAC_selected_55_80, edr_source_selected_55_80, areac_55_80) = calculate_newly_added_area(pathdab, "nac_selection_55_80_proj", area_bnd, fresh_crater_area, spatialReference, spatialReference_new) #print (ix) arcpy.Delete_management("miniarea_square") arcpy.Delete_management("miniarea_TMP") arcpy.Delete_management("CENTER_PROJ") arcpy.Delete_management("CENTER_TMP") arcpy.Delete_management("nac_selection_20_40") arcpy.Delete_management("nac_selection_20_40_proj") arcpy.Delete_management("nac_selection_40_55") arcpy.Delete_management("nac_selection_40_55_proj") arcpy.Delete_management("nac_selection_55_80") arcpy.Delete_management("nac_selection_55_80_proj") #NAC_selected_tmp = NAC_selected_20_40 + NAC_selected_40_55 + NAC_selected_55_80 #edr_selected_tmp = edr_source_selected_20_40 + edr_source_selected_40_55 + edr_source_selected_55_80 NAC_selected_all_20_40 = NAC_selected_all_20_40 + NAC_selected_20_40 edr_selected_all_20_40 = edr_selected_all_20_40 + edr_source_selected_20_40 areaa_20_40[ix] = areac_20_40 NAC_selected_all_40_55 = NAC_selected_all_40_55 + NAC_selected_40_55 edr_selected_all_40_55 = edr_selected_all_40_55 + edr_source_selected_40_55 areaa_40_55[ix] = areac_40_55 NAC_selected_all_55_80 = NAC_selected_all_55_80 + NAC_selected_55_80 edr_selected_all_55_80 = edr_selected_all_55_80 + edr_source_selected_55_80 areaa_55_80[ix] = areac_55_80 # save every 5 timesteps if (ix % 25 == 0): output_nbr = np.column_stack((np.array(areaa_20_40), np.array(areaa_40_55), np.array(areaa_55_80))) np.savetxt("numbers0_" + str(int(ix)) + ".txt", output_nbr, delimiter=";") np.savetxt("NAC_i20_40_0_" + str(int(ix)) + ".txt", np.array((NAC_selected_all_20_40)), delimiter=";",fmt="%s") np.savetxt("NAC_i40_55_0_" + str(int(ix)) + ".txt", np.array((NAC_selected_all_40_55)), delimiter=";",fmt="%s") np.savetxt("NAC_i55_80_0_" + str(int(ix)) + ".txt", np.array((NAC_selected_all_55_80)), delimiter=";",fmt="%s") np.savetxt("EDR_i20_40_0_" + str(int(ix)) + ".txt", np.array((edr_selected_all_20_40)), delimiter=";",fmt="%s") np.savetxt("EDR_i40_55_0_" + str(int(ix)) + ".txt", np.array((edr_selected_all_40_55)), delimiter=";",fmt="%s") np.savetxt("EDR_i55_80_0_" + str(int(ix)) + ".txt", np.array((edr_selected_all_55_80)), delimiter=";",fmt="%s") elif (ix == 2281): output_nbr = np.column_stack((np.array(areaa_20_40), np.array(areaa_40_55), np.array(areaa_55_80))) np.savetxt("numbers0_" + str(int(ix)) + ".txt", output_nbr, delimiter=";") np.savetxt("NAC_i20_40_0_" + str(int(ix)) + ".txt", np.array((NAC_selected_all_20_40)), delimiter=";",fmt="%s") np.savetxt("NAC_i40_55_0_" + str(int(ix)) + ".txt", np.array((NAC_selected_all_40_55)), delimiter=";",fmt="%s") np.savetxt("NAC_i55_80_0_" + str(int(ix)) + ".txt", np.array((NAC_selected_all_55_80)), delimiter=";",fmt="%s") np.savetxt("EDR_i20_40_0_" + str(int(ix)) + ".txt", np.array((edr_selected_all_20_40)), delimiter=";",fmt="%s") np.savetxt("EDR_i40_55_0_" + str(int(ix)) + ".txt", np.array((edr_selected_all_40_55)), delimiter=";",fmt="%s") np.savetxt("EDR_i55_80_0_" + str(int(ix)) + ".txt", np.array((edr_selected_all_55_80)), delimiter=";",fmt="%s") else: None ix = ix + 1 return (NAC_selected_all_20_40, edr_selected_all_20_40, areaa_20_40, NAC_selected_all_40_55, edr_selected_all_40_55, areaa_40_55, NAC_selected_all_55_80, edr_selected_all_55_80, areaa_55_80)
def intersect_ROI_DTM(pathdab, location_completely_within, outASCII, crater_id): ''' simple version of the script above. Only DTMs covering completely 4 times the radius of the crater has been selected. (points, buffer, envelope and the absolute path to the DTMs are provided) location_completely_within = "location_overlapDTM_completely_within" pathdab = "D:/NAC_DTM/NAC_AMES/arcpy/database.gdb/" outASCII = "D:/ANALYSIS/NAC_DTM/ASCII/" crater_id = np.genfromtxt("D:/ANALYSIS/NAC_DTM/ASCII/crater_id.txt", dtype=str) intersect_ROI_DTM(pathdab, location_completely_within, outASCII, crater_id) ''' # define paths and workspace (I need to create the gdb at some points) env.workspace = env.scratchWorkspace = pathdab # Make a layer from the feature class of the location of the centre of the crater arcpy.MakeFeatureLayer_management(location_completely_within, location_completely_within + "_lyr") # run buffer tool out = 'buffer' bufferField = "Diameter4txt" sideType = "FULL" endType = "ROUND" dissolveType = "NONE" dissolveField = "Distance" with arcpy.da.UpdateCursor(location_completely_within + "_lyr", ["x_coord", "y_coord", "abspath"]) as cursor: ix = 0 for row in cursor: print(ix) if os.path.isfile(outASCII + crater_id[ix] + '.asc'): ix = ix + 1 else: #query selection CENTER query = "CRATER_ID = '" + crater_id[ix] + "'" arcpy.SelectLayerByAttribute_management( location_completely_within + "_lyr", "NEW_SELECTION", query) # make a layer of the selection arcpy.CopyFeatures_management( location_completely_within + "_lyr", "CENTER_TMP") # old coordinate systems desc = arcpy.Describe("CENTER_TMP") spatialReference = desc.spatialReference # projection of the right coordinate systems (same as DTM?) desc_new = arcpy.Describe(row[2]) spatialReference_new = desc_new.spatialReference # projection of location arcpy.Project_management(in_dataset="CENTER_TMP", out_dataset="CENTER_PROJ", out_coor_system=spatialReference_new, transform_method="", in_coor_system=spatialReference, preserve_shape="NO_PRESERVE_SHAPE", max_deviation="", vertical="NO_VERTICAL") # buffer creation arcpy.Buffer_analysis("CENTER_PROJ", out, bufferField, sideType, endType, dissolveType) # run feature to envelope tool arcpy.FeatureEnvelopeToPolygon_management( out, "envelope_proj", "SINGLEPART") desc_proj = arcpy.Describe("envelope_proj") extent = desc_proj.extent top = extent.YMax bottom = extent.YMin left = extent.XMin right = extent.XMax ExtStr = "{} {} {} {}".format(left, bottom, right, top) # The following inputs are layers or table views: "dtm", "square_test" arcpy.Clip_management( in_raster=row[2], rectangle=ExtStr, out_raster=pathdab + "dtm_clip", in_template_dataset="envelope_proj", clipping_geometry="NONE", maintain_clipping_extent="NO_MAINTAIN_EXTENT") # Get input Raster properties inRas = arcpy.Raster('dtm_clip') # or I could convert it to ascii arcpy.RasterToASCII_conversion( inRas, outASCII + crater_id[ix] + '.asc') ix = ix + 1 arcpy.Delete_management("dtm_clip") arcpy.Delete_management("envelope_proj") arcpy.Delete_management(out) arcpy.Delete_management("CENTER_PROJ") arcpy.Delete_management("CENTER_TMP")
"SBA_August_2015.gdb", "SBA_December_2015.gdb" ] #Loop through all the geodatabases in this list and buffer each feature in the geodatabases for gdb in gdbs: print "Creating sample site buffers for %s" % (gdb) # Set environment settings env.workspace = os.path.join(gdbDir, gdb) # List features in the working geodatabase fcList = arcpy.ListFeatureClasses() # Loop through feature list and buffer each feature for fc in fcList: arcpy.Buffer_analysis(fc, bufDir + fc + "_buffer.shp", "2 Inches") arcpy.FeatureEnvelopeToPolygon_management( bufDir + fc + "_buffer.shp", bufDir + fc + "_squareBuffer.shp") ##--------------------------------------------------------------------------- ##SECOND PART OF SCRIPT: IMAGE EXTRACTION ##--------------------------------------------------------------------------- # Read through first line of sampleSites_assocaitedImage_ALL CSV file count = 0 for row in siteImgList: # Split the row to seperate the columns into a list columns = row.split(',') # Assign the columns I will need siteNum = columns[0] siteLat = columns[1] siteLon = columns[2] siteRas = columns[3]
def main(): env.workspace = 'C:\\Temp' #create new folder (if it does not exist already) where to store all processed layers newFold = 'Layers' newpath = os.path.join(env.workspace, 'Layers') if not os.path.exists(newpath): os.makedirs(newpath) #or r'C:\Temp' if you use a single '\' do not forget to add 'r' before the string #or 'C:/Temp' # Allow output to overwrite arcpy.gp.overwriteOutput = True # Local variables: Target_lyr = 'water_Broward.shp' Termite_loc = 'PreTreat03_NoDup2003.shp' LULC_lyr = 'd4_lu_gen_2010.shp' Roads_lyr = 'navteq_broward_streets.shp' TA_Multinet_lyr = 'teleatlas_broward_land_use.shp' #------------------------------------------------------------------------------ #A SpatialReference can be easily created from existing datasets and PRJ files: # #(1) Use a PRJ file as an argument to the SpatialReference class: #prjFile = os.path.join(arcpy.GetInstallInfo()["InstallDir"], # "Coordinate Systems/Geographic Coordinate Systems/North America/NAD 1983.prj") #spatialRef = arcpy.SpatialReference(prjFile) # #(2) Manually define a .prj string #prjFile = 'PROJCS[...]' #spatialRef = arcpy.SpatialReference(prjFile) # #(3) Describe a dataset and access its spatialReference property: #TargetCS = arcpy.Describe(Water_lyr).spatialReference #Geographic transformation - #transformation = 'NAD_1983_To_WGS_1984_1' #If ALL FEATURES need a datum transformation OR NONE of them needs one then #try: # res = arcpy.BatchProject_management(arcpy.ListFeatureClasses('*'), env.workspace, TargetCS, '', # transformation) OR '' instead of transformation # print 'projection of all datasets successful' # else: # print 'failed to project one or more datasets' #except: # print res.getMessages() #------------------------------------------------------------------------------ try: # Define a common target coordinate system desc = arcpy.Describe(Target_lyr) TargetSR = desc.spatialReference # If target layer has no projection defined print a message and stop if TargetSR.Name == "Unknown": arcpy.AddError( Target_lyr + "is used as target layer but has Unknown Projection!") sys.exit('Target Layer Has Unknown Projection!') # Use ListFeatureClasses to generate a list of inputs for inputFC in arcpy.ListFeatureClasses('*'): if inputFC != Target_lyr: # Strip name and extension from a feature class # fileName, fileExtension = os.path.splitext(inputFC) # Input spatial reference inputFC_SR = arcpy.Describe(inputFC).spatialReference # Determine if the input has a defined coordinate system if inputFC_SR.Name == "Unknown": print 'Projecting ' + inputFC + '...' arcpy.DefineProjection_management(inputFC, TargetSR) outFC = os.path.join(newpath, inputFC) # Make a copy of the current layer into the new folder arcpy.CopyFeatures_management(inputFC, outFC) print 'Projected ' + inputFC + ' from Unknown ' + \ ' to ' + TargetSR.Name # Check if current layer and target layer are same TYPE #(i.e. projected and geographic or viceversa) if inputFC_SR.type == TargetSR.type: #SAME TYPE...Check if same projection name: #CASE (1) SAME NAME..create copy and continue if inputFC_SR.Name == TargetSR.Name: outFC = os.path.join(newpath, inputFC) arcpy.CopyFeatures_management(inputFC, outFC) continue #CASE (2) DIFFERENT NAME..check if SAME DATUM OR NOT else: # if they have different names check their datum # (GCS method new in 10.1) if inputFC_SR.GCS.datumName == TargetSR.GCS.datumName: print 'Projecting ' + inputFC + '...' # if datum are the same just project without any transformation outFC = os.path.join(newpath, inputFC) arcpy.Project_management(inputFC, outFC, TargetSR) print 'Projected ' + inputFC + ' from ' + inputFC_SR.Name + \ ' to ' + TargetSR.Name else: # if datum are NOT the same then get transformations # available for your specific study area (based on extent) print 'Projecting ' + inputFC + '...' outlist = arcpy.ListTransformations( inputFC_SR, TargetSR, arcpy.Describe(inputFC).extent) transformation = outlist[0] outFC = os.path.join(newpath, inputFC) arcpy.Project_management(inputFC, outFC, TargetSR, transformation) print 'Projected ' + inputFC + ' from ' + inputFC_SR.Name + \ ' to ' + TargetSR.Name + '\nDatum Transformation: ' + transformation else: #DIFFERENT TYPE...Check if same projection name: #...check if SAME DATUM OR NOT #(GCS method new in 10.1) if inputFC_SR.GCS.datumName == TargetSR.GCS.datumName: print 'Projecting ' + inputFC + '...' #if datum are the same just project without any transformation outFC = os.path.join(newpath, inputFC) arcpy.Project_management(inputFC, outFC, TargetSR) print 'Projected ' + inputFC + ' from ' + inputFC_SR.Name + \ ' to ' + TargetSR.Name else: # if datum are NOT the same then get transformations # available for your specific study area (based on extent) print 'Projecting ' + inputFC + '...' outlist = arcpy.ListTransformations( inputFC_SR, TargetSR, arcpy.Describe(inputFC).extent) transformation = outlist[0] outFC = os.path.join(newpath, inputFC) arcpy.Project_management(inputFC, outFC, TargetSR, transformation) print 'Projected ' + inputFC + ' from ' + inputFC_SR.Name + \ ' to ' + TargetSR.Name + '\nDatum Transformation: ' + transformation else: # if current layer is the same as target layer make a copy and #skip to next iteration outFC = os.path.join(newpath, inputFC) arcpy.CopyFeatures_management(inputFC, outFC) continue print '\n' #------------ #END OF LOOP! #------------ # CALCULATE CENTROID OF POINTS AND CREATE A SQUARE/RECTANGULAR CLIPPING EXTENT # AROUND THAT POINT print 'Calculating centroid of point cloud...' fc = os.path.join(newpath, Termite_loc) shapefieldname = arcpy.Describe(fc).ShapeFieldName # Create search cursor Rows = arcpy.SearchCursor(Termite_loc) # Initialize local variables X_temp = 0.0 Y_temp = 0.0 counter = 0 # Enter for loop for each feature/row for row in Rows: counter = counter + 1 # Create the geometry object 'feat'. Identify the geometry field. feat = row.getValue(shapefieldname) # Use getPart() to return an array of point objects for a particular part of the geometry pnt = feat.getPart() X_temp = X_temp + pnt.X Y_temp = Y_temp + pnt.Y # Calculate centroid of point cloud X_centroid = X_temp / counter Y_centroid = Y_temp / counter # Create an empty feature class from point coords # Set local variables Centroid_pnt = arcpy.Point(X_centroid, Y_centroid) Centroid_pntGeometry = arcpy.PointGeometry(Centroid_pnt, TargetSR) out_name1 = os.path.join(newpath, 'Centroid_buffer.shp') out_name2 = os.path.join(newpath, 'Centroid_buffer_sq.shp') # Specify desired distance distance = '10000 Meters' # Create round buffer print 'Creating round buffer around centroid...' Centroid_buffer = arcpy.Buffer_analysis(Centroid_pntGeometry, out_name1, distance) # Create square buffer print 'Creating square buffer around centroid...' Centroid_buffer_sq = arcpy.FeatureEnvelopeToPolygon_management( Centroid_buffer, out_name2) # Delete the circle buffer...no need to keep it anymore arcpy.Delete_management(Centroid_buffer, '') print '\n' # CLIP ALL FEATURES with the squared buffer area env.workspace = newpath # Loop through a list of feature classes in the desired folder for fc in arcpy.ListFeatureClasses('*'): if fc == 'Centroid_buffer_sq.shp' or fc == Termite_loc: continue # Strip name and extension from a feature class fileName, fileExtension = os.path.splitext(fc) outFC = os.path.join(newpath, fileName + '_Clip' + fileExtension) #Clip each input feature class in the list print 'Clipping ' + fc + ' to square buffer area...' fc_clipped = arcpy.Clip_analysis(fc, Centroid_buffer_sq, outFC, 0.01) #delete original...just keep clipped one arcpy.Delete_management(fc, '') # Rename the clipped file to standard name arcpy.Rename_management(fc_clipped, fc) #END OF LOOP! print '\n' #SELECT FEATURES WITH CERTAIN ATTRIBUTES OF INTEREST # Make a layer from the feature class (temporary layer) arcpy.MakeFeatureLayer_management(LULC_lyr, 'LC_Agric') arcpy.MakeFeatureLayer_management(TA_Multinet_lyr, 'TA_Airport') arcpy.MakeFeatureLayer_management(Roads_lyr, 'NAVTEQ_buff') # Select only those attributes with class agricultural fields (i.e. DESCRIPT = 'AGRICULTURAL') print 'Selecting agricultural features from ' + LULC_lyr arcpy.SelectLayerByAttribute_management( 'LC_Agric', "NEW_SELECTION", " \"DESCRIPT\" = 'AGRICULTURAL' ") # Select only those attributes with feature type Airport Ground (9732) OR Airport Runway (9776) print 'Selecting airport ground & runway features from ' + TA_Multinet_lyr arcpy.SelectLayerByAttribute_management( 'TA_Airport', "NEW_SELECTION", " \"FEATTYP\" in (9732,9776) ") # Write the selected features to a new featureclass # Strip name and extension from a feature class fileName, fileExtension = os.path.splitext(LULC_lyr) LC_Agric = arcpy.CopyFeatures_management( 'LC_Agric', os.path.join(fileName + '_temp' + fileExtension)) fileName, fileExtension = os.path.splitext(TA_Multinet_lyr) TA_Airport = arcpy.CopyFeatures_management( 'TA_Airport', os.path.join(fileName + '_temp' + fileExtension)) # Make buffer around streets (10 m) fileName, fileExtension = os.path.splitext(Roads_lyr) linearDist = '10 Meters' print 'Creating buffer of ' + linearDist + ' for layer ' + Roads_lyr NAVTEQ_buff10 = arcpy.Buffer_analysis( Roads_lyr, os.path.join(fileName + '_temp' + fileExtension), linearDist, 'FULL', 'ROUND', 'ALL', '') arcpy.Delete_management(LULC_lyr, '') arcpy.Delete_management(TA_Multinet_lyr, '') arcpy.Delete_management(Roads_lyr, '') # Rename the clipped file to standard name LC_out = 'LC_Agric.shp' TA_out = 'TA_Airport.shp' NAVTQ_out = 'NAVTEQ_buff10.shp' Water_out = 'water_Broward.shp' arcpy.Rename_management(LC_Agric, 'LC_Agric.shp') arcpy.Rename_management(TA_Airport, 'TA_Airport.shp') arcpy.Rename_management(NAVTEQ_buff10, 'NAVTEQ_buff10.shp') print '\n' #UNION OF ALL SELECTED FEATURES print 'Creating Union of unsuitable habitat layers...' inFeatures = [LC_out, TA_out, NAVTQ_out, Water_out] outFeatures = 'UnsuitHab_Union' clusterTol = 0.001 UnsuitHab_Union = arcpy.Union_analysis(inFeatures, outFeatures, "ONLY_FID", clusterTol) #ERASE NON-SUITABLE HABITAT UNION FROM REFERENCE CLIPPING POLYGON FEATURE #to have a final suitable habitat vectory layer to use as polygon mask for our rasters print 'Creating suitable habitat polygon mask...' eraseOutput = 'Habitat_mask.shp' xyTol = "0.1 Meters" arcpy.Erase_analysis(Centroid_buffer_sq, UnsuitHab_Union, eraseOutput, xyTol) print '\n' #change workspace to raster folder env.workspace = 'C:\\Temp\\Raster' #simulation years (based on rasters) start_yr = 2003 end_yr = 2018 sim_yrs = range( start_yr, end_yr + 1 ) #when using range remember to add 1 to the last year to include it #Monte Carlo envelopes MC_envel = [0, 50, 100] counter = 0 #create list of labels (maybe better way to do it...but it works!) etiq = ['NA'] * len(sim_yrs) * len(MC_envel) mask_etiq = ['NA'] * len(sim_yrs) * len(MC_envel) for yr in sim_yrs: s = 'RS_' + str(yr) + '_' s_mask = 'Mask_' + str(yr) + '_' for m in MC_envel: s2 = str(m) + '.img' label = s + s2 etiq[counter] = label label_mask = s_mask + s2 mask_etiq[counter] = label_mask counter += 1 etiq = sorted(etiq) mask_etiq = sorted(mask_etiq) counter = 0 #loop through each raster in the folder for rast in arcpy.ListRasters('*'): #RESAMPLE ALL RASTERS DOWN TO 5meters (from 100m original) so that the #masking operation is more precise on the rasters # Determine the new output feature class path and name fileName, fileExtension = os.path.splitext(rast) # resampling cell size cell_size = '5' resampling_type = 'NEAREST' outRast = etiq[counter] print 'Resampling ' + rast + ' to ' + cell_size + ' meters...' arcpy.Resample_management(rast, outRast, cell_size, resampling_type) # Execute ExtractByMask inMaskData = os.path.join(newpath, 'Habitat_mask.shp') out_rast_masked = os.path.join(env.workspace, mask_etiq[counter]) print 'Extracting ' + rast + ' by mask with ' + inMaskData + ' ...' outExtractByMask = ExtractByMask(outRast, inMaskData) # Save the output outExtractByMask.save(out_rast_masked) print 'Saved mask to ' + out_rast_masked counter += 1 arcpy.Delete_management(rast, '') arcpy.Delete_management(outRast, '') #END OF LOOP! except arcpy.ExecuteError: print arcpy.GetMessages(2) arcpy.AddError(arcpy.GetMessages(2)) except Exception as e: print e.args[0] arcpy.AddError(e.args[0]) print arcpy.GetMessages()