def GetEndemics(extentShapefile, shpHucs, workDir, keyword): """ (string, string, string) -> string & saved csv file. Use this to create a CSV file of species' (including subspecies) whose ranges are endemic to a specified input AOI shapefile. Generally, the AOI shapefile should be a single polygon. The script uses a select by location function in which 12-digit HUCs are selected that are completely within the AOI shapefile. If there is more than one polygon, the selections will be made within each individual polygon - i.e. there will by multiple selections as opposed to one continuous set of HUCs. The shapefile must have projection and coordinate system that matches the 12-digit HUC shapefile from which species' ranges are derived. The final CSV file will contain the following fields: Species Code Scientific Name Common Name NOTE: Be careful with this function, finding endemics may be more difficult than it seems. This obviously does not take into account species' ranges outside CONUS since GAP ranges are not complete outside the lower 48 (with some AK, HI, PR exceptions). And, obviously again, this does not take into consideration ranges in other countries during different seasons. It would be possible to alter this script to look for seasonal endemism. As currently written, the sql query to get HUC range data includes all seasons and known, possibly, and potentially present ocurrence status. Also, bear in mind that you may need to take extra caution regarding endemic species that are distributed up to the edges of oceans. Arguments: extentShapfile -- A designated AOI shapefile with projection and coordinate system to match the 12-digit HUC range shapefile. shpHucs -- A 12-digit HUC range shapefile. workDir -- Where to save the csv file (KeywordEndemicSpecies.txt) keyword -- Keyword to use in output file name, whatever you want that to be. Example: >> csvPath = GetEndemics(extent="T:/Project/ProjectExtent.shp", workDir='T:/Project/', shpHUCs="T:/hucs.shp", keyword="ThisProject") """ import arcpy import pandas as pd, datetime from datetime import datetime starttime = datetime.now() # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # ++++ Directory & File Locations ++++ arcpy.env.workspace = workDir # *************************************************************** ''' ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Select HUCs of the CONUS HUC shapefile that are completely within the user defined source layer feature shapefile. Each must be made into a layer prior to using SelectLayerByLocation ''' print "\nSelecting HUCs completely within the designated shapefile ....\n" arcpy.MakeFeatureLayer_management(shpHucs, 'HUCs_lyr') arcpy.MakeFeatureLayer_management(extentShapefile, 'shp_lyr') arcpy.SelectLayerByLocation_management('HUCs_lyr', 'COMPLETELY_WITHIN', 'shp_lyr') # Make an empty list to append selHUCsList = [] # Get the fields from the input selected HUCs layer fields = arcpy.ListFields('HUCs_lyr') # Create a fieldinfo object fieldinfo = arcpy.FieldInfo() # Use only the HUC12RNG field and set it to fieldinfo for field in fields: if field.name == "HUC12RNG": fieldinfo.addField(field.name, field.name, "VISIBLE", "") # The selected HUCs layer will have fields as set in fieldinfo object arcpy.MakeTableView_management("HUCs_lyr", "selHUCsTV", "", "", fieldinfo) # Loop through the selected HUCs and add them to a list for row in sorted(arcpy.da.SearchCursor('selHUCsTV', ['HUC12RNG'])): selHUCsList.append(row[0]) # Make the selected HUCs list a set for comparing with species range HUCs selHUCsSet = set(selHUCsList) ''' ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Get HUC range data from the Species Database ''' print "\n++++++++++++++ Comparing species ranges to selected HUCs +++++++++++++++++\n" # Make an empty master dataframe dfMaster = pd.DataFrame() ## Make WHRdb and Species databse connections whrCursor, whrConn = gapdb.ConnectWHR() sppCursor, sppConn = gapdb.ConnectSppDB() # Build and SQL statement that returns CONUS # full species codes and names that are in the modeled list sql = """SELECT t.strUC, t.strCommonName, t.strScientificName, t.strsubSciNameText, t.ysnInclude, intRegionCode FROM dbo.tblAllSpecies as t WHERE (t.ysnInclude = 'True') AND t.intRegionCode < 7""" # Pull into a dataframe dfAllSpp = pd.read_sql(sql, whrConn) # Drop the region code and include fields dfAllSpp = dfAllSpp.drop(['intRegionCode','ysnInclude'], axis=1) # Drop duplicates to get unique species codes dfUnique = dfAllSpp.drop_duplicates(subset='strUC', keep='first') ''' Loop over the unique species list to calculate each one's range size and percentage ''' # Set up an iterator to get row index for dfUSpp dataframe # First, sort and reset the row index in dfUnique dataframe dfSort = dfUnique.sort_values(by='strUC') dfUSpp = dfSort.reset_index(drop=True) i = -1 for spp in dfUSpp['strUC']: print "Working on " + spp + " ...." # Add one to the iterartor i += 1 # Now, get the scientific name, subspecies name, # common name, and species code based on row index SN = dfUSpp['strScientificName'][i] SSN = dfUSpp['strsubSciNameText'][i] CN = dfUSpp['strCommonName'][i] SC = dfUSpp['strUC'][i] # Get the taxon from the species code if spp[0] == 'a': taxa = 'Amphibians' elif spp[0] == 'b': taxa = 'Birds' elif spp[0] == 'm': taxa = 'Mammals' else: taxa = 'Reptiles' # Build an SQL statement that returns relevant fields in the # appropriate taxa table tblRanges_<taxa> using a species code # Limit the HUC codes to only CONUS - i.e. < 190000000000 sql = """SELECT t.strHUC12RNG, t.strUC, t.intGapOrigin, t.intGapPres, t.intGapRepro, t.intGapSeas FROM dbo.tblRanges_""" + taxa + """ as t WHERE (t.strUC = '""" + str(spp) + """') AND t.strHUC12RNG < '190000000000'""" dfRngHUCs = pd.read_sql(sql, sppConn) # Select only known, possibly, or potentially present; # year-round, winter, or summer seasons select={'intGapPres':[1,2,3], 'intGapSeas':[1,3,4]} dfS1 = dfRngHUCs[dfRngHUCs[list(select)].isin(select).all(axis=1)] # Get the strHUC12RNG column into a set dfS1Set = set(dfS1[dfS1.columns[0]].tolist()) # Subtract this species' range HUC set from the shapefile's HUC set # to see if the set is empty => all range HUCs for the species would # then be entirely within the shapefile's interior HUCs if len(dfS1Set - selHUCsSet) == 0: print SN, "range is endemic to the input shapefile\n" # Add the species' info to a dataframe dfMaster = dfMaster.append({'Species Code':SC, 'Scientific Name':SN, 'subspecies Name':SSN, 'Common Name':CN}, ignore_index=True) else: print "Range not endemic to AOI. Moving on to next species...\n" # Check to see if there are any species with their range entirely # within the designated shapefile. If not print message to the screen if len(dfMaster) == 0: print " ========= No species have endemic range within the AOI =========\n" else: # Reorder columns in completed dataframe dfMaster = dfMaster[['Species Code', 'Scientific Name','Common Name']] # Export to text file outFileName = workDir + keyword + "EndemicSpeciesList.txt" dfMaster.to_csv(outFileName) # Return dfMaster return outFileName # Delete cursors and close db connections sppConn.close() whrConn.close() del sppCursor, sppConn del whrCursor, whrConn del dfAllSpp, dfUnique, dfSort, dfUSpp del dfS1, dfS1Set endtime = datetime.now() delta = endtime - starttime print "+"*35 print "Processing time: " + str(delta) print "+"*35 print("!!! BE SURE TO READ THE NOTES IN THE DOCUMENTATION !!!")
#todo: Banner and Parameters print '***\nEMU File Conversion\n\nSusan Jones\n8 January 2015\n***\n' csvFile = r'd:\tmp\AT_EMU.csv' kmlFile = r'd:\tmp\tmpEMU.kmz' outFolder = r'd:\tmp' outputGDB = r'd:\tmp\EMU_Smartrak.gdb' outputLayerfile = r'd:\tmp\EMU_Smartrak.lyr' arcpy.env.overwriteOutput = 1 arcpy.env.workspace = outputGDB #todo: make Spatial Reference sr = arcpy.SpatialReference(4326) #todo: make XY Event Layer print "\nMake EMU event Layer" fi = arcpy.FieldInfo() arcpy.MakeTableView_management(in_table=csvFile, out_view="EMU_VIEW", field_info=fi) arcpy.MakeXYEventLayer_management(table="EMU_VIEW", in_x_field="Longitude_wgs84", in_y_field="Latitude_wgs84", out_layer="EMU", spatial_reference=sr) #todo: copy shapefile print "\nCopy EMU_test feature class" if arcpy.Exists("EMU_Smartrak"): arcpy.Delete_management("EMU_Smartrak") arcpy.CopyFeatures_management("EMU", "EMU_Smartrak")
arcpy.AddMessage("Completed: Excel OUTPUT") # --------------------------------------------------------------------------------------------------------------------- # ONE TO MANY JOINS --------------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------------------------------- # -----------------Tree Queries--------------------- # hide unnecessary fields keep_fields = [ u'OBJECTID', u'Shape', u'_record_id', u'_latitude', u'_longitude', u'legacy_fulcrum_id', u'control_number', u'property_type', u'AGENCY_AREANAME' ] fields = arcpy.ListFields(OUTPUT_FC_HTMP_Points) fieldinfo = arcpy.FieldInfo() for field in fields: if field.name in keep_fields: fieldinfo.addField(field.name, field.name, "VISIBLE", "") else: fieldinfo.addField(field.name, field.name, "HIDDEN", "") # SQL Text Public_SQL = "\"AGENCY_AREANAME\" <> '[USFS : San Bernardino National Forest]' AND(\"AGENCY_AREANAME\" IS NOT NULL) AND (\"AGENCY_AREANAME\" NOT IN ('Undetermined', '[Undetermined]', 'Private', '[Private]', 'undetermined', '[undetermined]', 'private', '[private]'))" Private_SQL = "(\"AGENCY_AREANAME\") IS NULL OR (\"AGENCY_AREANAME\" IN ('Undetermined', '[Undetermined]', 'Private', '[Private]', 'undetermined', '[undetermined]', 'private', '[private]'))" # Process: Make Feature Layer (Public Trees Minus SBNF) arcpy.MakeFeatureLayer_management(OUTPUT_FC_HTMP_Points, "Public_Query_Layer", Public_SQL, "", fieldinfo) # Process: Make Feature Layer (Private Trees) arcpy.MakeFeatureLayer_management(OUTPUT_FC_HTMP_Points, "Private_Query_Layer", Private_SQL, "", fieldinfo)
"ARCGIS_SERVER", username=username, password=password) print "Log on to ARCGIS Server Successful" #Build field mappings from the input table input_mappings = { "Address": "well_address", "City": "well_city", "Region": "well_state", "Postal": "zip" } field_mappings = arcpy.FieldInfo() for field in input_mappings: field_mappings.addField(field, input_mappings[field], "VISIBLE", "NONE") print "field mapping built" #Perform batch geocoding address_locator = os.path.join(conn_file, "World.GeocodeServer") arcpy.GeocodeAddresses_geocoding(input_table, address_locator, field_mappings, output_feature_class) print arcpy.GetMessages()
def splitByLayer(fcToSplit, splitFC, fieldsToAssign, countField, onlyKeepLargest, outputFC, report_areas_overlap): desc = arcpy.Describe(fcToSplit) path, fileName = os.path.split(outputFC) shapeLengthFieldName ="" if desc.shapeType == "Polygon": shapeLengthFieldName = desc.areaFieldName dimension = 4 measure = "area" elif desc.shapeType == "Polyline": shapeLengthFieldName = desc.lengthFieldName dimension = 2 measure = "length" else: #arcpy.FeatureClassToFeatureClass_conversion(in_features=fcToSplit, #out_path=path, #out_name=fileName, #where_clause=None, #field_mapping=None, #config_keyword=None) #TODO - verifiy this is the proper call on points assignFieldsByIntersect(sourceFC=fcToSplit, assignFC=splitFC, fieldsToAssign=fieldsToAssign, outputFC=outputFC, report_areas_overlap=report_areas_overlap) return outputFC arcpy.CreateFeatureclass_management(out_path=path, out_name=fileName, geometry_type=desc.shapeType, template=fcToSplit, has_m=None, has_z=None, spatial_reference=desc.spatialReference, config_keyword=None, spatial_grid_1=None, spatial_grid_2=None, spatial_grid_3=None) #Add the reporting name field to set in the split field_assign_object = arcpy.ListFields(dataset=splitFC, wild_card=fieldsToAssign[-1], field_type=None) #Find the freport label field and add it to the output line layer to store results in #field = [field for field in field_assign_object if field.name == fieldsToAssign[-1]][0] field = filter(lambda field:field.name == fieldsToAssign[-1], field_assign_object)[0] arcpy.AddField_management(in_table=outputFC, field_name=field.baseName, field_type=field.type, field_precision=field.precision, field_scale=field.scale, field_length=field.length, field_alias=field.aliasName, field_is_nullable=field.isNullable, field_is_required=field.required, field_domain=field.domain) fldsInput1 = [f.name for f in arcpy.ListFields(fcToSplit) if f.name not in (desc.shapeFieldName,desc.oidFieldName,shapeLengthFieldName)] + \ ["OID@","shape@"] fldsInsert = [arcpy.ValidateFieldName(f.name,path) for f in arcpy.ListFields(fcToSplit) if f.name not in (desc.shapeFieldName,desc.oidFieldName,shapeLengthFieldName)] + \ [fieldsToAssign[-1],"OID@","shape@"] iOID = -2 iShape = -1 iAssignField = -3 iCountField = None fndField = None if countField is not None and countField in fldsInput1: for f in arcpy.ListFields(outputFC): if f.name == countField: fndField = f break if fndField is None: raise ValueError("Count field not found") if fndField.type != "Double" and fndField.type != "Single" and fndField.type != "Integer" and fndField.type != "SmallInteger": raise ValueError("Count is not numeric") iCountField = fldsInput1.index(countField) with arcpy.da.SearchCursor(splitFC, ["Shape@","OID@",fieldsToAssign[-1]],spatial_reference=desc.spatialReference) as scursor: reportingGeometries = {row[1]:{"Geometry":row[0],fieldsToAssign[-1]:row[2]} for row in scursor} tempWorkspace = arcpy.env.scratchGDB tempFCName = Common.random_string_generator() tempFC= os.path.join(tempWorkspace, tempFCName) #Hide all fields to eliminate and Target_id, Join_FID conflicts target_fi = arcpy.FieldInfo() for field in desc.fields: target_fi.addField(field.name,field.name,'HIDDEN','NONE') source_fi = arcpy.FieldInfo() for field in arcpy.Describe(splitFC).fields: source_fi.addField(field.name,field.name,'HIDDEN','NONE') target_sj_no_fields = arcpy.MakeFeatureLayer_management(fcToSplit,"target_sj_no_fields",field_info=target_fi) join_sj_no_fields = arcpy.MakeFeatureLayer_management(splitFC,"join_sj_no_fields",field_info=source_fi) geoToLayerMap = arcpy.SpatialJoin_analysis(target_features=target_sj_no_fields, join_features=join_sj_no_fields, out_feature_class=tempFC, join_operation="JOIN_ONE_TO_MANY", join_type="KEEP_COMMON", field_mapping=None, match_option="INTERSECT", search_radius=None, distance_field_name=None)[0] ddict = defaultdict(list) with arcpy.da.SearchCursor(geoToLayerMap, ("TARGET_FID", "JOIN_FID")) as sCursor: for row in sCursor: ddict[row[0]].append(reportingGeometries[row[1]]) layerToSplit = arcpy.MakeFeatureLayer_management(fcToSplit,"layerToSplit") result = arcpy.SelectLayerByLocation_management(layerToSplit, "CROSSED_BY_THE_OUTLINE_OF", splitFC) rowCount = int(arcpy.GetCount_management(layerToSplit)[0]) j = 0 rowsInserted = 0 totalDif = 0 with arcpy.da.SearchCursor(layerToSplit, fldsInput1) as scursor: with arcpy.da.InsertCursor(outputFC, fldsInsert) as icursor: for j,row in enumerate(scursor,1): newRows = [] lens = [] row = list(row) rowGeo = row[iShape] origLength = getattr(rowGeo, measure) row[iShape] = None for geo in ddict[row[iOID]]: newRow = copy.copy(row) #if not row[iShape].disjoint(geo): splitGeo = rowGeo.intersect(geo['Geometry'], dimension) newRow[iShape] = splitGeo splitLength = getattr(splitGeo, measure) if iCountField is not None: if row[iCountField] is not None and splitLength is not None and origLength is not None and origLength !=0: newRow[iCountField] = float(row[iCountField]) * (float(splitLength) / float(origLength)) else: pass lens.append(float(splitLength)) #newRows.append(copy.copy(newRow)) newRow.insert(iAssignField + 1, geo[fieldsToAssign[-1]]) newRows.append(newRow) if onlyKeepLargest == True: result = icursor.insertRow(newRows[lens.index(max(lens))]) rowsInserted = rowsInserted + 1 else: newOIDS = [] for newRow in newRows: result = icursor.insertRow(newRow) newOIDS.append(str(result)) rowsInserted = rowsInserted + 1 #if rowsInserted % 250 == 0: #print (rowsInserted) dif = sum(lens) / origLength if (dif > 1.0001 or dif < .9999) and report_areas_overlap == False: totalDif = totalDif + (origLength - sum(lens)) print ("Original Row ID: {3} and new features with OIDs of {0} combined count field did not add up to the original: new combined {1}, original {2}. \n This can be caused by self overlapping lines or data falling outside the split areas. \n\tLayer: {4}".format(",".join(newOIDS),str(sum(lens)),str(origLength),row[iOID],desc.catalogPath)) if totalDif > 0 and report_areas_overlap == False: print ("Total difference from source to results: {0}".format(totalDif)) result = arcpy.SelectLayerByLocation_management(in_layer=layerToSplit, selection_type="SWITCH_SELECTION") rowCount = int(arcpy.GetCount_management(layerToSplit)[0]) if rowCount > 0: none_split_fc_name = Common.random_string_generator() none_split_fc= os.path.join(tempWorkspace, none_split_fc_name) assignFieldsByIntersect(sourceFC=layerToSplit, assignFC=splitFC, fieldsToAssign=fieldsToAssign, outputFC=none_split_fc, report_areas_overlap=report_areas_overlap) result = arcpy.Append_management(inputs=none_split_fc, target=outputFC, schema_type = "NO_TEST", field_mapping=None, subtype=None) if arcpy.Exists(none_split_fc): arcpy.Delete_management(none_split_fc) if arcpy.Exists(tempFC): arcpy.Delete_management(tempFC) return outputFC
## Name: CreateFeatureLayerWithFieldInfo.py ## Purpose: Demostrate how to use a FieldInfo object with the ## MakeFeatureLayer tool to create a feature layer ## containing an altered field name ###################################################################### # Import the ArcPy site package and set the current workspace import arcpy arcpy.env.workspace = "C:/Student/PYTH/Selections/Corvallis.gdb" # Variables fldName = "PARK_NAME" newFldName = "NAME" # Create the FieldInfo object fldInfo = arcpy.FieldInfo() # The PARK_NAME field is to be changed to NAME in the feature layer # Add the change to the FieldInfo object fldInfo.addField(fldName, newFldName, "VISIBLE", "") # Set up SQL expression for MakeFeatureLayer where_clause parameter # We will want a subset of parks features that are large area parks # in the new feature layer. featureClass = "Parks" featLayer = "ParksLyr" newFeatureClass = "LargeParks" fieldName = "Shape_area" # Where clause: ESTAB > 0 and ESTAB < 1956 SQLExp = fieldName + " > 200000"
def rankPaths(source, pField, curSurface, outConnect, minWidth): arcpy.AddMessage('Generating ranked cost paths for ' + outConnect + '...') cList = [] zList = [] rList = [] ## # Append core areas to connected regions to connect regions that are bisected by source habitat ## ## # Generate Minimum convex hull of connected areas ## arcpy.MinimumBoundingGeometry_management(outConnect, "in_memory\\mcp", "CONVEX_HULL", "ALL") ## arcpy.Clip_analysis(source, "in_memory\\mcp", "in_memory\\src_clp") ## ## #Merge connected and source ## arcpy.Merge_management(["in_memory\\src_clp", outConnect], "in_memory\\connect_merge") ## ## #Dissolve merged connected patches ## arcpy.Dissolve_management("in_memory\\connect_merge", "in_memory\\out_connect_merge", "", "", "SINGLE_PART", "") ## outConnect = "in_memory\\out_connect_merge" # Set intersect tolerance to 3X link layer cell size to prevent Intersect from creating multiple line segments where slivers occur interTol = str(3 * int(arcpy.Describe(link).meanCellWidth)) minWidth = 2 * minWidth cstSurface = arcpy.sa.FocalStatistics(curSurface, arcpy.sa.NbrCircle(minWidth, "Map"), "MEAN", "DATA") # If connected region is not empty, extract cost surface by connected region to limit analysis to connected region if len(connectList) > 0: cstSurface2 = arcpy.CopyRaster_management(cstSurface, "cstSurface2") arcpy.AddMessage('Extracting cost surface by connected area...') cstSurface = arcpy.gp.ExtractByMask_sa(cstSurface, outConnect, "cstSurf") cstSurface = arcpy.Describe(cstSurface).name cstSurface2 = arcpy.Describe(cstSurface2).name # Create line segment where source patches touch connected regions to use as sources for cost paths # Make sure inputs are in same projection sourceProjName = arcpy.Describe(source).spatialreference.name curProjName = arcpy.Describe(cstSurface).spatialreference.name if not sourceProjName == curProjName: arcpy.AddMessage("\tReprojecting source layer...") pSource = arcpy.Project_management( source, os.path.join(arcpy.env.scratchWorkspace, "reproj.shp"), cstSurface) else: pSource = source ## # Add core ares back to current surfaces as zero cost regions ## arcpy.env.cellSize = '"%s"' % arcpy.Describe(cstSurface).catalogPath ## CellSize = str(arcpy.env.cellSize) ## arcpy.PolygonToRaster_conversion(pSource, pField, "in_memory\\rast_source", "", "", CellSize) ## no_null = arcpy.sa.Con(arcpy.sa.IsNull("in_memory\\rast_source"),0,1) ## cstSurface = arcpy.sa.Con(no_null, 0, cstSurface, "VALUE = 1") ## cstSurface2 = arcpy.sa.Con(no_null, 0, cstSurface2, "VALUE = 1") arcpy.AddMessage( '\tIntersecting source patches with connected area to create source regions...' ) pSource = arcpy.EliminatePolygonPart_management(pSource, "in_memory\\eliminate", "PERCENT", "", 10, "CONTAINED_ONLY") try: arcpy.Delete_management( os.path.join(arcpy.env.scratchWorkspace, "reproj.shp")) except: pass pSource = arcpy.Intersect_analysis([[pSource, 1], [outConnect, 1]], "in_memory\\intersect", "ALL", interTol, "LINE") pSource = arcpy.MultipartToSinglepart_management(pSource, "in_memory\\multipart") pSource = arcpy.UnsplitLine_management(pSource, "in_memory\\unsplit", pField) pSource = arcpy.MakeFeatureLayer_management(pSource, "pSource") # Calculate least-cost path for each pair-wise combination of source patches l = getCombinations(source, pField) values = l[0] combs = l[1] # break combination and not connected lists into unique elements and create list of regions with no connections if len(connectList) > 0: theList = connectList else: theList = noConnectList c = list(set(chain.from_iterable(theList))) # Create patch regions and cost distance rasters for each unique value in source patches arcpy.AddMessage( '\tCreating patch regions and cost distance rasters for each unique value in source patches...' ) for v in values: if v in c: v = str(int(v)) arcpy.AddMessage('\t\tProcessing patch region ' + v + '...') arcpy.SelectLayerByAttribute_management(pSource, "NEW_SELECTION", pField + " = " + v) arcpy.MakeFeatureLayer_management(pSource, "p_" + v) cd = arcpy.sa.CostDistance("p_" + v, cstSurface, "", os.path.join(workspace, "bklnk_" + v)) arcpy.MakeRasterLayer_management(cd, "CostDist_" + v) if len(connectList) > 0: rd = arcpy.sa.CostDistance( "p_" + v, cstSurface2, "", os.path.join(workspace, "r_bklnk_" + v)) arcpy.MakeRasterLayer_management(rd, "r_CostDist_" + v) # Create least-cost paths for each region pair in both directions arcpy.AddMessage( '\tGenerating least-cost path for each patch pair combination...') for c in combs: c1 = str(int(c[0])) c2 = str(int(c[1])) if c in theList: arcpy.AddMessage('\t\tCalculating least-cost path from region ' + c1 + ' to region ' + c2 + '...') cp = arcpy.sa.CostPath("p_" + c1, "CostDist_" + c2, "bklnk_" + c2, "BEST_SINGLE", "FID") cp1 = arcpy.MakeRasterLayer_management(cp, "CP_" + c1 + "_" + c2) arcpy.AddMessage('\t\tCalculating least-cost path from region ' + c2 + ' to region ' + c1 + '...') cp = arcpy.sa.CostPath("p_" + c2, "CostDist_" + c1, "bklnk_" + c1, "BEST_SINGLE", "FID") cp2 = arcpy.MakeRasterLayer_management(cp, "CP_" + c2 + "_" + c1) cList.append(str(cp1)) cList.append(str(cp2)) else: arcpy.AddWarning( '\t\tRegions ' + c1 + ' and ' + c2 + ' are not connected. Skipping cost path for this region pair...' ) # Create combined least-cost path polyline layer arcpy.AddMessage('\t\tMosaicing least-cost paths for region pairs...') arcpy.MosaicToNewRaster_management(cList, workspace, "lcp_mos", "", "", "", "1", "MAXIMUM") for c in cList: try: arcpy.Delete_management(c) except: pass arcpy.CalculateStatistics_management(os.path.join(workspace, "lcp_mos")) LCP = arcpy.sa.Con(os.path.join(workspace, "lcp_mos"), "1", "", "VALUE > 0") arcpy.Delete_management(os.path.join(workspace, "lcp_mos")) # Create least-cost paths by zone arcpy.AddMessage( '\tGenerating least-cost paths by zones for each patch pair combination...' ) # Create least-cost paths for each region pair in both directions for c in combs: c1 = str(int(c[0])) c2 = str(int(c[1])) if c in theList: arcpy.AddMessage('\t\tCalculating least-cost path from region ' + c1 + ' to region ' + c2 + '...') zp = arcpy.sa.CostPath("p_" + c1, "CostDist_" + c2, "bklnk_" + c2, "EACH_ZONE", "FID") zp1 = arcpy.MakeRasterLayer_management(zp, "ZP_" + c1 + "_" + c2) arcpy.AddMessage('\t\tCalculating least-cost path from region ' + c2 + ' to region ' + c1 + '...') zp = arcpy.sa.CostPath("p_" + c2, "CostDist_" + c1, "bklnk_" + c1, "EACH_ZONE", "FID") zp2 = arcpy.MakeRasterLayer_management(zp, "ZP_" + c2 + "_" + c1) zList.append(str(zp1)) zList.append(str(zp2)) # Create combined least-cost path polyline layer arcpy.AddMessage('\t\tMosaicing least-cost paths for region zones...') if arcpy.Exists(os.path.join(workspace, "zcp_mos")): arcpy.Delete_management(os.path.join(workspace, "zcp_mos")) arcpy.MosaicToNewRaster_management(zList, workspace, "zcp_mos", "", "", "", "1", "MAXIMUM") for z in zList: try: arcpy.Delete_management(z) except: pass arcpy.CalculateStatistics_management(os.path.join(workspace, "zcp_mos")) ZCP = arcpy.sa.Con(os.path.join(workspace, "zcp_mos"), "2", "", "VALUE > 0") # Create least-cost paths through compromised areas if len(connectList) > 0: # Create patch regions and cost distance rasters for each unique value in source patches arcpy.AddMessage('\tCalculating costs through restoration zones...') arcpy.AddMessage( '\tGenerating potential restoration paths for each patch pair combination...' ) # Create least-cost paths for each region pair in both directions for c in combs: c1 = str(int(c[0])) c2 = str(int(c[1])) if c in theList: arcpy.AddMessage( '\t\tCalculating least-cost path from region ' + c1 + ' to region ' + c2 + '...') rp = arcpy.sa.CostPath("p_" + c1, "r_CostDist_" + c2, "r_bklnk_" + c2, "EACH_ZONE", "FID") rp1 = arcpy.MakeRasterLayer_management(rp, "RP_" + c1 + "_" + c2) arcpy.AddMessage( '\t\tCalculating least-cost path from region ' + c2 + ' to region ' + c1 + '...') rp = arcpy.sa.CostPath("p_" + c2, "r_CostDist_" + c1, "r_bklnk_" + c1, "EACH_ZONE", "FID") rp2 = arcpy.MakeRasterLayer_management(rp, "RP_" + c2 + "_" + c1) rList.append(str(rp1)) rList.append(str(rp2)) # Create combined least-cost path polyline layer arcpy.AddMessage('\t\tMosaicing least-cost paths for region zones...') if arcpy.Exists(os.path.join(workspace, "rcp_mos")): arcpy.Delete_management(os.path.join(workspace, "rcp_mos")) arcpy.MosaicToNewRaster_management(rList, workspace, "rcp_mos", "", "", "", "1", "MAXIMUM") for r in rList: try: arcpy.Delete_management(r) except: pass arcpy.CalculateStatistics_management(os.path.join( workspace, "rcp_mos")) RCP = arcpy.sa.Con(os.path.join(workspace, "rcp_mos"), "3", "", "VALUE > 0") mList = [LCP, ZCP, RCP] else: mList = [LCP, ZCP] arcpy.AddMessage( '\tCombining least-cost paths by region and least-cost paths by region zones...' ) arcpy.MosaicToNewRaster_management(mList, workspace, "lcp_mos", "", "", "", "1", "MINIMUM") LCP = arcpy.RasterToPolyline_conversion(os.path.join(workspace, "lcp_mos"), "LCP", "", "", "NO_SIMPLIFY") # Create a fieldinfo object to rename grid_code field fieldinfo = arcpy.FieldInfo() fieldinfo.addField("GRID_CODE", "PATH_RNK", "VISIBLE", "") outLCP = arcpy.MakeFeatureLayer_management(str(LCP), "outLCP", "", "", fieldinfo) # arcpy.CopyFeatures_management(outLCP, os.path.join(workspace, outLCP.shp)) try: arcpy.Delete_management(os.path.join(workspace, "lcp_mos")) arcpy.Delete_management(os.path.join(workspace, "zcp_mos")) arcpy.Delete_management(os.path.join(workspace, "rcp_mos")) #arcpy.Delete_management("in_memory") except: pass return (outLCP)
def extractPriorities(link, curSurface, LCPlayer, fullBuild): mosList = [] ######## #workspace = 'C:\\workspace\\test' arcpy.AddMessage('Analyzing priority linkage areas...') arcpy.AddMessage('\tExtracting fuzzy member set of linkage bottlenecks...') fuzzyCurrent = arcpy.sa.FuzzyMembership(curSurface, arcpy.sa.FuzzyMSLarge(0.9, 1)) fuzzyCurrent.save('c:\\workspace\\fuzzcur.img') # curExtract = arcpy.sa.SetNull (curSurface, curSurface, 'Value = 0') fuzzySTD = arcpy.sa.FocalStatistics(curSurface, arcpy.sa.NbrCircle(1000, "MAP"), "STD", "DATA") fuzzySTD.save('c:\\workspace\\fuzzstd.img') #fuzzySTD = arcpy.sa.FocalStatistics(curSurface,arcpy.sa.NbrRectangle(3, 3, "CELL"),"STD", "DATA") fuzzySTD = arcpy.sa.FuzzyMembership(fuzzySTD, arcpy.sa.FuzzyLarge("", 1)) fuzzySTD.save('c:\\workspace\\fuzzstd2.img') arcpy.AddMessage( '\tExtracting fuzzy member set of linkage areas vulnerable to loss...') fuzzyLink = arcpy.sa.GreaterThan(link, fullBuild) fuzzyLink = arcpy.sa.Float(fuzzyLink) fuzzyLink.save('c:\\workspace\\fuzzlink.img') ## fuzzyLink = arcpy.sa.FuzzyMembership ( lost_link, arcpy.sa.FuzzyLarge(0.5, 5)) values = getCombinations(LCPlayer, 'PATH_RNK')[0] LCPlayer = arcpy.MakeFeatureLayer_management(LCPlayer, "lcpLayer") arcpy.AddMessage( '\tExtracting fuzzy member set of linkage path regions...') for v in values: if v == 1: string = 'Primary Linkage Path...' Spread = 10 Hedge = 'VERY' elif v == 2: string = 'Secondary Linkage Paths...' Spread = 5 Hedge = 'SOMEWHAT' elif v == 3: string = 'Potential Restoration Paths...' Spread = 5 Hedge = 'NONE' arcpy.AddMessage('\t\tProcessing ' + string) pName = 'prior_' + str(int(v)) arcpy.SelectLayerByAttribute_management("lcpLayer", "NEW_SELECTION", "PATH_RNK = " + str(v)) #### arcpy.AddWarning("pName: " + pName) cd = arcpy.sa.CostDistance(LCPlayer, curSurface) cd.save("c:\\workspace\\cd_" + str(v) + ".img") # Set the midpoint for fuzzy membership to 90% of the maximum value of the cost distance raster # NOTE: This could be a user input variable to allow control over selection tolerance. midPoint = float( arcpy.GetRasterProperties_management(cd, "MAXIMUM").getOutput(0)) midPoint = midPoint - (midPoint * 0.9) fuzzyDistance = arcpy.sa.FuzzyMembership( cd, arcpy.sa.FuzzySmall(midPoint, Spread), Hedge) #### fuzzyDistance.save('c:\\workspace\\fuzzDistance_' + str(v) + '.img') #fuzzyDistance.save(os.path.join('c:\\workspace\\fuzzy', 'fuzzdist_' + str(int(v)) + '.img')) overlay = arcpy.sa.FuzzyOverlay( [fuzzyCurrent, fuzzyDistance, fuzzySTD], 'AND') overlay = arcpy.sa.Slice(overlay, 2, 'EQUAL_INTERVAL') # overlay.save(os.path.join(workspace, 'fuzzyoverlay_' + str(int(v)) + '.img')) # Do vulnerable areas need to be enumerated separate from bottlenecks?????? overlay2 = arcpy.sa.FuzzyOverlay( [fuzzyLink, fuzzyDistance, fuzzyCurrent, fuzzySTD], 'AND') overlay2 = arcpy.sa.Slice(overlay2, 3, 'EQUAL_INTERVAL') overlay = arcpy.sa.Con(overlay2, 3, overlay, 'Value >= 2') #overlay = arcpy.sa.FuzzyOverlay ([overlay, overlay2], 'OR') overlay = arcpy.sa.ExtractByAttributes(overlay, 'Value >= 2') ## At Arcmap 10.2 the following line stopped working on the second pass of the for loop. It works fine if the commands are ## executed manually at the python command prompt. Another ArcGIS mystery. Workaround was to use Con to achieve the same result. ## overlay = arcpy.sa.Reclassify (overlay, 'Value', arcpy.sa.RemapValue([[2, int(v)],[3, int(v)]] )) overlay = arcpy.sa.Con(overlay, int(v), overlay, 'Value >= 2 AND Value <= 3') #overlay = arcpy.sa.Con(overlay, int(v)) arcpy.MakeRasterLayer_management(overlay, pName) mosList.append(pName) ## for var in [overlay, overlay2]: ## arcpy.Delete_management(var) ## for var in [fuzzyCurrent, fuzzySTD, fuzzyLink, fuzzyDistance]: ## arcpy.Delete_management(var) arcpy.SelectLayerByAttribute_management(LCPlayer, "CLEAR_SELECTION", "") arcpy.MosaicToNewRaster_management(mosList, workspace, "bneck_mos", "", "", "", "1", "MINIMUM") arcpy.CalculateStatistics_management(os.path.join(workspace, "bneck_mos")) ## # Add Cleanup routines. Memory could get full!!!!!! ## for var in [overlay, overlay2, fuzzyCurrent, fuzzySTD, fuzzyLink, fuzzyDistance]: ## arcpy.Delete_management(var) ### add filters here........................... arcpy.AddMessage('\tCleaning priority patch boundaries...') b_neck = arcpy.sa.BoundaryClean(os.path.join(workspace, "bneck_mos"), "ASCEND", "TWO_WAY") # b_neck.save('c:\\workspace\\test\\b_neck_clean.img') arcpy.AddMessage('\tExtracting restoration areas...') r_patch = arcpy.sa.BooleanAnd(b_neck, link) #r_patch.save('c:\\workspace\\test\\r_patch_bool.img') r_patch = arcpy.sa.ExtractByAttributes(r_patch, 'Value = 0') ## r_patch.save('c:\\workspace\\test\\r_patch_extract.img') arcpy.AddMessage('\tExtracting areas vulnerable to loss...') v_patch = arcpy.sa.GreaterThan(link, fullBuild) ## v_patch.save('c:\\workspace\\test\\v_patch_GT.img') #v_patch = arcpy.sa.Con(v_patch, b_neck, "", 'Value = 1') #v_patch.save('c:\\workspace\\test\\v_patch_Con.img') v_patch = arcpy.sa.ExtractByAttributes(v_patch, 'Value = 1') ## v_patch.save('c:\\workspace\\test\\v_patch_extract.img') arcpy.AddMessage('\tExtracting secure areas...') b_patch = arcpy.sa.Reclassify(b_neck, 'Value', arcpy.sa.RemapRange([[1, 3, 2]])) ### ## b_patch = arcpy.sa.Times(b_patch, 1) ## b_patch.save('c:\\workspace\\test\\b_patch_reclass.img') ### ## workspace2 = 'c:\\workspace\\test' ## b_neck.save(os.path.join(workspace2, 'bneck_mos.img')) arcpy.MosaicToNewRaster_management([r_patch, v_patch, b_patch], workspace, "prior_mos", "", "", "", "1", "MINIMUM") p_patch = arcpy.RasterToPolygon_conversion( os.path.join(workspace, "prior_mos"), 'in_memory\\p_patch', 0) b_neck = arcpy.RasterToPolygon_conversion(b_neck, 'in_memory\\b_neck', 0) ## p_patch = arcpy.RasterToPolygon_conversion (os.path.join(workspace, "prior_mos"), 'c:\\workspace\\p_patch', 0) ## b_neck = arcpy.RasterToPolygon_conversion (b_neck, 'c:\\workspace\\b_neck', 0) try: arcpy.Delete_management(r_patch) arcpy.Delete_management(v_patch) arcpy.Delete_management(b_patch) except: pass outP = arcpy.Intersect_analysis([b_neck, p_patch], 'in_memory\\outP') # outP = arcpy.Intersect_analysis ([b_neck, p_patch], 'c:\\workspace\\outP') arcpy.AddMessage('\tFormatting priority attributes...') ## Beginning with ArcGIS 10.2, field names produced from prior processing steps changed from "grid_code" and "grid_code_1" ## to "gridcode" and "gridcode_1" respectively. The following code checks field names and assigns the correct string to a ## variable for subsequent processing. Thanks ESRI for once again making random changes that break scripts with no performance ## benefit. fields = arcpy.ListFields(outP) for field in fields: if field.name == "grid_code": gc = "grid_code" elif field.name == "gridcode": gc = "gridcode" elif field.name == "grid_code_1": gc_1 = "grid_code_1" elif field.name == "gridcode_1": gc_1 = "gridcode_1" table = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', 'MiscFiles', 'priorityjointable.dbf')) arcpy.JoinField_management(outP, gc, table, "PATH_RANK", "PATH_TYPE") arcpy.JoinField_management(outP, gc_1, table, "PATCH_RANK", "PATCH_TYPE") arcpy.AddField_management(outP, "rnk", "SHORT") arcpy.CalculateField_management(outP, "rnk", '[' + gc + '] & [' + gc_1 + ']', "VB", "") if len(connectList) > 0: arcpy.JoinField_management(outP, 'rnk', table, "RNK", "PRIORITY_R") else: arcpy.JoinField_management(outP, 'rnk', table, "RNK", "REST_RNK") ## arcpy.AddField_management ('pLayer', 'PATH_TYPE', 'TEXT', "", "", 15) ## arcpy.AddField_management ('pLayer', 'PATCH_TYPE', 'TEXT', "", "", 15) # Get the fields from the input fields = arcpy.ListFields(outP) # Create a fieldinfo object fieldinfo = arcpy.FieldInfo() # Iterate through the fields and set them to fieldinfo for field in fields: if field.name == gc: fieldinfo.addField(field.name, "PATH_RANK", "VISIBLE", "") elif field.name == "PATH_TYPE": fieldinfo.addField(field.name, "PATH_TYPE", "VISIBLE", "") elif field.name == gc_1: fieldinfo.addField(field.name, "PATCH_RANK", "VISIBLE", "") elif field.name == "PATCH_TYPE": fieldinfo.addField(field.name, "PATCH_TYPE", "VISIBLE", "") elif field.name == "PRIORITY_R" or field.name == "REST_RNK": fieldinfo.addField(field.name, "PRIOR_RNK", "VISIBLE", "") else: fieldInfo = fieldinfo.addField(field.name, field.name, "HIDDEN", "") arcpy.MakeFeatureLayer_management(outP, 'pLayer', "", "", fieldinfo) ######### # arcpy.CopyFeatures_management("pLayer", "C:\\workspace\\pLayer.shp") arcpy.AddField_management("pLayer", "ACRES", "DOUBLE", "", "", "", "", "", "", "") arcpy.AddField_management("pLayer", "ACRES", "DOUBLE", "", "", "", "", "", "", "") arcpy.CalculateField_management("pLayer", "ACRES", "!shape.area@acres!", "PYTHON", "") arcpy.SelectLayerByAttribute_management("pLayer", "NEW_SELECTION", "ACRES < 5") arcpy.Eliminate_management("pLayer", "in_memory\\pLayer", "AREA") arcpy.Delete_management("pLayer") arcpy.MakeFeatureLayer_management("in_memory\\pLayer", 'outpLayer') arcpy.SelectLayerByAttribute_management("outpLayer", "NEW_SELECTION", "ACRES >= 5") return ('outpLayer')
try: ScriptUtils.AddMsgAndPrint("\tPreparing the workspace...", 0) cleanup() if arcpy.Exists(OldParcel_Neighborhoods_shp): arcpy.Delete_management(OldParcel_Neighborhoods_shp, "") if arcpy.Exists(Parcel_Neighborhoods_shp): arcpy.Delete_management(Parcel_Neighborhoods_shp, "") # Process: Make Feature Layer of the Old Parcels ScriptUtils.AddMsgAndPrint("\tProcessing the Old Parcels...", 0) whereClause = "\"PVA_NEIGHB\" >= 100000" # Create a fieldinfo object arcpy.MakeFeatureLayer_management(OLD_Parcels, "tmpOldParcels") fields = arcpy.ListFields("tmpOldParcels") fieldInfo = arcpy.FieldInfo() # Iterate through the fields and set them to fieldinfo for field in fields: name = field.name if name == "PVA_NEIGHB": fieldInfo.addField(name, "Neighborhoods", "VISIBLE", "") else: fieldInfo.addField(name, name, "VISIBLE", "") arcpy.MakeFeatureLayer_management(OLD_Parcels, OldParcels_Layer, whereClause, "", fieldInfo) # Process: 1st Dissolve arcpy.Dissolve_management(OldParcels_Layer, OldParcel_Neighborhoods_shp, "Neighborhoods", "LRSN COUNT", "MULTI_PART", "DISSOLVE_LINES") ScriptUtils.AddMsgAndPrint(
def SppInAOI(AOIShp, hucShp, workDir, origin, season, reproduction, presence): ''' (string, string, string, string, list, list, list, list) -> list Returns a list of species occurring within the provided polygon. Runtime is about 3-5 minutes. Arguments: AOIShp -- A shapefile polygon (dissolved) to investigate. Should have the same coordinate systems as the huc shapefile. hucShp -- A 12 digit huc shapefile that matches the GAP species database hucs. workDir -- Where to work and save output. origin -- Origin codes to include. season -- Season codes to include. reproduction -- Reproduction codes to include. presence -- Presence codes to include. Example: >>> sppList = SppInPolygon(AOIShp = "T:/Temp/BlueMountains2.shp", hucShp = config.hucs, workDir = "T:/Temp/", origin = [1], season = [1, 3, 4], reproduction = [1, 2, 3], presence = [1, 2, 3]) ''' import arcpy arcpy.ResetEnvironments() arcpy.env.overwriteOutput=True arcpy.env.workspace = workDir import pandas as pd ############################################## Get list of hucs within polygon ############################################################################### print("\nSelecting HUCs that intersect with the AOI shapefile\n") arcpy.management.MakeFeatureLayer(hucShp, 'HUCs_lyr') arcpy.management.MakeFeatureLayer(AOIShp, 'shp_lyr') arcpy.management.SelectLayerByLocation('HUCs_lyr', 'INTERSECT', 'shp_lyr') # Make an empty list to append selHUCsList = [] # Get the fields from the input selected HUCs layer fields = arcpy.ListFields('HUCs_lyr') # Create a fieldinfo object fieldinfo = arcpy.FieldInfo() # Use only the HUC12RNG field and set it to fieldinfo for field in fields: if field.name == "HUC12RNG": fieldinfo.addField(field.name, field.name, "VISIBLE", "") # The selected HUCs layer will have fields as set in fieldinfo object arcpy.MakeTableView_management("HUCs_lyr", "selHUCsTV", "", "", fieldinfo) # Loop through the selected HUCs and add them to a list for row in sorted(arcpy.da.SearchCursor('selHUCsTV', ['HUC12RNG'])): selHUCsList.append(row[0]) # Make the selected HUCs list a set for comparing with species range HUCs selHUCsSet = set(selHUCsList) ################################################# Get a species list to assess ############################################################################### print("Comparing species ranges to selected HUCs\n") ## Make WHRdb and Species databse connections whrCursor, whrConn = gapdb.ConnectWHR() sppCursor, sppConn = gapdb.ConnectSppDB() # Build and SQL statement that returns CONUS # full species codes and names that are in the modeled list sql = """SELECT t.strUC, t.strCommonName, t.strScientificName, t.strsubSciNameText, t.ysnInclude, intRegionCode FROM dbo.tblAllSpecies as t WHERE (t.ysnInclude = 'True') AND t.intRegionCode < 7""" # Pull into a dataframe dfAllSpp = pd.read_sql(sql, whrConn) # Drop the region code and include fields dfAllSpp = dfAllSpp.drop(['intRegionCode','ysnInclude'], axis=1) # Drop duplicates to get unique species codes dfUnique = dfAllSpp.drop_duplicates(subset='strUC', keep='first') ################################ Asses each species' occurence in polygon hucs ############################################################################### # List to collect species in AOI masterList = [] for SC in list(dfUnique.strUC): taxa = dictionaries.taxaDict[SC[0]] # What hucs are species' in? sql = """SELECT t.strHUC12RNG, t.strUC, t.intGapOrigin, t.intGapPres, t.intGapRepro, t.intGapSeas FROM dbo.tblRanges_""" + taxa + """ as t WHERE (t.strUC = '""" + str(SC) + """') AND t.strHUC12RNG < '190000000000'""" dfRngHUCs = pd.read_sql(sql, sppConn) # Which hucs have acceptable attributes? select={'intGapPres':presence, 'intGapSeas':season, 'intGapOrigin':origin, 'intGapRepro':reproduction} dfS1 = dfRngHUCs[dfRngHUCs[select.keys()].isin(select).all(axis=1)] # Get the strHUC12RNG column into a set SpeciesSet = set(dfS1[dfS1.columns[0]].tolist()) # Compare the species and AOI huc sets to see if there's any overlap. if len(selHUCsSet & SpeciesSet) > 0: masterList.append(SC) else: pass if len(masterList) == 0: print "!!!! There was some sort of problem !!!!\n" else: # Delete cursors and close db connections sppConn.close() whrConn.close() del sppCursor, sppConn del whrCursor, whrConn return masterList