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

    Sub Page_Load(sender As Object, e As EventArgs)
    
        dim street, city as string
    
        city = Request.QueryString("city")
        street = Request.QueryString("street")
        if city <> "" and street <> "" then
            Session("city") = city
            Session("street") = street
            Response.Redirect("city.aspx")
        end if
    
        if Not Page.IsPostBack then
            if Session("street") <> "" and session("city") <> "" then
                TextBoxStreetName.Text = Session("street")
                Select Case session("city")
                    case "Gdansk"
                        DropDownListObject.SelectedIndex = 1
                    case "Gdynia"
                        DropDownListObject.SelectedIndex = 3
                    case "Sopot"
                        DropDownListObject.SelectedIndex = 2
                    case else
                        DropDownListObject.SelectedIndex = 0
                End Select
            end if
        end if
    
    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_Smaller_Click(sender As Object, e As ImageClickEventArgs)
        GIS.Width  = Unit.Pixel( GIS.Width.Value  - 54 )
        GIS.Height = Unit.Pixel( GIS.Height.Value - 40 )
    End Sub
    
    Sub GIS_Larger_Click(sender As Object, e As ImageClickEventArgs)
        dim w, h, p as integer
    
        w = GIS.Width.Value + 54
        h = GIS.Height.Value + 40
        if w > 660 then
            p = w - 660
            w = 660
            h = h - (p*40/54)
        end if
        GIS.Width  = Unit.Pixel( w )
        GIS.Height = Unit.Pixel( h )
    End Sub
    
    Sub GIS_Click(sender As Object, e As ImageClickEventArgs)
        Dim uid as integer
    
        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 4
            GIS.Center(e.X, e.Y)
            uid = GIS.Locate( "places", e.X, e.Y)
            Session("uid") = uid
            Session("street") = ""
            if uid <> 0 then
                GIS.FitScope ( "places", "GIS_UID = '" & uid )
                GIS.Zoom = GIS.Zoom*0.5
            end if
        Case Else
            GIS.ZoomOut
        End select
    End Sub
    
    
    Sub GIS_Load(sender As Object, e As EventArgs)
        GIS.Open( ConfigurationSettings.AppSettings("ttk_maps") + "trojmiasto\trojmiasto.ttkgp" )
        GIS.API.SelectionColor=Convert.ToUInt32(RGB(255, 0, 0))
        GIS.API.SelectionTransparency=50        
    End Sub
    
    
    Sub GIS_BeforePaint(sender As Object, e As PaintEventArgs)
        dim criteria as string
    
        if Not Page.IsPostBack then
            if Session("street") <> "" and session("city") <> "" then
    
                criteria = "(SHORT like '" & Session("street") & "') AND (CITY LIKE '" & session("city") & "')"
                SelectStreets(criteria)
                GIS.FitScope ("ulice", "(SHORT like '" &  Session("street") & "') AND (CITY LIKE '" & Session("city") & "')")
                GIS.Zoom = GIS.Zoom*0.8
            end if
        else
            criteria = "(SHORT like '" & TextBoxStreetName.Text & "%') AND (CITY LIKE '" & DropDownListObject.SelectedItem.Value & "')"
            SelectStreets(criteria)
        end if
    End Sub
    
    Sub Button_FindStreet_Click(sender As Object, e As EventArgs)
        dim criteria as string
    
        criteria = "(SHORT like '" & TextBoxStreetName.Text & "%') AND (CITY LIKE '" & DropDownListObject.SelectedItem.Value & "')"
        SelectStreets(criteria)
        GIS.FitScope ("ulice", "(SHORT like '" &  TextBoxStreetName.Text & "%') AND (CITY LIKE '" & DropDownListObject.SelectedItem.Value & "')")
        GIS.Zoom = GIS.Zoom*0.8
    End Sub
    
    
    Sub SelectStreets(criteria as string)
        dim tmp as API.XGIS_Shape
        dim ll as API.XGIS_LayerVector
    
        dim oex, ex as API.XGIS_Extent
        dim city_old, city_tmp, street_old, street_tmp as string
    
        ll = GIS.API.Get("ulice")
        If ll Is Nothing Then
            Exit Sub
        End If
    
    
        ll.DeselectAll()
    
        dim Table as System.Data.DataTable = New System.Data.DataTable()
        dim dr as System.Data.DataRow
    
        Table.Columns.Add("City", GetType(String))
        Table.Columns.Add("Street", GetType(String))
        Table.Columns.Add("l_Street", GetType(String))

        Table.DefaultView.Sort = "City, Street"
    
        oex = GIS.API.VisibleExtent
        GIS.FullExtent()
        ex = GIS.API.VisibleExtent

        tmp = ll.FindFirst(ex, criteria, Nothing, "", True)
        city_old = "edee"
        street_old = "ajndshd"
        While Not tmp Is Nothing
            tmp = tmp.MakeEditable
            city_tmp = tmp.GetField("CITY")
            street_tmp = tmp.GetField("SHORT")
    
            if (street_tmp <> street_old) then
                dr = Table.NewRow()
                dr(0) = tmp.GetField("CITY")
                dr(1) = tmp.GetField("SHORT")
                dr(2) = tmp.GetField("NAZ_ULICY")
                if IsRowInTable(Table, dr) = False then
                    Table.Rows.Add(dr)
                end if

                city_old = dr(0)
                street_old = dr(1)
            else
                if (city_tmp <> city_old) then
                    dr = Table.NewRow()
                    dr(0) = tmp.GetField("CITY")
                    dr(1) = tmp.GetField("SHORT")
                    dr(2) = tmp.GetField("NAZ_ULICY")
                    if IsRowInTable(Table, dr) = False then                    
                        Table.Rows.Add(dr)
                    end if
                    city_old = dr(0)
                    street_old = dr(1)
                end if
    
            end if
            tmp.IsSelected=True
            tmp = ll.FindNext
        End While

        StreetList.DataSource = Table.DefaultView
        StreetList.DataBind
        StreetList.Visible = TRUE
        ex = Session("GISExtent")
        if not oex is nothing then
            GIS.API.VisibleExtent = oex
        end if
    End Sub

    Function IsRowInTable(table as System.Data.DataTable, dr as System.Data.DataRow) as boolean
        dim ndr as System.Data.DataRow
        for each ndr in table.Rows
            if (ndr(0) = dr(0)) and (ndr(1) = dr(1)) and (ndr(2) = dr(2)) then
                return True
            end if
        next 
        return False
    End Function


