def createDEMgridTopoVars(DEM, sitePoly, path, site): # same preamble from above import arcpy import numpy from arcpy import env env.workspace = "%s\\ChangeModeling\\%s" % (path, site) arcpy.env.overwriteOutput = True env.scratchWorkspace = "%s\\ChangeModeling\\Scratch.gdb" % path #---------create unique raster based on DEM raster using NumPy---------- # get info from DEM raster left = float( arcpy.GetRasterProperties_management(DEM, "LEFT").getOutput(0)) bottom = float( arcpy.GetRasterProperties_management(DEM, "BOTTOM").getOutput(0)) cellx = float( arcpy.GetRasterProperties_management(DEM, "CELLSIZEX").getOutput(0)) celly = float( arcpy.GetRasterProperties_management(DEM, "CELLSIZEY").getOutput(0)) print "Raster info imported" # take raster into numpy to create sequential array tempRaster = arcpy.Raster(DEM) my_array = arcpy.RasterToNumPyArray(tempRaster) # .shape pulls the rows and cols of the raster rasrows, rascols = my_array.shape uniqueval = 1 # straight up nested for loop # very manual way to assign unique value to each cell for rowNum in xrange(rasrows): for colNum in xrange(rascols): my_array[rowNum, colNum] = uniqueval uniqueval += 1 print "unique raster created" # bring back into arcGIS format, attach DEM's spatial reference information, and clip UniqueRaster = arcpy.NumPyArrayToRaster(my_array, arcpy.Point(left, bottom), cellx, celly, 0) UniqueRaster.save("UniqueRaster.tif") spatial_ref = arcpy.Describe(DEM).spatialReference UniqueRaster = arcpy.DefineProjection_management(UniqueRaster, spatial_ref) print "Unique raster exported to ArcGIS" # then RasterToPoly to create grid shapefile # has to be an integer UniqueRaster_int = arcpy.Int_3d(UniqueRaster, "UniqueRaster_int.tif") DEM_gr_larger = arcpy.RasterToPolygon_conversion(UniqueRaster_int, "DEM_grid_larger.shp", "NO_SIMPLIFY", "VALUE") # this is where we clip it to the analysis polygon, with wiggly edges and holes and all # need to do it here so area calculations will be sure to use the area we are # confident in classifying and analyzing. DEM_gr = arcpy.Clip_analysis(DEM_gr_larger, sitePoly, "DEM_grid.shp") print "unique raster converted to poly and clipped" # poly to points to get centroid of each cell # because we've already clipped to analysis_poly, these might not be the centers # of the original DEM cells, but they will fall into one of the orginal DEM cells # and therefore will have consistent values from the topographic variable rasters # note that 'centroid' results in some points being outside the boundaries of the # clipped DEM grid poly cell, and this causes a discrepancy with the way ExportXY # does it - it uses "inside". Also, for some reason this means it doesn't pick up # topographic variable values. So I will just go with "Inside". cellCenters = arcpy.FeatureToPoint_management(DEM_gr, "CellCenters.shp", "INSIDE") print "cell centroids created" # extract values to points to get all the DEM & topo vars attached to centroids # pull all the values from the topographic variable rasters # note that at this point, the topo.var. information is associated with the CellCenters dataset (point dataset) inRasterList = [["mediumDEM.tif", "elev"], ["med_slope.tif", "slope"], ["med_curvature.tif", "curv"], ["med_curvature_prof.tif", "curv_prof"], ["med_curvature_plan.tif", "curv_plan"], ["med_Eastn.tif", "eastn"], ["med_Northn.tif", "northn"], ["med_TMI.tif", "tmi"], ["med_Insol.tif", "insol"], ["med_DistToRidge.tif", "distridge"], ["med_HeatLoadIndex.tif", "heatind"]] cellCenters = arcpy.sa.ExtractMultiValuesToPoints("cellCenters.shp", inRasterList, "NONE") print "Extracted topo vars to cell centroids" # calculate distance to prairie (not for WC) if site != "WC": prairie = "%s\\%s_Prairie.shp" % (path, site) distToPrairie = arcpy.Near_analysis(cellCenters, prairie) print "Distance to Prairie Calculated" else: print "WC, no prairie"
if arcpy.CheckExtension("3D") == "Available": arcpy.AddMessage("Checking out 3D") arcpy.CheckOutExtension("3D") else: arcpy.AddError("Unable to get 3D analyst extension") arcpy.AddMessage(arcpy.GetMessages(0)) # Check that the DEM is projected and equidistant (10Meter) spatial_ref = arcpy.Describe(srcLocation).spatialReference if spatial_ref.factoryCode != 102005 and spatial_ref.factoryCode != 102010: sys.exit('Error: DEM must be an equidistant projection! Please use North ' 'America Equidistant Conic or USA Contiguous Equidist conic') # Convert Raster to integer OutRas = arcpy.sa.Con(Raster(srcLocation) > -999, 1, 0) IntRas = arcpy.Int_3d(OutRas) arcpy.AddMessage('Raster converted to integer raster') # Convert the Raster to a polygon feature class arcpy.RasterToPolygon_conversion(IntRas, tempSRC) arcpy.AddMessage('Integer raster converted to a polygon feature class') # Clip the feature class to the extent of the raster arcpy.Clip_analysis(infc, tempSRC, clippedFC) arcpy.AddMessage('Feature class clipped to extent of source raster') # Convert Multipoint feature class to a point feature class arcpy.MultipartToSinglepart_management(clippedFC, pointFC) arcpy.AddMessage('Multipoint converted to single point') # Parse through the points in a feature class, getting rid of mulit-points
"\n")) arcpy.Delete_management(kd_result) if arcpy.Exists(KDRaster): # arcpy.AlterAliasName(OutputKDr, KDrAlias) arcpy.Rename_management(KDRaster, KDrAlias) if not arcpy.Exists(KDVector): arcpy.AddMessage("Converting to Integer: {} - {}".format(KDRaster, time.strftime("%X"))) txtFile.write("Converting to Integer: {} - {}{}".format(KDRaster, time.strftime("%X"), "\n")) int_result = "{}_Tmp".format(KDRaster) arcpy.Int_3d(KDRaster, int_result) arcpy.AddMessage("Building KDv: {}".format(KDVector)) txtFile.write("Building KDv: {}{}".format(KDVector, "\n")) kdv_result = arcpy.RasterToPolygon_conversion(in_raster=int_result, out_polygon_features=KDVector, simplify="NO_SIMPLIFY", raster_field="Value", create_multipart_features="SINGLE_OUTER_PART", max_vertices_per_feature=None) arcpy.Delete_management(int_result) ResidualCon = "{}{}KernelD_GNAF2".format(out_gdb, "\\") if arcpy.Exists(ResidualCon):
## convert both polygons to rasters snapped to the canopy raster grid ##arcpy.env.snapRaster = "E:/FragEVI/processed/boston/bos.can.redux.tif" ##arcpy.PolygonToRaster_conversion(in_features="road2345_buffsD_NAD83", value_field="OBJECTID", out_rasterdataset="E:/FragEVI/processed/boston/newplanting/road2345_buffD_NAD83_R", cell_assignment="MAXIMUM_COMBINED_AREA", priority_field="NONE", cellsize="1") ##arcpy.PolygonToRaster_conversion(in_features="road2345_IbuffsD_NAD83", value_field="OBJECTID", out_rasterdataset="E:/FragEVI/processed/boston/newplanting/road2345_IbuffsD_NAD83_R", cell_assignment="MAXIMUM_COMBINED_AREA", priority_field="NONE", cellsize="1") print("converted polygon buffers to raster at canopy grid") ## (in R) reprocess the overlaid buffer rasters to produce a single 1/0 "donut" 1m raster broken up by the 4m buffer canopy buffer and eliminating non dev/residential buffers ## raster is "processed/boston/newplanting/road2345_donut_R.tif" print( "skipping over R script that converts buffers to suitable planting donut strips" ) ## convert donut raster to polygons, convert to line object (linear planting distance derivative) arcpy.Int_3d(in_raster_or_constant= "E:/FragEVI/processed/boston/newplanting/road2345_donut_R.tif", out_raster="E:/FragEVI/FragEVIworking.gdb/road2345_donut_Rint") arcpy.RasterToPolygon_conversion( in_raster="E:/FragEVI/FragEVIworking.gdb/road2345_donut_Rint", out_polygon_features= "E:/FragEVI/FragEVIworking.gdb/road2345_donut_polySimp", simplify="SIMPLIFY", raster_field="Value") print("converted donut raster to poly") ## filter polygons for too small areas, convert to line features, and export data attribute table arcpy.Select_analysis( in_features="E:/FragEVI/FragEVIworking.gdb/road2345_donut_polySimp", out_feature_class= "E:/FragEVI/FragEVIworking.gdb/road2345_donut_polySimp_filt", where_clause="Shape_Area > 2")
ucenter_pgroups.save("ucenter_pgroups.tif") ## DETERMINE REGIONS WITH > 50,000 POPULATION ## Convert regions to polygons in order to sum arcpy.RasterToPolygon_conversion("ucenter_pgroups.tif",\ "ucenter_poly",\ "NO_SIMPLIFY", "VALUE") ## Fill gaps (all enclosed pixel areas are incorporated into polygons) arcpy.Union_analysis(["ucenter_poly"],\ "ucenter_polyfill", "ALL",\ 0.01, "NO_GAPS") arcpy.Dissolve_management("ucenter_polyfill", "ucenter_polyfd", "", "", \ "SINGLE_PART") ## Convert population layer to integer type, build attribute table arcpy.Int_3d("region_pop.tif", "rpop_int") ## Sum population over polygons pop_sum_uce = ZonalStatistics("ucenter_polyfd", "FID",\ "rpop_int", "SUM", "NODATA") pop_sum_uce.save("pop_sum_uce") ## Reclassify summed population raster: ## - 0 = less than 50,000 ## - 4 = 50,000 or more uce_rules = RemapRange([[0, 49999, 0], [50000, 10000000, 4]]) ucenter_class = Reclassify("pop_sum_uce", "Value", uce_rules) ucenter_class.save("ucenter_class") ## Set urban center pixels to null in pop_class layer con_uce = Con(IsNull("ucenter_class"), 0, "ucenter_class")
def inundation_map_builder(cut_line_file, profile, output_raster, output_shapefile): xs_cut_lines = cut_line_file profile = profile output_shapefile = output_shapefile output_raster = output_raster try: if arcpy.CheckExtension("3D") == "Available": arcpy.CheckOutExtension("3D") else: # Raise a custom exception # raise LicenseError if arcpy.CheckExtension("Spatial") == "Available": arcpy.CheckOutExtension("Spatial") else: # Raise a custom exception # raise LicenseError # Local variables: modelbuild = "D:\\LittleCal_Inundations\\TINS\\modelbuildtin" modelbuild11 = "D:\\LittleCal_Inundations\\Rasters\\modelbuild11" cook_thorn = "D:\GIS\cook_thorn" modelbuild13 = "D:\\LittleCal_Inundations\\Rasters\\" + output_raster mdelbuild1int = "D:\\LittleCal_Inundations\\Rasters\\mdelbuild1int" modelbuild1dis_shp = "D:\\LittleCal_Inundations\\Shapefiles\\modelbuild1dis.shp" modelbuild1_shp = "D:\\LittleCal_Inundations\\Shapefiles\\" + output_shapefile mapping_xs = "D:\LittleCal_Inundations\Shapefiles\MappingXS.shp" bound_poly = "D:\\LittleCal_Inundations\\Shapefiles\\BoundingPolygonLeveet.shp" params = mapping_xs + " " + profile + " Soft_Line <None>; " + bound_poly + " <None> " + "Hard_Clip <None>" #params = "XSCutlines2.shp 05FEB2008_2400 Soft_Line <None>; BoundingPolygonLeveet.shp <None> Hard_Clip <None>" coord = "PROJCS['NAD83 / Illinois East (ftUS)',GEOGCS['GCS_North_American_1983'," + \ "DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]]," + \ "PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]," + \ "PROJECTION['Transverse_Mercator'],PARAMETER['false_easting',984250.0]," + \ "PARAMETER['false_northing',0.0],PARAMETER['central_meridian',-88.33333333333333]," + \ "PARAMETER['scale_factor',0.999975]," + \ "PARAMETER['latitude_of_origin',36.66666666666666]," + \ "UNIT['Foot_US',0.3048006096012192]]" # Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script # The following inputs are layers or table views: "Mapping_XS", "XS Cut Lines" arcpy.JoinField_management(in_data=mapping_xs, in_field="ProfileM", join_table=xs_cut_lines, join_field="ProfileM", fields=profile) # Process: Create TIN arcpy.CreateTin_3d(modelbuild, coord, params, "DELAUNAY") # Process: TIN to Raster arcpy.TinRaster_3d(modelbuild, modelbuild11, "FLOAT", "LINEAR", "CELLSIZE 2", "1") # Process: Raster Calculator modelbuild12 = ((Raster(modelbuild11) - Raster(cook_thorn)) * ((Raster(modelbuild11) - Raster(cook_thorn)) > 0)) # Process: Set Null arcpy.gp.SetNull_sa(modelbuild12, modelbuild12, modelbuild13, "VALUE = 0") # Process: Int arcpy.Int_3d(modelbuild13, mdelbuild1int) # Process: Raster to Polygon arcpy.RasterToPolygon_conversion(mdelbuild1int, modelbuild1dis_shp, "SIMPLIFY", "Value") arcpy.RepairGeometry_management(modelbuild1dis_shp) # Process: Dissolve arcpy.Dissolve_management(modelbuild1dis_shp, modelbuild1_shp, "", "", "MULTI_PART", "DISSOLVE_LINES") arcpy.DeleteField_management(in_table=mapping_xs, drop_field=profile) except LicenseError: print("3D or Spatial Analyst license is unavailable") except arcpy.ExecuteError: print(arcpy.GetMessages(2)) finally: arcpy.CheckInExtension("3D") arcpy.CheckInExtension("Spatial")
# Check that the DEM is projected and equidistant (10Meter) spatial_ref = arcpy.Describe(srcLocation10m).spatialReference if spatial_ref.factoryCode != 102005 and spatial_ref.factoryCode != 102010: sys.exit('Error: DEM must be an equidistant projection! Please use North ' 'America Equidistant Conic or USA Contiguous Equidist conic') # Check that the DEM is projected and equidistant(1Meter) spatial_ref = arcpy.Describe(srcLocation1m).spatialReference if spatial_ref.factoryCode != 102005 and spatial_ref.factoryCode != 102010: sys.exit('Error: DEM must be an equidistant projection! Please use North ' 'America Equidistant Conic or USA Contiguous Equidist conic') # Convert Raster to integer OutRas = arcpy.sa.Con(Raster(srcLocation10m) > -999, 1, 0) IntRas = arcpy.Int_3d(OutRas) arcpy.AddMessage('Raster converted to integer raster') # Convert the Raster to a polygon feature class arcpy.RasterToPolygon_conversion(IntRas, tempSRC) arcpy.AddMessage('Integer raster converted to a polygon feature class') # Clip the feature class to the extent of the raster arcpy.Clip_analysis(infc, tempSRC, clippedFC) arcpy.AddMessage('Feature class clipped to extent of source raster') # Convert Multipoint feature class to a point feature class arcpy.MultipartToSinglepart_management(clippedFC, pointFC) arcpy.AddMessage('Multipoint converted to single point') # Parse through the points in a feature class, getting rid of mulit-points