English

How To: Convert polyline themes into a new point (nodes) shapefile theme

Procedure

Summary

It is possible to convert a polyline theme into a point theme of the polyline's end points (or end nodes). The script below provides an example of how this can be done with Avenue. The script also takes into account the possibility that a polyline may be made up of several polyline parts and includes the end points of those as well.

Solution

1. Paste the following code into a new script window and compile.

2. Make the polyline theme active in the view.

3. Switch directly to the script window and click the run button.

Code:
'--- Script: node.ave
'--- This script converts a polyline theme into a new point shapefile theme,
'-- where each point represents the end points of every polyline.
'-- This script supports multi-part shapes so that if a polyline is made up
'-- of several parts, end points will be created for each polyline multi-part.
'-- The script exits gracefully if active theme does not contain polylines.

theView = av.getactivedoc
theTheme = theView.GetActiveThemes.Get(0)
inFTab = theTheme.GetFTab
inNumRecs = inFTab.GetNumRecords
inShapeFld = inFTab.FindField("Shape")
outPointList = {}

if ((inShapeFld.gettype = #FIELD_SHAPELINE).Not)then
MsgBox.Warning("Active theme is not polyline","")
return nil
end

outfile = FileDialog.Put("*.shp".AsFileName, "*.shp",
"Output Point Shapefile")
outFTab = FTab.MakeNew(outfile, point)
outIDfld = Field.Make("myID",#FIELD_SHORT,6,0)
outShapeFld = outFTab.FindField("Shape")
outFTab.AddFields({outIDfld})

for each rec in inFTab
av.ShowMsg("Creating point theme of nodes...")
av.SetStatus(((rec+1)/inNumRecs) * 100)
aPolyline = inFTab.ReturnValue(inShapeFld, rec)
for each polypiece in aPolyline.AsList
pt = polypiece.Get(0)
outPointList.Add(pt)
pt = polypiece.Get((polypiece.AsList.Count) - 1)
outPointList.Add(pt)
end
end

for each aPoint in outPointList
recnum = outFTab.AddRecord
outFTab.SetValue(outShapeFld, recnum, aPoint)
outFTab.SetValue(outIDfld, recnum, recnum)
end

outFTab.SetEditable(false)
av.ShowMsg("Done")
av.ClearStatus
newTheme = FTheme.Make(outFTab)
theView.AddTheme(newTheme)
newTheme.SetVisible(true)

'--- End of Script