from Libraries.MapFunctions import CreateLineGeometry, CreatePointGeometry
from Libraries.MapFunctions import GetShapeFileFeatures, GetShapeFileCoordinateSystem
from Libraries.StandardFunctions import *
from Libraries.NetworkFunctions import *

from NetTopologySuite.Extensions.Features import Feature

from Libraries.SobekWaterFlowFunctions import *  # Import libraries region
from Libraries.StandardFunctions import *  # Import libraries region

branchShapeFile ="C:/Users/sschoeller/Documents/2840-7561A_Chennai/Plaene_Eingang/Dhi/2018-05-22_Modell/180522-aus_SchoellerSebastian_MIKE-URBAN-Export/shp/Haltungen.shp"
network = HydroNetwork(Name = "Imported network")
number = 1

# read shape file features and use them to create branches in the network
for feature in GetShapeFileFeatures(branchShapeFile):
#    test = feature.Attributes.TryGetValue("ObjBez")
#    print test
#    print 'ObjBez' in test
    # create start and end node
    firstCoordinate = feature.Geometry.Coordinates[0]
    lastCoordinate = feature.Geometry.Coordinates[feature.Geometry.Coordinates.Length -1]
    startNode = HydroNode(str(feature.Attributes.TryGetValue("KnotenZu")) ,Geometry = CreatePointGeometry(firstCoordinate.X, firstCoordinate.Y))
    endNode = HydroNode(str(feature.Attributes.TryGetValue("KnotenAb")), Geometry = CreatePointGeometry(lastCoordinate.X, lastCoordinate.Y))

#endregion    
    # create channel
    channel = Channel(startNode, endNode, Name = str(feature.Attributes.TryGetValue("ObjBez")) )
    channel.Geometry = feature.Geometry
    
    # add nodes and branch to network
    network.Nodes.AddRange([startNode, endNode])
    network.Branches.Add(channel)
    number += 1
    
    # create cross section for channel
    # checking for circular channel
    if int(feature.Attributes.TryGetValue("Profilart")[1][:2]) == 0:
        crossSection = CreateBranchObjectOnBranchUsingChainage(BranchObjectType.Culvert, str(feature.Attributes.TryGetValue("Profilart")), channel, 7)
        crossSection.GeometryType.Round
        crossSection.Diameter(float(feature.Attributes.TryGetValue("ProfBreite")))
    # checking for egg-shape channel
    elif int(feature.Attributes.TryGetValue("Profilart")[1][:2]) == 1:
        crossSection = CreateBranchObjectOnBranchUsingChainage(BranchObjectType.Culvert, str(feature.Attributes.TryGetValue("Profilart")), channel, 7)
        crossSection.GeometryType.Egg
    # checking for rectangular channel
    elif int(feature.Attributes.TryGetValue("Profilart")[1][:2]) == 5:
        crossSection = CreateBranchObjectOnBranchUsingChainage(BranchObjectType.Culvert, str(feature.Attributes.TryGetValue("Profilart")), channel, 7)
        #crossSection.GeometryType = 2
        crossSection.CrossSectionDefinitionAtInletAbsolute.SetGeometry = 'Rectangle'
        #crossSection.GeometryType.Rectangle

network.CoordinateSystem = GetShapeFileCoordinateSystem(branchShapeFile)

# remove all duplicate nodes (from branches where the nodes overlap)
MergeNodesWithSameGeometry(network)

# add a weir (named "Weir1") to the second branch at a chainage of 0.2 
#weir = CreateBranchObjectOnBranchUsingChainage(BranchObjectType.Weir, "Weir1", network.Branches[1], 0.2)

# find a weir by the name "Weir1"
#weir = GetBranchObjectByType(network, BranchObjectType.Weir, "Weir1")

# create model
flowModel = WaterFlowModel1D()
flowModel.Network = network
flowModel.Name = "1D Flow model"
 
AddToProject(flowModel)