def doDimensions(self, dFrame, lyr): # Set Up Stuff tmpDrive = "T:" tmpFGDBname = "\\tmpfgdb_" + getpass.getuser() + ".gdb" tmpFGDB = tmpDrive + tmpFGDBname dimFC = tmpFGDB + "\\DimensionsFC" tmpFC1 = tmpDrive + "\\tmpFC1.shp" #tmpFC2 = tmpFGDB + "\\tmpFC2" # Create a new temp FGDB if it doesn't already exist if not arcpy.Exists(tmpFGDB): arcpy.AddMessage("creating new " + tmpFGDB) arcpy.CreateFileGDB_management(tmpDrive, tmpFGDBname) else: # if FGDB exists, then remove all the old layers arcpy.AddMessage("Deleting old dimensions feature class if exists") delList = [dimFC, tmpFC1] for feat in delList: self.delLayer(feat) arcpy.AddMessage("finished deleting old temp feature classes") arcpy.AddMessage("converting polygon to polyline") arcpy.PolygonToLine_management(lyr, tmpFC1,"IGNORE_NEIGHBORS") #dimLayer = arcpy.mapping.Layer(dimFC) # create layer object from dimFC feat class arcpy.SplitLine_management(tmpFC1, dimFC) arcpy.AddMessage("Splitting up the lines of polyline layer") mainLyr = arcpy.mapping.ListLayers(self.mxd, "Dimensions", self.mainDF)[0] detailLyr = arcpy.mapping.ListLayers(self.mxd, "iDimensions", self.detailDF)[0] arcpy.AddMessage("updating the dimensions layer") # changing the dimensions layer source to the new one mainLyr.replaceDataSource(tmpFGDB, "FILEGDB_WORKSPACE", "DimensionsFC") detailLyr.replaceDataSource(tmpFGDB, "FILEGDB_WORKSPACE", "DimensionsFC") arcpy.AddMessage(tmpFGDB + " is my temp fgdb name") arcpy.AddMessage("Dimensions data source changed: ") #arcpy.AddMessage(upLyr.dataSource) # !!! turning off the dimensions lyr on the main data frame if dimensions was already used on the inset map # !!! otherwise dimensions is turned on for both... not ideal. if dFrame == self.detailDF: arcpy.AddMessage("inset measurements only") mainLyr.visible = False detailLyr.visible = True arcpy.AddMessage("main: " + str(mainLyr.visible)) else: arcpy.AddMessage("main map measurements only") mainLyr.visible = True detailLyr.visible = False arcpy.AddMessage("main: " + str(mainLyr.visible)) arcpy.RefreshActiveView() return lyr
def Voronoi_Lines(inFC,Output): try: #Variables temp = 'in_memory\\tempdata' temp2 = 'in_memory\\tempdata2' arcpy.FeatureVerticesToPoints_management(inFC,temp, "ALL") arcpy.CreateThiessenPolygons_analysis(temp, temp2, "ONLY_FID") arcpy.PolygonToLine_management(temp2, temp) arcpy.Intersect_analysis([temp, inFC], temp2, "ALL") arcpy.MultipartToSinglepart_management(temp2, Output) fieldNames = [] for field in arcpy.ListFields(Output): if not field.required and field.name != 'Id': fieldNames.append(field.name) arcpy.DeleteField_management(Output,fieldNames) Centerline(Output) Width(Output,inFC) Deviation(Output) except Exception,e: arcpy.AddError('%s'%(e))
def getEncroachArea(encrType,encrVals,bufferDist,impVals,impType): arcpy.gp.Reclassify_sa(NLCD_MHB, "Value", encrVals, encrType + "_mhb.tif", "NODATA") arcpy.RasterToPolygon_conversion(encrType + "_mhb.tif", encrType + "_poly_mhb.shp", "SIMPLIFY", "VALUE") arcpy.PolygonToLine_management(encrType + "_poly_mhb.shp", encrType + "_edge_mhb.shp", "IGNORE_NEIGHBORS") arcpy.Buffer_analysis(encrType + "_edge_mhb.shp", encrType + "_" + bufferDist + "m_edge_mhb.shp", bufferDist + " Meters", "FULL", "ROUND", "ALL", "") arcpy.gp.ExtractByMask_sa(NLCD_MHB, encrType + "_" + bufferDist + "m_edge_mhb.shp", "nlcd_" + encrType + "_" + bufferDist + "m.tif") arcpy.gp.Reclassify_sa("nlcd_" + encrType + "_" + bufferDist + "m.tif", "VALUE", impVals, impType + "_encroach_" + bufferDist + "m.tif", "NODATA")
def polygon_to_polyline(polygon): arcpy.PolygonToLine_management(polygon, polygon[0:-4] + "_linia.shp") polylines = [] for row in arcpy.da.SearchCursor(polygon[0:-4] + "_linia.shp", ["SHAPE@"]): polylines.append(row[0]) return polylines[0]
def main(): """ DOC STRING """ # Make Feature Layers arcpy.MakeFeatureLayer_management( INPUTDATA.MAPPING_METHOD_POLYGON, # in_features MAPPING_METHOD_LAYER_1, # out_layer "\"100Bry_ele\" <> 0", # where_clause "", # workspace FIELD_ARGS) arcpy.MakeFeatureLayer_management(INPUTDATA.MAPPING_METHOD_POLYGON, MAPPING_METHOD_LAYER_2, "\"VAGut_ele\" <> 0", "", FIELD_ARGS) arcpy.MakeFeatureLayer_management(INPUTDATA.MAPPING_METHOD_POLYGON, MAPPING_METHOD_LAYER_3, "\"LIMWA_ele\" <> 0", "", FIELD_ARGS) # Convert Polygons to Rasters arcpy.PolygonToRaster_conversion(MAPPING_METHOD_LAYER_1, "100Bry_ele", V100_YR_RAS, "CELL_CENTER", "NONE", "25") arcpy.PolygonToRaster_conversion(MAPPING_METHOD_LAYER_2, "VAGut_ele", VA_RAS, "CELL_CENTER", "NONE", "25") arcpy.PolygonToRaster_conversion(MAPPING_METHOD_LAYER_3, "LIMWA_ele", LIMWA_RAS, "CELL_CENTER", "NONE", "25") # Convert Rasters to TINs arcpy.RasterTin_3d(V100_YR_RAS, V100_YR_TIN, "0.01", "1500000", "1") arcpy.RasterTin_3d(VA_RAS, VA_TIN, "0.01", "1500000", "1") arcpy.RasterTin_3d(LIMWA_RAS, LIMWA_TIN, "0.01", "1500000", "1") # Calculate Surface Difference new TINs vs. TERRAIN arcpy.SurfaceDifference_3d(V100_YR_TIN, INPUTDATA.TERRAIN, V100_YR_SURF_DIFF_SHP, "0", "0", OUTPUT_RASTER_1, "10", "", "") arcpy.SurfaceDifference_3d(VA_TIN, INPUTDATA.TERRAIN, VA_SURF_DIFF_SHP, "0", "0", OUTPUT_RASTER_2, "10", "", "") arcpy.SurfaceDifference_3d(LIMWA_TIN, INPUTDATA.TERRAIN, LIMWA_SURF_DIFF_SHP, "0", "0", OUTPUT_RASTER_3, "10", "", "") # Process: Polygon To Line arcpy.PolygonToLine_management(V100_YR_SURF_DIFF_SHP, V100_YR_RUNUP_BDY_SHP, "IDENTIFY_NEIGHBORS") arcpy.PolygonToLine_management(VA_SURF_DIFF_SHP, VA_BDY_SHP, "IDENTIFY_NEIGHBORS") arcpy.PolygonToLine_management(LIMWA_SURF_DIFF_SHP, LIMWA_BDY_SHP, "IDENTIFY_NEIGHBORS")
def DTE(tiffile): ## Retrieve country ISO: iso = tiffile.split("/")[3].split("\\")[0] print("Working on {0}".format(iso)) ## Set the output path: outpath = tiffile.split(os.path.basename(tiffile))[0] print("\tProjecting Data...") ## Project the dataset ## ## Retrieve projection from dictionary: intermediate_prj = prj_dict[iso] ## Set the ouput data name: outpath = ensure_dir(tiffile.split(".tif")[0] + "tmp/") output = outpath + "_projected.tif" ## Project the dataset: data_desc = arcpy.Describe(tiffile) input_prj = data_desc.SpatialReference.exportToString() arcpy.ProjectRaster_management(tiffile, output, intermediate_prj, "NEAREST", "100", "#", "#", input_prj) ## Retrieve the projected dataset: prjtif = output print("\tPolygonizing...") ## Calculate the DTE ## ## Raster to Polygon: arcpy.RasterToPolygon_conversion(prjtif, "D:/tmp/" + iso + "_polyras.shp", "NO_SIMPLIFY", "VALUE") polyras = "D:/tmp/" + iso + "_polyras.shp" ## Convert the polygon to a feature layer: polyfeat = arcpy.MakeFeatureLayer_management(polyras, "polyras") print("\tLinizing...") ## Polygon to Line: arcpy.PolygonToLine_management(polyfeat, "D:/tmp/" + iso + "_polyline.shp", "IGNORE_NEIGHBORS") polyline = "D:/tmp/" + iso + "_polyline.shp" print("\tCalculating Distance to Line...") ## Distance to Line: output = "D:/tmp/" + iso + "dst.tif" dstras = arcpy.sa.EucDistance(polyline, cell_size=100) dstras.save(output) ## Convert the internal numbers to negative using rastermath: print("\tCalculating negative values...") outpath = ensure_dir(tiffile.split(".tif")[0] + "Output/") output = outpath + "_projected_DTE.tif" urb = arcpy.Raster(tiffile) dteras = arcpy.sa.Con(urb == 1, -1, 0) * dstras + arcpy.sa.Con( urb == 0, 1, 0) * dstras dteras.save(output)
def create_national_border(in_shp, out_shp, datum, projection): print "...Converting polygons into lines" temp_shp = "temp.shp" arcpy.PolygonToLine_management(in_shp, temp_shp) print "...Defining datum" arcpy.DefineProjection_management(temp_shp, datum) print "...Projecting" project4karabakh(temp_shp, out_shp, projection) print "...Deleting intermediate files" arcpy.Delete_management(temp_shp)
def getBounds(): arcpy.SelectLayerByAttribute_management(parcels, "NEW_SELECTION", "Subsection = 0") arcpy.Dissolve_management(parcels, dissolve, "", "", "MULTI_PART", "DISSOLVE_LINES") arcpy.PolygonToLine_management(dissolve, boundline, "IDENTIFY_NEIGHBORS") arcpy.MakeFeatureLayer_management(boundline, bndlineLayer) arcpy.SelectLayerByLocation_management(parcels, "INTERSECT", bndlineLayer, "", "NEW_SELECTION")
def wall(): arcpy.env.workspace = nhd arcpy.env.compression = "NONE" arcpy.env.snapRaster = huc6mosaic arcpy.env.cellSize = "10" albers = arcpy.SpatialReference() albers.factoryCode = 102039 albers.create() NAD83 = arcpy.SpatialReference() NAD83.factoryCode = "4269" NAD83.create() arcpy.env.overwriteOutput = "TRUE" # Project HUC12 and Flowlines to USGS Albers then select the local HUC8s arcpy.Project_management("WBD_HU8", "huc8albers", albers, "", NAD83) arcpy.CopyFeatures_management("NHDFlowline", "Flowline") arcpy.Project_management("Flowline", "flowlinealbers", albers, "", NAD83) arcpy.AddMessage("Projected HUC8s and Flowlines to Albers.") # Select out Subregion's HUC8s to fc "WBD_HU8_Local" arcpy.MakeFeatureLayer_management("huc8albers", "huc8", "", nhd) arcpy.MakeFeatureLayer_management("flowlinealbers", "flowline_layer", "", nhd) arcpy.SelectLayerByLocation_management("huc8", "INTERSECT", "flowline_layer") arcpy.CopyFeatures_management("huc8", "WBD_HU8_Local") arcpy.AddMessage("Selected local HUC8s.") # Convert to lines and add a field to local HUC8s for wall height arcpy.PolygonToLine_management("WBD_HU8_Local", "WallLines") arcpy.AddField_management("WallLines", "WallHeight", "DOUBLE", "", "", "7") arcpy.CalculateField_management("WallLines", "WallHeight", hgt, "PYTHON") # Convert wall lines to raster arcpy.FeatureToRaster_conversion("WallLines", "WallHeight", outfolder + "\\" + "Walls.tif") walls = os.path.join(outfolder, "Walls.tif") subregion_number = os.path.basename(nhd) nhdsubregion = subregion_number[4:8] # Add rasters together arcpy.env.workspace = infolder rasters = arcpy.ListRasters("NED*") for raster in rasters: arcpy.env.extent = raster wallsObject = Raster(walls) elevObject = Raster(raster) walled_ned = Con(IsNull(wallsObject), elevObject, (wallsObject + elevObject)) walled_ned.save(os.path.join(outfolder, os.path.basename(raster))) return
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 main(): arcpy.env.overwriteOutput = True arcpy.env.workspace = sys.argv[1] outspace = sys.argv[2] files = arcpy.ListFeatureClasses() for f in files: fh = arcpy.Describe(f) if fh.shapeType == 'Polygon': outf = os.path.splitext(f)[0] + 'Lines' infile = os.path.join(arcpy.env.workspace, f) outfile = os.path.join(outspace, outf) arcpy.PolygonToLine_management(infile, outfile) print('{} created.'.format(outfile))
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 wall(nhd_gdb, rasters_list, outfolder, height='500', projection=arcpy.SpatialReference(102039)): """For one or more HU8s within the same subregion (nhd_gdb variable), adds walls at the boundaries to force flow direction that do not cross the boundary.""" env.workspace = 'in_memory' env.outputCoordinateSystem = projection env.compression = "NONE" env.snapRaster = rasters_list[0] # they all have the same snap env.cellSize = '10' env.pyramids = "PYRAMIDS -1 SKIP_FIRST" arcpy.CheckOutExtension("Spatial") # HU8 layer huc8_fc = os.path.join(nhd_gdb, "WBD_HU8") arcpy.MakeFeatureLayer_management(huc8_fc, "huc8_layer") # create output folder with HUC4 in the name huc4_code = re.search('\d{4}', os.path.basename(nhd_gdb)).group() walled_dir = os.path.join(outfolder, 'walled' + huc4_code) if not os.path.exists(walled_dir): os.mkdir(walled_dir) # make the walls raster arcpy.PolygonToLine_management(huc8_fc, 'wall_lines') arcpy.AddField_management('wall_lines', "height", "DOUBLE") arcpy.CalculateField_management('wall_lines', "height", '500', "PYTHON") arcpy.FeatureToRaster_conversion('wall_lines', "height", 'wall_raster') wallsObject = Raster('wall_raster') for raster in rasters_list: out_name = os.path.join( walled_dir, os.path.basename(raster).replace('fel.tif', '_wfel.tif')) cu.multi_msg('Creating output {0}'.format(out_name)) env.extent = raster elevObject = Raster(raster) walled_ned = Con(IsNull(wallsObject), elevObject, (wallsObject + elevObject)) walled_ned.save(out_name) for item in ['huc8_layer', 'wall_lines', 'wall_raster']: arcpy.Delete_management(item) arcpy.ResetEnvironments() arcpy.CheckInExtension("Spatial")
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 line_creator(in_bat): """ Create lines around selected buildings :param in_bat: (string) path of buildings shapefile """ print("Creating lines...") arcpy.env.workspace = arcpy.Describe(in_bat).path arcpy.env.overwriteOutput = True dissolve_buf = "dissolve_buf.shp" CHdissolve_buf = "ConvexHulldissolve_buf.shp" tmp_buf = arcpy.Buffer_analysis(in_features=in_bat, out_feature_class=r"in_memory\tempOutput", buffer_distance_or_field="8 FEET", line_side="OUTSIDE_ONLY", line_end_type="ROUND", dissolve_option="NONE") tmp_dis = arcpy.Dissolve_management( in_features=tmp_buf, out_feature_class=r"in_memory\tempOutput1", multi_part=True) tmp_single = arcpy.MultipartToSinglepart_management( in_features=tmp_dis, out_feature_class=dissolve_buf) tmp_field = arcpy.AddField_management(in_table=tmp_single, field_name="group", field_type="SHORT") with arcpy.da.UpdateCursor(tmp_field, "group") as cursor: i = 0 for row in cursor: row[0] = i cursor.updateRow(row) i += 1 create_convex_hull(tmp_field, grouping_field="group") arcpy.PolygonToLine_management(in_features=CHdissolve_buf, out_feature_class="lines") arcpy.Delete_management(dissolve_buf) arcpy.Delete_management(CHdissolve_buf) lines_shp = arcpy.env.workspace + "/lines.shp" return lines_shp
def line_extend_within_polygon(boundary_polygon, lines_to_extend): """ This python function can extend the lines within a boundary polygon upto the border line. Here, boundary_polygon is the polygon shapefile and the lines_to_extend is the line shapefile which will be extended upto the border using the function. """ arcpy.PolygonToLine_management(in_features=boundary_polygon, out_feature_class="boundary_line", neighbor_option="IDENTIFY_NEIGHBORS") arcpy.RepairGeometry_management(in_features=lines_to_extend, delete_null="DELETE_NULL") coastline = "boundary_line" directions = lines_to_extend g = arcpy.Geometry() bank = arcpy.CopyFeatures_management(coastline, g)[0] for i in range(2): with arcpy.da.UpdateCursor(directions, "Shape@") as cursor: for row in cursor: line = row[0] pStart = line.firstPoint pEnd = line.lastPoint L = line.length dX = (pEnd.X - pStart.X) / L dY = (pEnd.Y - pStart.Y) / L p = pEnd m = 0 while True: l = bank.distanceTo(p) L += l p.X = pStart.X + dX * L p.Y = pStart.Y + dY * L m += 1 if m > 100: break if l < 0.001: break if m > 100: continue row[0] = arcpy.Polyline(arcpy.Array([pStart, p])) cursor.updateRow(row) if i == 0: arcpy.FlipLine_edit(in_features=directions) # deleting the boundary_line layer arcpy.Delete_management(coastline) return
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 wall(nhd_gdb, rasters_list, outfolder, height='500', projection=arcpy.SpatialReference(102039)): """For one or more HU8s within the same subregion (nhd_gdb variable), adds walls at the boundaries to force flow direction that do not cross the boundary.""" env.workspace = 'in_memory' env.outputCoordinateSystem = projection env.compression = "NONE" env.snapRaster = rasters_list[0] # they all have the same snap env.cellSize = '10' env.pyramids = "PYRAMIDS -1 SKIP_FIRST" arcpy.CheckOutExtension("Spatial") # HU8 layer huc12_fc = os.path.join(nhd_gdb, "WBDHU12") arcpy.MakeFeatureLayer_management(huc12_fc, "huc12_layer") # make the walls raster arcpy.PolygonToLine_management(huc12_fc, 'wall_lines') arcpy.AddField_management('wall_lines', "height", "DOUBLE") arcpy.CalculateField_management('wall_lines', "height", '500000', "PYTHON") arcpy.FeatureToRaster_conversion('wall_lines', "height", 'wall_raster') wallsObject = Raster('wall_raster') for raster in rasters_list: out_name = os.path.join(raster.replace('.tif', '_walled.tif')) arcpy.AddMessage('Creating output {0}'.format(out_name)) env.extent = raster elevObject = Raster(raster) walled_ned = Con( IsNull(wallsObject), elevObject, Con(LessThan(elevObject, -58000), elevObject, wallsObject)) walled_ned.save(out_name) for item in ['huc8_layer', 'wall_lines', 'wall_raster']: arcpy.Delete_management(item) arcpy.ResetEnvironments() arcpy.CheckInExtension("Spatial") return out_name
def DTE(tiffile, iso, in_file, tag=""): ''' tiffile is the full path to the input raster of interest iso is the three letter string representing the country of interest in_file is the file name of the original .tif (including the .tif ''' print("Working on {0} {1}".format(iso, tag)) if maupp == False: print("\tProjecting Data...") ## Project the dataset ## ## Retrieve projection from dictionary: intermediate_prj = prj_dict[iso] ## Set the ouput data name and temporary folder: tmppath = ensure_dir(out_dir + "/tmp/") output = tmppath + in_file.split('.tif')[0] + "_projected.tif" ## Project the dataset: data_desc = arcpy.Describe(tiffile) input_prj = data_desc.SpatialReference.exportToString() arcpy.ProjectRaster_management(tiffile, output, intermediate_prj, "NEAREST", "100", "#", "#", input_prj) ## Retrieve the projected dataset: prjtif = output print("\tPolygonizing...") ## Calculate the DTE ## ## Raster to Polygon: arcpy.RasterToPolygon_conversion(prjtif, tmppath + iso + "_polyras.shp", "NO_SIMPLIFY", "VALUE") polyras = tmppath + iso + "_polyras.shp" if maupp: urbtif = os.path.dirname(tiffile) + "\\Derived\\" + os.path.basename( tiffile).rstrip(".shp") + ".tif" ## Project the dataset ## ## Retrieve projection from dictionary: intermediate_prj = prj_dict[iso] ## Project the raster version: tmppath = ensure_dir(out_dir + "/tmp/") output = tmppath + os.path.basename(urbtif).split( '.tif')[0] + "_projected.tif" ## Project the dataset: data_desc = arcpy.Describe(urbtif) input_prj = data_desc.SpatialReference.exportToString() arcpy.ProjectRaster_management(urbtif, output, intermediate_prj, "NEAREST", "100", "#", "#", input_prj) ## Retrieve the projected dataset: urbtif = output ## Project the shapefile: ## Set the output data name and temporary folder: tmppath = ensure_dir(out_dir + "/tmp/") inshp = tiffile ## Project that shapefile: output = polyras = tmppath + iso + "_polyras.shp" arcpy.Project_management(inshp, output, intermediate_prj, preserve_shape="PRESERVE_SHAPE") polyras = output ## Convert the polygon to a feature layer: polyfeat = arcpy.MakeFeatureLayer_management(polyras, "polyras") print("\tLinizing...") ## Polygon to Line: arcpy.PolygonToLine_management(polyfeat, tmppath + iso + "_polyline.shp", "IGNORE_NEIGHBORS") polyline = tmppath + iso + "_polyline.shp" print("\tCalculating Distance to Line...") ## Distance to Line: output = tmppath + iso + "dst.tif" dstras = arcpy.sa.EucDistance(polyline, cell_size=100) dstras.save(output) ## Convert the internal numbers to negative using rastermath: print("\tCalculating negative values...") outpath = ensure_dir(out_dir) output = tmppath + iso + "_projected_DTE.tif" if maupp == False: urb = arcpy.Raster(tiffile) if maupp: urb = arcpy.Raster(urbtif) dteras = arcpy.sa.Con(urb == 1, -1, 0) * dstras + arcpy.sa.Con( urb == 0, 1, 0) * dstras dteras.save(output) ## Reproject back to WGS 84 using the original as the snap raster: ## Retrieve the info we want out of the file name: reg = ".*([A-Z]{3}_[0-9]{4})[.]tif" if esa: reg = ".*(\d{4})[.]tif" if maupp: reg = "(MAUPP_[A-Z]{3}).*[.]shp" m = re.search(reg, in_file) ## Construct output file path: output = out_dir + m.group(1) + tag + '_DTE_WGS84.tif' if esa: output = out_dir + i + "_" + m.group(1) + tag + '_DTE_WGS84.tif' ## Declare snap raster: #arcpy.env.snapRaster = arcpy.Raster(tiffile) arcpy.ProjectRaster_management(dteras, output, input_prj, 'BILINEAR', "0.0008333", "#", "#", intermediate_prj)
maxd = row.MAX_DISTANCE arcpy.TableSelect_analysis(tmpf + "stats1", tmpf + "select1", "DISTANCE = " + str(maxd)) rows = arcpy.SearchCursor(tmpf + "select1") row = rows.next() fidr = row.INPUT_FID # Extract point with the FID of the MAX_DISTANCE ! arcpy.FeatureClassToFeatureClass_conversion(tmpf + "tmp2.shp", path_river, "src_R" + str(i + 1) + ".shp", "FID = " + str(fidr)) print(" Calcul length of the basin " + str(i + 1)) # Find max length of the basin (not the length of the stream): for each watershed, # compute the distance between the outlet and each cell of the # watershed limit. Take the longest arcpy.PolygonToLine_management(path_river_r+raster_fnme+"_wshed"+str(i+1)+".shp", \ tmpf+raster_fnme+"+_wshed_line"+str(i+1)+".shp") arcpy.FeatureVerticesToPoints_management(tmpf+raster_fnme+"_wshed_line"+str(i+1)+".shp", \ tmpf+raster_fnme+"_wshed_pts"+str(i+1)+".shp","END") # It seems to be correct, but it gives just one point, # the line is not cut in several lines # Maybe, we have to check that this is always correct ! arcpy.PointDistance_analysis(tmpf+raster_fnme+"_wshed_pts"+str(i+1)+".shp", \ path_river+"exu_R"+str(i+1)+".shp", \ tmpf+"table_dwshed"+str(i+1)) arcpy.Statistics_analysis(tmpf+"table_dwshed"+str(i+1)+".dbf", \ tmpf+"stats2", [["DISTANCE", "MAX"]]) rows = arcpy.SearchCursor(tmpf + "stats2") row = rows.next() Lwatershedmax = row.MAX_DISTANCE # clean print("")
def databaseSetup(output_workspace, output_gdb_name, hu_dataset, hu8_field, hu12_field, hucbuffer, nhd_path, elevation_projection_template, alt_buff, version=None): """Set up the local folders and copy hydrography data into input geodatabases. This tool creates folder corresponding to each local hydrologic unit, usually a HUC8, and fills those folders with the flowlines, inwalls, and outwalls that will be used later to hydro-enforce the digital elevation model for each hydrologic unit. This tool also creates a global geodatabase with a feature class for the whole domain. Parameters ---------- output_workspace : str Output directory where processing will occur. output_gdb_name : str Global file geodatabase to be created. hu_dataset : str Feature class that defines local folder geographic boundaries. hu8_field : str Field name in "hu_dataset" to dissolve boundaries to local folder extents. hu12_field : str Field name in "hu_dataset" from which inwalls are generated. hucbuffer : str Distance to buffer local folder bounds in map units. nhd_path : str Path to workspace containing NHD geodatabases. elevation_projection_template : str Path to DEM file to use as a projection template. alt_buff : str Alternative buffer to use on local folder boundaries. version : str Package version number. Returns ------- None Notes ----- As this tool moves through each local hydrologic unit it searches the *nhd_path* for a geodatabase with hydrography data with the same HUC-4 as the local hydrologic unit. If this cannot be found the tool will skip that local hydrologic unit. Non-NHD hydrography data can be used with this tool, but it must be named and organized exactly as the NHD hydrography. """ if version: arcpy.AddMessage('StreamStats Data Preparation Tools version: %s' % (version)) # set up geoprocessor, with spatial analyst license if arcpy.CheckExtension("Spatial") == "Available": arcpy.CheckOutExtension("Spatial") else: arcpy.addmessage('License Error') # Set script to overwrite if files exist arcpy.env.overwriteOutput = True localName = "local" subName = "subWatershed" GDB_name = "input_data.gdb" #set scratch and arcpy workspaces arcpy.env.workspace = output_workspace arcpy.env.scratchWorkspace = output_workspace #disable Z & M values arcpy.env.outputZFlag = "Disabled" arcpy.AddMessage('Z: ' + arcpy.env.outputZFlag) arcpy.env.outputMFlag = "Disabled" arcpy.AddMessage('M: ' + arcpy.env.outputMFlag) try: #name output fileGDB output_gdb = os.path.join(output_workspace, output_gdb_name + ".gdb") #output_gdb = output_workspace + "\\" + output_gdb_name + ".gdb" #create container geodatabase if arcpy.Exists(output_gdb): arcpy.Delete_management(output_gdb) arcpy.CreateFileGDB_management(output_workspace, output_gdb_name + ".gdb") #dissolve at 8 dig level and put in output workspace hu8_dissolve = arcpy.Dissolve_management( hu_dataset, os.path.join(output_gdb, "huc8index"), hu8_field) elev_spatial_ref = arcpy.Describe( elevation_projection_template ).spatialReference # read the elevation spatial ref. orig_spatial_ref = arcpy.Describe( hu_dataset ).spatialReference # read the local division spatial ref. # Setup loop to iterate thru each HUC in WBD dataset #fields = hu8_field with arcpy.da.SearchCursor(hu8_dissolve, hu8_field) as cursor: for row in cursor: #Get current huc 8 current_hu8 = str(row[0]) current_db = os.path.join(output_workspace, current_hu8, GDB_name) #current_db = output_workspace + "\\" + row[0] + "\\input_data.gdb" arcpy.AddMessage("") #arcpy.AddMessage("%s = \"%s\"" % (hu8_field, current_hu8)) #check to make sure NHD exists and set variable names, if no NHD for HUC, skip it arcpy.AddMessage("Starting processing local folder %s...." % (current_hu8)) arcpy.AddMessage(" Checking to see if NHD exists for %s" % (current_hu8[:4])) NHDExists = False if arcpy.Exists( os.path.join( nhd_path, "NHD_H_" + current_hu8[:4] + "_HU4_GDB" + ".gdb")): orig_4dig_NHD = os.path.join( nhd_path, "NHD_H_" + current_hu8[:4] + "_HU4_GDB" + ".gdb") NHDExists = True else: arcpy.AddMessage( " 4 DIGIT NHD DOES NOT EXIST FOR THE CURRENT HUC") arcpy.AddMessage( " Please download NHD for this HUC and/or ensure NHD geodatabase is named correctly" ) NHDExists = False #If NHD exists for current HUC 8, then do the work if NHDExists: #Create folder for HU inside output folder hydrog_projection_template = os.path.join( orig_4dig_NHD, "Hydrography", "NHDFlowline" ) # get a file to generate hydrography clip. hydrog_spatial_ref = arcpy.Describe( hydrog_projection_template ).spatialReference # make spatial reference object for reproject later arcpy.CreateFolder_management(output_workspace, current_hu8) arcpy.CreateFolder_management( os.path.join(output_workspace, current_hu8), "Layers") arcpy.CreateFolder_management( os.path.join(output_workspace, current_hu8), "tmp") # make scratch workspace later for hydroDEM. #Create file geodatabase to house data arcpy.CreateFileGDB_management( os.path.join(output_workspace, current_hu8), GDB_name) #start output file creation #---------------------------------- #WBD Processing #---------------------------------- arcpy.AddMessage(" Doing WBD processing") #create variables for huc buffers hucbuffer_custom = os.path.join( current_db, "local_buffer" + str(hucbuffer)) hucbuffer_custom_elev_dd83 = os.path.join( current_db, "local_buffer_elev" + str(hucbuffer) + "_dd83") hucbuffer_custom_hydrog_dd83 = os.path.join( current_db, "local_buffer_hydrog" + str(hucbuffer) + "_dd83") hucbuffer_alt = os.path.join(current_db, "local_buffer%s" % (alt_buff)) #start process arcpy.AddMessage( " Selecting current local hydrologic unit.") arcpy.Select_analysis( hu_dataset, os.path.join(current_db, subName), "\"%s\" = \'%s\'" % (hu8_field, current_hu8)) arcpy.AddMessage(" Dissolving sub-watershed polygons") arcpy.Dissolve_management( os.path.join(current_db, subName), os.path.join(current_db, localName), hu8_field) arcpy.AddMessage( " Creating inner and outer wall polyline feature classes" ) arcpy.PolygonToLine_management( os.path.join(current_db, subName), os.path.join(current_db, "huc12_line")) arcpy.PolygonToLine_management( os.path.join(current_db, localName), os.path.join(current_db, "outer_wall")) arcpy.Erase_analysis( os.path.join(current_db, "huc12_line"), os.path.join(current_db, "outer_wall"), os.path.join(current_db, "inwall_edit")) arcpy.AddMessage( " Creating user-defined buffered outwall dataset") arcpy.Buffer_analysis(os.path.join(current_db, localName), hucbuffer_custom, hucbuffer, "FULL", "ROUND") arcpy.AddMessage( " Creating %s meter buffered outwall dataset" % (alt_buff)) arcpy.Buffer_analysis(os.path.join(current_db, localName), hucbuffer_alt, "%s METERS" % (alt_buff), "FULL", "ROUND") arcpy.AddMessage( " Creating unprojected buffered outwall dataset for elevation and hydrography clips" ) arcpy.Project_management(hucbuffer_custom, hucbuffer_custom_elev_dd83, elev_spatial_ref, in_coor_system=orig_spatial_ref) arcpy.Project_management(hucbuffer_custom, hucbuffer_custom_hydrog_dd83, hydrog_spatial_ref, in_coor_system=orig_spatial_ref) arcpy.AddMessage(" Creating sink point feature class") arcpy.CreateFeatureclass_management( os.path.join(output_workspace, current_hu8, "input_data.gdb"), "sinkpoint_edit", "POINT", "", "", "", os.path.join(current_db, localName)) #erase huc 12 line dataset after inwall is created if arcpy.Exists(os.path.join(current_db, "huc12_line")): arcpy.Delete_management( os.path.join(current_db, "huc12_line")) #---------------------------------- #NHD Processing #---------------------------------- arcpy.AddMessage(" Doing NHD processing") #Create NHD feature dataset within current HU database arcpy.AddMessage( " Creating NHD feature dataset in local hydrologic unit workspace" ) arcpy.CreateFeatureDataset_management( current_db, "Hydrography", orig_spatial_ref) arcpy.CreateFeatureDataset_management( current_db, "Reference", orig_spatial_ref) #process each feature type in NHD featuretypelist = [ "NHDArea", "NHDFlowline", "NHDWaterbody" ] for featuretype in featuretypelist: #clip unprojected feature arcpy.AddMessage(" Clipping " + featuretype) arcpy.Clip_analysis( os.path.join(orig_4dig_NHD, "Hydrography", featuretype), hucbuffer_custom_hydrog_dd83, os.path.join(current_db, featuretype + "_dd83")) #project clipped feature arcpy.AddMessage(" Projecting " + featuretype) arcpy.Project_management( os.path.join(current_db, featuretype + "_dd83"), os.path.join(current_db, featuretype + "_project"), orig_spatial_ref) arcpy.CopyFeatures_management( os.path.join(current_db, featuretype + "_project"), os.path.join(current_db, "Hydrography", featuretype)) #delete unprojected and temporary projected NHD feature classes arcpy.Delete_management( os.path.join(current_db, featuretype + "_dd83")) arcpy.Delete_management( os.path.join(current_db, featuretype + "_project")) #create editable dendrite feature class from NHDFlowline arcpy.AddMessage( " Creating copy of NHDFlowline to preserve as original" ) arcpy.CopyFeatures_management( os.path.join(current_db, "Hydrography", "NHDFlowline"), os.path.join(current_db, "Hydrography", "NHDFlowline_orig")) arcpy.AddMessage(" Adding fields to NHDFlowline") arcpy.AddField_management( os.path.join(current_db, "Hydrography", "NHDFlowline"), "comments", "text", "250") arcpy.AddField_management( os.path.join(current_db, "Hydrography", "NHDFlowline"), "to_steward", "text", "50") arcpy.AddMessage(" Finished local %s" % current_hu8) #if no NHD, skip the HUC else: arcpy.AddMessage( " Processing skipped for this HUC--NO NHD") #del cursor, row # handle errors and report using gp.addmessage function except: #If we have messages of severity error (2), we assume a GP tool raised it, # so we'll output that. Otherwise, we assume we raised the error and the # information is in errMsg. # if arcpy.GetMessages(2): arcpy.AddError(arcpy.GetMessages(2)) arcpy.AddError(arcpy.GetMessages(2)) else: arcpy.AddError(str(errMsg))
def get_centerline (feature, dem, workspace, power = 5, eu_cell_size = 10): """Returns a center line feature of the given polygon feature based on cost over an euclidean distance raster and cost path. points are seeded using minimum and maximum elevation.""" centerline = workspace + '\\centerline.shp' center_length = 0 center_slope = 0 smoothing = 4 trim_distance = "100 Meters" try: # Setup extents / environments for the current feature ARCPY.env.extent = feature.shape.extent desc = ARCPY.Describe(feature) XMin_new = desc.extent.XMin - 200 YMin_new = desc.extent.YMin - 200 XMax_new = desc.extent.XMax + 200 YMax_new = desc.extent.YMax + 200 ARCPY.env.extent = ARCPY.Extent(XMin_new, YMin_new, XMax_new, YMax_new) ARCPY.env.overwriteOutput = True ARCPY.env.cellSize = eu_cell_size ARCPY.env.snapRaster = dem # Get minimum and maximum points resample = ARCPY.Resample_management (dem, 'in_memory\\sample', eu_cell_size) masked_dem = spatial.ExtractByMask (resample, feature.shape) # Find the maximum elevation value in the feature, convert them to # points and then remove all but one. maximum = get_properties (masked_dem, 'MAXIMUM') maximum_raster = spatial.SetNull(masked_dem, masked_dem, 'VALUE <> ' + maximum) maximum_point = ARCPY.RasterToPoint_conversion(maximum_raster, 'in_memory\\max_point') rows = ARCPY.UpdateCursor (maximum_point) for row in rows: if row.pointid <> 1: rows.deleteRow(row) del row, rows # Find the minimum elevation value in the feature, convert them to # points and then remove all but one. minimum = get_properties (masked_dem, 'MINIMUM') minimum_raster = spatial.SetNull(masked_dem, masked_dem, 'VALUE <> ' + minimum) minimum_point = ARCPY.RasterToPoint_conversion(minimum_raster, 'in_memory\\min_point') rows = ARCPY.UpdateCursor (minimum_point) for row in rows: if row.pointid <> 1: rows.deleteRow(row) del row, rows # Calculate euclidean Distance to boundary line for input DEM cells. polyline = ARCPY.PolygonToLine_management(feature.shape, 'in_memory\\polyline') eucdist =spatial.EucDistance(polyline, "", eu_cell_size, '') masked_eucdist = spatial.ExtractByMask (eucdist, feature.shape) # Calculate the cost raster by inverting the euclidean distance results, # and raising it to the power of x to exaggerate the least expensive route. cost_raster = (-1 * masked_eucdist + float(maximum))**power # Run the cost distance and cost path function to find the path of least # resistance between the minimum and maximum values. The results are set # so all values equal 1 (different path segments have different values) # and convert the raster line to a poly-line. backlink = 'in_memory\\backlink' cost_distance = spatial.CostDistance(minimum_point, cost_raster, '', backlink) cost_path = spatial.CostPath(maximum_point, cost_distance, backlink, 'EACH_CELL', '') cost_path_ones = spatial.Con(cost_path, 1, '', 'VALUE > ' + str(-1)) # Set all resulting pixels to 1 r_to_p = ARCPY.RasterToPolyline_conversion (cost_path_ones, 'in_memory\\raster_to_polygon') del ARCPY.env.extent # Delete current extents (need here but do not know why) # Removes small line segments from the centerline shape. These segments are # a byproduct of cost analysis. lines = str(ARCPY.GetCount_management(r_to_p)) #check whether we have more than one line segment if float(lines) > 1: # If there is more then one line rows = ARCPY.UpdateCursor(r_to_p) for row in rows: if row.shape.length == eu_cell_size: # delete all the short 10 m lines rows.deleteRow(row) del row, rows lines = str(ARCPY.GetCount_management(r_to_p)) if float(lines) > 1: ARCPY.Snap_edit(r_to_p, [[r_to_p, "END", "50 Meters"]]) # make sure that the ends of the lines are connected r_to_p = ARCPY.Dissolve_management(r_to_p, 'in_memory\\raster_to_polygon_dissolve') # Smooth the resulting line. Currently smoothing is determined by minimum # and maximum distance. The greater change the greater the smoothing. smooth_tolerance = (float(maximum) - float(minimum)) / smoothing ARCPY.SmoothLine_cartography(r_to_p, centerline, 'PAEK', smooth_tolerance, 'FIXED_CLOSED_ENDPOINT', 'NO_CHECK') field_names = [] # List of field names in the file that will be deleted. fields_list = ARCPY.ListFields(centerline) for field in fields_list: # Loop through the field names if not field.required: # If they are not required append them to the list of field names. field_names.append(field.name) # Add new fields to the center line feature ARCPY.AddField_management(centerline, 'GLIMSID', 'TEXT', '', '', '25') ARCPY.AddField_management(centerline, 'LENGTH', 'FLOAT') ARCPY.AddField_management(centerline, 'SLOPE', 'FLOAT') ARCPY.DeleteField_management(centerline, field_names) # Remove the old fields. # Calculate the length of the line segment and populate segment data. ARCPY.CalculateField_management(centerline, 'LENGTH', 'float(!shape.length@meters!)', 'PYTHON') rows = ARCPY.UpdateCursor (centerline) for row in rows: row.GLIMSID = feature.GLIMSID # Get GLIMS ID and add it to segment center_length = row.LENGTH # Get the length of the center line # Calculate slope of the line based on change in elevation over length of line center_slope = round(math.degrees(math.atan((float(maximum) - float(minimum)) / row.LENGTH)), 2) row.SLOPE = center_slope # Write slope to Segment rows.updateRow(row) # Update the new entry del row, rows #Delete cursors and remove locks # Flip Line if needed - Turn min point and end point into a line segment if # the length of this line is greater then the threshold set, flip the line. end_point = ARCPY.FeatureVerticesToPoints_management(centerline, 'in_memory\\end_point', 'END') merged_points = ARCPY.Merge_management ([end_point, minimum_point], 'in_memory\\merged_points') merged_line = ARCPY.PointsToLine_management (merged_points, 'in_memory\\merged_line') merged_line_length = 0 # Get the line Length rows = ARCPY.SearchCursor (merged_line) for row in rows: merged_line_length += row.shape.length del row, rows # if the line length is greater then a quarter the entire feature length, flip if merged_line_length > (center_length/4): ARCPY.FlipLine_edit(centerline) # This function attempts to extend the line and clip it back to the # feature extents in order to create a line that runs from edge to edge #trimmed_line = ARCPY.Merge_management([polyline, centerline], 'in_memory\\line_merge') trimmed_line = ARCPY.Append_management (polyline, centerline, 'NO_TEST') ARCPY.TrimLine_edit (trimmed_line, trim_distance, "DELETE_SHORT") ARCPY.ExtendLine_edit(trimmed_line, trim_distance, "EXTENSION") rows = ARCPY.UpdateCursor (trimmed_line) for row in rows: if row.LENGTH == 0.0: rows.deleteRow(row) del row, rows # Recalculate length. Must be after 0.0 lengths are deleted or they will # not be removed above. ARCPY.CalculateField_management(centerline, 'LENGTH', 'float(!shape.length@meters!)', 'PYTHON') ARCPY.env.overwriteOutput = False return centerline, center_length, center_slope, False except: ARCPY.env.overwriteOutput = False return centerline, '', '', True
"Unable to build Selection Query String from Areasymbol Parameter", 2) exit() # Make feature layer of selected surveys based on ssaList parameter ## THIS IS CREATED IN SOURCE WORKSPACE, NOT scratchWorkspace ## arcpy.MakeFeatureLayer_management(inputFC, selSoilsFL, sQuery) # Edge Matching checks start here try: # Dissolve soils to create boundaries arcpy.Dissolve_management(selSoilsFL, diss_Bound, inField) AddMsgAndPrint("Dissolved input to create boundary", 0) # Convert Soil polys to line for Selected surveys arcpy.PolygonToLine_management(selSoilsFL, soil_lines, "IDENTIFY_NEIGHBORS") AddMsgAndPrint("Converted Soils to lines", 0) # Make soil_lines a Feature Layer arcpy.MakeFeatureLayer_management(soil_lines, "soil_linesFL") # Build whereclause for Select by Attribute whereclause = """%s = -1""" % arcpy.AddFieldDelimiters( "soil_linesFL", 'LEFT_FID') AddMsgAndPrint("Built where clause " + whereclause) # Select soil_lines cooincident with dissolved boundary layer arcpy.SelectLayerByLocation_management( "soil_linesFL", "SHARE_A_LINE_SEGMENT_WITH", diss_Bound) AddMsgAndPrint("Selected lines based on boundary", 0)
endLines = 'in_memory\\endLines' endPoints = 'in_memory\\endPoints' endPointsLyr = patchOutputFolder #define the environment variables description = arcpy.Describe(DTM) cellsize = description.children[0].meanCellHeight buffDist = cellsize * 2 pointDistance = str(cellsize) + ' Meters' arcpy.env.snapRaster = DTM arcpy.env.outputCoordinateSystem = arcpy.Describe(DTM).spatialReference #complete all the geoprocessing steps up to iterator arcpy.Buffer_analysis(flowObs, bufferedFlowObs, buffDist, 'FULL', 'FLAT', '', '', '') arcpy.PolygonToLine_management(bufferedFlowObs, lines, '') arcpy.SplitLine_management(lines, splitlines) arcpy.MakeFeatureLayer_management(splitlines, FLsplitlines, '', '', '') arcpy.SelectLayerByLocation_management(FLsplitlines, 'INTERSECT', flowObs, '', 'NEW_SELECTION', 'INVERT') arcpy.DeleteRows_management(FLsplitlines) arcpy.CopyFeatures_management(FLsplitlines, endLines, '', '', '', '') arcpy.GeneratePointsAlongLines_management(endLines, endPoints, 'DISTANCE', pointDistance, '', 'END_POINTS') arcpy.gp.ExtractMultiValuesToPoints_sa(endPoints, str(DTM) + ' Heights', 'None') arcpy.MakeFeatureLayer_management(endPoints, endPointsLyr) IDs = [] with arcpy.da.SearchCursor(endPointsLyr, ('RIGHT_FID', )) as cursor: for row in cursor:
PrintMsg(" \n" + sp + str(iCnt) + ". " + fld2Name + " " + AS + ": processing " + Number_Format(iSel, 0, True) + " features", 0) # Populating dAtt dictionary from a featurelayer causes problems because it may not include the # neccesary information for the adjacent polygon. This may slow things down a bit, but # it will prevent errors. WAIT! This shouldn't matter if we are only checking within the # survey area. # Save primary values for each polygon in the entire featureclass with arcpy.da.SearchCursor(selLayer, ["OID@",fld1Name]) as cursor: for row in cursor: dAtt[row[0]] = row[1] # Convert the selected mapunit polygon features to a temporary polyline featurelayer PrintMsg("\t\tConverting polygon input to a polyline featureclass...", 0) env.XYTolerance = xyTol arcpy.PolygonToLine_management(selLayer, comFC, "IDENTIFY_NEIGHBORS") # Assign field names for left polygon id and right polygon id lPID = "LEFT_FID" rPID = "RIGHT_FID" theQuery = "(" + arcpy.AddFieldDelimiters(comFC, "LEFT_FID") + " > -1 AND " + arcpy.AddFieldDelimiters(comFC, "RIGHT_FID") + " > -1 )" # include these records for copying to final sQuery = "(" + arcpy.AddFieldDelimiters(comFC, "LEFT_FID") + " > -1 AND " + arcpy.AddFieldDelimiters(comFC, "RIGHT_FID") + " > -1 )" # exclude these records from cursor PrintMsg("\t\tIdentifying adjacent polygon boundaries with the same '" + fld1Name + "' value...", 0) # Add left and right fields for common line test attribute if inputFC.endswith(".shp"): # Need to limit fieldname to 10 characters because of DBF restrictions lFld = "L_" + fld1Name[0:8] rFld = "R_" + fld1Name[0:8] else: lFld = "L_" + fld1Name
# Converts polygons to line features representing the geologic contacts for every quad # This process collects the FID information about the neighboring polygons for each line segment # The FID is then matched with the label on each polygon to get the geologic unit information import arcpy # Set the workspace environment arcpy.env.workspace = "C:\\ArcMap_SSD_Workspace\\Nashville_Files.gdb" feature_class_list = arcpy.ListFeatureClasses("*_arc") # convert polygons to lines keeping neighbor FID information by default arcpy.PolygonToLine_management(in_features=fc, out_feature_class=outFeatureClass) # use FID info to get the geologic units on either side of the geologic contact for fc in feature_class_list: # get the original FIDs of the polygons polygon_features = fc.replace('_arc', '_polygon') in_features = fc join_field_left = "LEFT_FID" join_field_right = "RIGHT_FID" join_field_target = "OBJECTID" field_name = ["Geology"] arcpy.JoinField_management(in_data=in_features, in_field=join_field_left, join_table=polygon_features,
def wetland_order(rivex, stream_area_fc, nwi, out_fc): arcpy.env.workspace = 'in_memory' arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(102039) arcpy.env.extent = nwi # Buffer the wetland perimeters by 30 meters cu.multi_msg('Creating 30m wetland buffers...') arcpy.Buffer_analysis(nwi, "wetland_buffers", "30 meters", "OUTSIDE_ONLY") arcpy.env.extent = "wetland_buffers" cu.multi_msg('Preparing for river line and area merge...') arcpy.CopyFeatures_management(rivex, 'rivex_extent') arcpy.CopyFeatures_management(stream_area_fc, 'stream_area_extent') arcpy.MakeFeatureLayer_management('rivex_extent', 'rivex_lyr') arcpy.SelectLayerByLocation_management('rivex_lyr', 'COMPLETELY_WITHIN', stream_area_fc) arcpy.CopyFeatures_management('rivex_lyr', 'rivex_for_splitting') arcpy.SelectLayerByAttribute_management('rivex_lyr', 'SWITCH_SELECTION') arcpy.CopyFeatures_management('rivex_lyr', 'rivex_not_areas') split_strahler('stream_area_extent', 'rivex_for_splitting', 'stream_area_split') # areas TO lines arcpy.PolygonToLine_management('stream_area_split', 'streamarea_to_line', False) # Merge features together arcpy.Merge_management(['streamarea_to_line', 'rivex_not_areas'], 'merged_rivers', 'NO_TEST') # FOR THE LINE-BASED PORTION # Spatial join connected wetlands and streams ##################Field Maps######################## fms = arcpy.FieldMappings() fm_strahlermax = arcpy.FieldMap() fm_strahlersum = arcpy.FieldMap() fm_lengthkm = arcpy.FieldMap() fm_wetid = arcpy.FieldMap() fm_strahlermax.addInputField('merged_rivers', "Strahler") fm_strahlersum.addInputField('merged_rivers', "Strahler") fm_lengthkm.addInputField('merged_rivers', "LengthKm") fm_wetid.addInputField("wetland_buffers", "WET_ID") fm_lengthkm.mergeRule = 'Sum' fm_strahlermax.mergeRule = 'Max' fm_strahlersum.mergeRule = 'Sum' lengthkm_name = fm_lengthkm.outputField lengthkm_name.name = 'StreamKm' lengthkm_name.aliasName = 'StreamKm' fm_lengthkm.outputField = lengthkm_name strahlermax_name = fm_strahlermax.outputField strahlermax_name.name = 'StrOrdMax' strahlermax_name.aliasName = 'StrOrdMax' fm_strahlermax.outputField = strahlermax_name strahlersum_name = fm_strahlersum.outputField strahlersum_name.name = 'StrOrdSum' strahlersum_name.aliasName = 'StrOrdSum' fm_strahlersum.outputField = strahlersum_name fms.addFieldMap(fm_strahlermax) fms.addFieldMap(fm_strahlersum) fms.addFieldMap(fm_lengthkm) fms.addFieldMap(fm_wetid) ##################################################### arcpy.SpatialJoin_analysis("wetland_buffers", 'merged_rivers', "wetland_spjoin_streams", '', '', fms) # Get the stream count from the join count cu.rename_field("wetland_spjoin_streams", 'Join_Count', "StreamCnt", True) # Join the new fields back to the original feature class based on WET_ID join_fields = ['StrOrdMax', 'StrOrdSum', 'StreamKm', 'StreamCnt'] arcpy.CopyFeatures_management(nwi, out_fc) arcpy.JoinField_management(out_fc, 'WET_ID', 'wetland_spjoin_streams', 'WET_ID', join_fields) # Set these to 0 where there is no connection cu.redefine_nulls(out_fc, join_fields, [0, 0, 0, 0]) # Classify VegType: 4 options based on class code in ATTRIBUTE field arcpy.AddField_management(out_fc, "VegType", "TEXT") with arcpy.da.UpdateCursor(out_fc, ["ATTRIBUTE", "VegType"]) as cursor: for row in cursor: attr_abbv = row[0][:3] if attr_abbv == "PEM" or attr_abbv == "PAB": row[1] = "PEMorPAB" elif attr_abbv == "PFO": row[1] = "PFO" elif attr_abbv == "PSS": row[1] = "PSS" else: row[1] = "Other" cursor.updateRow(row) # Determine the regime from the letter code. Examples: PSS1E ---> E, # PEM1/SS1Fb --> F class_codes = 'RB UB AB US ML EM SS FO'.split() regime_codes = 'A B C E F G H J K'.split() arcpy.AddField_management(out_fc, "Regime", "TEXT") with arcpy.da.UpdateCursor(out_fc, ["ATTRIBUTE", "Regime"]) as cursor: for row in cursor: # All the wetlands are already palustrine, so if we remove the class # codes, any capital letters left besides the P in front # are the regime code # example codes: PEM1E, PSS1/EM1E, PEM1/5C, PUSA, PSSf # If you ever can figure out the regex for this instead, go ahead. code_value = row[0] regime_value = 'unknown' # this bit searches for the class codes and replaces them with nothing # this is necessary because meaning of A, B, E, F is context dependent for class_code in class_codes: if class_code in code_value: code_value = code_value.replace(class_code, '') for char in code_value: if char in regime_codes: regime_value = char row[1] = regime_value cursor.updateRow(row) # Calculate WetOrder from StrOrdSum arcpy.AddField_management(out_fc, "WetOrder", "TEXT") with arcpy.da.UpdateCursor(out_fc, ["StrOrdSum", "WetOrder"]) as cursor: for row in cursor: if row[0] == 0: row[1] = "Isolated" elif row[0] == 1: row[1] = "Single" elif row[0] == None: row[1] = "Isolated" else: row[1] = "Connected" cursor.updateRow(row) arcpy.Delete_management('in_memory')
def build_court_markers(output_gdb, output_feature_class): print('Creating basketball court markers.') fields = ('SHAPE@', 'NAME') fc = os.path.join(output_gdb, output_feature_class) if not arcpy.Exists(os.path.join(output_gdb, output_feature_class)): arcpy.CreateFeatureclass_management(output_gdb, output_feature_class, "POLYLINE", "#", "DISABLED", "DISABLED", arcpy.SpatialReference(3857)) arcpy.AddField_management(fc, fields[1], "TEXT", field_length=20) cursor = arcpy.da.InsertCursor(fc, fields) wing_hash_1 = [(-250, 280 - 52.5), (-250 + 30, 280 - 52.5)] cursor.insertRow([wing_hash_1, "Hash Mark"]) wing_hash_2 = [(250, 280 - 52.5), (250 - 30, 280 - 52.5)] cursor.insertRow([wing_hash_2, "Hash Mark"]) wing_hash_3 = [(-250, 280 + 380 - 52.5), (-250 + 30, 280 + 380 - 52.5)] cursor.insertRow([wing_hash_3, "Hash Mark"]) wing_hash_4 = [(250, 280 + 380 - 52.5), (250 - 30, 280 + 380 - 52.5)] cursor.insertRow([wing_hash_4, "Hash Mark"]) circle_1 = [(40, -12, 5), (40, 0)] cursor.insertRow([circle_1, "Circle"]) circle_2 = [(-40, -12, 5), (-40, 0)] cursor.insertRow([circle_2, "Circle"]) circle_3 = [(40, 835 + 12, 5), (40, 835)] cursor.insertRow([circle_3, "Circle"]) circle_4 = [(-40, 835 + 12, 5), (-40, 835)] cursor.insertRow([circle_4, "Circle"]) baseline_hash_1 = [(110, -52.5), (110, -52.5 + 5)] cursor.insertRow([baseline_hash_1, "Baseline Hash Mark"]) baseline_hash_2 = [(-110, -52.5), (-110, -52.5 + 5)] cursor.insertRow([baseline_hash_2, "Baseline Hash Mark"]) baseline_hash_3 = [(-110, 940 - 52.5), (-110, 940 - 52.5 - 5)] cursor.insertRow([baseline_hash_3, "Baseline Hash Mark"]) baseline_hash_4 = [(110, 940 - 52.5), (110, 940 - 52.5 - 5)] cursor.insertRow([baseline_hash_4, "Baseline Hash Mark"]) back_board = [(-30, -12.5), (30, -12.5)] cursor.insertRow([back_board, "Backboard"]) back_board = [(-30, 847.5), (30, 847.5)] cursor.insertRow([back_board, "Backboard"]) half_court = [(-250, 470 - 52.5), (250, 470 - 52.5)] cursor.insertRow([half_court, "Half Court"]) three = [(-219, -52.5), (-219, 140 - 52.5)] cursor.insertRow([three, "Three Point Line"]) three = [(219, -52.5), (219, 140 - 52.5)] cursor.insertRow([three, "Three Point Line"]) three = [(-219, 940 - 52.5), (-219, 940 - (140 + 52.5))] cursor.insertRow([three, "Three Point Line"]) three = [(219, 940 - 52.5), (219, 940 - (140 + 52.5))] cursor.insertRow([three, "Three Point Line"]) #4-Feet by basket pt_geometry = arcpy.PointGeometry(arcpy.Point(0, 0)) arcpy.Buffer_analysis(pt_geometry, "in_memory\\lane_arc1", 40) #237.5)# pt_geometry = arcpy.PointGeometry(arcpy.Point(0, 835)) arcpy.Buffer_analysis(pt_geometry, "in_memory\\lane_arc2", 40) #237.5)# arcpy.CreateFeatureclass_management("in_memory", "lane_arc_clipper", "POLYGON", "#", "DISABLED", "DISABLED", arcpy.SpatialReference(3857)) arcpy.AddField_management("in_memory\\lane_arc_clipper", fields[1], "TEXT", field_length=20) lane_clip_cursor = arcpy.da.InsertCursor("in_memory\\lane_arc_clipper", fields) clip_poly = [(-250, 0), (250, 0), (250, 50), (-250, 50)] lane_clip_cursor.insertRow([clip_poly, "Lane"]) clip_poly = [(-250, 835), (250, 835), (250, 740), (-250, 740)] lane_clip_cursor.insertRow([clip_poly, "Lane"]) arcpy.PolygonToLine_management("in_memory\\lane_arc1", "in_memory\\lane_arc_line_1") arcpy.PolygonToLine_management("in_memory\\lane_arc2", "in_memory\\lane_arc_line_2") arcpy.Clip_analysis("in_memory\\lane_arc_line_1", "in_memory\\lane_arc_clipper", "in_memory\\clip_lane_arc1") arcpy.Clip_analysis("in_memory\\lane_arc_line_2", "in_memory\\lane_arc_clipper", "in_memory\\clip_lane_arc2") arcpy.Append_management("in_memory\\clip_lane_arc1", fc, "NO_TEST", "", "") arcpy.Append_management("in_memory\\clip_lane_arc2", fc, "NO_TEST", "", "") #Create 3Point Arc pt_geometry = arcpy.PointGeometry(arcpy.Point(0, 0)) arcpy.Buffer_analysis(pt_geometry, "in_memory\\arc", 235.833) #237.5)# pt_geometry = arcpy.PointGeometry(arcpy.Point(0, 835)) arcpy.Buffer_analysis(pt_geometry, "in_memory\\arc2", 235.833) #237.5)# arcpy.CreateFeatureclass_management("in_memory", "clipper", "POLYGON", "#", "DISABLED", "DISABLED", arcpy.SpatialReference(3857)) arcpy.AddField_management("in_memory\\clipper", fields[1], "TEXT", field_length=20) clip_cursor = arcpy.da.InsertCursor("in_memory\\clipper", fields) clip_poly = [(-250, 940 - (140 + 52.5)), (250, 940 - (140 + 52.5)), (250, 140 - 52.5), (-250, 140 - 52.5)] clip_cursor.insertRow([clip_poly, "Three Point Line"]) ## clip_poly = [(-250, 940-52.5), ## (250, 940-52.5), ## (250, 140-52.5), ## (-250, 140-52.5)] ## clip_cursor.insertRow([clip_poly, "Three Point Line"]) arcpy.PolygonToLine_management("in_memory\\arc", "in_memory\\line_arc") arcpy.PolygonToLine_management("in_memory\\arc2", "in_memory\\line_arc2") arcpy.Clip_analysis("in_memory\\line_arc", "in_memory\\clipper", "in_memory\\clip_res") arcpy.Clip_analysis("in_memory\\line_arc2", "in_memory\\clipper", "in_memory\\clip_res2") arcpy.Append_management("in_memory\\clip_res", fc, "NO_TEST", "", "") arcpy.Append_management("in_memory\\clip_res2", fc, "NO_TEST", "", "") print("Done.")