Split with Line

TatukGIS Internet Server Samples

Shapes after split: ...

Source code

This sample demonstrates the use of topology functionality (CreateShape, XGIS_Topology.Create and XGIS_Topology.SplitByArc functions) in the TatukGIS Internet Server to split a polygon shape into multiple polygons using a splitting line drawn by the on-line user.

To split polygon simply draw the splitting line by clicking in the map window. The splitting line can be complex, formed with an unlimited number of vertices, and can cut the polygon into more than just two shapes. Click on the "Split Shape" button to render the result of the splitting operation. To start again from the beginning, just click "Create New Line" button.


splitByArc.aspx    Top

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

    Dim GisUtils As API.XGIS_Utils
    Dim layerPolygon, layerObj, layerArc As API.XGIS_LayerVector
    Dim shpPolygon As API.XGIS_Shape
    Dim shpArc As API.XGIS_Shape    
    
    Sub Page_Load(sender As Object, e As EventArgs)
        if not Page.IsPostBack then
            session("wkt_point") = Nothing
            session("wkt_line") = Nothing
            session("shape_list") = Nothing
            ButtonLine.Visible=False
            ButtonSplit.Visible=True
        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)
        GIS.ZoomIn
    End Sub
    
    Sub GIS_ZoomOut_Click(sender As Object, e As ImageClickEventArgs)
        GIS.ZoomOut
    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)
        GIS.Width  = Unit.Pixel( GIS.Width.Value  + 54 )
        GIS.Height = Unit.Pixel( GIS.Height.Value + 40 )
    End Sub
    
    Sub GIS_Load(sender As Object, e As EventArgs)
        GisUtils = New API.XGIS_Utils
        layerPolygon = New API.XGIS_LayerVector
        layerPolygon = New API.XGIS_LayerVector
        layerPolygon = New API.XGIS_LayerVector
    
        GIS.Open( GisUtils.GisSamplesDataDir + "topology3.shp" )
    
        layerPolygon = GIS.API.Items.Item(0)
        If layerPolygon Is Nothing Then
            Exit Sub
        End If
        layerPolygon.Params.Area.OutlineColor = RGB(180, 180, 180)
        layerPolygon.Params.Area.Color = RGB(220, 220, 220)
        
       'get polygon to split
        shpPolygon = layerPolygon.GetShape(1).MakeEditable
        If shpPolygon Is Nothing Then
            Exit Sub
        End If      
  
  
        'create layer for line
        layerArc = New API.XGIS_LayerVector     
        layerArc.Params.Line.Color = RGB(200,56,36)
        layerArc.Params.Line.Width = 25
        If layerArc Is Nothing Then Exit Sub
        GIS.API.Add(layerArc)    
         
       'create layer for new shapes - after split
        layerObj = New API.XGIS_LayerVector()
        layerObj.Name = "Splits"
        GIS.API.Add(layerObj)
    
   
        GIS.FullExtent()
        GIS.ImageType = XgisImageType.PNG24
    End Sub
    
    Sub GIS_Click(sender As Object, e As ImageClickEventArgs)
        Prepare_Line(sender, e)
    End Sub
    
    Sub ButtonSplit_Click(sender As Object, e As EventArgs)
        SetTopology()
    End Sub    

    Sub ButtonLine_Click(sender As Object, e As EventArgs)
        session("wkt_point") = Nothing
        session("wkt_line") = Nothing
        session("shape_list") = Nothing
        lbl_desc1.Text = "Shapes after split: ..."
        ButtonLine.Visible=False
        ButtonSplit.Visible=True
    End Sub  
    
    Sub GIS_Paint(sender As Object, e As PaintEventArgs)
    
       'Draw line and cut polygons if necessery
        Draw_WKTLine()
        Draw_DivPolygons()
        GIS.Draw()
    End Sub
    
    Sub Draw_DivPolygons()
    
        Dim n As Integer
        Dim shape_list As API.XList
    
        shape_list = session("shape_list")
        If Not shape_list Is Nothing Then
            For n = 0 To shape_list.Count - 1
                layerObj.AddShape(shape_list.Item(n))
            Next n
            lbl_desc1.Text = "Shapes after split: " &shape_list.Count
            shape_list = Nothing
        End If
    End Sub
    
    
    Function Draw_WKTLine()
        Dim shp as API.XGIS_Shape
        dim wkt_line as string
    
        wkt_line =  session("wkt_line")
        if wkt_line <> "" then
            shp = GisUtils.GisCreateShapeFromWKT( wkt_line )
            shp = layerArc.AddShape ( shp )
    
           'draw a line if lenth isn't 0
            if shp.Length() > 0 then
                session("wkt_point") = ""
            else
                Draw_WKTPoint()
            end if
        end if
    End Function    
    
    Sub Draw_WKTPoint()
        Dim shp as API.XGIS_Shape
        dim wkt_point as string
    
        wkt_point =  session("wkt_point")
       'draw first point of line or area
        if (wkt_point <> "") then
            shp = GisUtils.GisCreateShapeFromWKT( wkt_point )
            shp = layerObj.AddShape ( shp )
    
           'Add color to point
            layerObj.Params.Marker.Color=RGB(200,56,36)
            layerObj.Params.Marker.Style = API.XGIS_MarkerStyle.XgisMarkerStyleCircle
            layerObj.Params.Marker.Size = -7
            layerObj.Params.Marker.OutlineColor=RGB(200,56,36)
            layerObj.Params.Marker.OutlineWidth=-1
    
        end if
    End sub    
    
   
    Sub Prepare_Line(sender As Object, e As ImageClickEventArgs)
        dim p As API.XPoint
        dim p_gis As API.XGIS_Point
        Dim shp as API.XGIS_Shape
        Dim shp2 as API.XGIS_Shape
    
        dim wkt_line as string
        wkt_line =  session("wkt_line")
        if wkt_line <> "" then
           'create a line from WKT
            shp = GisUtils.GisCreateShapeFromWKT( wkt_line )
            shp = layerArc.AddShape ( shp )
        else
           'create shapes from first point as point and line
            shp = layerArc.CreateShape(  API.XGIS_ShapeType.XgisShapeTypeArc )
            shp.Lock(2)
            shp.AddPart()
            shp.Unlock()
            shp2 = layerArc.CreateShape(  API.XGIS_ShapeType.XgisShapeTypePoint )
            shp2.Lock(2)
            shp2.AddPart()
            p = GisUtils.Point(e.X, e.Y )
            p_gis = GIS.API.ScreenToMap(p)
            shp2.AddPoint( GisUtils.GisPoint( p_gis.X, p_gis.Y ) )
            shp2.Unlock()
           'save a point as WKT
            session("wkt_point") = shp2.ExportToWKT()
            shp2.Delete()
        end if
    
       'add point to line
        p = GisUtils.Point(e.X, e.Y )
        p_gis = GIS.API.ScreenToMap(p)
        shp.Lock(2)
        shp.AddPoint( GisUtils.GisPoint( p_gis.X, p_gis.Y ) )
        shp.Unlock()
       'save a line as WKT
        session("wkt_line") = shp.ExportToWKT()
        shp.Delete()
    
    End sub
    
    Sub SetTopology()
        Dim topologyObj As API.XGIS_Topology
        Dim n As Integer
        Dim shape_list As API.XList
  
        dim wkt_line as string
    
        wkt_line =  session("wkt_line")
        if not session("wkt_line") is Nothing then
    
            shpArc = GisUtils.GisCreateShapeFromWKT( wkt_line )
            shpArc = layerObj.AddShape ( shpArc )
    
           'draw a line if lenth isn't 0
            if shpArc.Length() > 0 then
                session("wkt_point") = ""
            else
                Draw_WKTPoint()
            end if
    
            topologyObj = New API.XGIS_Topology()
            shape_list = topologyObj.SplitByArc(shpPolygon, shpArc, True)
           'set color to new polygons
            If Not shape_list Is Nothing Then
                For n = 0 To shape_list.Count - 1
                    shape_list.Item(n).Params.Area.Color = Rnd(255) * 256 * 256 + Rnd(255) * 256 + Rnd(256)
                Next n
            End If
            session("shape_list") = shape_list
            shpArc.Delete()
            shape_list = Nothing

            ButtonLine.Visible=True
            ButtonSplit.Visible=False
         end if
    End Sub
    

