def create_index(fn, fields, index_name): if (isOptimizable(fn)): if index_name not in [index.name for index in arcpy.ListIndexes(fn)]: log('Creating index ' + index_name + ' in ' + fn) arcpy.AddIndex_management(fn, fields, index_name, 'UNIQUE', 'ASCENDING') return
def get_indexes(self): """Get geodatabase table indexes as ordered dicts.""" indexes = [] for index in arcpy.ListIndexes(self.path): od = OrderedDict() for k, v in GDB_TABLE_INDEX_PROPS.items(): if k == 'fields': od[v] = ', '.join( sorted((f.name for f in getattr(index, k, '')))) else: od[v] = getattr(index, k, '') indexes.append(od) return indexes
def createIndex(targetpath,indexFields = []): """创建空间索引和属性索引,创建属性索引之前判断是否存在该索引""" arcpy.AddSpatialIndex_management(targetpath) indexlist = [str(index.name.lower()) for index in arcpy.ListIndexes(targetpath)] for field in indexFields: if field not in indexlist: try: arcpy.AddIndex_management(targetpath,field,field) except arcpy.ExecuteError: arcpy.GetMessages()
def efficient_merge(feature_class_list, output_fc, filter=''): fc_count = len(feature_class_list) all_exist_test = all(arcpy.Exists(fc) for fc in feature_class_list) # EXECUTE # Start with FC containing largest extent to prevent spatial grid errors descriptions = [arcpy.Describe(fc).extent for fc in feature_class_list] fc_areas = [ int(d.XMax - d.XMin) * int(d.YMax - d.YMin) for d in descriptions ] index = [i for i, x in enumerate(fc_areas) if x == max(fc_areas)] first_fc = feature_class_list[index[0]] indexes = arcpy.ListIndexes(first_fc) feature_class_list.remove(first_fc) # This is a fast and stable merge method for this number of features compared to arcpy Merge if all_exist_test: print( "Beginning merge of {} feature classes, copying first feature class to output..." .format(fc_count)) arcpy.Select_analysis(first_fc, output_fc, filter) arcpy.SetLogHistory = False # speeds up iterative updates, won't write to geoprocessing for every step insertRows = arcpy.da.InsertCursor(output_fc, ["SHAPE@", "*"]) for fc in feature_class_list: searchRows = arcpy.da.SearchCursor(fc, ["SHAPE@", "*"], filter) counter = 0 for searchRow in searchRows: insertRows.insertRow(searchRow) counter += 1 del searchRow, searchRows print("Merged {0} features from {1}".format(counter, fc)) del insertRows arcpy.SetLogHistory = True # Rebuild indexes try: arcpy.AddIndex_management(output_fc, 'Permanent_Identifier', 'IDX_Permanent_Identifier') except: arcpy.AddWarning( 'Could not build Permanent_Identifier index because there is no such field.' ) else: print( "ERROR: One or more feature class paths is not valid. Merged feature class not created." )
def has_index_on_pid(fc): indices = arcpy.ListIndexes(fc) print('Listing all indices:') for index in indices: print("Field : {0}".format(index.fields[0].name)) print("IsAscending : {0}".format(index.isAscending)) print("IsUnique : {0}".format(index.isUnique)) if 'wdpa_pid'.upper() not in [ index.fields[0].name.upper() for index in indices ]: print('No index on wdpa_pid, please add an index first') return False return True
def Inventory(): arcpy.env.workspace = ng911 print "Geodatabase Name" for db in gdb: print db print "Feature Dataset Name" for ds in datasets: print " " + ds print "Tables" for table in tables: tc = arcpy.GetCount_management(table) print " " + (table) + ", " + str(tc) + " records" print "Feature Classes" for fc in fcs: fcc = arcpy.GetCount_management(fc) indxs = arcpy.ListIndexes(fc) print " " + (fc) + ", " + str(fcc) + " records" print " INDEXES:" for index in indxs: print(" Name : {0}".format(index.name)) print(" IsUnique : {0}".format(index.isUnique))
def RecreateIndexes(CenterlineSequence, CalibrationPoint, Route, RouteIDs): tableArray = [[ CenterlineSequence, ['ROUTEID', 'NETWORKID', 'FROMDATE', 'TODATE'] ]] tableArray += [[ CalibrationPoint, ['ROUTEID', 'NETWORKID', 'FROMDATE', 'TODATE', 'MEASURE'] ]] tableArray += [[Route, ['FROMDATE', 'TODATE'] + RouteIDs]] for indexGroup in tableArray: for fieldName in indexGroup[1]: tableName = os.path.basename(indexGroup[0]) indexName = 'IX_%s' % (fieldName) if len(arcpy.ListIndexes(indexGroup[0], indexName)) == 1: arcpy.RemoveIndex_management(indexGroup[0], indexName) try: arcpy.AddIndex_management(indexGroup[0], fieldName, indexName) arcpy.AddMessage('Created index %s on field %s in table %s' % (indexName, fieldName, tableName)) except: arcpy.AddWarning( 'Unable to create index %s on field %s in table %s' % (indexName, fieldName, tableName))
def querySDELayer(self): fcDesc = arcpy.Describe(self.fcName) spatial_ref = fcDesc.spatialReference spatialIndexes = arcpy.ListIndexes(self.fcName) if spatial_ref.name == "Unknown": print("{0} has an unknown spatial reference".format(self.fcName)) else: srName = spatial_ref.name spatRefString = spatial_ref.exportToString() srDomain = spatial_ref.domain.split(" ") srPCSCode = spatial_ref.PCSCode srGCSCode = spatial_ref.GCSCode zUnits = spatial_ref.ZFalseOriginAndUnits.split(" ") xyTolerance = spatial_ref.XYTolerance xyResolution = spatial_ref.XYResolution siExists = fcDesc.hasSpatialIndex try: conn = arcpy.ArcSDESQLExecute(workspc) sql = "SELECT * FROM {0} WHERE table_name = '{1}'".format(self.layerTable, self.fcName) sde_return = conn.execute(sql) except: try: conn = arcpy.ArcSDESQLExecute(workspc) sql = "SELECT * FROM {0} WHERE table_name = '{1}'".format(self.layerTable, self.fcName) sde_return = conn.execute(sql) except: try: conn = arcpy.ArcSDESQLExecute(workspc) sqlOra = "SELECT * FROM {0} WHERE table_name = '{1}'".format(self.layerTable, self.fcName) sde_return = conn.execute(sqlOra) except Exception as err: print sql print err sde_return = False if len(str(sde_return)) > 0: if sde_return == True: print "\tNo Layer ID found" else: for val in sde_return: print "\tLayer ID: {0}".format(val[0]) print "\tLayer Description: {0}".format(val[1]) print "\tOwner: {0}".format(val[4]) print "\tShape Type: {0}".format(fcDesc.shapeType) if spatial_ref.isHighPrecision == True: print "\tHigh Precision: True" else: print "\tHigh Precision: False" if val[6] != 1213464721: print "eflag value: {0}".format(str(val[6])) print "\tI/O Mode: Normal" else: print "eflag value: {0}".format(str(val[6])) print "\tI/O mode: LOAD ONLY" print "Spatial Reference Properties:" print "\tLayer Envelope:" print "\t\tMin X: {0}\t Min Y: {1}".format(val[11], val[12]) print "\t\tMax X: {0}\t Max Y: {1}".format(val[13], val[14]) if siExists == True: print "\tSpatial Index: Yes" for index in spatialIndexes: if index.name[0] == "S" or index.name[0] == 's': print "\t\t--> " + index.name else: print "\tSpatial Index: No" print "\t\tGrid 1: {0}".format(val[8]) print "\t\tGrid 2: {0}".format(val[9]) print "\t\tGrid 3: {0}".format(val[10]) print "\tXY Tolerance: {0}".format(xyTolerance) print "\tXY Resolution %d.5" % xyResolution print "\tXY Domain:" print "\t\t" + srDomain[0] + " " + srDomain[2] print "\t\t" + srDomain[1] + " " + srDomain[3] print "\tZ Offset: {0}".format(zUnits[0]) print "\tZ Units: {0}".format(zUnits[1]) print "\tCoordinate System : {0}".format(srName) print "\tSpatial Reference: {0}".format(spatRefString) if srPCSCode > 0: print "\t Projected Coordinate System Code:: {0}".format(srPCSCode) else: print "\tUndefined Projected Coordinate System" if srGCSCode > 0: print "\tGeographic Coordinate System Code:: {0}".format(srGCSCode) else: print "\tUndefined Geographic Coordinate System" print "\tCreation Date: " + datetime.datetime.fromtimestamp(int(val[19])).strftime('%Y-%m-%d') else: print "Error reading layer ID" return sde_return
cname = ca[2] for cr in ca[3:]: cname = cname + "_" + cr cty = County(cname, cfips, jfield) clist.append(cty) try: print "Fetching list of layers..." sys.stdout.flush() jList = arcpy.ListFeatureClasses("Parcels_*_Joined", "Polygon") for c in clist: idxList = [idx.name for idx in arcpy.ListIndexes(c.layerName)] # add index on parcel ID field to original layer if not c.indexName in idxList: print "Adding index on " + c.parcelField + " to " + c.layerName + "..." sys.stdout.flush() arcpy.AddIndex_management(c.layerName, c.parcelField, c.indexName) print arcpy.GetMessages() sys.stdout.flush() # copy source layer to destination layer if not c.destLayer in jList: print "Copying " + c.layerName + " to " + c.destLayer + "..." sys.stdout.flush() arcpy.CopyFeatures_management(c.layerName, c.destLayer) print arcpy.GetMessages()
def remove_indexes (table, index_spec): existing_indexes = [i.name for i in arcpy.ListIndexes(table)] for field, index in index_spec: if index in existing_indexes: log_debug ('Removing index %s from field %s in table %s' % (index, field, table)) arcpy.RemoveIndex_management(table, [index])
def add_indexes (table, index_spec): existing_indexes = [i.name for i in arcpy.ListIndexes(table)] for field, index in index_spec: if index not in existing_indexes: log_debug ('Adding index %s to field %s in table %s' % (index, field, table)) arcpy.management.AddIndex(table, [field], index, "NON_UNIQUE", "NON_ASCENDING")
# -*- coding: utf-8 -*- """ Created on Wed Feb 13 09:14:38 2019 @author: tvolpe """ import arcpy arcpy.env.workspace = 'Q:/Projects/Parks/ParkInventory/2018/2018 Park INV' fc = 'Q:/Projects/Parks/ParkInventory/2018/2018 Park INV/2015_2018_Park_INV/test_data.shp' indexes = arcpy.ListIndexes(fc) for index in indexes: print("Name : {0}".format(index.name)) print("IsAscending : {0}".format(index.isAscending)) print("IsUnique : {0}".format(index.isUnique))
def main_func(): start = datetime.datetime.now() err_msg = None global emailAttachments try: log_path = os.path.join(sys.path[0], 'logs') log, logfile = etgLib.create_log(log_path, log_name) if log == None: exit_sys(log, "can't create a log file", start) emailAttachments = [logfile] # set workspace arcpy.env.workspace = contextdSdePath etgLib.log_start(log) etgLib.log_info(log, "script parameters:") etgLib.log_info(log, "------------------------") # etgLib.log_info(log, "working folder: {0}".format(wkgFolder)) etgLib.log_info(log, "pre prod CONTEXTSdePath: {0}".format(contextdSdePath)) ## =============================================================== ## Process: add attribute index to PARCEL_LABEL_PT_1 feature class ## =============================================================== etgLib.log_info( log, 'add attribute indexes to PARCEL_LABEL_PT_1 feature class in CONTEXT sde...', True) fc_name = contextSdePrefix + fc_parcel_label_pt inFCpath = os.path.join(contextdSdePath, fc_name) if arcpy.Exists(inFCpath) == False: exit_sys( log, "{} does not exist in context sde database".format(fc_name), start, sendMail) indexes = arcpy.ListIndexes( inFCpath) # ----> Lists both attribute and spatial indexes!!! indexPrefix = None etgLib.log_info(log, 'get the existing index prefix ...') for index in indexes: m = re.search(r'^R(\d+)_pk', index.name) if m != None: indexPrefix = m.group(1) break if indexPrefix == None: exit_sys(log, "no index prefix found in the existing indexes", start, sendMail) etgLib.log_info(log, 'index prefix : {0}'.format(indexPrefix)) list_index_to_create = [] list_index_to_create.append( ("Owneridx", ["owner1", "owner2", "owner3", "owner4"])) # field names are case sensitive! list_index_to_create.append(("VestSrvidx", ["VESTING_SURVEY_ID"])) list_index_to_create.append(("FullAppidx", ["FULL_APP"])) list_index_to_create.append(("Parcelidx", ["PARCEL_ID"])) list_index_to_create.append( ("Titleidx", ["title1", "title2", "title3", "title4"])) for index_to_create in list_index_to_create: newIndex = "R{0}_{1}".format(indexPrefix, index_to_create[0]) #print('InFCPath: {0}, Index name: {1}'.format(inFCpath,newIndex)) fields = index_to_create[1] etgLib.log_info(log, 'creating index : {0}'.format(newIndex)) arcpy.AddIndex_management(inFCpath, fields, newIndex, "NON_UNIQUE", "NON_ASCENDING") ## =============================================================== ## Process: add attribute index to ROAD_CL_Dissove_1 feature class ## =============================================================== etgLib.log_info( log, 'add attribute indexes to ROAD_CL_Dissove_1 feature class...', True) fc_name = contextSdePrefix + fc_road_cl_dissolve inFCpath = os.path.join(contextdSdePath, fc_name) if arcpy.Exists(inFCpath) == False: exit_sys( log, "{} does not exist in context sde database".format(fc_name), start, sendMail) indexes = arcpy.ListIndexes( inFCpath) # ----> Lists both attribute and spatial indexes!!! indexPrefix = None etgLib.log_info(log, 'get the existing index prefix ...') for index in indexes: m = re.search(r'^R(\d+)_pk', index.name) if m != None: indexPrefix = m.group(1) break if indexPrefix == None: exit_sys(log, "no index prefix found in the existing indexes", start, sendMail) etgLib.log_info(log, 'index prefix : {0}'.format(indexPrefix)) list_index_to_create = [] list_index_to_create.append(("Locidx", ["RD_LOC"])) list_index_to_create.append(("Rdnameidx", ["ROAD_NAME1"])) list_index_to_create.append(("Locatonidx", ["LOCATION1"])) for index_to_create in list_index_to_create: newIndex = "R{0}_{1}".format(indexPrefix, index_to_create[0]) fields = index_to_create[1] etgLib.log_info(log, 'creating index : {0}'.format(newIndex)) arcpy.AddIndex_management(inFCpath, fields, newIndex, "NON_UNIQUE", "NON_ASCENDING") except Exception as e: err_msg = "ERROR while running {0}: {1}".format(script_name, e) etgLib.log_close(log, start) print("Finished!!! Please check the result in ArcMap or ArcCatalog") if sendMail: if err_msg != None: emailSubject = 'Run {} - Failed'.format(script_name) else: emailSubject = 'Run {} - Successful'.format(script_name) etgLib.send_email(emailFrom, emailTo, emailSubject, emailText, emailAttachments, smtpServer)
sys.stdout.flush() if not fieldExists(layerName, "Shape_Length"): print "Adding shape length to " + layerName + "..." sys.stdout.flush() arcpy.AddGeometryAttributes_management(layerName, "PERIMETER_LENGTH") print arcpy.GetMessages() sys.stdout.flush() print "Renaming length field for " + layerName + "..." sys.stdout.flush() arcpy.AlterField_management(layerName, "PERIMETER", "Shape_Length") print arcpy.GetMessages() sys.stdout.flush() ida = [idx.name for idx in arcpy.ListIndexes(layerName)] for idx in idxList: ia = idx.split(':') iname = ia[0] ifield = ia[1] if not iname in ida: print "Adding index " + iname + "..." sys.stdout.flush() arcpy.AddIndex_management(layerName, ifield, iname) print arcpy.GetMessages() sys.stdout.flush() print "Setting coordinate system..." sys.stdout.flush() arcpy.DefineProjection_management(layerName, outSr)