HOW TO

Convert polygons or polylines into line segments

Last Published: April 25, 2020

Summary

The script in this article takes a polygon or polyline theme and break it into a line theme with a separate feature for each line segment.

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 script window:

    Code:
    '-- Pgn2Line.ave

    '-- This script takes a polygon or polyline theme and
    '-- extracts each individual line segment between vertices.
    '-- The list of these line segments is used to create a
    '-- new line theme.

    theView=av.getactivedoc
    theTheme=theView.getactivethemes.get(0)
    theFtab=theTheme.getftab
    theFtabCount=theFtab.getnumrecords
    thesf=theFtab.FindField("Shape")
    theNewLOL={}

    for each r in theftab
    av.ShowMsg("Counting line segments...")
    av.SetStatus(((r+1)/theFtabCount) * 100)
    thePg=theftab.returnvalue(thesf,r)
    theNewLOP={}
    theLOLOP=thePG.aslist
    for each l in theLOLOP
    theLOP=l.aslist
    for each thept in theLOP
    theNewLOP.Add(thept)
    end
    end
    theNewLOPcount=theNewLOP.Count
    pt1i=0
    pt2i=1
    for each apt in 2..theNewLOPcount
    pt1=theNewLOP.Get(pt1i)
    pt2=theNewLOP.Get(pt2i)
    theNewLine=Line.Make(pt1, pt2)
    theNewLOL.Add(theNewLine)
    pt1i=pt1i+1
    pt2i=pt2i+1
    end
    end

    av.clearmsg
    av.clearstatus

    '-- Make the new polyline shapefile

    theNewLOLcount=theNewLOL.Count.asstring
    msgbox.info("The line theme will have"++theNewLOLcount++"lines in it","Almost done")

    out_name = FileDialog.Put( "*.shp".asfilename, "*.shp", "Output Shapefile")

    if (out_name = nil) then
    exit
    end

    myidfld = Field.Make("myID",#FIELD_LONG,6,0)
    fields = List.Make
    Fields.Add(myIDfld)
    tblrecno=0

    '-- Create the new Ftab and add fields

    tbl = Ftab.MakeNew( out_name, Polyline )
    tbl.AddFields( fields )

    '-- Add the records, ID value, and polyline to the ftab

    for each npl in 0..(theNewLOL.count-1)
    av.showmsg("Creating new shapefile"++out_name.asstring++"...")
    av.setstatus(((npl+1)/theNewLOLcount.asnumber) *100)
    tbl.addrecord
    tbl.SetValue(myIDfld, npl, npl)
    tbl.SetValue(tbl.FindField("Shape"), npl, theNewLOL.Get(npl).aspolyline)
    end

    theNewFtheme=Ftheme.Make(tbl)
    theView.AddTheme(theNewfTheme)
    theNewFtheme.SetVisible(true)
    theNewFtheme.SetActive(true)
    thetheme.SetVisible(false)
    thetheme.setactive(false)
    theview.Invalidate
    av.clearmsg
    av.clearstatus
    msgbox.Info("Finished!","")

  3. Click the Compile button.
    [O-Image] Script compile button
  4. Make the view and theme active.
  5. Click the script window.
  6. Click the Run button.
    [O-Image] Run compiled script button
    Note:
    This script will not work with island polygons.

Article ID:000004028

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