Describe Data: Method spatialReference Does Not Exist

2490
7
09-06-2017 11:28 AM
TylerSearls2
New Contributor III

I've obtained a script which is meant to establish a point at every vertice within a (line) feature class. Despite my best efforts the script will not run, and returns "AttributeError: DescribeData: Method spatialReference does not exist" (line 19). Hoping someone can help. The script is attached.

7 Replies
RebeccaStrauch__GISP
MVP Esteemed Contributor

I didn't take a look at the script, and understand that it may be doing other things you need, but just in case you are just trying to get the vertices to points, take a look at Feature Vertices To Points—Help | ArcGIS for Desktop 

Creates a feature class containing points generated from specified vertices or locations of the input features

TylerSearls2
New Contributor III

Thanks Rebecca Strauch, GISP‌. I am solely trying to get points from vertices, however I don't have the appropriate license to run the  Feature Vertices To Points—Help | ArcGIS for Desktop tool. This is a workaround.

RebeccaStrauch__GISP
MVP Esteemed Contributor

ahh..that would make a difference. Always good to mention that in you post.  Also, much easier for others to respond it you try https://community.esri.com/people/curtvprice/blog/2014/09/25/posting-code-blocks-in-the-new-geonet?s...‌ to they don't have to download, etc.  I took the liberty of doing that here for you.  It seems like a simple issue so someone will probably answer in short order (but I don't have time to look at it right now)

# ---------------------------------------------------------------------------
# Creates points at each line vertice
# Tyler Searls, July 2017
# SERSC, Sacvkille, NB
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy, os

inFc = arcpy.GetParameterAsText(0)
workspace = arcpy.GetParameterAsText(1)
fcName = arcpy.GetParameterAsText(2)

# Create Points at Vertices
def VerticesToPoints(inFC, workspace, fcName):

    outFc = workspace + "\\" + fcName
    dsc = arcpy.Describe(inFC)
    sr = dsc.spatialReference
    arcpy.CreateFeatureclass_management(workspace, fcName, "POINT", 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

outFc = workspace + fcName
VerticesToPoints(inFc, workspace, fcName)
arcpy.SetParameter(3, outFc)
DanPatterson_Retired
MVP Esteemed Contributor

How are you providing the parameters to the script? Is it attached as the script to a tool in arctoolbox? 

Are you trying to run this from a python IDE? Does just the line 'import arcpy' work if you are running it from an IDE?

PS, Sackville.... spelling.... SERSC, Sacvkille, NB

0 Kudos
TylerSearls2
New Contributor III

Dan Patterson‌ the script is attached to a script tool - yes. It is also nested within a larger script, and as such the parameters are passed in through this larger script.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

something isn't getting passed or read because if works

# arcpy imported already

in_fc = r"C:\some_path\testdata.gdb\polygon"

desc = arcpy.Describe(in_fc)

sr = desc.spatialReference

sr.name

'NAD_1983_CSRS_MTM_9'
RebeccaStrauch__GISP
MVP Esteemed Contributor

I took the function out of the picture for testing and found two things. If you do use the function, you are assigning the outFc in two places...I would leave it in the function since that is the proper format (that is, you do need the \\ , but there are other ways to do this. 

The other issue was the syntax for the CreateFeatureclass_Management, at least when I ran it with a sample line FC (with an Albers projection).  Create Feature Class—Help | ArcGIS for Desktop 

CreateFeatureclass_management (out_path, out_name, {geometry_type}, {template}, 
{has_m}, {has_z}, {spatial_reference}, {config_keyword}, {spatial_grid_1}, 
{spatial_grid_2}, {spatial_grid_3}

Try this: (the "#" in my CreateFeatureclass line mean, "use defaults")

import arcpy, os

inFc = arcpy.GetParameterAsText(0)
workspace = arcpy.GetParameterAsText(1)
fcName = arcpy.GetParameterAsText(2)

# Create Points at Vertices
def VerticesToPoints(inFC, workspace, fcName):

     outFc = workspace + "\\" + fcName
     dsc = arcpy.Describe(inFC)
     sr = dsc.spatialReference
     #arcpy.CreateFeatureclass_management(workspace, fcName, "POINT", sr)
     arcpy.CreateFeatureclass_management(workspace, fcName,"POINT", "#", "#", "#", 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

#outFc = workspace + fcName
#outFc = workspace + "\\" + fcName
VerticesToPoints(inFc, workspace, fcName)
arcpy.SetParameter(3, outFc)

I'm not sure why you were getting and error with the spatial reference, but you don't mention the version of the software you are using, so maybe that is an issue.  But if you still have issues, run the lines without the function first to make sure it is working with your inputs and outputs.  Add a  arcpy.Exists(<variable>) once in a while to make sure you don't have that issue (like if you used line 25 in the original script (that I posted for you).  Also, sprinkle in plenty of print or arcpy.AddMessage to make sure the variables are what you expected.  Those are all just good habits to get into when it comes to debugging in general (and of course...add comments.  You may know what it does now...but years from now, you will appreciate the effort you put in now.)

Hope this works for you