def mapUnitPolys(stdLithDict, outputDir, logfile): addMsgAndPrint(' Translating GeologicMap/MapUnitPolys...') try: arcpy.MakeTableView_management('DescriptionOfMapUnits', 'DMU') if stdLithDict <> 'None': arcpy.AddField_management('DMU', "StdLith", "TEXT", '', '', '255') rows = arcpy.UpdateCursor('DMU') row = rows.next() while row: if row.MapUnit in stdLithDict: row.StdLith = stdLithDict[row.MapUnit] rows.updateRow(row) row = rows.next() del row, rows arcpy.MakeFeatureLayer_management("GeologicMap/MapUnitPolys", "MUP") arcpy.AddJoin_management('MUP', 'MapUnit', 'DMU', 'MapUnit') arcpy.AddJoin_management('MUP', 'DataSourceID', 'DataSources', 'DataSources_ID') arcpy.CopyFeatures_management('MUP', 'MUP2') DM = 'DescriptionOfMapUnits_' DS = 'DataSources_' MU = 'MapUnitPolys_' for field in (MU + 'DataSourceID', DM + 'MapUnit', DM + 'OBJECTID', DM + DM + 'ID', DM + 'Label', DM + 'Symbol', DM + 'DescriptionSourceID', DS + 'OBJECTID', DS + DS + 'ID', DS + 'Notes', DS + 'URL'): arcpy.DeleteField_management('MUP2', field) dumpTable('MUP2', 'MapUnitPolys.shp', True, outputDir, logfile, False, 'MapUnitPolys') except: addMsgAndPrint(arcpy.GetMessages()) addMsgAndPrint(' Failed to translate MapUnitPolys')
def PipeDepthCheck(errtype,inDB1,inDB2,outDB,logDB): #inDB1 is Pipe #inDB2 is Node returnValue = 0 errcount = 0 dateofrun = datetime.datetime.now() view = arcpy.MakeTableView_management(inDB1,"tempview") view2 = arcpy.MakeTableView_management(inDB2,"tempview2") joinres = arcpy.AddJoin_management(view,[x.name for x in arcpy.ListFields(view) if "UpstreamNodeID" in x.name][0],view2,[x.name for x in arcpy.ListFields(view2) if "ObjectID" in x.name][0],"KEEP_COMMON") fields = [x.name for x in arcpy.ListFields(joinres) if "ObjectID" in x.name or "InvertLevel" in x.name or "GroundLevel" in x.name] #[u'GIS_VCS_DD_TEST.DANDAS.WW_Pipe.ObjectID', u'GIS_VCS_DD_TEST.DANDAS.WW_Pipe.UpstreamInvertLevel', u'GIS_VCS_DD_TEST.DANDAS.WW_Pipe.DownstreamInvertLevel', u'GIS_VCS_DD_TEST.DANDAS.WW_Node.ObjectID', u'GIS_VCS_DD_TEST.DANDAS.WW_Node.InvertLevel', u'GIS_VCS_DD_TEST.DANDAS.WW_Node.InvertLevelOriginID', u'GIS_VCS_DD_TEST.DANDAS.WW_Node.GroundLevel', u'GIS_VCS_DD_TEST.DANDAS.WW_Node.GroundLevelOriginID'] cursorS = arcpy.da.SearchCursor(joinres,fields) cursorI = arcpy.da.InsertCursor(outDB,[x.name for x in arcpy.ListFields(outDB)[1:]]) #UpstreamDict = {} for row in cursorS: if (isinstance(row[1],float) or isinstance(row[1],int)) and (isinstance(row[4],float) or isinstance(row[4],int) ) and (isinstance(row[6],float) or isinstance(row[6],int) ): if row[1] < row[4] or row[1] > row[6]: cursorI.insertRow(["Join with Node","Pipe","Upstream",globalScriptSettings.getErrID(),row[0],row[3],row[6],row[1],row[4]]) errcount += 1 else: cursorI.insertRow(["Join with Node","Pipe","Upstream",globalScriptSettings.getErrID(),row[0],row[3],row[6],row[1],row[4]]) errcount += 1 arcpy.RemoveJoin_management(joinres) del cursorS joinres2 = arcpy.AddJoin_management(view,[x.name for x in arcpy.ListFields(view) if "DownstreamNodeID" in x.name][0],view2,[x.name for x in arcpy.ListFields(view2) if "ObjectID" in x.name][0],"KEEP_COMMON") cursorSearch = arcpy.da.SearchCursor(joinres2,fields) #caughtlist = set() for row in cursorSearch: if (isinstance(row[2],float) or isinstance(row[2],int)) and (isinstance(row[4],float) or isinstance(row[4],int) ) and (isinstance(row[6],float) or isinstance(row[6],int) ): if row[2] < row[4] or row[2] > row[6]: cursorI.insertRow(["Join with Node","Pipe","Downstream",globalScriptSettings.getErrID(),row[0],row[3],row[6],row[2],row[4]]) errcount += 1 else: cursorI.insertRow(["Join with Node","Pipe","Downstream",globalScriptSettings.getErrID(),row[0],row[3],row[6],row[2],row[4]]) errcount += 1 logcursorI = arcpy.da.InsertCursor(logDB,[x.name for x in arcpy.ListFields(logDB)[1:]]) del cursorSearch arcpy.RemoveJoin_management(joinres2) arcpy.Delete_management(view) arcpy.Delete_management(view2) del cursorI logcursorI.insertRow([dateofrun,errtype,errcount,globalScriptSettings.getRunCount()]) del logcursorI returnValue = errcount return returnValue
def linesAndPoints(fc, outputDir, logfile): addMsgAndPrint(' Translating {}...'.format(fc)) cp = fc.find('/') fcShp = fc[cp + 1:] + '.shp' LIN2 = fc[cp + 1:] + '2' LIN = 'xx' + fc[cp + 1:] #addMsgAndPrint(' Copying features from {} to {}'.format(fc, LIN2)) arcpy.CopyFeatures_management(fc, LIN2) arcpy.MakeFeatureLayer_management(LIN2, LIN) fieldNames = fieldNameList(LIN) if 'Type' in fieldNames: arcpy.AddField_management(LIN, 'Definition', 'TEXT', '#', '#', '254') arcpy.AddJoin_management(LIN, 'Type', 'Glossary', 'Term') arcpy.CalculateField_management(LIN, 'Definition', '!Glossary.Definition![0:254]', 'PYTHON') arcpy.RemoveJoin_management(LIN, 'Glossary') # command below are 9.3+ specific sourceFields = arcpy.ListFields(fc, '*SourceID') for sField in sourceFields: nFieldName = sField.name[:-2] arcpy.AddField_management(LIN, nFieldName, 'TEXT', '#', '#', '254') arcpy.AddJoin_management(LIN, sField.name, 'DataSources', 'DataSources_ID') arcpy.CalculateField_management(LIN, nFieldName, '!DataSources.Source![0:254]', 'PYTHON') arcpy.RemoveJoin_management(LIN, 'DataSources') arcpy.DeleteField_management(LIN, sField.name) dumpTable(LIN2, fcShp, True, outputDir, logfile, False, fc[cp + 1:]) arcpy.Delete_management(LIN) arcpy.Delete_management(LIN2)
def TVCCInRefrence(errtype, inDB1, inDB2, inDB3, outDB, logDB): #inDB1 is TVCCInspection #inDB2 is Report #inDB3 is PipeReport returnValue = 0 errcount = 0 dateofrun = datetime.datetime.now() view = arcpy.MakeTableView_management(inDB1, "tempview") view2 = arcpy.MakeTableView_management(inDB2, "tempview2") view3 = arcpy.MakeTableView_management(inDB3, "tempview3") joinres = arcpy.AddJoin_management(view, "ReportID", view2, "ObjectID", "KEEP_ALL") joinres2 = arcpy.AddJoin_management(joinres, [ x.name for x in arcpy.ListFields(joinres) if "Report.ObjectID" in x.name ][0], view3, "ReportID", "KEEP_ALL") joinfields = [ x.name for x in arcpy.ListFields(joinres2) if "ObjectID" in x.name or "InActive" in x.name ] #[u'GIS_VCS_DD_TEST.DANDAS.WW_CCTVInspection.ObjectID', u'GIS_VCS_DD_TEST.DANDAS.WW_CCTVInspection.InActive', u'GIS_VCS_DD_TEST.DANDAS.WW_Report.ObjectID', u'GIS_VCS_DD_TEST.DANDAS.WW_PipeReport.ObjectID'] cursorSwhere = arcpy.da.SearchCursor(joinres2, joinfields, [ x.name for x in arcpy.ListFields(joinres2) if "PipeReport.ObjectID" in x.name ][0] + " IS NULL") cursorI = arcpy.da.InsertCursor( outDB, [x.name for x in arcpy.ListFields(outDB)[1:]]) logcursorI = arcpy.da.InsertCursor( logDB, [x.name for x in arcpy.ListFields(logDB)[1:]]) for row in cursorSwhere: cursorI.insertRow([ "Join with Report and PipeReport", "TVCCInspection", "", globalScriptSettings.getErrID(), row[0], row[1] ]) errcount += 1 del cursorI del cursorSwhere logcursorI.insertRow( [dateofrun, errtype, errcount, globalScriptSettings.getRunCount()]) del logcursorI arcpy.RemoveJoin_management(joinres2) arcpy.Delete_management(view) arcpy.Delete_management(view2) arcpy.Delete_management(view3) returnValue = errcount return returnValue
def pipe_attribute_add(pipeline): try: pipelyr = arcpy.MakeFeatureLayer_management(pipeline, "pipelyr") print "开始执行起始点" arcpy.AddMessage(u"开始执行起始点") P_start = select_identify_point("START") print P_start[0] base_p_start = os.path.basename(P_start[0]) P_end = select_identify_point('END') base_p_end = os.path.basename(P_end[0]) arcpy.AddJoin_management( pipelyr, "PipeLineNumber", P_start, "PipeLineNumber") arcpy.CalculateField_management( pipelyr, 'StartPointNum', "!%s.POINTNUMBER!"% base_p_start , "PYTHON_9.3") arcpy.CalculateField_management( pipelyr, 'OrgName', "!%s.OrgName_1!" % base_p_start, "PYTHON_9.3") arcpy.CalculateField_management( pipelyr, 'OrgCode', "!%s.OrgCode_1!" % base_p_start, "PYTHON_9.3") arcpy.RemoveJoin_management( pipelyr,base_p_start) print "起始点字段计算结束" arcpy.AddMessage(u"起始点字段计算结束") print "\n" print "开始执行终点" arcpy.AddMessage(u"开始执行终点") arcpy.AddJoin_management( pipelyr, "PipeLineNumber", P_end, "PipeLineNumber") arcpy.CalculateField_management( pipelyr, 'EndPointNum', "!POINTNUMBER!", "PYTHON_9.3") arcpy.RemoveJoin_management( pipelyr, base_p_end) pipewithattr = arcpy.CopyFeatures_management(pipelyr, "pipewithattr") arcpy.DeleteFeatures_management(pipeline) arcpy.Append_management(pipewithattr,pipeline) print "点号提取完成" arcpy.AddMessage(u"点号提取完成") except arcpy.ExecuteError as e: print e else: arcpy.Delete_management(pipewithattr)
def fc_joins(fcName, fcPath, glosPath, ds_path): """ Joins feature class with the Glossary and DataSources tables and calculates values to new fields. Returns a feature layer with the joins """ # reporting fc_in_fds = fds_and_name(fcPath) gems.addMsgAndPrint(' Joining {} to Glossary and DataSources...' .format(fc_in_fds)) if debug: print 1 print_field_names(fcPath) try: fcShp = fcName + '.shp' LIN = 'xx' + fcName #remove_joins(fcPath) arcpy.MakeFeatureLayer_management(fcPath, LIN) fieldNames = gems.fieldNameList(LIN) if 'Type' in fieldNames: arcpy.AddField_management(LIN, 'Definition', 'TEXT', '#', '#', '254') arcpy.AddJoin_management(LIN, 'Type', glosPath, 'Term') arcpy.CalculateField_management(LIN, 'Definition', '!Glossary.Definition![0:254]', 'PYTHON') arcpy.RemoveJoin_management(LIN, 'Glossary') # command below are 9.3+ specific sourceFields = arcpy.ListFields(fcPath, '*SourceID') for sField in sourceFields: nFieldName = sField.name[:-2] arcpy.AddField_management(LIN, nFieldName, 'TEXT', '#', '#', '254') arcpy.AddJoin_management(LIN, sField.name, ds_path, 'DataSources_ID') arcpy.CalculateField_management(LIN, nFieldName, '!DataSources.Source![0:254]', 'PYTHON') arcpy.RemoveJoin_management(LIN, 'DataSources') arcpy.DeleteField_management(LIN, sField.name) arcpy.MakeFeatureLayer_management(LIN, 'feature_layer') # cleanup gems.testAndDelete(LIN) arcpy.DeleteField_management(fcPath, ['Definition', nFieldName]) return 'feature_layer' except: gems.addMsgAndPrint(arcpy.GetMessages()) gems.addMsgAndPrint(' COULD NOT CREATE JOINS FOR {}'.format(fc_in_fds))
def CheckNulls(int_fc, in_rows, ov_rows, in_f, out_f): ap.AddField_management(in_rows, "CheckField", "DOUBLE") ap.MakeTableView_management(in_rows, "rows_view") ap.AddJoin_management("rows_view", "RtID", ov_rows, "RtID") codeblock = [ # This first field calculator function returns 1 when the input field # contains values, and 0 if it is null. """def NullVals(field): if field == None: return 0 else: return 1""", # This second function is used to populate the final field indicating whether # the input data is complete or not """def status(mean_field): if mean_field < 1: return 'N' else: return 'Y'""" ] ap.CalculateField_management( "rows_view", "CheckField", "NullVals(!{}.{}!)".format(ov_rows, in_f), "PYTHON3", codeblock[0]) ap.Delete_management("rows_view") # Once a new field is calculated with the NullVals function, values with the # same intersection ID are averaged together. A mean of 1 indicates data # completeness, less than 1 indicates at least one value is missing. ap.Statistics_analysis(in_rows, r"in_memory\stats_table", [["CheckField", "MEAN"]], "ID") ap.AddField_management(int_fc, out_f, "TEXT") ap.MakeFeatureLayer_management(int_fc, "int_lyr") ap.AddJoin_management("int_lyr", "MSLINK", r"in_memory\stats_table", "ID") ap.CalculateField_management( "int_lyr", out_f, "status(!stats_table.MEAN_CheckField!)", "PYTHON3", codeblock[1]) ap.Delete_management(r"in_memory\stats_table") ap.Delete_management("int_lyr")
def ParcelPopulation( tract_fc, parcel_fc, pop_field, count_field, res_units, tract_id): """This function estimates the residential populations for parcel features, based on weighted parcel code counts and tract-level populations. It takes 6 arguments: tract_fc - census tract feature class with population and weighted count fields parcel_fc - residential parcel feature class pop_field - population field in tract table count_field - weighted code count field in tract table res_units - field in parcel_fc containing estimated number of residential units (code weight) tract_id - Unique identifier field for tracts (GEOID) The output is a parcel population field in the input parcel feature class""" ap.AddField_management(tract_fc, "PPU", "DOUBLE") ap.CalculateField_management( tract_fc, "PPU", "!{}!/!{}!".format(pop_field, count_field)) ap.AddField_management(parcel_fc, "ParcelPop", "DOUBLE") ap.MakeFeatureLayer_management(parcel_fc, "parcel_lyr") ap.AddJoin_management("parcel_lyr", tract_id, tract_fc, tract_id) ap.AddField_management("parcel_lyr", "PPU", "DOUBLE") ap.CalculateField_management( "parcel_lyr", "PPU", "!{}.PPU!".format(tracts), "PYTHON3") ap.Delete_management("parcel_lyr") ap.CalculateField_management( parcel_fc, "ParcelPop", "!{}!*!PPU!".format(res_units), "PYTHON3")
def collectWJZTB(targetpath, photopath): """关联图斑和照片点图层,得到关联数据""" baseName1 = arcpy.Describe(targetpath).baseName baseName2 = arcpy.Describe(photopath).baseName arcpy.AddMessage("6_7_创建临时图层") arcpy.MakeFeatureLayer_management(targetpath, baseName1) arcpy.MakeFeatureLayer_management(photopath, baseName2) arcpy.AddMessage("6_7_图层关联") arcpy.AddJoin_management(baseName1, "TSTYBM", baseName2, "TSTYBM") where_clause = u" exp_tblx = '一般核查图斑' and ZZJZTB = '0' and exp_tbxhdm = '' and exp_wjzlx = '' and %s.TSTYBM is null" % ( baseName2) datas = [] tempFields = [ "TSTYBM", "exp_sjdlbm", "exp_czcsxm", "exp_dlbm", "exp_gdzzsxdm", "exp_gdlx", "CZCSXM" ] searchFields = [baseName1 + "." + f for f in tempFields] arcpyDeal.createTempDatas(searchFields, tempFields, baseName1, datas, where_clause=where_clause) return datas
def joinWithCatalogAndSave(fc_workspace, featureClass, rc_workspace, rasterCatalog, outWorkspace): featureLayer = 'feature_layer' arcpy.MakeFeatureLayer_management(os.path.join(fc_workspace, featureClass), featureLayer) arcpy.AddJoin_management(featureLayer, "Photo", os.path.join(rc_workspace, rasterCatalog), "Name") outLayer = os.path.join(outWorkspace, featureClass + "withPhotos") arcpy.CopyFeatures_management(featureLayer, outLayer) # Delete unnecessary fields and rename field names to appropriate names extra_fields = [ "{}_{}".format(rasterCatalog, name) for name in ["OBJECTID", "Name", "Shape_Length", "Shape_Area"] ] for field in extra_fields: arcpy.DeleteField_management(outLayer, field) fieldsToRename = filter(lambda name: featureClass in name, (f.name for f in arcpy.ListFields(outLayer))) for fieldName in fieldsToRename: if "Raster" in fieldName: # arcpy.AlterField_management( outLayer,fieldName,"Image" ) continue else: newFieldName = fieldName[len(featureClass) + 1:] arcpy.AlterField_management(outLayer, fieldName, newFieldName)
def rasterAvgs(INT, dem, rname, wname): """Uses arcpy Spatial Analysis package to calculate raster averages Arguments: INT {feature layer} -- intersected feature layer dem {raster layer} -- slope or precipitation raster layer rname {string} -- 'slope' or 'precipitation', prepended to watershed name wname {string} -- watershed name """ arcpy.CheckOutExtension('Spatial') zstatdem = arcpy.sa.ZonalStatisticsAsTable(INT, 'uID', dem, rname + "_" + wname, "DATA", "MEAN") meanField = rname + "_mean" fasterJoin(INT, 'uID', zstatdem, 'uID', ("MEAN", ), (meanField, )) sel = arcpy.MakeFeatureLayer_management(INT, "omit_" + rname + "_" + wname, '"{0}" IS NULL'.format(meanField)) if getCountInt(sel) > 0: selpt = arcpy.FeatureToPoint_management( sel, "omit_" + rname + "_centroid_" + wname) selex = arcpy.sa.ExtractValuesToPoints( selpt, dem, "omit_" + rname + "_val_" + wname) arcpy.AddJoin_management(sel, 'uID', selex, 'uID') meanFieldJ = wname + "." + meanField rastervaluJ = "omit_" + rname + "_val_" + wname + ".RASTERVALU" arcpy.CalculateField_management(sel, meanFieldJ, '[' + rastervaluJ + ']', 'VB') arcpy.Delete_management(sel) arcpy.CheckInExtension('Spatial')
def join(): # Import system modules import arcpy # Set environment settings arcpy.env.workspace = os.getcwd() arcpy.env.qualifiedFieldNames = False file_path = os.path.join('maps', period) if os.path.exists(file_path + '.shp'): for item in ['.shp', '.shx', '.cpg', '.dbf', '.prj']: os.remove(file_path + item) # Set local variables joinTable = file_path + '.csv' joinField = "via_to" outFeature = file_path + '.shp' # Join the feature layer to a table veg_joined_table = arcpy.AddJoin_management(inFeatures, joinField, joinTable, joinField, join_type='KEEP_COMMON') # Copy the layer to a new permanent feature class arcpy.CopyFeatures_management(veg_joined_table, outFeature)
def Joining(layer, joinfield, *layers): outLayer = arcpy.MakeFeatureLayer_management(layer) for i in layers: arcpy.AddJoin_management(outLayer, joinfield, i, joinfield) arcpy.AddWarning(outLayer) return outLayer
def _join_ini_fields(in_fl, join_list): """ Execute any joins specified by the ini dict :param in_fl: the feature layer we're operating on :param join_list: a list of joins, from the __joins__ keyword in the .ini dict. Example: __joins__ = nom_conces ON concessions.nom_conces,concessions.attributai ON societes.societe :return: """ if isinstance(join_list, basestring): join_list = [join_list] for join in join_list: print join src_info, to_info = join.split(' ON ') to_table, to_field = to_info.split('.') # Handle any fields that were originally from other tables and are now joined to the feature layer # i.e. concessions.attributai becomes cmr_open_data_en.CMR.concessions.attributai if '.' in src_info: src_field = _get_full_field_name(src_info, in_fl) else: src_field = src_info logging.debug("Joining field {0} in source dataset to {1} " "in {2}".format(src_field, to_table, to_field)) arcpy.AddJoin_management(in_fl, src_field, to_table, to_field, "KEEP_ALL")
def ShpFixGeometry(fileRoot, item, newTempDir, timeItem, type): ###检查几何 arcpy.env.scratchWorkspace = newTempDir out_table = newTempDir + '\\' + 'outputTable' arcpy.CheckGeometry_management( fileRoot + '\\' + 'shapefile' + '_' + str(resolution) + '\\' + type + '_' + timeItem + '_' + str(resolution) + '\\' + item, out_table) ###修复几何 fcs = [] for row in arcpy.da.SearchCursor(out_table, ("CLASS")): if not row[0] in fcs: fcs.append(row[0]) for fc in fcs: # print("Processing " + fc) lyr = 'temporary_layer' if arcpy.Exists(lyr): arcpy.Delete_management(lyr) tv = "cg_table_view" if arcpy.Exists(tv): arcpy.Delete_management(tv) arcpy.MakeTableView_management(out_table, tv, ("\"CLASS\" = '%s'" % fc)) arcpy.MakeFeatureLayer_management(fc, lyr) arcpy.AddJoin_management(lyr, arcpy.Describe(lyr).OIDFieldName, tv, "FEATURE_ID") arcpy.RemoveJoin_management(lyr, os.path.basename(out_table)) arcpy.RepairGeometry_management(lyr)
def calc_counts(): #join Waze_OPTCS with Alerts # Join Waze_Optics with Waze Data cluster_original_join = arcpy.AddJoin_management("Waze_OPTICS", "SOURCE_ID", "Alerts", "ObjectId", "KEEP_ALL") # Count # waze incidents in each cluster summ_stats = arcpy.analysis.Statistics(cluster_original_join, "Alerts_Statistics", "Alerts.ObjectId COUNT", "Waze_OPTICS.CLUSTER_ID") arcpy.RemoveJoin_management (cluster_original_join) # Add Frequency Field arcpy.AddField_management("Waze_Ellipses", "TotalCount", "LONG", None, None, None, None, "NULLABLE", "NON_REQUIRED", None) # Join Ellipses to previously created Counts from above ellipses_counts_join = arcpy.AddJoin_management("Waze_Ellipses", "CLUSTER_ID", summ_stats, "CLUSTER_ID", "KEEP_ALL") # Update Counts Field # arcpy.CalculateField_management(ellipses_counts_join, "Waze_Ellipses.totalcounts", "!Static_Waze_Data_Statistics.FREQUENCY!", "PYTHON3", None) arcpy.management.CalculateField(ellipses_counts_join, "TotalCount", "!Alerts_Statistics.FREQUENCY!", "PYTHON3", None) arcpy.RemoveJoin_management (ellipses_counts_join) print("Counts added to Ellipses")
def add_stats(stats_vw, core_id, fld_pre, table_vw, join_col): """Add zonal and calculated statistics to stick table.""" tmp_mea = fld_pre + "_tmp_mea" tmp_std = fld_pre + "_tmp_std" umin2std = fld_pre + "umin2std" # Add fields to stick table - has to be done before join arcpy.AddField_management(table_vw, tmp_mea, "Float", "", "", "", "", "NULLABLE") arcpy.AddField_management(table_vw, tmp_std, "Float", "", "", "", "", "NULLABLE") arcpy.AddField_management(table_vw, umin2std, "Float", "", "", "", "", "NULLABLE") # Join distance table to zonal stats table arcpy.AddJoin_management(table_vw, join_col, stats_vw, core_id) tbl_name = arcpy.Describe(table_vw).baseName stats_tbl_nm = arcpy.Describe(stats_vw).baseName # Insert values into fields mean_value = "!" + stats_tbl_nm + ".MEAN" + "!" std_value = "!" + stats_tbl_nm + ".STD" + "!" mea_fld = "!" + tbl_name + "." + tmp_mea + "!" std_fld = "!" + tbl_name + "." + tmp_std + "!" arcpy.CalculateField_management(table_vw, tmp_mea, mean_value, "PYTHON_9.3") arcpy.CalculateField_management(table_vw, tmp_std, std_value, "PYTHON_9.3") expression = mea_fld + " - " + std_fld + " - " + std_fld arcpy.CalculateField_management(table_vw, umin2std, expression, "PYTHON_9.3") # Remove join arcpy.RemoveJoin_management(table_vw, stats_tbl_nm)
def heatmaps(locator, analysis_field_variables, path_results, path_variables): """ algorithm to calculate heat maps out of n variables of interest Parameters ---------- path_variables: path to folder with variables to be mapped: the options: - .../../../demand - .../../../emissions analysis_field_variables: array when the path variables is selected, an array of n variables 'string' will be elaborated based on the selection of n input fields. For this a form like the one used in the Arcgis function "merge/FieldMap could be used. path_results : string path to store results: folder heatmaps Returns ------- heat map of variable n: .tif heat map file per variable of interest n. """ arcpy.env.overwriteOutput = True arcpy.CheckOutExtension("Spatial") # local variables # create dbf file tempfile_name = "tempfile" tempfile = locator.get_temporary_file('tempfile.shp') tempfile_db_name = "data" tempfile_db = locator.get_temporary_file('data.dbf') arcpy.CopyRows_management(path_variables, out_table=tempfile_db, config_keyword="") arcpy.FeatureToPoint_management(locator.get_building_geometry(), tempfile, "CENTROID") arcpy.MakeFeatureLayer_management(tempfile, "lyr", "#", "#") for field in analysis_field_variables: arcpy.AddField_management("lyr", field, "DOUBLE", "#", "#", "#", "#", "NULLABLE", "NON_REQUIRED", "#") # vector.append([]) arcpy.AddJoin_management("lyr", "Name", tempfile_db, "Name", "KEEP_ALL") for field in analysis_field_variables: arcpy.CalculateField_management( in_table="lyr", field="%(tempfile_name)s.%(field)s" % locals(), expression="calc_non_null(!%(tempfile_db_name)s.%(field)s!)" % locals(), expression_type="PYTHON_9.3", code_block= "def calc_non_null(x):\n if x is None:\n return 0\n elif x == '':\n return 0\n else:\n return x\n" ) # calculate heatmaps for field in analysis_field_variables: arcpy.gp.Idw_sa(tempfile, field, os.path.join(path_results, field), "1", "2", "VARIABLE 12")
def braids(in_network_fc, tmp_network_tbl): arcpy.AddMessage("...braids") in_network_fc_lyr = "in_network_fc_lyr" arcpy.MakeFeatureLayer_management(in_network_fc, in_network_fc_lyr) # Set global variables FTR_CODE = 2 # Create temporary in_memory version of input stream network tmp_network_fc = r"in_memory\tmp_network_fc" #arcpy.FeatureClassToFeatureClass_conversion(in_network_fc_lyr, "in_memory", "tmp_network_fc") arcpy.FeatureClassToFeatureClass_conversion(in_network_fc_lyr, "in_memory", "tmp_network_fc") arcpy.MakeFeatureLayer_management(r"in_memory\tmp_network_fc", "tmp_network_fc_lyr") # Select reaches based on "IsBraided" attribute field expr = """"{0}" = {1}""".format("IsBraided", 1) arcpy.SelectLayerByAttribute_management("tmp_network_fc_lyr", "NEW_SELECTION", expr) arcpy.FeatureClassToFeatureClass_conversion("tmp_network_fc_lyr", "in_memory", "braids") arcpy.MakeFeatureLayer_management(r"in_memory\braids", "braids_lyr") # Add code values to network table arcpy.AddJoin_management(tmp_network_tbl, "ReachID", "braids_lyr", "ReachID", "KEEP_COMMON") arcpy.CalculateField_management(tmp_network_tbl, "FTR_CODE", FTR_CODE, "PYTHON_9.3") arcpy.RemoveJoin_management(tmp_network_tbl) # Clean up arcpy.Delete_management(tmp_network_fc) return
def exportAttributeJoin(inputFc, idFieldInputFc, inputTable, idFieldTable, workspace): arcpy.env.workspace = workspace #Creating a new table populated with values from the inputTable that were joined to the inputFC joinedTable = arcpy.AddJoin_management(inputFc, idFieldInputFc, inputTable, idFieldTable) #Creating a new feature class newFeature = arcpy.management.CreateFeatureclass(workspace, "newFeature.shp") #Copying data from the joined table to the new feature class output = arcpy.CopyFeatures_management(joinedTable, newFeature) print(output) #Printing results of how many records matched count1 = 0 count2 = 0 #Iterating through the input table to check if the values made it into the joined table #Counter created to track every item from the imput table #Second counter will not count items that are not within the joined table for e in inputTable: if e not in joinedTable: count1 = count1 + 1 count2 = count2 + 1 #Printing total number of matched records over total number of items in the input table print(count1 + " records matched of " + count2 + " total.")
def general_join(target_name, target_lay, joinfield1, joined, joinfield2, keep_common=True): # input: # target_name: Name of the shapefile to save the joined data to # target_lay: this is a layer as made with arcpy.MakeFeatureLayer_management # joinfield1: the name of the field in target_lay to join on # joined: this is a filename for the table/shapefile to join to # joinfield2: the name of the field in joined to join on # keep_common (optional): True means use KEEP_COMMON, False means KEEP_ALL # need the next line to make sure that column names join properly arcpy.env.qualifiedFieldNames = False path = os.path.dirname(target_name) # maintain path of target (same workspace) if keep_common: join_type = "KEEP_COMMON" else: join_type = "KEEP_ALL" print "\nJoining field {0:s} of {1:s} to field {2:s} of {3:s}".format( joinfield2, target_lay, joinfield2, joined) arcpy.AddJoin_management(target_lay, joinfield1, joined, joinfield2, join_type) # save back down the results if arcpy.Exists(os.path.join(path,'tmpjunkus.shp')): print 'Removing old version of tmpjunkus.shp' print u'This is a holding temporary file to save down {0:s}'.format(target_name) print 'tmpjunkus.shp will be deleted' arcpy.Delete_management(os.path.join(path, 'tmpjunkus.shp')) arcpy.CopyFeatures_management(target_lay, os.path.join(path, 'tmpjunkus.shp')) if arcpy.Exists(target_name): print 'Removing old version of %s' % target_name arcpy.Delete_management(target_name) arcpy.Rename_management(os.path.join(path, 'tmpjunkus.shp'), target_name)
def execute(self, params, messages): cpp_layer = params[0].valueAsText cpp_eo_reps = params[1].valueAsText cpp_lyr = arcpy.MakeFeatureLayer_management(cpp_layer,"cpp_lyr") cpp_eo_reps_lyr = arcpy.MakeFeatureLayer_management(cpp_eo_reps,"cpp_eo_reps_lyr") arcpy.AddJoin_management(cpp_lyr,"EO_ID",cpp_eo_reps,"EO_ID","KEEP_ALL") status_field = os.path.basename(cpp_layer)+".Status" null_field = os.path.basename(cpp_eo_reps)+".EO_ID" reviewby_field = os.path.basename(cpp_layer)+".ReviewBy" reviewdate_field = os.path.basename(cpp_layer)+".ReviewDate" reviewnotes_field = os.path.basename(cpp_layer)+".ReviewNotes" arcpy.SelectLayerByAttribute_management(cpp_lyr,"NEW_SELECTION","{} <> 'n' AND {} IS NULL".format(status_field,null_field)) today = date.today() date = today.strfttime('%d/%m/%Y') with arcpy.da.UpdateCursor(cpp_lyr,[reviewby_field,reviewdate_field,reviewnotes_field]) as cursor: for row in cursor: row[0] = "mmoore" row[1] = date row[2] = "Not approved because CPP no longer meets age, tracking, or accuracy criteria" cursor.updateRow(row)
def Pow_od_lini(): desired_distance1 = 68 temp_points = r'in_memory\points' #Tworzy punkt srodkowy wielok?ta, calculate near distance and angle and join this to polygons arcpy.FeatureToPoint_management( budynek_A, 'temp_points' ) #Tworzy klas? obiekt?w zawieraj?c? punkty wygenerowane z reprezentatywnych lokalizacji obiekt?w wej?ciowych. arcpy.Near_analysis( 'temp_points', droga, "", "NO_LOCATION", "ANGLE", "PLANAR" ) # Oblicza odleg?o?? i k?t mi?dzy obiektami wej?ciowymi a najbli?szymi obiektami w innej warstwie lub klasie obiekt?w. arcpy.MakeFeatureLayer_management( budynek_A, 'polygon_lyr' ) #Tworzy warstw? obiekt?w z wej?ciowej klasy obiekt?w lub pliku warstwy. arcpy.AddJoin_management( 'polygon_lyr', 'OBJECTID', 'temp_points', 'OBJECTID' ) #??czy warstw? z inn? warstw? lub tabel? na podstawie wsp?lnego pola (dodaje do nowej warstwy Budynek A informacje o odleg?osci do najblizszej drogi "NEAR_DIST" z warstwy punktowej ) #arcpy.CopyFeatures_management('polygon_lyr',budynek) #Kopiuje obiekty z wej?ciowej klasy obiekt?w lub warstwy do nowej klasy obiekt?w. arcpy.FeatureClassToFeatureClass_conversion('polygon_lyr', dane_wyjsc, 'Budynek_A') #Move the polygons with arcpy.da.UpdateCursor('Budynek_A', [ 'SHAPE@X', 'SHAPE@Y', 'temp_points_NEAR_DIST', 'temp_points_NEAR_ANGLE' ]) as cursor: for row in cursor: xnew = row[0] - (row[2] - desired_distance1) * math.sin( bearing_to_radians(row[3])) ynew = row[1] - (row[2] - desired_distance1) * math.cos( bearing_to_radians(row[3])) row[0] = xnew row[1] = ynew cursor.updateRow(row)
def fieldSubtract(): path = '' rainRes = 'F:/Test/Paper180614/Temp/' + 'rainResidual.shp' rainRGS = 'F:/Test/Paper180614/Temp/' + 'RainGauges.shp' A = 'rainRes' arcpy.MakeFeatureLayer_management(rainRes, A) B = 'RainGauges' arcpy.MakeFeatureLayer_management(rainRGS, B) arcpy.AddJoin_management(A, "FID", B, "FID") # arcpy.CalculateField_management(A, 'rainResidual.' + 'M201412','!' +'rainResidual.'+ 'M201412'+'!' +'-'+'!' + 'RainGauges.'+ 'M201412'+'!', # "PYTHON_9.3") # for y in range(15,18): # for m in range(1,13): # fieldName = 'M' + str((2000 + y) * 100 + m) # arcpy.CalculateField_management(A, 'rainResidual.' + fieldName, # '!' + 'rainResidual.' + fieldName + '!' + '-' + '!' + 'RainGauges.' + fieldName + '!', # "PYTHON_9.3") # for y in range(15,18): # for s in range(1,5): # fieldName='S'+str((2000+y)*10+s) # arcpy.CalculateField_management(A, 'rainResidual.' + fieldName, # '!' + 'rainResidual.' + fieldName + '!' + '-' + '!' + 'RainGauges.' + fieldName + '!', # "PYTHON_9.3") # for y in range(15, 18): # fieldName = 'Y' + str((2000 + y)) # arcpy.CalculateField_management(A, 'rainResidual.' + fieldName, # '!' + 'rainResidual.' + fieldName + '!' + '-' + '!' + 'RainGauges.' + fieldName + '!', # "PYTHON_9.3") arcpy.RemoveJoin_management(A)
def route_data(route, block): new_tbl = str(block)[:-4] + "_" + str(route)[:-4] arcpy.CopyRows_management(route, new_tbl) route_tbl = str(new_tbl) + "_tvw" arcpy.MakeTableView_management(new_tbl, route_tbl) arcpy.AddField_management(route_tbl, "GEOID10", "TEXT", "", "", 15, "GEOID10") arcpy.AddField_management(route_tbl, "SITE", "TEXT", "", "", 75, "SITE") expression1 = "(!Name![0:15])" expression2 = "(!Name![18:])" expression3 = "(!SITE![:-6])" arcpy.CalculateField_management(route_tbl, "GEOID10", expression1, "PYTHON_9.3") arcpy.CalculateField_management(route_tbl, "SITE", expression2, "PYTHON_9.3") arcpy.CalculateField_management(route_tbl, "SITE", expression3, "PYTHON_9.3") arcpy.AddJoin_management(block, "GEOID10", route_tbl, "GEOID10") field_name = str(route_tbl)[:-4] expression4 = "(!" + field_name + ".Total_Length!)" expression5 = "(!DIST!/5280)" arcpy.CalculateField_management(block, "DIST", expression4, "PYTHON_9.3") arcpy.CalculateField_management(block, "DIST", expression5, "PYTHON_9.3") arcpy.RemoveJoin_management(block) return
def FieldCombine(): rainIMERG = 'F:/Test/Paper180614/Data/IEMRG/' + 'RG83_IMERG.shp' # path = 'F:/Test/Paper180614/Data/IEMRG/RG83_IMERG.shp/' A = 'rainIMERG' arcpy.MakeFeatureLayer_management(rainIMERG, A) RGIMERGpath = 'F:/Test/Paper180614/Data/IEMRG/RG83/' arcpy.env.workspace = RGIMERGpath fs = arcpy.ListFeatureClasses() for i in range(0, len(fs), 1): if (len(fs[i]) == 16): curField = 'M' + str(fs[i][6:len(fs[i]) - 4]) print curField elif (len(fs[i]) == 15): curField = 'S' + str(fs[i][6:11]) print curField else: curField = 'Y' + str(fs[i][6:10]) print curField B = curField arcpy.MakeFeatureLayer_management(fs[i], B) arcpy.AddJoin_management(A, "FID", B, "FID") arcpy.CalculateField_management( A, 'RG83_IMERG.' + curField, "!" + fs[i][0:len(fs[i]) - 4] + ".RASTERVALU!", "PYTHON_9.3") arcpy.RemoveJoin_management(A)
def joinPL_to_CMS(environnement, inFeature, layerName, in_field, joinTable, joinField, outFeature, sql_clause): try: #Set environements settings env.workspace = environnement env.qualifiedFieldNames = False # Create a feature layer from the PL featureclass arcpy.MakeFeatureLayer_management(inFeature, layerName) print("Feature layer succesfully created") # Join the feature layer to a table arcpy.AddJoin_management(layerName, in_field, joinTable, joinField, "KEEP_COMMON") print("Join succesfully added") # Select desired features from PL with SQL_Clause arcpy.SelectLayerByAttribute_management(layerName, "NEW_SELECTION", sql_clause) print("Selection done succesfully") arcpy.CopyFeatures_management(layerName, outFeature) print("Copy done succesfully") except Exception as e: # If an error occurred, print line number and error message tb = sys.exc_info()[2] print "Line %i" % tb.tb_lineno print e.message
def lightningDay(clip_feature, grid_feature, cell_size): # if arcpy.Exists("lightningDay.tif"): # return # 建立圆形缓冲区 point_feature = ''.join(["point", str(cell_size)]) arcpy.FeatureToPoint_management(grid_feature, point_feature) buffer_feature = ''.join(["buffer", str(cell_size)]) buffer_distance = ''.join([str(cell_size), " ", "Kilometers"]) arcpy.Buffer_analysis(point_feature, buffer_feature, buffer_distance) intersect_feature = ''.join(["intersect", str(cell_size)]) arcpy.Intersect_analysis([clip_feature, buffer_feature], intersect_feature) field_frequency = ''.join(["FID_buffer", cell_size]) frequency_table1 = ''.join(["intersect", str(cell_size), "_frequency1"]) arcpy.Frequency_analysis(intersect_feature, frequency_table1, ["Date", field_frequency]) frequency_table2 = ''.join(["intersect", str(cell_size), "_frequency2"]) arcpy.Frequency_analysis(frequency_table1, frequency_table2, field_frequency) layer = ''.join(["point", str(cell_size)]) arcpy.MakeFeatureLayer_management(point_feature, layer) field_join = ''.join(["FID_buffer", cell_size]) arcpy.AddJoin_management(layer, "ORIG_FID", frequency_table2, field_join) day_points = "dayPoints" arcpy.FeatureToPoint_management(layer, day_points) lightning_day_raster = Spline(day_points, "FREQUENCY") lightning_day_raster.save("lightningDay")
def main(): """Join a table to a featureclass and select the desired attributes""" try: inFeatures = str(sys.argv[1]) layerName = str(sys.argv[2]) joinTable = str(sys.argv[3]) joinField = str(sys.argv[4]) outFeature = str(sys.argv[5]) # Set environment settings arcpy.env.workspace = os.getcwd() arcpy.env.qualifiedFieldNames = False # Create a feature layer from the vegtype featureclass arcpy.MakeFeatureLayer_management(inFeatures, layerName) # Join the feature layer to a table arcpy.AddJoin_management(layerName, joinField, joinTable, joinField, "KEEP_COMMON") # Copy the layer to a new permanent feature class arcpy.CopyFeatures_management(layerName, outFeature) except Exception as err: print(err.args[0])
def route_data(route, block): arcpy.AddMessage("Joining clostest routes to blocks...") new_tbl = str(block)[:-4] + "_" + str(route)[:-4] arcpy.CopyRows_management(route, new_tbl) route_tbl = str(new_tbl) + "_tvw" arcpy.MakeTableView_management(new_tbl, route_tbl) arcpy.AddField_management(route_tbl, "GEOID10", "TEXT", "", "", 15, "GEOID10") arcpy.AddField_management(route_tbl, "SITE", "TEXT", "", "", 75, "SITE") arcpy.CalculateField_management(route_tbl, "GEOID10", "(!Name![0:15])", "PYTHON_9.3") arcpy.CalculateField_management(route_tbl, "SITE", "(!Name![18:])", "PYTHON_9.3") arcpy.CalculateField_management(route_tbl, "SITE", "(!SITE![:-6])", "PYTHON_9.3") arcpy.AddJoin_management(block, "GEOID10", route_tbl, "GEOID10") field_name = str(route_tbl)[:-4] arcpy.CalculateField_management(block, "DIST", "(!" + field_name + ".Total_Length!)", "PYTHON_9.3") arcpy.CalculateField_management(block, "DIST", "(!DIST!/5280)", "PYTHON_9.3") arcpy.RemoveJoin_management(block) arcpy.AddMessage("...completed!") return