<%@ Page Language="VB" Debug="True" %>
<%@ Register TagPrefix="ttkGIS" Namespace="TatukGIS.IS" Assembly="TatukGIS.IS" %>
<script runat="server">

    dim lst As New ArrayList
    dim route1 as System.data.DataTable
    dim route2 as System.data.DataTable
    dim GisUtils As New API.XGIS_Utils
    
    dim MaxGPSPoints as integer
    
    Sub init_Route()
        dim dr as System.Data.dataRow
        dim i as integer
        dim x, y as double
    
        route1 = New System.data.DataTable
        route2 = New System.data.DataTable
    
        route1.Columns.Add("x", GetType(Double))
        route1.Columns.Add("Y", GetType(Double))
    
        route2.Columns.Add("x", GetType(Double))
        route2.Columns.Add("Y", GetType(Double))
    
        MaxGPSPoints = 99
    
       'set first airplane starting coordinates
        x = 14.55
        y = 51.80
    
       'set trace for first airplane
        For i=0 to MaxGPSPoints
            x = x + 0.083
            if x > 18 then  y = y - 0.047
            dr = route1.NewRow()
            dr(0) = x
            dr(1) = y
            route1.Rows.Add(dr)
        next
    
       'set second airplane starting coordinates
        x = 14.20
        y = 53.90
    
       'set trace for second airplane
        For i=0 to MaxGPSPoints
            x = x + 0.095
            if y > 52.7 then  y = y - 0.05
            dr = route2.NewRow()
            dr(0) = x
            dr(1) = y
            route2.Rows.Add(dr)
        next
    
       'set actual airplanes position
        If session("StartCounting") = Nothing or Request.QueryString("stop") = 1 then
            Session("position") = 0
            session("StartCounting") = 1
        Else
            if Not Page.IsPostBack then Session("position") = Session("position") + 1
        End If
        If Session("position") > MaxGPSPoints then Session("position") = 0
    End Sub
    
    Sub Page_Load(sender As Object, e As EventArgs)
        Dim obj_prj As API.XGIS_ProjectionAbstract
        Dim ProjectionList As New API.XGIS_ProjectionList
        Dim i As Integer
    
        try
           'prepare list of projections
            obj_prj = ProjectionList.FindEx("GED")
            If Not Page.IsPostBack then
                Lst_Projection.Items.Add(obj_prj.Description)
            End if
            lst.Add(obj_prj)
    
            obj_prj = ProjectionList.FindEx("GEC")
            If Not Page.IsPostBack then
                Lst_Projection.Items.Add(obj_prj.Description)
            End if
            lst.Add(obj_prj)
    
            For i = 0 To ProjectionList.Count - 1
                obj_prj = ProjectionList.Projection(i)
                If obj_prj.Code = "UTM" Then      'UTM is a bit to restrictive to show whole country
                ElseIf obj_prj.Code = "NZMG" Then 'NZTM is a wrong projection to show Poland country
                ElseIf obj_prj.Code = "GED" Then  'GED is add on the begining of list
                ElseIf obj_prj.Code = "GEC" Then  'GEC is add on the begining of list
                Else
                    If Not Page.IsPostBack then
                        Lst_Projection.Items.Add(obj_prj.Description)
                    End if
                    lst.Add(obj_prj)
                End If
            Next i
    
        Catch EX As Exception
            Response.Redirect("MapError.aspx")
        end Try
    
       'set proper focus and projection
        if  Page.IsPostBack then
            session("projection") = Lst_Projection.SelectedIndex
            session("focus") = Rbt_focus.SelectedIndex
        else
            if  IsNumeric(cint(session("projection"))) then
                Lst_Projection.SelectedIndex = session("projection")
            end if
    
            if  IsNumeric(cint(session("focus"))) then
                Rbt_focus.SelectedIndex = session("focus")
            end if
        end if
    
       'set buttons and labels visibility and texts
        if Request.QueryString("stop") <> 2 then
            Btn_Start.Visible = False
            Btn_Stop.Visible = True
            Lst_Projection.Visible = False
            Lbl_Projection.Visible = True
            Lbl_Projection.Text = "  " & lst.Item(Lst_Projection.SelectedIndex).Description
            Lbl_Comment.Text = "<i>Select the <b>Pause Tracking</b> button in order to select a different projection.</i>"
        else
            Btn_Stop.Visible = False
            Btn_Start.Visible = True
            Lst_Projection.Visible = True
            Lbl_Projection.Visible = False
            Lbl_Comment.Text = "<i>Select new projection from the drop down list and wait a moment for result.</i>"
        end if
    
    End Sub
    
    Function GetActualPosition() as integer
        return(Session("position"))
    End Function
    
    Sub GIS_Load(sender As Object, e As EventArgs)
        try
           'open map
            GIS.Open( ConfigurationSettings.AppSettings("ttk_maps") + "poland_wgS84\poland_WGS84.ttkgp" )
           'init route (GPS points table) for each airplane
            init_Route()
        Catch EX As Exception
            Response.Redirect("MapError.aspx")
        end Try
    End Sub
    
    Sub GIS_BeforePaint(sender As Object, e As PaintEventArgs)
        dim SymbolList As New API.XGIS_SymbolList
        dim ProjectionList As New API.XGIS_ProjectionList
    
        dim lt, ll  As API.XGIS_LayerVector
        dim plane1, plane2, lshp, pshp, ashp, bshp As API.XGIS_Shape
        dim ex as API.XGIS_Extent
        dim pt as API.XGIS_Point
        dim p as API.XPoint
        dim i As Integer
        dim angle1, angle2 as double
    
        if GIS.API.IsEmpty Then Exit Sub
        if route1 is Nothing Then Exit Sub
        if route2 is Nothing Then Exit Sub
    
       'add a new layer
        lt = New API.XGIS_LayerVector
        GIS.API.Add(lt)
    
       'add WGS84 GPS points for each airplane
        pshp = lt.CreateShape(API.XGIS_ShapeType.XgisShapeTypeMultiPoint)
        pshp.Params.Marker.Color = Convert.ToUInt32(RGB(0,0,255))
        pshp.Params.Marker.Size = -4
        pshp.Lock(API.XGIS_Lock.XgisLockExtent)
        pshp.AddPart()
        'for i=0 to MaxGPSPoints
        for i=0 to GetActualPosition()
            pshp.AddPoint( GisUtils.GisPoint( route1.Rows(i).Item("X"), route1.Rows(i).Item("Y") ) )
        next
        pshp.Unlock()
    
        pshp.AddPart()
        'for i=0 to MaxGPSPoints
        for i=0 to GetActualPosition()
            pshp.AddPoint( GisUtils.GisPoint( route2.Rows(i).Item("X"), route2.Rows(i).Item("Y") ) )
        next
        pshp.Unlock()
    
    
       'draw a route that airplanes done
        lshp = lt.CreateShape(API.XGIS_ShapeType.XgisShapeTypeArc)
        lshp.Params.Line.Color = Convert.ToUInt32(RGB(80,80,80))
        lshp.Params.Line.Width = -1
        lshp.AddPart()
        lshp.Lock(API.XGIS_Lock.XgisLockExtent)
       'prepare shape to get airplan angle
        ashp = lt.CreateShape(API.XGIS_ShapeType.XgisShapeTypeArc)
        ashp.AddPart()
        ashp.Lock(API.XGIS_Lock.XgisLockExtent)
        for i=0 to GetActualPosition()
        'for i=0 to MaxGPSPoints
            pt =  GisUtils.GisPoint(route1.Rows(i).Item("X"), route1.Rows(i).Item("Y"))
            lshp.AddPoint( pt )
            if i > (GetActualPosition() - 2) then
                ashp.AddPoint( pt )
            end if
        next
        lshp.Unlock()
        angle1 = ashp.GetAngle(False)
        ashp.Unlock()
        ashp.Delete()
    
        lshp.AddPart()
        lshp.Lock(API.XGIS_Lock.XgisLockExtent)
        bshp = lt.CreateShape(API.XGIS_ShapeType.XgisShapeTypeArc)
        bshp.AddPart()
        bshp.Lock(API.XGIS_Lock.XgisLockExtent)
        for i=0 to GetActualPosition()
        'for i=0 to MaxGPSPoints
            pt =  GisUtils.GisPoint(route2.Rows(i).Item("X"), route2.Rows(i).Item("Y"))
            lshp.AddPoint( pt )
            if i > (GetActualPosition() - 2) then
                bshp.AddPoint( pt )
            end if
        next
        lshp.Unlock()
        angle2 = bshp.GetAngle(False)
        bshp.Unlock()
        bshp.Delete()
    
       'set layer params
        lt.Params.Labels.Font.Size = 7
        lt.Params.Labels.Font.Bold = True
        lt.Params.Labels.Position = API.XGIS_LabelPositions.XgisLabelPositionMiddleCenter
        lt.Params.Labels.Pattern = API.XBrushStyle.XbsClear
        lt.Params.Labels.Allocator = False
       'draw first airplan
        plane1 = lt.CreateShape(API.XGIS_ShapeType.XgisShapeTypePoint)
        plane1.Params.Marker.Symbol = SymbolList.Prepare(GisUtils.GisSamplesDataDir + "2267.cgm")
        plane1.Params.Marker.SymbolRotate = Math.PI*2 + angle1
        plane1.Params.Marker.Size = -24
        plane1.Params.Marker.Color = Convert.ToUInt32(RGB(0,0,0))
        plane1.Params.Labels.OutlineWidth = 0
        plane1.Params.Labels.Color= System.Convert.ToUInt32(RGB(0,0,0))
        plane1.Params.Labels.FontColor= System.Convert.ToUInt32(RGB(200,200,255))
        plane1.Lock(API.XGIS_Lock.XgisLockExtent)
        plane1.AddPart()
        plane1.AddPoint( GisUtils.GisPoint( route1.Rows(GetActualPosition()).Item("X"), route1.Rows(GetActualPosition()).Item("Y") ) )
        plane1.Params.Labels.Value = "1"
        plane1.Unlock()
    
       'draw second airplan
        plane2 = lt.CreateShape(API.XGIS_ShapeType.XgisShapeTypePoint)
        plane2.Params.Marker.Symbol = SymbolList.Prepare(GisUtils.GisSamplesDataDir + "2267.cgm")
        plane2.Params.Marker.SymbolRotate = Math.PI*2 + angle2
        plane2.Params.Marker.Size = -24
        plane2.Params.Marker.Color = Convert.ToUInt32(RGB(0,0,0))
        plane2.Params.Labels.OutlineWidth = 0
        plane2.Params.Labels.Color= System.Convert.ToUInt32(RGB(0,0,0))
        plane2.Params.Labels.FontColor= System.Convert.ToUInt32(RGB(200,200,255))
        plane2.Lock(API.XGIS_Lock.XgisLockExtent)
        plane2.AddPart()
        plane2.AddPoint( GisUtils.GisPoint( route2.Rows(i-1).Item("X"), route2.Rows(i-1).Item("Y") ) )
        plane2.Params.Labels.Value = "2"
        plane2.Unlock()
    
       'reproject map
       'first - set layers projection and units (Geodetic unprojected WGS84)
        For i=0 to GIS.API.Items.Count -1
            ll = GIS.API.Items.Item(i)
            ll.Projection = ProjectionList.FindEx("GED")
            ll.Units.Units = API.XGIS_UnitsType.XgisUnitsTypeDegreeDecimal
        Next
    
       'second - set map projection (choosen by user) and set adequate map units
        GIS.API.Projection = lst.Item(Lst_Projection.SelectedIndex)
        if GIS.API.Projection.Code = "GED" or GIS.API.Projection.Code = "GEC" then
            GIS.API.Units.Units = API.XGIS_UnitsType.XgisUnitsTypeDegreeDecimal
        else
            GIS.API.Units.Units = API.XGIS_UnitsType.XgisUnitsTypeMeter
        end if
    
       'recalc map extent
        GIS.API.RecalcExtent()
    
    
    
        if lst.Item(Lst_Projection.SelectedIndex).Code <> session("projection_old") then
           'if user set new projection first change extent to Full
            GIS.FullExtent
    
           'try to show the same part of map as it was before
            if not session("projection_old") is Nothing then
                pt = GIS.API.Items.Item(0).Project(Session("GISCenterPoint"))
                p = GIS.API.MapToScreen(pt)
                GIS.Center(p.x, p.y)
                GIS.Scale = Session("GISScale")
            end if
            session("projection_old") = lst.Item(Lst_Projection.SelectedIndex).Code
    
        else
           'else reverse previous map extent
            ex = Session("GISExtent")
            if not Session("GISExtent") is nothing then
                GIS.API.VisibleExtent = ex
            end if
        end if
    
       'if focus is set on one of airplanes then center map on this plane
        if Rbt_focus.SelectedIndex = 1 then
            p =  GIS.API.MapToScreen(plane1.Centroid())
        end if
    
        if Rbt_focus.SelectedIndex = 2 then
            p =  GIS.API.MapToScreen(plane2.Centroid())
    
        end if
        if Rbt_focus.SelectedIndex <> 0 then
            GIS.Center(p.x, p.y)
        end if
    
       'if focus is changed and scale is too small - change scale to bigger
        if (Rbt_focus.SelectedIndex <> 0) and (Rbt_focus.SelectedIndex <> session("focus_old")) then
            If GIS.Scale < 1/1000000 then GIS.Scale = 1/1000000
            session("focus_old") = Rbt_focus.SelectedIndex
        end if
    
    End Sub
    
    Sub GIS_Paint(sender As Object, e As PaintEventArgs)
       'draw map
        GIS.Draw
    
       'remember visible extent and scale to restore it after page redirection
        Session("GISExtent") = GIS.API.VisibleExtent
        Session("GISScale") = GIS.Scale
    
       'remember center point of visible map
        dim pt as API.XGIS_Point
        pt = GIS.API.Items.Item(0).Unproject(GISUtils.GisCenterPoint(GIS.API.VisibleExtent))
        Session("GISCenterPoint") = pt
    
       'write a scale
        Lbl_Scale.Text = "Scale: " &GIS.ScaleAsText
    End Sub
    
    Sub Btn_Stop_Click(sender As Object, e As EventArgs)
        Response.Redirect("TrackProjMap.aspx?stop=2")
    End Sub
    
    Sub Btn_Reset_Click(sender As Object, e As EventArgs)
        Response.Redirect("TrackProjMap.aspx?stop=1")
    End Sub
    
    Sub Btn_Start_Click(sender As Object, e As EventArgs)
        Response.Redirect("TrackProjMap.aspx?stop=0")
    End Sub
    
    Sub GIS_FullExtent_Click(sender As Object, e As ImageClickEventArgs)
        GIS.FullExtent
    End Sub
    
    Sub GIS_ZoomIn_Click(sender As Object, e As ImageClickEventArgs)
        Select Case GIS_Options.SelectedItem.Value
        Case 1
            GIS.Zoom = GIS.Zoom*2
        Case 2
            GIS.Zoom = GIS.Zoom*8
        Case Else
            GIS.ZoomIn
        End select
    End Sub
    
    Sub GIS_ZoomOut_Click(sender As Object, e As ImageClickEventArgs)
        Select Case GIS_Options.SelectedItem.Value
        Case 1
            GIS.Zoom = GIS.Zoom/2
        Case 2
            GIS.Zoom = GIS.Zoom/8
        Case Else
            GIS.ZoomOut
        End select
    End Sub
    
    Sub GIS_Click(sender As Object, e As ImageClickEventArgs)
        Select Case GIS_Options.SelectedItem.Value
        Case 1
            GIS.Center(e.X, e.Y)
            GIS_ZoomIn_Click(sender, e)
        Case 2
            GIS.Center(e.X, e.Y)
            GIS_ZoomIn_Click(sender, e)
        Case 3
            GIS.Center(e.X, e.Y)
        Case Else
            GIS.ZoomOut
        End select
    End Sub

