def convert_ras(in_huc, in_bf, in_strm): if in_bf: arcpy.AddMessage("Preparing the stream network...") arcpy.MakeFeatureLayer_management(in_strm, r"in_strm_lyr") arcpy.MakeFeatureLayer_management(in_bf, r"in_bf_lyr") arcpy.Clip_analysis("in_strm_lyr", in_huc, r"in_memory\strm_clip") arcpy.Clip_analysis("in_bf_lyr", in_huc, r"in_memory\bf_clip") arcpy.MakeFeatureLayer_management(r"in_memory\strm_clip", "strm_clip_lyr") arcpy.MakeFeatureLayer_management(r"in_memory\bf_clip", "bf_clip_lyr") cellSize = arcpy.env.cellSize # convert streams to a raster format arcpy.AddField_management("strm_clip_lyr", "VAL", "SHORT") arcpy.CalculateField_management("strm_clip_lyr", "VAL", "1", "PYTHON_9.3") arcpy.PolylineToRaster_conversion("strm_clip_lyr", "VAL", r"in_memory\strm_ras", "", "", cellSize) # convert bankful polygon arcpy.AddField_management("bf_clip_lyr", "VAL", "SHORT") arcpy.CalculateField_management("bf_clip_lyr", "VAL", "1", "PYTHON_9.3") arcpy.PolygonToRaster_conversion("bf_clip_lyr", "VAL", r"in_memory\bf_ras", "CELL_CENTER", "", cellSize) # Mosaic stream and bankful rasters strm_final = arcpy.MosaicToNewRaster_management( r"in_memory\bf_ras;in_memory\strm_ras", "in_memory", "strmMSC", "", "8_BIT_UNSIGNED", cellSize, 1, "LAST", "") return strm_final else: arcpy.AddMessage("Preparing the stream network...") arcpy.MakeFeatureLayer_management(in_strm, r"in_strm_lyr") arcpy.Clip_analysis("in_strm_lyr", in_huc, r"in_memory\strm_clip") arcpy.MakeFeatureLayer_management(r"in_memory\strm_clip", "strm_clip_lyr") #cellSize = arcpy.env.cellSize # convert streams to a raster format arcpy.AddField_management("strm_clip_lyr", "VAL", "SHORT") arcpy.CalculateField_management("strm_clip_lyr", "VAL", "1", "PYTHON_9.3") strm_final = arcpy.PolylineToRaster_conversion( "strm_clip_lyr", "VAL", r"in_memory\strm_final") return strm_final
def get_threshold_automatically(true_river): global demRaster, workspace, flow_acc, acc_start_of_river, start_of_drainages, start_of_drainages2, actual_drainage_network, start_of_drainages_fullpath, start_of_drainages2_fullpath get_unique_point(true_river, start_of_drainages="p_o_start") arcpy.FeatureToPoint_management(start_of_drainages_fullpath, start_of_drainages2_fullpath, "CENTROID") acc_value_of_ob_river = ExtractMultiValuesToPoints( start_of_drainages2_fullpath, [[flow_acc, "acc"]], "NONE") acc_start_of_river = table_to_list(start_of_drainages2_fullpath, "acc") elevSTDResult = arcpy.GetRasterProperties_management( demRaster, "CELLSIZEX") elev_cell_size = int(elevSTDResult.getOutput(0)) #arcpy.AddMessage(true_river) #arcpy.AddMessage(str(elev_cell_size)) oid = arcpy.Describe(true_river).OIDFieldname #"FID" arcpy.PolylineToRaster_conversion(true_river, oid, actual_drainage_network, "MAXIMUM_LENGTH", "NONE", elev_cell_size) cellcount_of_river = count_cells(actual_drainage_network)[0] acc_list = median_(flow_acc)[0] #myset = list(set(acc_list)) myset = list(set(acc_start_of_river)) sorted2 = sorter(myset) #arcpy.AddMessage(sorted2) my_exp = find_exp(flow_acc, sorted2[0], cellcount_of_river, sorted2)
def shp_to_rst(shp, inField, cellsize, outRaster, template=None, snap=None): """ Feature Class to Raster """ if template: tempEnvironment0 = arcpy.env.extent arcpy.env.extent = template if snap: tempSnap = arcpy.env.snapRaster arcpy.env.snapRaster = snap obj_describe = arcpy.Describe(shp) geom = obj_describe.ShapeType if geom == 'Polygon': arcpy.PolygonToRaster_conversion(shp, inField, outRaster, "CELL_CENTER", "NONE", str(cellsize)) elif geom == 'Polyline': arcpy.PolylineToRaster_conversion(shp, inField, outRaster, "MAXIMUM_LENGTH", "NONE", str(cellsize)) if template: arcpy.env.extent = tempEnvironment0 if snap: arcpy.env.snapRaster = tempSnap
def _get_mat_stream_seg(self, stream): """Get numpy array of integers detecting whether there is a stream on corresponding pixel of raster (number equal or greater than 1000 in return numpy array) or not (number 0 in return numpy array). :param stream: Polyline with stream in the area. :return mat_stream_seg: Numpy array """ stream_seg = self.storage.output_filepath('stream_seg') arcpy.PolylineToRaster_conversion(stream, self._primary_key, stream_seg, "MAXIMUM_LENGTH", "NONE", self.spix) # TODO: reclassification rule is invalid, stream_seg is the same as stream_rst # stream_seg = self.storage.output_filepath('stream_seg') # arcpy.gp.Reclassify_sa( # stream_rst, "VALUE", # "NoDataValue 1000", stream_seg, "DATA" # ) ll_corner = arcpy.Point(self.ll_corner[0], self.ll_corner[1]) mat_stream_seg = arcpy.RasterToNumPyArray(stream_seg, ll_corner, self.cols, self.rows) mat_stream_seg = mat_stream_seg.astype('int16') # TODO: is no_of_streams needed (-> mat_stream_seg.max()) count = arcpy.GetCount_management(stream_seg) no_of_streams = int(count.getOutput(0)) self._get_mat_stream_seg_(mat_stream_seg, no_of_streams) return mat_stream_seg
def run(self): self.e.load() print "Starting Creating Facets processing..." if arcpy.Exists(self.i.cmx): # this forces overwriting the cmx table arcpy.Delete_management(self.i.cmx) arcpy.gp.ZonalStatisticsAsTable_sa(self.i.catchment, "HydroID", self.i.fac, self.i.cmx, "DATA", "MAXIMUM") # --- old code --- # arcpy.gp.MakeFeatureLayer(self.i.catchment, "lyr") # arcpy.AddJoin_management("lyr", "HydroID", self.i.cmx, "HydroID", "KEEP_ALL") # arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION","cmx.MAX > cmx.COUNT") # arcpy.Clip_analysis(self.i.drl, "lyr", self.i.drl_c, "") # arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION","cmx.MAX <= cmx.COUNT") # ESSA PARTE NAO FOI FEITA # arcpy.Clip_analysis(self.i.lfp, "lyr", self.i.lfp_c, "") # --- old code --- self.process_c(self.i.drl, self.i.drl_c) # self.process_c(self.i.lfp, self.i.lfp_c) arcpy.Erase_analysis(self.i.drl, self.i.drl_c, self.i.lfp_ct) arcpy.SpatialJoin_analysis(target_features=self.i.lfp,join_features=self.i.lfp_ct,out_feature_class=self.i.lfp_c,join_operation="JOIN_ONE_TO_ONE",join_type="KEEP_COMMON",match_option="INTERSECT") arcpy.Merge_management(self.i.drl_c + ";" + self.i.lfp_c, self.i.fm_vec) arcpy.PolylineToRaster_conversion(self.i.fm_vec, "HydroID", self.i.fm_ras, "MAXIMUM_LENGTH", "NONE", self.e.cs) arcpy.gp.Divide_sa(self.i.fm_ras, self.i.fm_ras, self.i.fm_ras_d) arcpy.gp.Reclassify_sa(self.i.fm_ras_d, "VALUE", "1 NODATA;NODATA 0", self.i.fm_ras_r, "DATA") arcpy.gp.Combine_sa(self.i.fm_ras_r + ";" + self.i.cat, self.i.fm_ras_c) arcpy.RasterToPolygon_conversion(self.i.fm_ras_c, self.i.facets, "NO_SIMPLIFY", "VALUE") # --- old code --- # arcpy.RemoveJoin_management("lyr", "") # arcpy.SelectLayerByAttribute_management("lyr", "CLEAR_SELECTION", "") # arcpy.Delete_management("lyr") # --- old code --- print "Ending Creating Facets processing..."
def CalculateBoundary(dem, InundPolygon,cellSize,WS): arcpy.PolygonToLine_management(InundPolygon, WS+'\polyline') #Convert inundation extent polygon to polyline arcpy.PolylineToRaster_conversion(WS+'\\polyline', 'OBJECTID', WS+'\linerast15', 'MAXIMUM_LENGTH', 'NONE', cellSize) #Convert polyline to raster print 'after polyline to raster' inRaster = Raster(WS+'\linerast15') inTrueRaster = dem inFalseConstant = '#' whereClause = "VALUE >= 0" print 'Con' boundary = Con(inRaster, inTrueRaster, inFalseConstant, whereClause) #extract the boundary cells elevation from a DEM boundary.save('boundary1') #name of output boundary cell elevation raster return boundary
def CalculateBoundary(dem, inund_polygon, cell_size, ws): """ Return a raster line representation with associated underlying DEM values as the values. Take in a inundated flood polygon, create a polyline representation of the input inundation_polygon. Next, convert flood polygon polyline calculated in the first step to a raster. Then, set values of newly created 'raster line' to the underlying dem values. Finally, save the raster line to the workspace Much of the naming conventions found in this function follow the arcpy documentation for the 'Con' function. Input: dem -> ArcPy raster object inundation_polygon -> str cell_size -> int ws -> str Return: str of raster line """ print('Converting inundation polygon to inundation polyline') # Convert inundation extent polygon to polyline arcpy.PolygonToLine_management(inund_polygon, ws + '\polyline') print('Converting inundation polyline to raster') # Convert polyline to raster arcpy.PolylineToRaster_conversion(ws + '\\polyline', 'OBJECTID', ws + '\linerast15', 'MAXIMUM_LENGTH', 'NONE', cell_size) raster_polyline = Raster(ws + '\linerast15') # The input whose values will be used as the output cell values if the condition is false. input_false_constant = '#' where_clause = 'VALUE >= 0' # Extract the boundary cells elevation from DEM boundary = Con(raster_polyline, dem, input_false_constant, where_clause) # TODO: Ask Cohen about this and its purpose # Minimum neighborhood analysis OutRasTemp = FocalStatistics(dem, 'Circle 2 CELL', 'MINIMUM', 'DATA') OutRasTemp.save('BoundaryFocal_circ2') where_clause = 'VALUE > 0' boundary = Con(OutRasTemp, boundary, input_false_constant, where_clause) boundary.save('boundary_elev') return boundary
def execute_BridgeCorrection(r_dem, str_bridges, str_result, messages, language="FR"): with arcpy.EnvManager(snapRaster=r_dem): with arcpy.EnvManager(extent=r_dem): linebridges = arcpy.CreateScratchName( "lines", data_type="FeatureClass", workspace=arcpy.env.scratchWorkspace) arcpy.PolygonToLine_management(str_bridges, linebridges, "IGNORE_NEIGHBORS") r_linebridges = arcpy.CreateScratchName( "rlines", data_type="RasterDataset", workspace=arcpy.env.scratchWorkspace) arcpy.PolylineToRaster_conversion(linebridges, "ORIG_FID", r_linebridges, cellsize=r_dem) r_polybridges = arcpy.CreateScratchName( "rpoly", data_type="RasterDataset", workspace=arcpy.env.scratchWorkspace) arcpy.PolygonToRaster_conversion( str_bridges, arcpy.Describe(str_bridges).OIDFieldName, r_polybridges, cellsize=r_dem) r_bridges = arcpy.sa.Con( arcpy.sa.IsNull(r_polybridges) == 1, r_linebridges, r_polybridges) z_bridges = arcpy.sa.ZonalStatistics(r_bridges, "VALUE", r_dem, "MINIMUM") temp_isnull = arcpy.sa.IsNull(z_bridges) temp_dem = arcpy.sa.Con(temp_isnull, z_bridges, r_dem, "VALUE = 0") temp_fill = arcpy.sa.Fill(temp_dem) result = arcpy.sa.Con(temp_isnull, temp_fill, r_dem, "VALUE = 0") result.save(str_result) arcpy.Delete_management(linebridges) arcpy.Delete_management(r_linebridges)
def CalculateBoundary(dem, InundPolygon, cellSize, WS, outputName): arcpy.PolygonToLine_management(InundPolygon, WS + '\polyline') arcpy.PolylineToRaster_conversion(WS + '\\polyline', 'OBJECTID', WS + '\linerast15', 'MAXIMUM_LENGTH', 'NONE', cellSize) arcpy.AddMessage('after polyline to raster') inRaster = Raster(WS + '\linerast15') inTrueRaster = dem inFalseConstant = '#' whereClause = "VALUE >= 0" arcpy.AddMessage('Con') boundary = Con(inRaster, inTrueRaster, inFalseConstant, whereClause) boundary.save(outputName) return outputName
def CalculateBoundary(dem, InundPolygon,cellSize,WS): arcpy.PolygonToLine_management(InundPolygon, WS+'\polyline') #Convert inundation extent polygon to polyline arcpy.PolylineToRaster_conversion(WS+'\\polyline', 'OBJECTID', WS+'\linerast15', 'MAXIMUM_LENGTH', 'NONE', cellSize) #Convert polyline to raster print ('after polyline to raster') inRaster = Raster(WS+'\linerast15') inTrueRaster = dem inFalseConstant = '#' whereClause = "VALUE >= 0" print ('Con') boundary = Con(inRaster, inTrueRaster, inFalseConstant, whereClause) #extract the boundary cells elevation from a DEM OutRasTemp = FocalStatistics(dem, 'Circle 2 CELL', "MINIMUM", "DATA") OutRasTemp.save('BoundaryFocal_circ2') whereClause = "VALUE > 0" boundary = Con(OutRasTemp, boundary, inFalseConstant, whereClause) boundary.save('boundary_elev') #name of output boundary cell elevation raster return boundary
def roadtoheat(site, inshp, res, kernel_dir, keyw, inFID, heatfield, sitedic, snapras, outdir): expr = """{0} = {1}""".format(arcpy.AddFieldDelimiters(inshp, inFID), str(site)) print(expr) arcpy.MakeFeatureLayer_management(in_features=inshp, out_layer='lyr') arcpy.SelectLayerByAttribute_management('lyr', selection_type='NEW_SELECTION', where_clause=expr) #print(site) nshp = len( [row[0] for row in arcpy.da.SearchCursor('lyr', [inFID])] ) #int(arcpy.GetCount_management('lyr').getOutput(0)) would be faster but often has a bug #print(nshp) if nshp > 0: #print('{} features'.format(nshp)) arcpy.ResetEnvironments() arcpy.env.extent = sitedic[site] arcpy.env.snapRaster = snapras outras = os.path.join(outdir, 'hpmstiger_{0}{1}.tif'.format(heatfield, site)) if not arcpy.Exists(outras): print('{} does not exist, generate heatmap'.format(outras)) tmpdir = os.path.join(os.path.dirname(outdir), 'tmp_{}'.format(str(site))) os.mkdir(tmpdir) arcpy.env.scratchWorkspace = tmpdir arcpy.PolylineToRaster_conversion('lyr', value_field=heatfield, out_rasterdataset=outras, priority_field=heatfield, cellsize=res) customheatmap(kernel_dir=kernel_dir, in_raster=outras, scratch_dir=tmpdir, out_gdb=outdir, out_var=heatfield + str(site), divnum=100, keyw=keyw, ext='.tif', verbose=True) arcpy.Delete_management(tmpdir) arcpy.SelectLayerByAttribute_management( 'lyr', selection_type='CLEAR_SELECTION') #might not be necessary arcpy.Delete_management('lyr') #might not be necessary
def execute_DEMsDownstreamCorrection(ends_polygons_dir, dem_bridges_dir, output_folder, messages): arcpy.env.workspace = ends_polygons_dir polylist = arcpy.ListFeatureClasses() for poly in polylist: rastername = os.path.splitext(poly)[0] print(rastername) dem = os.path.join(dem_bridges_dir, rastername) arcpy.env.extent = dem # Converting the polygons in rasters lines = arcpy.CreateScratchName("lines", data_type="FeatureClass", workspace="in_memory") arcpy.PolygonToLine_management(poly, lines, "IGNORE_NEIGHBORS") r_lines = arcpy.CreateScratchName("rlines", data_type="RasterDataset", workspace=arcpy.env.scratchWorkspace) arcpy.PolylineToRaster_conversion(lines, "ORIG_FID", r_lines, cellsize=dem) r_poly = arcpy.CreateScratchName("rpoly", data_type="RasterDataset", workspace=arcpy.env.scratchWorkspace) arcpy.PolygonToRaster_conversion(poly, arcpy.Describe(poly).OIDFieldName, r_poly, cellsize=dem) rasterized = arcpy.sa.Con(arcpy.sa.IsNull(r_lines), arcpy.sa.SetNull(arcpy.sa.IsNull(r_poly), 1), 1) rasterized.save(os.path.join(ends_polygons_dir, rastername)) # Correcting the DEMs dem = os.path.join(dem_bridges_dir, rastername) correcteddem = arcpy.sa.Con(arcpy.sa.IsNull(rasterized), dem) correcteddem.save(os.path.join(output_folder, rastername))
def function(DEM, streamNetwork, smoothDropBuffer, smoothDrop, streamDrop, outputReconDEM): try: # Set environment variables arcpy.env.extent = DEM arcpy.env.mask = DEM arcpy.env.cellSize = DEM # Set temporary variables prefix = "recon_" streamRaster = prefix + "streamRaster" # Determine DEM cell size and OID column name size = arcpy.GetRasterProperties_management(DEM, "CELLSIZEX") OIDField = arcpy.Describe(streamNetwork).OIDFieldName # Convert stream network to raster arcpy.PolylineToRaster_conversion(streamNetwork, OIDField, streamRaster, "", "", size) # Work out distance of cells from stream distanceFromStream = EucDistance(streamRaster, "", size) # Elements within a buffer distance of the stream are smoothly dropped intSmoothDrop = Con(distanceFromStream > float(smoothDropBuffer), 0, (float(smoothDrop) / float(smoothDropBuffer)) * (float(smoothDropBuffer) - distanceFromStream)) del distanceFromStream # Burn this smooth drop into DEM. Cells in stream are sharply dropped by the value of "streamDrop" binaryStream = Con(IsNull(Raster(streamRaster)), 0, 1) reconDEMTemp = Raster(DEM) - intSmoothDrop - (float(streamDrop) * binaryStream) del intSmoothDrop del binaryStream reconDEMTemp.save(outputReconDEM) del reconDEMTemp log.info("Reconditioned DEM generated") except Exception: log.error("DEM reconditioning function failed") raise
arcpy.AddMessage("Calculated eucludian distance") arcpy.AddMessage("-------------------------") # Create update cursor for feature class rows = arcpy.UpdateCursor(river_network) for row in rows: row.river_depth = 1 row.river_cell_size = cell_size rows.updateRow(row) # Delete cursor and row objects to remove locks on the data del row del rows river_network_raster = arcpy.PolylineToRaster_conversion( river_network, "river_depth", "river_raster", '#', '#', cell_size) arcpy.AddMessage("Converted river network to raster") arcpy.AddMessage("-------------------------") river_network_cell_size1 = arcpy.PolylineToRaster_conversion( river_network, "river_cell_size", "river_raster_cell1", '#', '#', cell_size) river_network_cell_size2 = arcpy.PolylineToRaster_conversion( river_network, "river_cell_size", "river_raster_cell2", '#', '#', cell_size) arcpy.AddMessage("Converted river cell_size to raster") arcpy.AddMessage("-------------------------") #### THE WHITE BOX DECAY ALGORITHM #### # Z = E – (G/G+D))k * H Z = newly calculated grid cell elevation (m) # E = old grid cell elevation (m)
#=============================================================================== # CODING #=============================================================================== #/creation of the set of points Shape = arcpy.Describe(inFC).shapeType fieldname = [f.name for f in arcpy.ListFields(inFC)] # CopyinFC = arcpy.CopyFeatures_management(inFC, "%ScratchWorkspace%\\CopyinFC") arcpy.AddMessage("Converting inFC into raster - Step " + str(ncurrentstep) + "/" + str(nstep)) CellSize = str(arcpy.GetRasterProperties_management(FAC, "CELLSIZEX")) arcpy.env.snapRaster = FAC if str(Shape) == "Polyline": inFCtoRaster = arcpy.PolylineToRaster_conversion( inFC, fieldname[0], "%ScratchWorkspace%\\inFCtoRaster", "", "", CellSize) elif str(Shape) == "Polygon": inFCtoRaster = arcpy.PolygonToRaster_conversion( inFC, fieldname[0], "%ScratchWorkspace%\\inFCtoRaster", "MAXIMUM_AREA", "", CellSize) ncurrentstep += 1 arcpy.AddMessage("Converting raster into points - Step " + str(ncurrentstep) + "/" + str(nstep)) RasterToPts = arcpy.RasterToPoint_conversion( inFCtoRaster, "%ScratchWorkspace%\\RasterToPts", "") #/extraction of the number of cells drained into each points ncurrentstep += 1 arcpy.AddMessage("Extracting FlowAcc values into a point shapefile - Step " +
tmpdir = os.path.join( os.path.dirname(os.path.dirname(outras)), 'tmp_{}'.format(str(os.path.basename(outras)))) os.mkdir(tmpdir) arcpy.env.scratchWorkspace = tmpdir #Rasterize print(selexpr) arcpy.MakeFeatureLayer_management(NTMsplitdiss, 'transit_lyr', where_clause=selexpr) tmplyr = os.path.join(tmpdir, 'transit{}.shp'.format(tile)) arcpy.CopyFeatures_management('transit_lyr', tmplyr) arcpy.PolylineToRaster_conversion( tmplyr, value_field='SUM_NTM_routes_selproj_split_adjustnum_int'[0:10], out_rasterdataset=outras, cellsize=restemplate) except: traceback.print_exc() try: if arcpy.Exists(tmplyr): arcpy.Delete_management(tmplyr) os.rmdir(tmpdir) except: pass #Remove intermediate products print('Deleting scratch workspace...') arcpy.Delete_management(tmplyr)
waterbody_albers = os.path.join(scratch, "Waterbody_Albers.shp") # Make a shapefile from NHDFlowline and project to EPSG 102039 arcpy.FeatureClassToShapefile_conversion("NHDFlowline", scratch) flowline_nad83 = os.path.join(scratch, "NHDFlowline.shp") arcpy.Project_management(flowline_nad83, os.path.join(scratch, "Flowline_Albers.shp"), nad83, "", albers) flowline_albers = os.path.join(scratch, "Flowline_Albers.shp") # Add CSI field to flowline_albers and waterbody_albers then calculate unique identifiers for features. arcpy.AddField_management(flowline_albers, "CSI", "TEXT") arcpy.CalculateField_management(flowline_albers, "CSI", '''"%s%s" % ("Flowline", !FID!)''', "PYTHON") arcpy.AddField_management(waterbody_albers, "CSI", "TEXT") arcpy.CalculateField_management(waterbody_albers, "CSI", '''"%s%s" % ("Waterbody", !FID!)''', "PYTHON") # Rasterize flowline_albers and waterbody_albers using "CSI" field for the raster's cell value arcpy.PolylineToRaster_conversion(flowline_albers, "CSI", os.path.join(scratch, "flowline_raster.tif"), "", "", 10) arcpy.PolygonToRaster_conversion(waterbody_albers, "CSI", os.path.join(scratch, "waterbody_raster.tif"), "", "", 10) flowline_raster = os.path.join(scratch, "flowline_raster.tif") waterbody_raster = os.path.join(scratch, "waterbody_raster.tif") # Switch to scratch workspace env.workspace = scratch # Mosaic the rasters together favoring waterbodies over flowlines. arcpy.MosaicToNewRaster_management("flowline_raster.tif;waterbody_raster.tif", scratch, "seeds_mosaic.tif", albers, "32_BIT_FLOAT", "10", "1", "LAST", "LAST")
row1[4] = 1 upcursor1.updateRow(row1) bls = arcpy.PolygonToRaster_conversion(rocks, "blshape", "blshape.tif", "CELL_CENTER") d1 = arcpy.PolygonToRaster_conversion(rocks, "d1", "d1.tif", "CELL_CENTER") d2 = arcpy.PolygonToRaster_conversion(rocks, "d2", "d2.tif", "CELL_CENTER") d3 = arcpy.PolygonToRaster_conversion(rocks, "d3", "d3.tif", "CELL_CENTER") dens = arcpy.PolygonToRaster_conversion(rocks, "density", "density.tif", "CELL_CENTER") if rock_net == "#" or not rock_net: arcpy.AddMessage("No ROCK_NET file") else: rn_number = arcpy.PolylineToRaster_conversion(rock_net, "net_number", "net_number.tif", "MAXIMUM_LENGTH") rn_energy = arcpy.PolylineToRaster_conversion(rock_net, "net_energy", "net_energy.tif", "MAXIMUM_LENGTH") rn_height = arcpy.PolylineToRaster_conversion(rock_net, "net_height", "net_height.tif", "MAXIMUM_LENGTH") resampled_dem = arcpy.Resample_management(dem, "dem_r.tif", cellsize, "CUBIC") arcpy.env.workspace = folder arcpy.env.extent = dem arcpy.env.cellSize = cellsize arcpy.AddMessage("CREATING ASCII Files......")
tolerance = (2.0**0.5)*float(restemplate.getOutput(0))/2 arcpy.env.workspace = os.path.dirname(soundtransit) ExplodeOverlappingLines(PStransitbus_splitdiss, tolerance) #For each set of non-overlapping lines, create its own raster tilef = 'expl' tilelist = list(set([row[0] for row in arcpy.da.SearchCursor(PStransitbus_splitdiss, [tilef])])) outras_base = os.path.join(rootdir, 'results/transit.gdb/busnum_') arcpy.env.snapRaster = template_ras for tile in tilelist: outras = outras_base + str(tile) if not arcpy.Exists(outras): selexpr = '{0} = {1}'.format(tilef, tile) print(selexpr) arcpy.MakeFeatureLayer_management(PStransitbus_splitdiss, 'bus_lyr', where_clause= selexpr) arcpy.PolylineToRaster_conversion('bus_lyr', value_field='SUM_PStransit_busroutes_proj_split_adjustnum_int', out_rasterdataset=outras, cellsize=restemplate) #Mosaic to new raster arcpy.env.workspace = os.path.split(outras_base)[0] transitras_tiles = arcpy.ListRasters('busnum_*') arcpy.MosaicToNewRaster_management(transitras_tiles, arcpy.env.workspace, os.path.split(PStransitras)[1], pixel_type='32_BIT_UNSIGNED', number_of_bands= 1, mosaic_method = 'SUM') for tile in transitras_tiles: print('Deleting {}...'.format(tile)) arcpy.Delete_management(tile) arcpy.ClearEnvironment('Workspace') #---------------------------------------------------------------------------------------------------------------------- # PREPARE DATA ON ROAD GRADIENTS (layers generated for initial sampling) #----------------------------------------------------------------------------------------------------------------------- #Run on NED19 max-min for line itself
if not os.path.exists(filegdb): arcpy.CreateFileGDB_management(workingDir, "NHD_natural_" + str(FID) + ".gdb") NHD = filegdb + "/" + "NHD_" + str(FID) arcpy.Clip_analysis(NHDshp, boundary, NHD) #Test to see if any features are present featureCount = int(arcpy.GetCount_management(NHD).getOutput(0)) if featureCount > 0: ########################################################################################### #Convert NHD to raster ########################################################################################### NHDRast = "WBDHU8_" + str(FID) + "_" + str( resolution) + "m_Drainage_Network_NHD_notsnapped_natural.img" arcpy.PolylineToRaster_conversion(NHD, "Order_", NHDRast) ########################################################################################### #Get accumulation raster ########################################################################################### #Fill DEM DEMfill = arcpy.sa.Fill(DEM) #Get flow direction raster flowdr = arcpy.sa.FlowDirection(DEMfill) #Get flow accumulation raster flowaccum = arcpy.sa.FlowAccumulation(flowdr) flowaccum.save("WBDHU8_Albers_" + str(FID) + "_" + str(resolution) + "m_dem_Flow_Accumulation_natural.img")
arcpy.AddMessage('Rasterizing estuary polygons...') rd_Estuary = scratchGDB + os.sep + 'rdEstuary' + huc4 try: arcpy.PolygonToRaster_conversion(EstuaryPolys, 'Burn', rd_Estuary, "MAXIMUM_COMBINED_AREA", 'None', inSnap) raster_list.append(rd_Estuary) except: arcpy.AddMessage('There are no estuary polygons to rasterize') #Rasterize the streams arcpy.AddMessage('Rasterizing streams...') rd_Streams = scratchGDB + os.sep + 'rdStreams' + huc4 try: arcpy.PolylineToRaster_conversion(burnStreams, 'Burn', rd_Streams, "MAXIMUM_COMBINED_LENGTH", 'None', inSnap) raster_list.append(rd_Streams) except: arcpy.AddMessage('There are no streams to rasterize') #Rasterize the non-stream flowpaths arcpy.AddMessage('Rasterizing non-stream flowpaths...') rd_FlowPaths = scratchGDB + os.sep + 'rdFlowPaths' + huc4 try: arcpy.PolylineToRaster_conversion(burnFlowPaths, 'Burn', rd_FlowPaths, "MAXIMUM_COMBINED_LENGTH", 'None', inSnap) raster_list.append(rd_FlowPaths) except:
## Identify nhdflowlines within NHDArea polygons print "Intersecting nhdflowlines with NHD area polygons" arcpy.Intersect_analysis([finalArea, NHDStream], streaminAreaTemp, "#", "#", "line") # Remove coastline arcs arcpy.MakeFeatureLayer_management(streaminAreaTemp, "StreamInAreaTemp") arcpy.SelectLayerByAttribute_management("StreamInAreaTemp", "NEW_SELECTION", expression4) arcpy.CopyFeatures_management("StreamInAreaTemp", streaminArea) ext = Raster(boundaryRaster).extent arcpy.env.extent = ext arcpy.CheckOutExtension("Spatial") ## Convert shapefiles/layers to 30m rasters arcpy.PolylineToRaster_conversion(NHDStream, "BUFFID", streamRaster, "MAXIMUM_LENGTH", "NONE", 30) print "NHDStream to raster" arcpy.PolylineToRaster_conversion(streaminArea, "BUFFID", strminArea, "MAXIMUM_LENGTH", "NONE", 30) arcpy.BuildRasterAttributeTable_management(strminArea, "Overwrite") print "streaminNHDArea to raster" arcpy.PolygonToRaster_conversion(finalArea, "FID", nhdarea, "CELL_CENTER", "", 30) print "NHDArea to raster" arcpy.PolygonToRaster_conversion(NHDLake2, "BUFFID", nhdlake, "CELL_CENTER", "", 30) print "NHD Waterbodies to raster" ## Run cost allocation function to assign reach ID values to NHDArea river polygons costArea = Con(nhdarea, 1, "", "VALUE > -1") costArea.save(costarea)
def prepare_streams(dmt, dmt_copy, mat_dmt_fill, null, mat_nan, mat_fd, vpix, spix, rows, cols, ll_corner, NoDataValue, addfield, delfield, output, dmt_clip, intersect, null_shp, gp): # creating the geoprocessor object gp = arcgisscripting.create() # setting the workspace environment gp.workspace = gp.GetParameterAsText( constants.PARAMETER_PATH_TO_OUTPUT_DIRECTORY) # Overwriting output arcpy.env.overwriteOutput = 1 # Check extensions arcpy.CheckOutExtension("3D") arcpy.CheckOutExtension("Spatial") # SET INPUT: # Set input stream = gp.GetParameterAsText(constants.PARAMETER_STREAM) # dmt = data_preparation.dmt # dmt_copy = data_preparation.dmt_copy # mat_dmt = data_preparation.mat_dmt_fill # mat_nan = data_preparation.mat_nan # mat_fd = data_preparation.mat_fd # vpix = data_preparation.vpix # spix = data_preparation.spix # rows = data_preparation.rows # cols = data_preparation.cols # gp = data_preparation.gp # ll_corner = data_preparation.ll_corner # NoDataValue = data_preparation.NoDataValue # addfield = data_preparation.addfield # delfield = data_preparation.delfield STREAM_RATIO = 1 # cropped raster info dmt_desc = arcpy.Describe(dmt_copy) # lower left corner coordinates x_coordinate = dmt_desc.extent.XMin y_coordinate = dmt_desc.extent.YMin arcpy.env.snapRaster = dmt # Set output # output = data_preparation.output temp_dp = output + os.sep + "temp_dp" if not os.path.exists(temp_dp): os.makedirs(temp_dp) tempgdb_dp = arcpy.CreateFileGDB_management(temp_dp, "temp_dp.gdb") # WATER FLOWS ACCORDING DMT: # dmt_clip = data_preparation.dmt_clip dmt_fill, flow_direction, flow_accumulation, slope = arcgis_dmtfce.dmtfce( dmt_clip, temp_dp, "TRUE", "TRUE", "NONE") # Setnull try: setnull = arcpy.sa.SetNull(flow_accumulation, 1, "VALUE < 300") # hodnota value?? setnull.save(temp_dp + os.sep + "setnull") except: prt.message("Unexepted error during setnull calculation:", sys.exc_info()[0]) raise # Stream to feature """try: toky_dmt = arcpy.sa.StreamToFeature(setnull, flow_direction, temp_dp+os.sep+"toky_dmt", "SIMPLIFY") except: prt.message("Unexepted error during stream to future calculation:", sys.exc_info()[0]) raise""" # WATER FLOWS ACCORDING DIBAVOD: # Clip toky = temp_dp + os.sep + "toky.shp" tokyLoc = temp_dp + os.sep + "toky.shp" # intersect = data_preparation.intersect hranice = temp_dp + os.sep + "hranice.shp" # null = data_preparation.null_shp hranice = arcpy.Clip_analysis(null, intersect, hranice) hranice2 = arcpy.Buffer_analysis(hranice, temp_dp + os.sep + "hranice2.shp", -spix / 3, "FULL", "ROUND", "NONE") # hranice2 = hranice toky = arcpy.Clip_analysis(stream, hranice2, toky) arcpy.DeleteField_management(toky, [ "EX_JH", "POZN", "PRPROP_Z", "IDVT", "UTOKJ_ID", "UTOKJN_ID", "UTOKJN_F" ]) # Feature vertices to points - START prt.message("Feature vertices to points - START...") start = arcpy.FeatureVerticesToPoints_management( toky, temp_dp + os.sep + "start", "START") # Feature vertices to points - END prt.message("Feature vertices to points - END...") end = arcpy.FeatureVerticesToPoints_management(toky, temp_dp + os.sep + "end", "END") # Extract value to points - END prt.message("Extract value to points - END...") xxx = temp_dp + os.sep + "end_point" end_point = arcpy.sa.ExtractValuesToPoints(end, dmt_clip, xxx, "NONE", "VALUE_ONLY") # Extract value to points - START arcpy.AddMessage("Extract value to points - START...") xxx = temp_dp + os.sep + "start_point" start_point = arcpy.sa.ExtractValuesToPoints(start, dmt_clip, xxx, "NONE", "VALUE_ONLY") # Join arcpy.JoinField_management(toky, "FID", start_point, "ORIG_FID") arcpy.JoinField_management(toky, "FID", end_point, "ORIG_FID") arcpy.DeleteField_management(toky, [ "SHAPE_LEN", "SHAPE_LENG", "SHAPE_LE_1", "NAZ_TOK_1", "TOK_ID_1", "SHAPE_LE_2", "SHAPE_LE_3", "NAZ_TOK_12", "TOK_ID_12", "SHAPE_LE_4", "ORIG_FID_1" ]) # Flip selected lines fc = temp_dp + os.sep + "toky.shp" field = ["OBJECTID", "RASTERVALU", "RASTERVA_1"] prt.message("Flip lines...") # mat_tok_usek # jj tu se zamkne toky a zustane zamcen... toky_t = arcpy.MakeFeatureLayer_management(toky, temp_dp + os.sep + "tok_t.shp") arcpy.SelectLayerByAttribute_management(toky_t, "NEW_SELECTION", "RASTERVALU < RASTERVA_1") arcpy.FlipLine_edit(toky_t) # arcpy.DeleteField_management(toky_t, # ["RASTERVALU","RASTERVA_1","ORIG_FID","ORIG_FID_1"]) arcpy.DeleteField_management( toky, ["RASTERVALU", "RASTERVA_1", "ORIG_FID", "ORIG_FID_1"]) # Feature vertices to points - START prt.message("Feature vertices to points - START...") start = arcpy.FeatureVerticesToPoints_management( toky, temp_dp + os.sep + "start", "START") # Feature vertices to points - END prt.message("Feature vertices to points - END...") end = arcpy.FeatureVerticesToPoints_management(toky, temp_dp + os.sep + "end", "END") # Extract value to points - START prt.message("Extract value to points - START...") start_point_check = arcpy.sa.ExtractValuesToPoints( start, dmt, temp_dp + os.sep + "start_point_check", "NONE", "VALUE_ONLY") arcpy.AddXY_management(start_point_check) # Extract value to points - END prt.message("Extract value to points - END...") end_point_check = arcpy.sa.ExtractValuesToPoints( end, dmt, temp_dp + os.sep + "end_point_check", "NONE", "VALUE_ONLY") arcpy.AddXY_management(end_point_check) # Join A = arcpy.JoinField_management(toky, "FID", start_point_check, "ORIG_FID") B = arcpy.JoinField_management(toky, "FID", end_point_check, "ORIG_FID") arcpy.DeleteField_management( toky, ["NAZ_TOK_1", "NAZ_TOK_12", "TOK_ID_1", "TOK_ID_12"]) fc = toky field = ["FID", "RASTERVALU", "POINT_X", "RASTERVA_1", "POINT_X_1"] with arcpy.da.SearchCursor(fc, field) as cursor: for row in cursor: if row[1] > row[3]: continue else: prt.message("Flip line") arcpy.FlipLine_edit(fc) addfield(toky, "to_node", "DOUBLE", -9999) fc = toky field_end = [ "FID", "POINT_X", "POINT_Y", "POINT_X_1", "POINT_Y_1", "to_node" ] field_start = [ "FID", "POINT_X", "POINT_Y", "POINT_X_1", "POINT_Y_1", "to_node" ] with arcpy.da.SearchCursor(fc, field_start) as cursor_start: for row in cursor_start: a = (row[1], row[2]) d = row[0] with arcpy.da.UpdateCursor(fc, field_end) as cursor_end: for row in cursor_end: b = (row[3], row[4]) if a == b: row[5] = d cursor_end.updateRow(row) else: row[5] = "-9999" arcpy.DeleteField_management(toky, [ "SHAPE_LEN", "SHAPE_LE_1", "SHAPE_LE_2", "SHAPE_LE_3", "SHAPE_LE_4", "SHAPE_LE_5", "SHAPE_LE_6", "SHAPE_LE_7", "SHAPE_LE_8", "SHAPE_LE_9", "SHAPE_L_10", "SHAPE_L_11", "SHAPE_L_12", "SHAPE_L_13", "SHAPE_L_14" ]) arcpy.DeleteField_management(toky, ["ORIG_FID", "ORIG_FID_1", "SHAPE_L_14"]) stream_rst1 = temp_dp + os.sep + "stream_rst" stream_rst = arcpy.PolylineToRaster_conversion(toky, "FID", stream_rst1, "MAXIMUM_LENGTH", "NONE", spix) tok_usek = temp_dp + os.sep + "tok_usek" arcpy.gp.Reclassify_sa( stream_rst, "VALUE", "NoDataValue 1000", tok_usek, "DATA") # jj no data 1000 kdyz in useku bude od 10000 mat_tok_usek = arcpy.RasterToNumPyArray(temp_dp + os.sep + "tok_usek", ll_corner, cols, rows) mat_tok = arcpy.RasterToNumPyArray(temp_dp + os.sep + "tok_usek", ll_corner, cols, rows) # hit = arcpy.NumPyArrayToRaster(mat_tok_usek,ll_corner,spix) # hit.save(temp_dp+"\\hit") pocet = len(mat_tok_usek) e = range(pocet) # useky toku + posledni NoData cell_stream = [] # cropped raster info reclass_desc = arcpy.Describe(tok_usek) ReclNoDataValue = reclass_desc.noDataValue mat_tok_usek = mat_tok_usek.astype('int16') for i in range(rows): for j in range(cols): if mat_tok_usek[i][j] < 0: mat_tok_usek[i][j] = 0 else: mat_tok_usek[i][j] += 1000 poz = [mat_tok_usek[i][j], i, j] cell_stream.append(poz) for i in range(rows): for j in range(cols): if mat_tok[i][j] != 255: mat_tok[i][j] = 3 else: continue # hit1 = arcpy.NumPyArrayToRaster(mat_tok,ll_corner,spix) # hit1.save(temp_dp+"\\hit1") mat_nan = arcpy.NumPyArrayToRaster(mat_nan, ll_corner, spix) mat_nan.save(temp_dp + os.sep + "mat_nan") # kavka - proc se tu uklada tady # HYDRAULIKA TOKU addfield(toky, "length", "DOUBLE", 0.0) # (m) # addfield(toky,"vegetace","TEXT", "trava") #tabulka pro drsnosti, vstupem # bude tabulka suseky toku - tvar a opevneni(vegetace) ci rovnou drsnost # na tvrdo? addfield(toky, "sklon", "DOUBLE", 0.0) # (-) # addfield(toky,"drsnost","DOUBLE", 0) addfield(toky, "V_infl_ce", "DOUBLE", 0.0) # (m3) addfield(toky, "V_infl_us", "DOUBLE", 0.0) # (m3) addfield(toky, "V_infl", "DOUBLE", 0.0) # (m3) addfield(toky, "Q_outfl", "DOUBLE", 0.0) # (m3/s) addfield(toky, "V_outfl", "DOUBLE", 0.0) # (m3) addfield(toky, "V_outfl_tm", "DOUBLE", 0.0) # (m3) addfield(toky, "V_zbyt", "DOUBLE", 0.0) # (m3) addfield(toky, "V_zbyt_tm", "DOUBLE", 0.0) # (m3) addfield(toky, "V", "DOUBLE", 0.0) # (m3) addfield(toky, "h", "DOUBLE", 0.0) # (m) addfield(toky, "vs", "DOUBLE", 0.0) # (m/s) addfield(toky, "NS", "DOUBLE", 0.0) # (m) addfield(toky, "total_Vic", "DOUBLE", 0.0) # (m3) addfield(toky, "total_Viu", "DOUBLE", 0.0) # (m3) addfield(toky, "max_Q", "DOUBLE", 0.0) # (m3/s) addfield(toky, "max_h", "DOUBLE", 0.0) # (m) addfield(toky, "max_vs", "DOUBLE", 0.0) # (m/s) addfield(toky, "total_Vo", "DOUBLE", 0.0) # (m3) addfield(toky, "total_Vi", "DOUBLE", 0.0) # (m3) addfield(toky, "total_NS", "DOUBLE", 0.0) # (m3) addfield(toky, "total_Vz", "DOUBLE", 0.0) # (m3) # sklon fc = toky field = [ "FID", "RASTERVALU", "RASTERVA_1", "sklon", "SHAPE@LENGTH", "length" ] with arcpy.da.UpdateCursor(fc, field) as cursor: for row in cursor: sklon_koryta = (row[1] - row[2]) / row[4] if sklon_koryta == 0: raise ZeroSlopeError(row(0)) row[3] = sklon_koryta cursor.updateRow(row) row[5] = row[4] cursor.updateRow(row) # tvar koryt tab_stream_tvar = gp.GetParameterAsText(constants.PARAMETER_STREAMTABLE) tab_stream_tvar_code = gp.GetParameterAsText( constants.PARAMETER_STREAMTABLE_CODE) stream_tvar_dbf = temp_dp + os.sep + "stream_tvar.dbf" arcpy.CopyRows_management(tab_stream_tvar, stream_tvar_dbf) sfield = ["cislo", "smoderp", "tvar", "b", "m", "drsnost", "Q365"] try: arcpy.JoinField_management(toky, tab_stream_tvar_code, stream_tvar_dbf, tab_stream_tvar_code, "cislo;tvar;b;m;drsnost;Q365") except: arcpy.AddField_management(toky, "smoderp", "TEXT") arcpy.CalculateField_management(toky, "smoderp", "0", "PYTHON") arcpy.JoinField_management(toky, tab_stream_tvar_code, stream_tvar_dbf, tab_stream_tvar_code, "cislo;tvar;b;m;drsnost;Q365") with arcpy.da.SearchCursor(toky, sfield) as cursor: for row in cursor: for i in range(len(row)): if row[i] == " ": prt.message( "Value in tab_stream_tvar are no correct - STOP, check shp file toky in output" ) sys.exit() fields = arcpy.ListFields(toky) # field_names = [field.name for field in fields if field.type != # 'Geometry'] field_names = [field.name for field in fields] toky_tmp = [[] for field in fields] for row in arcpy.SearchCursor(toky): field_vals = [row.getValue(field) for field in field_names] # field_vals for i in range(len(field_vals)): toky_tmp[i].append(field_vals[i]) del row # all columns names in """[u'FID', u'Shape', u'Id', u'Id_1', u'Id_12', u'Id_12_13', u'Id_12_1_14', u'Id_12_1_15', u'RASTERVALU', u'POINT_X', u'POINT_Y', u'Id_12_1_16', u'Id_12_1_17', u'Id_12_1_18', u'RASTERVA_1', u'POINT_X_1', u'POINT_Y_1', u'to_node', u'length', u'sklon', u'V_infl_ce', u'V_infl_us', u'V_infl', u'Q_outfl', u'V_outfl', u'V_outfl_tm', u'V_zbyt', u'V_zbyt_tm', u'V', u'h', u'vs', u'NS', u'total_Vic', u'total_Viu', u'max_Q', u'max_h', u'max_vs', u'total_Vo', u'total_Vi', u'total_NS', u'total_Vz', u'smoderp', u'CISLO', u'TVAR', u'B', u'M', u'DRSNOST', u'Q365']""" tokylist = [] # Kubuv vyber tokylist.append(toky_tmp[field_names.index('FID')]) tokylist.append(toky_tmp[field_names.index('POINT_X')]) tokylist.append(toky_tmp[field_names.index('POINT_Y')]) tokylist.append(toky_tmp[field_names.index('POINT_X_1')]) tokylist.append(toky_tmp[field_names.index('POINT_Y_1')]) tokylist.append(toky_tmp[field_names.index('to_node')]) tokylist.append(toky_tmp[field_names.index('length')]) tokylist.append(toky_tmp[field_names.index('sklon')]) try: tokylist.append(toky_tmp[field_names.index('smoderp')]) except ValueError: tokylist.append(toky_tmp[field_names.index('SMODERP')]) try: tokylist.append(toky_tmp[field_names.index('cislo')]) except ValueError: tokylist.append(toky_tmp[field_names.index('CISLO')]) try: tokylist.append(toky_tmp[field_names.index('tvar')]) except ValueError: tokylist.append(toky_tmp[field_names.index('TVAR')]) try: tokylist.append(toky_tmp[field_names.index('b')]) except ValueError: tokylist.append(toky_tmp[field_names.index('B')]) try: tokylist.append(toky_tmp[field_names.index('m')]) except ValueError: tokylist.append(toky_tmp[field_names.index('M')]) try: tokylist.append(toky_tmp[field_names.index('drsnost')]) except ValueError: tokylist.append(toky_tmp[field_names.index('DRSNOST')]) try: tokylist.append(toky_tmp[field_names.index('q365')]) except ValueError: tokylist.append(toky_tmp[field_names.index('Q365')]) return tokylist, cell_stream, mat_tok_usek, STREAM_RATIO, tokyLoc
def genpolytoraster(inlayer, field, outlayer, stream=False, um=False, chap=False, fire=False, flowlinePA=False, flowlineIE=False, flowlineI=False, road=False): """ inlayer and outlayer need to be in quotes EVeg full path is: Y:\Tahoe\GISdata\Scratch3.gdb\EVeg_070813_1545 Field for streams should be StrOrder_Grp Field for Aspen as cover (to get one type) is NRCS_PLANT_CODE Field for Meadow (to get one type) is STATE Geology layer should be preprocessed to match extent of project area Field for Geology100k ["Y:/Tahoe/GISdata/WorkGDBCreated051214.gdb/Geology_0523_1817"] is ORIG_LABEL Field for Condition is ConditionClass2 Field for Aspen as condition is "CondClass" Field for Chaparral is "AYHR10" """ # set environment settings arcpy.env.snapRaster = "Y:/Tahoe/GISdata/Lattice_Clip30m.gdb/Lattice_Clip30m_ProjBound" arcpy.env.extent = "Y:/Tahoe/GISdata/Lattice_Clip30m.gdb/Lattice_Clip30m_ProjBound" # Set local variables valField = field outPath = "Y:/Tahoe/GISdata/WorkGDBCreated051214.gdb/" outRaster = outPath + outlayer assignmentType = "MAXIMUM_AREA" cellSize = 30 if stream: assignmentType = "MAXIMUM_LENGTH" arcpy.MakeFeatureLayer_management(inlayer, "featurelayer") rule1 = """ "StrOrder_Grp" = '1-large' """ arcpy.SelectLayerByAttribute_management("featurelayer", "NEW_SELECTION", rule1) arcpy.PolylineToRaster_conversion(in_features="featurelayer", value_field=valField, out_rasterdataset=outRaster, cell_assignment=assignmentType, cellsize=cellSize) arcpy.Delete_management("featurelayer") if um: # background processing may need to be turned off for this to work arcpy.MakeFeatureLayer_management(inlayer, "featurelayer") rule2 = """ "ORIG_LABEL" = 'um' """ arcpy.SelectLayerByAttribute_management("featurelayer", "NEW_SELECTION", rule2) print "selection complete" arcpy.PolygonToRaster_conversion(in_features="featurelayer", value_field=valField, out_rasterdataset=outRaster, cell_assignment=assignmentType, cellsize=cellSize) print "conversion complete" arcpy.Delete_management("featurelayer") print "deleted featurelayer" if chap: arcpy.MakeFeatureLayer_management(inlayer, "featurelayer") rule = """ "REGIONAL_DOMINANCE_TYPE_1" In ('BX','CL','CQ','CS','CW','KP','MN','CH','CI','CG','CM','CP','CV','CX','CY') """ arcpy.SelectLayerByAttribute_management("featurelayer", "NEW_SELECTION", rule) arcpy.PolygonToRaster_conversion(in_features="featurelayer", value_field=valField, out_rasterdataset=outRaster, cell_assignment=assignmentType, cellsize=cellSize) arcpy.Delete_management("featurelayer") if fire: arcpy.MakeFeatureLayer_management(inlayer, "featurelayer") rule = """ "BURNSEV" = 4 AND "BEST_ASSESS" = 'YES' """ arcpy.SelectLayerByAttribute_management("featurelayer", "NEW_SELECTION", rule) arcpy.PolygonToRaster_conversion(in_features="featurelayer", value_field=valField, out_rasterdataset=outRaster, cell_assignment=assignmentType, cellsize=cellSize) arcpy.Delete_management("featurelayer") if flowlinePA: assignmentType = "MAXIMUM_LENGTH" arcpy.MakeFeatureLayer_management(inlayer, "featurelayer") rule1 = """ "FCode" In (46006, 55800) """ arcpy.SelectLayerByAttribute_management("featurelayer", "NEW_SELECTION", rule1) arcpy.PolylineToRaster_conversion(in_features="featurelayer", value_field=valField, out_rasterdataset=outRaster, cell_assignment=assignmentType, cellsize=cellSize) arcpy.Delete_management("featurelayer") if flowlineIE: assignmentType = "MAXIMUM_LENGTH" arcpy.MakeFeatureLayer_management(inlayer, "featurelayer") rule1 = """ "FCode" In (46003,46007) """ arcpy.SelectLayerByAttribute_management("featurelayer", "NEW_SELECTION", rule1) arcpy.PolylineToRaster_conversion(in_features="featurelayer", value_field=valField, out_rasterdataset=outRaster, cell_assignment=assignmentType, cellsize=cellSize) arcpy.Delete_management("featurelayer") if flowlineI: assignmentType = "MAXIMUM_LENGTH" arcpy.MakeFeatureLayer_management(inlayer, "featurelayer") rule1 = """ "FCode" = 46003 """ arcpy.SelectLayerByAttribute_management("featurelayer", "NEW_SELECTION", rule1) arcpy.PolylineToRaster_conversion(in_features="featurelayer", value_field=valField, out_rasterdataset=outRaster, cell_assignment=assignmentType, cellsize=cellSize) arcpy.Delete_management("featurelayer") if road: # use field 'CREATED_BY' assignmentType = "MAXIMUM_LENGTH" arcpy.PolylineToRaster_conversion(in_features=inlayer, value_field=valField, out_rasterdataset=outRaster, cell_assignment=assignmentType, cellsize=cellSize) #arcpy.Delete_management("featurelayer") else: arcpy.PolygonToRaster_conversion(in_features=inlayer, value_field=valField, out_rasterdataset=outRaster, cell_assignment=assignmentType, cellsize=cellSize)
# Set the study area extent using a shapefile study_area = arcpy.GetParameterAsText(7) study_area_limits = extents(study_area) arcpy.env.extent = ' '.join([str(value) for value in study_area_limits]) raster_list = list() pix_to_point_list = list() delta = int(max_box) while i >= 0: # The name of the raster generated in each step is its 'delta' value. It is saved in the output directory output_raster = os.path.join(output_dir, str(delta) + 'm.tif') raster_list.append(output_raster) if desc_input.shapeType == 'Point': arcpy.PointToRaster_conversion(input, fld, output_raster, 'MOST_FREQUENT', '', delta) if desc_input.shapeType == 'Polyline': arcpy.PolylineToRaster_conversion(input, fld, output_raster, 'MAXIMUM_LENGTH', '', delta) pixel_to_point = arcpy.RasterToPoint_conversion(output_raster, os.path.join(output_dir, str(delta) + 'm')) pix_to_point_list.append(pixel_to_point) delta = delta / 2 i = i - 1 # Duplicates the input for second largest box in order to compute the box count for the largest box size pix_to_point_list[0] = pix_to_point_list[1] # Creates the output point shapefile. The automatically created field ('Id') stores a number that identifies the box where the couting was done arcpy.CreateFeatureclass_management(os.path.dirname(output_shape), os.path.basename(output_shape), 'POINT', spatial_reference=study_area) # Stores the box size for a given count, in a given box and box size arcpy.AddField_management(output_shape, 'box_size', 'DOUBLE') # Stores the box count for a given count, in a given box and box size
def polylineToRaster(inputpath, outputpath): arcpy.PolylineToRaster_conversion(inputpath, "valuefield", outputpath, "MAXIMUM_COMBINED_LENGTH", "valuefield", "1000")
def shp_to_raster(shp, inSource, cellsize, nodata, outRaster, epsg=None, rst_template=None, api='gdal', snap=None): """ Feature Class to Raster cellsize will be ignored if rst_template is defined * API's Available: - gdal; - arcpy; - pygrass; - grass; """ if api == 'gdal': from osgeo import gdal, ogr from gasp.prop.ff import drv_name if not epsg: from gasp.prop.prj import get_shp_sref srs = get_shp_sref(shp).ExportToWkt() else: from gasp.prop.prj import epsg_to_wkt srs = epsg_to_wkt(epsg) # Get Extent dtShp = ogr.GetDriverByName( drv_name(shp)).Open(shp, 0) lyr = dtShp.GetLayer() if not rst_template: x_min, x_max, y_min, y_max = lyr.GetExtent() x_res = int((x_max - x_min) / cellsize) y_res = int((y_max - y_min) / cellsize) else: from gasp.fm.rst import rst_to_array img_temp = gdal.Open(rst_template) geo_transform = img_temp.GetGeoTransform() y_res, x_res = rst_to_array(rst_template).shape # Create output dtRst = gdal.GetDriverByName(drv_name(outRaster)).Create( outRaster, x_res, y_res, gdal.GDT_Byte ) if not rst_template: dtRst.SetGeoTransform((x_min, cellsize, 0, y_max, 0, -cellsize)) else: dtRst.SetGeoTransform(geo_transform) dtRst.SetProjection(str(srs)) bnd = dtRst.GetRasterBand(1) bnd.SetNoDataValue(nodata) gdal.RasterizeLayer(dtRst, [1], lyr, burn_values=[1]) del lyr dtShp.Destroy() elif api == 'arcpy': import arcpy if rst_template: tempEnvironment0 = arcpy.env.extent arcpy.env.extent = template if snap: tempSnap = arcpy.env.snapRaster arcpy.env.snapRaster = snap obj_describe = arcpy.Describe(shp) geom = obj_describe.ShapeType if geom == u'Polygon': arcpy.PolygonToRaster_conversion( shp, inField, outRaster, "CELL_CENTER", "NONE", cellsize ) elif geom == u'Polyline': arcpy.PolylineToRaster_conversion( shp, inField, outRaster, "MAXIMUM_LENGTH", "NONE", cellsize ) if rst_template: arcpy.env.extent = tempEnvironment0 if snap: arcpy.env.snapRaster = tempSnap elif api == 'grass' or api == 'pygrass': """ Vectorial geometry to raster If source is None, the convertion will be based on the cat field. If source is a string, the convertion will be based on the field with a name equal to the given string. If source is a numeric value, all cells of the output raster will have that same value. """ __USE = "cat" if not inSource else "attr" if type(inSource) == str or \ type(inSource) == unicode else "val" if type(inSource) == int or \ type(inSource) == float else None if not __USE: raise ValueError('\'source\' parameter value is not valid') if api == 'pygrass': from grass.pygrass.modules import Module m = Module( "v.to.rast", input=shp, output=outRaster, use=__USE, attribute_column=inSource if __USE == "attr" else None, value=inSource if __USE == "val" else None, overwrite=True, run_=False, quiet=True ) m() else: from gasp import exec_cmd rcmd = exec_cmd(( "v.to.rast input={} output={} use={}{} " "--overwrite --quiet" ).format( shp, outRaster, __USE, "" if __USE == "cat" else " attribute_column={}".format(inSource) \ if __USE == "attr" else " val={}".format(inSource) )) else: raise ValueError('API {} is not available'.format(api)) return outRaster
time.sleep(5) arcpy.AddMessage('>> Created fishnet in tmp gdb') 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
streamlink=StreamLink (rivg, flowdir) if arcpy.Exists(path+"/output.gdb/"+streamfile): arcpy.Delete_management(path+"/output.gdb/"+streamfile) print('deleted previous stream file and create a new one') streamnet=path+"/output.gdb/"+streamfile print('creating stream shapefile') StreamToFeature(streamlink, flowdir, streamnet, "NO_SIMPLIFY") # Find contributing area to each cell along stream network if arcpy.Exists(rivg): arcpy.Delete_management(rivg) arcpy.PolylineToRaster_conversion (streamnet, "arcid", rivg, "MAXIMUM_LENGTH","NONE", env.cellSize) local=Watershed(flowdir, rivg, "VALUE") if arcpy.Exists("local"): arcpy.Delete_management("local") local.save("local") arcpy.AddField_management (streamnet, "local", "LONG") arcpy.JoinField_management(streamnet,"arcid",local,"Value","Count") fields=['COUNT','local'] with arcpy.da.UpdateCursor(streamnet, fields) as cursor: for row in cursor: if row[0] is None:
def BurnCutlines(output_workspace, cutlines, dem, widen_cells): # Check out the extension license arcpy.CheckOutExtension("Spatial") # Set environment variables arcpy.env.overwriteOutput = True arcpy.env.workspace = output_workspace arcpy.env.extent = dem arcpy.env.snapRaster = dem arcpy.env.cellSize = arcpy.Describe(dem).meanCellHeight arcpy.env.compression = "LZ77" arcpy.env.outputCoordinateSystem = dem # List parameter values arcpy.AddMessage("Workspace: {}".format(arcpy.env.workspace)) arcpy.AddMessage("Cutlines: " "{}".format(arcpy.Describe(cutlines).baseName)) arcpy.AddMessage("DEM: {}".format(arcpy.Describe(dem).baseName)) arcpy.AddMessage("widen_cells: {}".format(str(widen_cells))) # Determine the resolution of dem cellsize = float(arcpy.GetRasterProperties_management( dem, "CELLSIZEX").getOutput(0)) # Create a list of cutline OIDs cutlines_desc = arcpy.Describe(cutlines) cutlines_oid = cutlines_desc.OIDFieldName oidList = [] with arcpy.da.SearchCursor(cutlines, [cutlines_oid]) as cursor: for row in cursor: id = row[0] oidList.append(id) arcpy.AddMessage("Cutline OIDs: {}".format(oidList)) # Convert cutlines to raster cutline_ras = os.path.join(output_workspace, "cutline_ras") OID = arcpy.Describe(cutlines).OIDFieldName arcpy.PolylineToRaster_conversion( in_features = cutlines, value_field = OID, out_rasterdataset = cutline_ras, cellsize = cellsize) # Increase width of each cutline if int(widen_cells) > 0: arcpy.AddMessage("Expanding cutlines...") cutline_ras = arcpy.sa.Expand(cutline_ras, int(widen_cells), oidList) # Determine the minimum elevation along each cutline arcpy.AddMessage("Calculating minumum elevation for each cutline...") cutline_min = arcpy.sa.ZonalStatistics(in_zone_data = cutline_ras, zone_field = "VALUE", in_value_raster = dem, statistics_type = "MINIMUM") # Con operation to burn cutline_ext into DEM arcpy.AddMessage("Burning cutlines into DEM...") dem_hydro = arcpy.sa.Con(arcpy.sa.IsNull(cutline_min), dem, cutline_min) # Save dem_hydro to the output_workspace dem_hydro_path = os.path.join(output_workspace, "dem_hydro") arcpy.CopyRaster_management(in_raster = dem_hydro, out_rasterdataset = dem_hydro_path) arcpy.AddMessage("Saved hydro modified DEM.") # Calculate raster statistics and build pyramids arcpy.CalculateStatistics_management(dem_hydro_path) arcpy.BuildPyramids_management(dem_hydro_path) arcpy.AddMessage("Calculated raster statistics and pyramids.") # Return arcpy.SetParameter(4, dem_hydro_path) # Cleanup arcpy.Delete_management(in_data = cutline_ras)