# -*- coding: utf-8 -*-
import os
import sys
import log
import uuid
import json
import arcpy
import appconfig
from arcpy import env 
import importlib,sys
importlib.reload(sys)



#线性测量单位关键字：METERS(米)、KILOMETERS(千米)
#面积测量单位关键字：SQUAREMETERS(平方米)、SQUAREKILOMETERS(平方千米)

# 缓冲分析
def Buffer_analysis(inList, comObj, outGdb, xzq):
    in_node = inList[0]
    in_features = in_node['sjy']
    out_feature_class = r"{0}\M{1}_Buffer_{2}".format(outGdb,xzq,in_node["key"])

    #设置工作空间
    if in_node['category'] == "sjy":
        env.workspace = appconfig.SDE[in_node['sjylx']]
    else:
        env.workspace = outGdb
    #SDE+os.sep+table
    arcpy.Buffer_analysis(in_features, out_feature_class, comObj["buffer"])
    return out_feature_class

# 条件筛选
def Select_analysis(inList, comObj, outGdb, xzq):
    in_node = inList[0]
    in_features = in_node['sjy']
    out_feature_class = r"{0}\M{1}_Select_{2}".format(outGdb,xzq,in_node["key"])

    #设置工作空间
    if in_node['category'] == "sjy":
        env.workspace = appconfig.SDE[in_node['sjylx']]
    else:
        env.workspace = outGdb 
    arcpy.Select_analysis(in_features, out_feature_class, comObj["gltj"])
    return out_feature_class

# 相交分析
def Intersect_analysis(inList, comObj, outGdb, xzq):
    in_features = []
    out_feature_class = r"{0}\M{1}_Intersect_".format(outGdb,xzq)
    env.workspace = outGdb   #设置工作空间
    for in_node in inList:
        out_feature_class += "_" + in_node["key"]
        if in_node['category'] == "sjy":
            sde = appconfig.SDE[in_node['sjylx']]
            in_features.append(sde + os.sep + in_node['sjy'])
        else:
            in_features.append(in_node['sjy'])

    arcpy.Intersect_analysis(in_features, out_feature_class)
    return out_feature_class

  # 相交取反分析
def SymDiff_analysis(inList, comObj, outGdb, xzq):
    in_features = []
    out_feature_class = r"{0}\M{1}_SymDiff_".format(outGdb,xzq)
    env.workspace = outGdb   #设置工作空间
    for in_node in inList:
        out_feature_class += "_" + in_node["key"]
        if in_node['category'] == "sjy":
            sde = appconfig.SDE[in_node['sjylx']]
            in_features.append(sde + os.sep + in_node['sjy'])
        else:
            in_features.append(in_node['sjy'])

    arcpy.SymDiff_analysis(in_features, out_feature_class)
    return out_feature_class

# 融合
def Dissolve_management(inList, comObj, outGdb, xzq):
    in_node = inList[0]
    in_features = in_node['sjy']
    out_feature_class = r"{0}\M{1}_Dissolve_{2}".format(outGdb,xzq,in_node["key"])

    #设置工作空间
    if in_node['category'] == "sjy":
        env.workspace = appconfig.SDE[in_node['sjylx']]
    else:
        env.workspace = outGdb   
    arcpy.Dissolve_management(in_features, out_feature_class)
    return out_feature_class
    


#--------------------------------------------------
# 添加字段 
def AddField_management(in_table, field_name, field_type):
    arcpy.AddField_management(in_table, field_name, field_type)

# 计算字段 
def CalculateField_management(in_table, field, pyExp):
    arcpy.CalculateField_management(in_table, field, pyExp, "PYTHON_9.3")

# 获取指定列的值
def GetFieldValueList(inTable,inField):
    value_list=[]
    rows=arcpy.da.SearchCursor(inTable,inField)
    for row in rows:
        value_list.append(row[0])
    del row
    del rows
    return value_list