</script>
<html>
<head>
    <title>TatukGIS Internet Server Samples</title>
    <link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
    <a name="top"> 
    <h1>Split with Line 
    </h1>
    <h2 class="comment"><a href="../../samplesAPI.htm">TatukGIS Internet Server Samples</a> 
    </h2>
    <form runat="server">
        <table cellspacing="0" cellpadding="0">
            <tbody>
                <tr>
                    <td width="25%">
                        <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 width="60%">
                    </td>
                    <td align="right" width="15%">
                        <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>
                </tr>
                <tr>
                    <td colspan="3">
                        <p>
                            <ttkGIS:XGIS_VIEWERIS id="GIS" onclick="GIS_Click" runat="server" Width="360px" Height="280px" OnLoad="GIS_Load" BorderColor="CornflowerBlue" BorderWidth="1px" OnPaint="GIS_Paint"></ttkGIS:XGIS_VIEWERIS>
                        </p>
                    </td>
                </tr>
            </tbody>
        </table>
        <table width="362">
            <tbody>
                <tr>
                    <td>
                        <asp:Button id="ButtonLine" onclick="ButtonLine_Click" runat="server" Font-Bold="True" ForeColor="Black" Text="Create New Line"></asp:Button>
                        <asp:Button id="ButtonSplit" onclick="ButtonSplit_Click" runat="server" Font-Bold="True" ForeColor="Black" Text="Split Shape"></asp:Button>
                    </td>
                    <td align="right">
                        <asp:Label id="Lbl_Desc1" runat="server" font-bold="True">Shapes after split: ...</asp:Label>
                    </td>
                </tr>

            </tbody>
        </table>
    </form>
    </a>
</body>
</html>