HOW TO

Convert multi-part polygons to a new polyline shapefile

Last Published: April 25, 2020

Summary

The ArcView sample script cvtplypl.ave converts the active polygon vector theme into a new polyline shapefile. However, if a polygon has multiple parts, this script only converts the first part. This document provides a sample script that will convert all parts of a multi-part polygon to polylines.

Procedure



  1. Open a new script window.

    A. Activate the Project window.
    B. Click the Scripts icon.
    C. Click New.

  2. Copy the following code into the new script window.

    Code:
    '--- Script: MPpoly2line.ave
    '--- This script converts selected polygons in the active them to polylines
    '-- to create a new shapefile. If no features are currently selected all polygons
    '-- will be processed. This script is modified from the "cvtplypl.ave" sample
    '-- script that comes with ArcView GIS 3.0 and supports multi-part polygons
    '
    '--- This script requires: a View must be the active document, a polygon theme
    '-- must be the active theme. Use the following as an update script:
    '
    '-Update script for control...
    '-v = av.GetActiveDoc
    '-t = v.GetActiveThemes.Get(0)
    '-SELF.SetEnabled((t <> NIL) AND
    '- (t.GetFTab.FindField("shape").GetType = #FIELD_SHAPEPOLY))
    '
    ' Self:
    '
    ' Returns:

    theView = av.GetActiveDoc
    thmThemeIn = theView.GetActiveThemes.Get(0)

    '--- Specify the output shapefile...

    fnDefault = FileName.Make("$HOME").MakeTmp("shape","shp")
    fnOutput = FileDialog.Put( fnDefault,"*.shp","Output Shape File" )
    if (fnOutput = nil) then exit end
    fnOutput.SetExtension("shp")
    ftbOutput = FTab.MakeNew( fnOutput, POLYLINE )
    ftbOutput.AddFields({Field.Make("ID", #FIELD_LONG, 8, 0)})

    '--- Use selected shapes if there are any, otherwise iterate through the entire
    FTab

    if (thmThemeIn.GetFTab.GetSelection.Count > 0) then
    colToProcess = thmThemeIn.GetFTab.GetSelection
    nRecs = colToProcess.Count
    else
    colToProcess = thmThemeIn.GetFTab
    nRecs = colToProcess.GetNumRecords
    end

    nCount = 0
    nRecsAdded = 0
    fldShapeIn = thmThemeIn.GetFTab.FindField("shape")
    fldShapeOut = ftbOutput.FindField("shape")
    fldIDOut = ftbOutput.FindField("id")
    for each r in colToProcess
    nCount = nCount + 1
    av.SetStatus((nCount / nRecs) * 100)
    shpIn = thmThemeIn.GetFTab.ReturnValue(fldShapeIn,r)
    i = -1
    for each plinePart in shpIn.AsList
    i = i + 1
    shpNew = Polyline.Make({shpIn.AsList.Get(i)})
    nRecNew = ftbOutput.AddRecord
    ftbOutput.SetValue(fldShapeOut,nRecNew,shpNew)
    ftbOutput.SetValue(fldIDOut,nRecNew,nCount)
    nRecsAdded = nRecsAdded + 1
    end
    end
    ftbOutput.SetEditable(false)
    av.SetStatus(100)

    if (nRecsAdded = 0) then
    MsgBox.Error("Unable to convert polygons to polylines.","Convert Polygon to Polyline")
    exit
    else
    MsgBox.Info(nRecsAdded.AsString++"shapes converted.","Convert Polyline to Polygon")
    end

    if (MsgBox.YesNo("Add shapefile as theme to a view?","Convert Polygon to Polyline", true).Not) then
    exit
    end

    '--- Create a list of views and allow the user to choose which view to
    '-- add the new theme to...
    lstViews = {}
    for each d in av.GetProject.GetDocs
    if (d.Is(View)) then
    lstViews.Add( d )
    end
    end
    lstViews.Add("<New View>")

    vweAddTo = MsgBox.ListAsString( lstViews,"Add Theme to:","Convert Polyline to Polygon" )

    '--- Get the specified view, make the theme, and add it...
    if (vweAddTo <> nil) then
    if (vweAddTo = "<New View>") then
    vweAddTo = View.Make
    vweAddTo.GetWin.Open
    end
    thmNew = FTheme.Make( ftbOutput )
    vweAddTo.AddTheme( thmNew )
    vweAddTo.GetWin.Activate
    end
    '--- End of script: MPpoly2line.ave

  3. Compile the script by clicking the compile button.
    [O-Image] Script compile button
  4. Run the script on an active View containing the active theme to be converted by clicking the run button.
    [O-Image] Run compiled script button

Article ID:000005213

Software:
  • Legacy Products

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic