def updateFc(self, fc): ''' 更新要素类 ''' flag = False if isinstance(fc, AIFc): name = fc.getName() nalias = fc.getAlias() #新字段别名集合 nFields = fc.getFields() #新字段集合 nFieldNames = [nfld.getName() for nfld in nFields] #新字段名集合 if not AIUtils.isEmpty(name): if arcpy.Exists(name): ofc = self.queryFc(fc) oalias = ofc.getAlias() # 旧字段别名集合 oFields = ofc.getFields() # 旧字段集合 oFieldNames = [ofld.getName() for ofld in oFields] #旧字段名集合 try: # 更改别名 if not AIUtils.isEmpty(nalias) and not AIStrUtils.isEqual(oalias, nalias): arcpy.AlterAliasName(name, nalias) # 比对字段 # 删除字段(排除保留字段) for ofn in oFieldNames: if ofn not in nFieldNames and \ not AIStrUtils.isEqual(ofn, 'OBJECTID') and \ not AIStrUtils.isEqual(ofn, 'NAME') and \ not AIStrUtils.isEqual(ofn, 'SHAPE') and \ not 'AREA' in ofn and \ not 'LEN' in ofn: arcpy.DeleteField_management(name, ofn) # 添加字段(排除保留字段) for nfn in nFieldNames: if nfn not in oFieldNames: fldName = AIStrUtils.toUpper(nfn) if not AIStrUtils.isEqual(fldName, 'OBJECTID') and \ not AIStrUtils.isEqual(fldName, 'NAME') and \ not AIStrUtils.isEqual(fldName, 'SHAPE') and \ not 'AREA' in fldName and \ not 'LEN' in fldName: # 需要添加的字段 field = None # 查找需要添加的字段 for nfld in nFields: if AIStrUtils.isEqual(nfld.getName(), nfn): field = nfld break # 字段名 fldName = arcpy.ValidateFieldName(fldName, self.__wksName) # 字段别名 fldAlias = field.getAlias() # 字段类型 Integer--LONG Double--DOUBLE String--TEXT Date--DATE fldType = AIStrUtils.toUpper(AIStrUtils.trim(field.getFtype())) fldType = 'LONG' if AIStrUtils.isEqual(fldType, 'INTEGER') else \ 'TEXT' if AIStrUtils.isEqual(fldType, 'STRING') else fldType # 字段小数点位数(浮点型) fldScale = field.getScale() # 字段长度(文本型) fldLength = field.getLength() # 字段是否允许为空 fldIsNullable = field.getIsNullable() arcpy.AddField_management(name, fldName, fldType, None, fldScale, \ fldLength, fldAlias, fldIsNullable, False) flag = True except: raise else: raise Exception('featureclass not exist!') else: raise Exception('featureclass name cannot be empty!') else: raise Exception('input parameter type not correct!') return flag
fdsName = None if AIUtils.isDict(fcFds) and 'name' in fcFds.keys() and not AIUtils.isEmpty(fcFds['name']): fdsName = fcFds['name'] fdsName = fdsName if ('.' in fdsName) else fdsName if AIUtils.isEmpty(sdeUsername) else (sdeUsername + '.') + fdsName wksName = sdeOutFullName if AIUtils.isEmpty(fdsName) else os.path.join(sdeOutFullName, fdsName).replace('\\', '/') fcWkid = 4326 if not AIUtils.isInt(fcWkid) else fcWkid # 创建要素类 arcpy.CreateFeatureclass_management(wksName, fcName, fcFtype, None, "DISABLED", "DISABLED", arcpy.SpatialReference(fcWkid)) # 更改别名 if not AIUtils.isEmpty(fcAlias): arcpy.AlterAliasName(fcName, fcAlias) # 添加字段(排除保留字段) if AIUtils.isList(fcFields): for field in fcFields: keys = field.keys() fldName = AIStrUtils.toUpper(field['name']) if not AIStrUtils.isEqual(fldName, 'OBJECTID') and \ not AIStrUtils.isEqual(fldName, 'NAME') and \ not AIStrUtils.isEqual(fldName, 'SHAPE') and \ not 'AREA' in fldName and \ not 'LEN' in fldName: # 字段名 fldName = arcpy.ValidateFieldName(fldName, wksName) # 字段别名 fldAlias = fldName if not 'alias' in keys else field['alias'] # 字段类型 Integer--LONG Double--DOUBLE String--TEXT Date--DATE fldType = 'TEXT' if AIStrUtils.isEqual(AIStrUtils.toUpper(field['ftype']), 'STRING') else field['ftype'] # 字段小数点位数(浮点型) fldScale = 0 if not 'scale' in keys else field['scale'] # 字段长度(文本型) fldLength = 50 if not 'length' in keys else field['length']
def addFc(self, fc): ''' 保存要素类 ''' flag = False if isinstance(fc, AIFc): name = fc.getName() ftype = fc.getFtype() alias = fc.getAlias() wkid = fc.getWkid() fds = fc.getFds() if not AIUtils.isEmpty(name) and not AIUtils.isEmpty(ftype): if not arcpy.Exists(name): # 要素集名称 fdsName = None if not AIUtils.isEmpty(fds) and isinstance(fds, AIFds) and not AIUtils.isEmpty(fds.getName()): fdsName = fds.getName() fdsName = fdsName if ('.' in fdsName) else fdsName if AIUtils.isEmpty(self.__userName) else (self.__userName + '.') + fdsName wksName = self.__wksName if AIUtils.isEmpty(fdsName) else os.path.join(self.__wksName, fdsName).replace('\\', '/') fields = fc.getFields() sr = arcpy.SpatialReference(4326) if not AIUtils.isInt(wkid) else arcpy.SpatialReference(wkid) try: # 创建要素类 arcpy.CreateFeatureclass_management(wksName, name, ftype, None, "DISABLED", "DISABLED", sr) # 更改别名 if not AIUtils.isEmpty(alias): arcpy.AlterAliasName(name, alias) # 添加字段(排除保留字段) for field in fields: fldName = AIStrUtils.toUpper(field.getName()) if not AIStrUtils.isEqual(fldName, 'OBJECTID') and \ not AIStrUtils.isEqual(fldName, 'NAME') and \ not AIStrUtils.isEqual(fldName, 'SHAPE') and \ not 'AREA' in fldName and \ not 'LEN' in fldName: # 字段名 fldName = arcpy.ValidateFieldName(fldName, self.__wksName) # 字段别名 fldAlias = field.getAlias() # 字段类型 Integer--LONG Double--DOUBLE String--TEXT Date--DATE fldType = 'TEXT' if AIStrUtils.isEqual(AIStrUtils.toUpper(field.getFtype()), 'STRING') else field.getFtype() # 字段小数点位数(浮点型) fldScale = field.getScale() # 字段长度(文本型) fldLength = field.getLength() # 字段是否允许为空 fldIsNullable = field.getIsNullable() arcpy.AddField_management(name, fldName, fldType, None, fldScale, \ fldLength, fldAlias, fldIsNullable, False) flag = True except: raise else: raise Exception('featureclass name cannot be duplicated!') else: raise Exception('featureclass name and geometry type cannot be empty!') else: raise Exception('input parameter type not correct!') return flag
# 比对字段 if AIUtils.isList(oFieldNames) and AIUtils.isList(nFieldNames): # 删除字段(排除保留字段) for ofn in oFieldNames: if ofn not in nFieldNames and \ not AIStrUtils.isEqual(ofn, 'OBJECTID') and \ not AIStrUtils.isEqual(ofn, 'NAME') and \ not AIStrUtils.isEqual(ofn, 'SHAPE') and \ not 'AREA' in ofn and \ not 'LEN' in ofn: arcpy.DeleteField_management(fcName, ofn) # 添加字段(排除保留字段) for nfn in nFieldNames: if nfn not in oFieldNames: fldName = AIStrUtils.toUpper(nfn) if not AIStrUtils.isEqual(fldName, 'OBJECTID') and \ not AIStrUtils.isEqual(fldName, 'NAME') and \ not AIStrUtils.isEqual(fldName, 'SHAPE') and \ not 'AREA' in fldName and \ not 'LEN' in fldName: # 需要添加的字段 field = None # 查找需要添加的字段 for nfld in nFields: if AIStrUtils.isEqual(nfld['name'], nfn): field = nfld break keys = field.keys() # 字段名 fldName = arcpy.ValidateFieldName(fldName, wksName)