def BuildSpatialIndexParams(self, p_InputWS): env.workspace = p_InputWS ds = [''] for fds in arcpy.ListDatasets(): ds.append("/" + str(fds)) for currentDS in ds: env.workspace = p_InputWS + currentDS self.Message(p_InputWS + currentDS + ":-----------------------------") for fc in arcpy.ListFeatureClasses(): desc = arcpy.Describe(fc) indexGrid = [] try: resultIdx = arcpy.CalculateDefaultGridIndex_management(fc) for i in range(0, resultIdx.outputCount): indexGrid.append(resultIdx.getOutput(i)) except: pass result = arcpy.GetCount_management(fc) count = int(result.getOutput(0)) gridVal = self.RulesForCalculateDefaultGridIndex( int(indexGrid[0]), count) messageText = ("def_array.append(['%s/%s' , '%s' , %s, %s])" ) % (currentDS, fc, desc.shapeType, gridVal, count) self.Message(messageText, 1)
def SetDefaultSpatialIndex(self, p_InputWS): env.workspace = p_InputWS ds = [''] for fds in arcpy.ListDatasets(): ds.append("/" + str(fds)) for currentDS in ds: env.workspace = p_InputWS + currentDS messageText = str(p_InputWS + currentDS) + ":-----------------------------" self.Message(messageText, 0) for fc in arcpy.ListFeatureClasses(): desc = arcpy.Describe(fc) if desc.hasSpatialIndex == False: indexGrid = [] result = arcpy.GetCount_management(fc) count = int(result.getOutput(0)) try: resultIdx = arcpy.CalculateDefaultGridIndex_management( fc) for i in range(0, resultIdx.outputCount): indexGrid.append(float(resultIdx.getOutput(i))) spIdx = self.RulesForCalculateDefaultGridIndex( int(indexGrid[0]), count) arcpy.AddSpatialIndex_management(fc, spIdx) messageText = ( "Neuer Index def_array.append(['%s/%s' , '%s' , %s])" ) % (currentDS, fc, desc.shapeType, spIdx) self.Message(messageText, 0) except: messageText = ( "Index '%s/%s' , '%s' von Hand setzen! Vorschlag ArcGIS:%s" ) % (currentDS, fc, desc.shapeType, indexGrid) self.Message(messageText, 2)
def calculate_grid_size(fc): ''' Berechnet die Grid Size einer Feature Class. Der von arcpy berechnete Wert wird dabei auf eine ganze Zahl gerundet. :param fc: Feature Class, für die die Grid Size gerechnet wird. ''' fc_count = int(arcpy.GetCount_management(fc)[0]) grid_size = 0 if fc_count > 0: # CalculateDefaultGridIndex_management bricht mit Fehler ab, # wenn die Featureclass nur aus Features mit leerer Geometrie # besteht. Kann bei den Nachführungstabellen vorkommen. try: result = arcpy.CalculateDefaultGridIndex_management(fc) grid_size = float(result.getOutput(0)) grid_size = int(round(grid_size)) except: grid_size = 0 return grid_size
def csv2section(): arcpy.env.overwriteOutput = True inPt = arcpy.GetParameterAsText(0) outFeature = arcpy.GetParameterAsText(1) X1 = arcpy.GetParameterAsText(2) Y1 = arcpy.GetParameterAsText(3) X2 = arcpy.GetParameterAsText(4) Y2 = arcpy.GetParameterAsText(5) reserveField = arcpy.GetParameterAsText(6) try: outPath, outFC = os.path.split(outFeature) #change C:\Users\leizengxiang\Desktop\drawCsvInArcgis to your directory, and change the wgs84.prj to your projection file arcpy.CreateFeatureclass_management(outPath, outFC, "POLYLINE", "", "DISABLED", "ENABLED", "C:\Users\leizengxiang\Desktop\drawCsvInArcgis\\wgs84.prj") if reserveField: field = arcpy.ListFields(inPt, reserveField)[0] arcpy.AddField_management(outFeature, field.name, field.type) oCur, iCur, sRow, feat = None, None, None, None shapeName = "Shape" idName = "id" oCur = arcpy.SearchCursor(inPt) iCur = arcpy.InsertCursor(outFeature) array = arcpy.Array() ID = -1 PID = 0 LID = 0 if reserveField: RESERVE = 0 TEMPX1 = 0 TEMPX2 = 0 TEMPY1 = 0 TEMPY2 = 0 for sRow in oCur: TEMPX1 = sRow.getValue(X1) TEMPX2 = sRow.getValue(X2) TEMPY1 = sRow.getValue(Y1) TEMPY2 = sRow.getValue(Y2) (TEMPX1,TEMPY1,TEMPX2,TEMPY2)=getXY(TEMPX1,TEMPY1,TEMPX2,TEMPY2) pt1=arcpy.Point(TEMPX1,TEMPY1,None, None, PID) PID += 1 pt2=arcpy.Point(TEMPX2,TEMPY2,None, None, PID) PID += 1 array.add(pt1) array.add(pt2) if reserveField: RESERVE = sRow.getValue(reserveField) feat = iCur.newRow() feat.setValue(shapeName, array) LID += 1 if reserveField: feat.setValue(reserveField, RESERVE) iCur.insertRow(feat) array.removeAll() except Exception as err: arcpy.AddError(err[0]) finally: if oCur: del oCur if iCur: del iCur if sRow: del sRow if feat: del feat try: # Update the spatial index(es) # r = arcpy.CalculateDefaultGridIndex_management(outFeature) arcpy.AddSpatialIndex_management(outFeature, r.getOutput(0), r.getOutput(1), r.getOutput(2)) except: pass
def csv2line(): arcpy.env.overwriteOutput = True inPt = arcpy.GetParameterAsText(0) outFeature = arcpy.GetParameterAsText(1) X = arcpy.GetParameterAsText(2) Y = arcpy.GetParameterAsText(3) Z = arcpy.GetParameterAsText(4) idField = arcpy.GetParameterAsText(5) reserveField = arcpy.GetParameterAsText(6) maxvField = "MAX_V" try: outPath, outFC = os.path.split(outFeature) #change C:\Users\leizengxiang\Desktop\drawCsvInArcgis to your directory, and change the wgs84.prj to your projection file arcpy.CreateFeatureclass_management( outPath, outFC, "POLYLINE", "", "DISABLED", "ENABLED", "C:\Users\leizengxiang\Desktop\drawCsvInArcgis\\wgs84.prj") field1 = arcpy.ListFields(inPt, idField)[0] arcpy.AddField_management(outFeature, field1.name, field1.type) if reserveField: field2 = arcpy.ListFields(inPt, reserveField)[0] arcpy.AddField_management(outFeature, field2.name, field2.type) # Add v arcpy.AddField_management(outFeature, maxvField, "double") oCur, iCur, sRow, feat = None, None, None, None shapeName = "Shape" idName = "id" oCur = arcpy.SearchCursor(inPt) iCur = arcpy.InsertCursor(outFeature) array = arcpy.Array() ID = -1 PID = 0 LID = 0 if reserveField: RESERVE = 0 MAXV = 0 TEMPV = 0 X1 = 0 X2 = 0 Y1 = 0 Y2 = 0 Z1 = 0 Z2 = 0 for sRow in oCur: X2 = sRow.getValue(X) Y2 = sRow.getValue(Y) Z2 = sRow.getValue(Z) pt = arcpy.Point(X2, Y2, Z2, None, PID) PID += 1 currentValue = sRow.getValue(idField) if ID == -1: ID = currentValue if reserveField: RESERVE = sRow.getValue(reserveField) X1 = X2 Y1 = Y2 Z1 = Z2 if ID <> currentValue: if array.count >= 2: feat = iCur.newRow() feat.setValue(idField, ID) feat.setValue(shapeName, array) feat.setValue(idName, LID) LID += 1 if reserveField: feat.setValue(reserveField, RESERVE) feat.setValue(maxvField, MAXV) iCur.insertRow(feat) else: arcpy.AddIDMessage("WARNING", 1059, str(ID)) X1 = X2 Y1 = Y2 Z1 = Z2 MAXV = 0 array.removeAll() if reserveField: RESERVE = sRow.getValue(reserveField) if (Z1 < Z2) and (X1 != X2 or Y1 != Y2): TEMPV = 0.36 * getdis(X1, Y1, X2, Y2) / (Z2 - Z1) #KM/H else: TEMPV = 0 MAXV = getmax(MAXV, TEMPV) array.add(pt) X1 = X2 Y1 = Y2 Z1 = Z2 ID = currentValue if array.count > 1: feat = iCur.newRow() feat.setValue(idField, currentValue) feat.setValue(shapeName, array) feat.setValue(idName, LID) if reserveField: feat.setValue(reserveField, RESERVE) feat.setValue(maxvField, MAXV) iCur.insertRow(feat) else: arcpy.AddIDMessage("WARNING", 1059, str(ID)) array.removeAll() except Exception as err: arcpy.AddError(err[0]) finally: if oCur: del oCur if iCur: del iCur if sRow: del sRow if feat: del feat try: # Update the spatial index(es) # r = arcpy.CalculateDefaultGridIndex_management(outFeature) arcpy.AddSpatialIndex_management(outFeature, r.getOutput(0), r.getOutput(1), r.getOutput(2)) except: pass
def convertPointsToLine(inPts, outFeatures, IDField, cursorSort, close): try: # Assign empty values to cursor and row objects iCur, sRow, feat = None, None, None desc = arcpy.Describe(inPts) shapeName = desc.shapeFieldName # Create the output feature class outPath, outFC = os.path.split(outFeatures) arcpy.CreateFeatureclass_management(outPath, outFC, "POLYLINE", "", getZM("outputMFlag", desc.hasM), getZM("outputZFlag", desc.hasZ), inPts) outShapeName = arcpy.Describe(outFeatures).shapeFieldName # If there is an IDField, add the equivalent to the output if IDField: f = arcpy.ListFields(inPts, IDField)[0] fName = arcpy.ValidateFieldName(f.name, outPath) arcpy.AddField_management(outFeatures, fName, f.type, f.precision, f.scale, f.length, f.aliasName, f.isNullable, f.required, f.domain) # Open an insert cursor for the new feature class iCur = arcpy.InsertCursor(outFeatures) # Create an array needed to create features array = arcpy.Array() # Initialize a variable for keeping track of a feature's ID. ID = -1 fields = shapeName if cursorSort: fields += ";" + cursorSort for sRow in arcpy.gp.SearchCursor(inPts, "", None, fields, cursorSort, arcpy.env.extent): #for sRow in arcpy.gp.searchCursor() pt = sRow.getValue(shapeName).getPart(0) if IDField: currentValue = sRow.getValue(IDField) else: currentValue = None if ID == -1: ID = currentValue if ID <> currentValue: if array.count >= 2: # To close, add first point to the end # if close: array.add(array.getObject(0)) feat = iCur.newRow() if IDField: if ID: #in case the value is None/Null feat.setValue(IDField, ID) feat.setValue(outShapeName, array) iCur.insertRow(feat) else: arcpy.AddIDMessage("WARNING", 1059, unicode(ID)) array.removeAll() array.add(pt) ID = currentValue # Add the last feature if array.count > 1: # To close, add first point to the end if close: array.add(array.getObject(0)) feat = iCur.newRow() if IDField: if ID: #in case the value is None/Null feat.setValue(IDField, currentValue) feat.setValue(outShapeName, array) iCur.insertRow(feat) else: arcpy.AddIDMessage("WARNING", 1059, unicode(ID)) array.removeAll() except Exception as err: import traceback arcpy.AddError( traceback.format_exception_only(type(err), err)[0].rstrip()) finally: if iCur: del iCur if sRow: del sRow if feat: del feat try: # Update the spatial index(es) # r = arcpy.CalculateDefaultGridIndex_management(outFeatures) arcpy.AddSpatialIndex_management(outFeatures, r.getOutput(0), r.getOutput(1), r.getOutput(2)) except: pass