#!usr/bin/python
import arcpy
import sys
import os

GDBworkspace = r'./static.gdb'
saveToGDB = r'./data.gdb'
rvtPath = r'./test.rvt'
# inputLinkFeatures = GDBworkspace + "\\" + 'Link54114toSZ'
inputLinkFeatures = GDBworkspace + "\\" + 'LinkSZto54114'
outputFeatures = saveToGDB + "\\" + 'test_54'
outputResult = saveToGDB + "\\" + 'test_84'
featureSets = []
Web_Mercator = r"PROJCS['WGS_1984_Web_Mercator_Auxiliary_Sphere',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator_Auxiliary_Sphere'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],PARAMETER['Auxiliary_Sphere_Type',0.0],UNIT['Meter',1.0]]"
Beijing_1954_114 = r"PROJCS['Beijing_1954_3_Degree_GK_CM_114E',GEOGCS['GCS_Beijing_1954',DATUM['D_Beijing_1954',SPHEROID['Krasovsky_1940',6378245.0,298.3]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Gauss_Kruger'],PARAMETER['False_Easting',500000.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',114.0],PARAMETER['Scale_Factor',1.0],PARAMETER['Latitude_Of_Origin',0.0],UNIT['Meter',1.0]]"
arcpy.env.overwriteOutput = True
arcpy.env.qualifiedFieldNames = "UNQUALIFIED"
arcpy.env.workspace = rvtPath

try:
    datasets = arcpy.ListDatasets()
    for ds in datasets:
        for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
            if fc == 'LocationPoints':
                continue
            path = os.path.join(arcpy.env.workspace, ds, fc)
            # print(path)
            arcpy.MakeFeatureLayer_management (path, fc+'_lyr')
            arcpy.Layer3DToFeatureClass_3d(fc+'_lyr', "in_memory"+ "\\" + fc+'_fea')
    arcpy.env.workspace = "in_memory"
    for fc in arcpy.ListFeatureClasses():
        path = os.path.join(arcpy.env.workspace, fc)
        featureSets.append(path)
    arcpy.Merge_management(featureSets,outputFeatures)
    # arcpy.DefineProjection_management(outputFeatures, Beijing_1954_114)
    arcpy.Delete_management("in_memory")
    arcpy.TransformFeatures_edit(outputFeatures, inputLinkFeatures, "AFFINE")
    arcpy.Project_management(outputFeatures, outputResult, Web_Mercator, '54to84')
except arcpy.ExecuteError:
    errorMsgs = arcpy.GetMessages(2)
    arcpy.AddError(str(errorMsgs))
    arcpy.AddMessage("Failed!")
    pass