help with a python code

263
7
Jump to solution
01-22-2022 01:13 AM
Labels (1)
matu89
by
New Contributor II

hey , i have the basic licence therere for i cant use Feature Vertices to Points tool.

i've found a code over the internet from this  blog:

Emil's Arc/GIS/Python: ArcGIS polygon to lines and feature vertices to points without an ArcGIS adva...

this is the code:

#feature vertices to points
import arcpy def VerticesToPoints (inFc, workspace, fcName): outFc = UniqueFileName(workspace, fcName) try: #let esri do it (advanced license) arcpy.FeatureVerticesToPoints_management (inFc, outFc) return outFc except: pass #no advanced license sr = arcpy.Describe (inFc).spatialReference outPath, outName = os.path.split (outFc) arcpy.CreateFeatureclass_management (outPath, outName, "POINT", spatial_reference = sr) with arcpy.da.InsertCursor (outFc, "SHAPE@") as iCurs: with arcpy.da.SearchCursor (inFc, "SHAPE@") as sCurs: for geom, in sCurs: for i in range (geom.partCount): part = geom.getPart (i) for pnt in part: if not pnt: continue row = (pnt,) iCurs.insertRow (row) del iCurs del sCurs return outFc


now, i tried to put my inputs in this code but it wont run..

my inputs are:
the existing polyine is called :'zpoints.shp'
and my output directory is :'c:/gis/project'
the new point layer to be created is called: 'test.shp'

where i need to write my inputs in the code?

thanks for the help!

 

0 Kudos
1 Solution

Accepted Solutions
MicZatorsky_AEC
Occasional Contributor

As Luke said, you need to call the function AND you need to define the function UniqueFileName() - as this code calls it.   

The code below adds a small function to do what I guess UniqueFileName() was trying to do, and shows you how to call VerticesToPoints() without having to create a script tool.


 

#feature vertices to points
import arcpy

def UniqueFileName(workspace, fcName):
    return workspace + "\\" + fcName


def VerticesToPoints(inFc, workspace, fcName):

    outFc = UniqueFileName(workspace, fcName)
    try:
        #let esri do it (advanced license)
        arcpy.FeatureVerticesToPoints_management (inFc, outFc)
        return outFc
    except:
        pass
    #no advanced license
    sr = arcpy.Describe (inFc).spatialReference
    outPath, outName = os.path.split (outFc)
    arcpy.CreateFeatureclass_management (outPath,
                                         outName,
                                         "POINT",
                                         spatial_reference = sr)
    with arcpy.da.InsertCursor (outFc, "SHAPE@") as iCurs:
        with arcpy.da.SearchCursor (inFc, "SHAPE@") as sCurs:
            for geom, in sCurs:
                for i in range (geom.partCount):
                    part = geom.getPart (i)
                    for pnt in part:
                        if not pnt:
                            continue
                        row = (pnt,)
                        iCurs.insertRow (row)
    del iCurs
    del sCurs
    return outFc

inFc = "tmp_ln" # input line feature class
workspace = r"C:\temp\Scratch.gdb"  # GDB
fcName = "tmp_pt" # output point FC

VerticesToPoints(inFc, workspace, fcName)

 





View solution in original post

7 Replies
Luke_Pinner
MVP Regular Contributor

It's a function. You need to call it: 

VerticesToPoints ("C:/path/to/zpoints.shp", "c:/gis/project", "test.shp")

You could also research how to create a python script tool. 

MicZatorsky_AEC
Occasional Contributor

As Luke said, you need to call the function AND you need to define the function UniqueFileName() - as this code calls it.   

The code below adds a small function to do what I guess UniqueFileName() was trying to do, and shows you how to call VerticesToPoints() without having to create a script tool.


 

#feature vertices to points
import arcpy

def UniqueFileName(workspace, fcName):
    return workspace + "\\" + fcName


def VerticesToPoints(inFc, workspace, fcName):

    outFc = UniqueFileName(workspace, fcName)
    try:
        #let esri do it (advanced license)
        arcpy.FeatureVerticesToPoints_management (inFc, outFc)
        return outFc
    except:
        pass
    #no advanced license
    sr = arcpy.Describe (inFc).spatialReference
    outPath, outName = os.path.split (outFc)
    arcpy.CreateFeatureclass_management (outPath,
                                         outName,
                                         "POINT",
                                         spatial_reference = sr)
    with arcpy.da.InsertCursor (outFc, "SHAPE@") as iCurs:
        with arcpy.da.SearchCursor (inFc, "SHAPE@") as sCurs:
            for geom, in sCurs:
                for i in range (geom.partCount):
                    part = geom.getPart (i)
                    for pnt in part:
                        if not pnt:
                            continue
                        row = (pnt,)
                        iCurs.insertRow (row)
    del iCurs
    del sCurs
    return outFc

inFc = "tmp_ln" # input line feature class
workspace = r"C:\temp\Scratch.gdb"  # GDB
fcName = "tmp_pt" # output point FC

VerticesToPoints(inFc, workspace, fcName)

 





matu89
by
New Contributor II

hey,

still dont work for me:

matu89_0-1643014888864.png

 

here is the data from Arcgis Pro:

matu89_1-1643014958342.png

 

what i did wrong?

 

0 Kudos
HuubZwart
Occasional Contributor

The describe function fails because the path to 'zzpoints' does not exist. Note it takes it path relative to the currently active workspace: you could set the active workspace in your script before you access the feature class:

arcpy.env.workspace = r"C:\Users\sasim\Documents\ArcGIS\Projects\MyProjects\MyProject.gdb"
MicZatorsky_AEC
Occasional Contributor

Exactly.  Or provide a fully qualified path to the feature class:

inFc = r"C:\Users\sasim\Documents\ArcGIS\Projects\MyProject\MyProject.gdb\zpointss" # input line feature class
matu89
by
New Contributor II

thank you very much! it works !!

 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

@matu89, please mark some responses as accepted solution to close out this thread.

0 Kudos