</script>
<html>
<head>
    <title>TatukGIS IS Sample - Street Map Querying</title>
</head>
<body>
    <form runat="server">
        <h2>Street Map Querying (Gdansk-Gdynia Street Map) 
        </h2>
        <p class="comment">
            <table cellspacing="0" cellpadding="2" width="500" bgcolor="#eeeeee" border="0">
                <tbody>
                    <tr>
                        <th align="left">
                            <asp:Label id="Label_ObjCategory_text" runat="server">Choose city:</asp:Label>
                        </th>
                        <th align="left">
                            <asp:Label id="Label_ObjName_text" runat="server">Specify street name:</asp:Label>
                        </th>
                        <td>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:DropDownList id="DropDownListObject" runat="server">
                                <asp:ListItem Value="%" Selected="True">All districts</asp:ListItem>
                                <asp:ListItem Value="Gdansk">Gdansk</asp:ListItem>
                                <asp:ListItem Value="Sopot">Sopot</asp:ListItem>
                                <asp:ListItem Value="Gdynia">Gdynia</asp:ListItem>
                            </asp:DropDownList>
                        </td>
                        <td>
                            <asp:TextBox id="TextBoxStreetName" runat="server"></asp:TextBox>
                        </td>
                        <td>
                            <asp:Button id="Button_FindStreet" onclick="Button_FindStreet_Click" runat="server" Text="Find"></asp:Button>
                        </td>
                    </tr>
                    <tr>
                        <td class="comment" colspan="2">
                            Examples of specifying a street name: 1)Gdansk: "Klonowicza", "Grunwaldzka"; 2)Sopot:
                            "Mokwy"; 3)Gdynia: "Wielkopolska", "Adwokacka" - specify street (the first few letters
                            are normally sufficient) 
                        </td>
                        <td>
                             </td>
                    </tr>
                    <tr>
                        <td colspan="3">
                              
                        </td>
                    </tr>
                </tbody>
            </table>
        </p>
        <p>
            <asp:DataGrid id="StreetList" runat="server" BorderStyle="Solid" AutoGenerateColumns="False" BorderWidth="1px" BorderColor="Maroon" Width="502px">
                <HeaderStyle font-bold="True" backcolor="Maroon" forecolor="White"></HeaderStyle>
                <Columns>
                    <asp:BoundColumn DataField="City" HeaderText="City"></asp:BoundColumn>
                    <asp:TemplateColumn HeaderText="Short Street Name">
                        <ItemTemplate>
                            <asp:Hyperlink runat="server" Text='<%#Container.DataItem("street")%>' NavigateUrl='<%# "city.aspx?city=" & Server.UrlEncode(Container.DataItem("city")) & "&street=" & Server.UrlEncode(Container.DataItem("street"))%>' />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:BoundColumn DataField="l_street" HeaderText="Long Street Name"></asp:BoundColumn>
                </Columns>
            </asp:DataGrid>
        </p>
        <p>
            <table cellspacing="0" cellpadding="0">
                <tbody>
                    <tr>
                        <td nowrap="nowrap" align="left">
                            <asp:ImageButton id="GIS_Smaller" onclick="GIS_Smaller_Click" runat="server" ImageUrl="/TatukGIS_IS8/img/smaller.gif"></asp:ImageButton>
                            <asp:ImageButton id="GIS_Larger" onclick="GIS_Larger_Click" runat="server" ImageUrl="/TatukGIS_IS8/img/larger.gif"></asp:ImageButton>
                        </td>
                        <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" Width="240px" TextAlign="Right" 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>
                </tbody>
            </table>
            <table cellspacing="0" cellpadding="0">
                <tbody>
                    <tr>
                        <td>
                            <TTKGIS:XGIS_VIEWERIS id="GIS" onclick="GIS_Click" runat="server" BorderWidth="1px" BorderColor="CornflowerBlue" Width="500px" ImageType="PNG24" OnLoad="GIS_Load" Height="360px" OnBeforePaint="GIS_BeforePaint" SpoolUrl="/Spool/"></TTKGIS:XGIS_VIEWERIS>
                        </td>
                    </tr>
                </tbody>
            </table>
        </p>
    </form>
</body>
</html>