# -*- coding: utf-8 -*-
import os
import sys
import json
import utilsgis
import utils
import log
import importlib, sys

importlib.reload(sys)


def run(fun, data):
    try:
        eval("{0}".format(fun))(data)
        # 输出成功信息 做交互用
        print("####OK####")
    except:
        msg = str(sys.exc_info()).decode('string-escape')
        print("####ERROR####" + msg)
        log.error(msg)


# shp转json
def shp2geojson(data):
    shpfile = data["shpfile"]
    geojson = data["geojson"]
    utilsgis.shp2geojson(shpfile, geojson)


# json转shp
def esriJson2shp(data):
    jsonfile = data["json"]
    shpfile = data["shpfile"]
    utilsgis.esriJson2shp(jsonfile, shpfile)


# json存入SDE
def esriJson2sde(data):
    jsonfile = data["json"]
    shpfile = data["table"]
    utilsgis.esriJson2sde(jsonfile, shpfile)


# shp存入SDE
def shp2sde(data):
    shpfile = data["shpfile"]
    table = data["table"]
    fields = None
    if data.get('fields') != None:
        fields = data["fields"];
    utilsgis.shp2sde(shpfile, table, fields)


# SDE转shp
def sde2shp(data):
    table = data["table"]
    where = data["where"]
    shpfile = data["shpfile"]
    utilsgis.sde2shp(table, shpfile, where)


# CAD转SHP
def dwgToShp(data):
    dwgfile = data["dwgfile"]
    espg = data["espg"]
    utilsgis.dwg2shp(dwgfile, espg)


if __name__ == '__main__':
    # shp2sde({"shpfile":r"E:\99project\P008KJGH\SVN\branches\eeds\Mxds\Maps\fzxz.shp", "table":"KJGH.T_FZSS_FXRW_GIS",
    #        "fields":[{"name":"RWBSM","type":"TEXT","value":"aab"},{"name":"RWLX","type":"TEXT","value":"ccd"}] })
    # shp2sde({"shpfile":"E:\\99project\\P008KJGH\\SVN\\trunk\\SoureCode\\2.Api\\QM.KJGH.GHSS\\QM.KJGH.GHSS\\files\\temp\\SHP_20210820024201.shp","table":"KJGH.T_FZSS_FXRW_GIS",
    #         "fields":[{"name":"RWBSM","type":"TEXT","value":"d1b0737c231849dd9130403502c48daa"},{"name":"RWLX","type":"TEXT","value":"辅助选址"}]})
    # print(1234567)
    # esriJson2sde({"json":"E:\\99project\\P008KJGH\\SVN\\trunk\\SoureCode\\2.Api\\QM.KJGH.GHSS\\QM.KJGH.GHSS\\files\\temp\\SHP_20210820235143.json","table":"KJGH.T_FZSS_FXRW_GIS"})
    # log.info('开始入库')

    # sde2geojson({"table":"KJGH.T_FZSS_FZXZ_JG_GIS","geojson":"E:\\99project\\aaa.json","where":"\"RWBSM\"='a801628a23fd40d19e1115bba498ab2c'"})

    if len(sys.argv) == 3:
        fun = sys.argv[1]
        jsonData = utils.b64Decode(sys.argv[2])
        log.info(jsonData)
        run(fun, json.loads(jsonData))