</script>
<html>
<head>
    <link href="http://www.tatukgis.com/css/main.css" type="text/css" rel="stylesheet" /><%
    if Request.QueryString("stop") <> 2 then
    %> 
    <meta http-equiv="Refresh" content="3; URL=TrackProjMap.aspx" />
    <%
    end if
    %>
</head>
<body>
    <form runat="server">
        <table cellspacing="0" cellpadding="0" border="0">
            <tbody>
                <tr>
                    <td nowrap="nowrap" align="left">
                        <asp:ImageButton id="GIS_FullExtent" onclick="GIS_FullExtent_Click" runat="server" ImageUrl="/TatukGIS_IS8/img/fullextent.gif"></asp:ImageButton>
                        <asp:ImageButton id="GIS_ZoomIn" onclick="GIS_ZoomIn_Click" runat="server" ImageUrl="/TatukGIS_IS8/img/zoomin.gif"></asp:ImageButton>
                        <asp:ImageButton id="GIS_ZoomOut" onclick="GIS_ZoomOut_Click" runat="server" ImageUrl="/TatukGIS_IS8/img/zoomout.gif"></asp:ImageButton>
                    </td>
                    <td nowrap="nowrap" align="left">
                        <asp:RadioButtonList id="GIS_Options" runat="server" RepeatDirection="Horizontal">
                            <asp:ListItem Value="1" Selected="True">Zoom x2</asp:ListItem>
                            <asp:ListItem Value="2">Zoom x8</asp:ListItem>
                            <asp:ListItem Value="3">Center</asp:ListItem>
                        </asp:RadioButtonList>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <TTKGIS:XGIS_VIEWERIS id="GIS" onclick="GIS_Click" runat="server" Width="520px" Height="380px" OnLoad="GIS_Load" BorderColor="CornflowerBlue" BorderWidth="1px" onBeforePaint="GIS_BeforePaint" onPaint="GIS_Paint"></TTKGIS:XGIS_VIEWERIS>
                    </td>
                </tr>
                <tr>
                    <td align="right" colspan="2">
                        <asp:Label id="Lbl_Scale" runat="server" cssclass="8font" text="Scale:"></asp:Label>
                    </td>
                </tr>
            </tbody>
        </table>
        <br />
        <table width="522" bgcolor="#e0e0e0">
            <tbody>
                <tr>
                    <td align="right" width="140">
                        <asp:Button id="Btn_Reset" onclick="Btn_Reset_Click" runat="server" Text="Reset Positions"></asp:Button>
                    </td>
                    <td align="left" width="380">
                        <asp:Button id="Btn_Start" onclick="Btn_Start_Click" runat="server" Text="Resume Tracking"></asp:Button>
                        <asp:Button id="btn_Stop" onclick="Btn_Stop_Click" runat="server" Text="Pause Tracking"></asp:Button>
                    </td>
                </tr>
                <tr>
                    <td align="left" width="140">
                        <asp:Label id="Lbl_Focus" runat="server" text="Focus: " Font-Size="10pt" ForeColor="Black" Font-Bold="True"></asp:Label>
                    </td>
                    <td align="left" width="380">
                        <asp:RadioButtonList id="Rbt_Focus" runat="server" RepeatDirection="Horizontal" ForeColor="Black" AutoPostBack="True">
                            <asp:ListItem Value="0" Selected="True">None</asp:ListItem>
                            <asp:ListItem Value="1">1st plane</asp:ListItem>
                            <asp:ListItem Value="2">2nd plane</asp:ListItem>
                        </asp:RadioButtonList>
                    </td>
                </tr>
                <tr>
                    <td align="left" width="140">
                        <asp:Label id="Lbl_Reproject" runat="server" text="Projection: " Font-Size="10pt" ForeColor="black" Font-Bold="True"></asp:Label>
                    </td>
                    <td align="left" width="380">
                        <asp:DropDownList id="Lst_Projection" runat="server" AutoPostBack="True"></asp:DropDownList>
                        <asp:Label id="Lbl_Projection" runat="server" text="" Font-Size="10pt"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td align="left" colspan="2">
                        <hr />
                        <asp:Label id="Lbl_Comment" runat="server"></asp:Label>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>