Select to view content in your preferred language

Something simple that I can't seem to troubleshoot?

2905
9
Jump to solution
09-09-2021 07:02 AM
RPGIS
by
Frequent Contributor

Hi,

I am currently working on a custom python tool that works until a certain point. The issue that I am having, and it is probably really simple but I can't seem to trouble shoot, is specifying the feature class using os.path.join method (unless I configured this wrong but running a random test proves otherwise).

The input workspace is set by a created gdb derived from one of the user inputs. Everything works until this point and I am now at a standstill. Any help on this would be greatly appreciated.

import arcpy

Project_GDB = 'input random gdb'

SubBoundaryFC_Name = 'SubdivisionBoundary'
SubdivisionFC = os.path.join(Project_GDB, SubBoundaryFC_Name)
SubBoundary = arcpy.management.FeatureToPolygon(FCs_forSubdivisionBoundary, SubBoundaryFC_Name)
arcpy.AddMessage('{} has been created.'.format(SubBoundary))
0 Kudos
1 Solution

Accepted Solutions
RPGIS
by
Frequent Contributor

Hi @BlakeTerhune,

So I figured out the issue with part of the script that was giving me issues.

 

#Check to see if the following project already exists. If project exists, overwrite the existing project.
        Project_GDB = ''
        for root, directory, files in os.walk(folder_location):
                GDB = os.path.split(root)[-1]

                if GDB == ProjectGDB:
                        Project_GDB  = root
                else:
                        Project_GDB = arcpy.management.CreateFileGDB(folder_location, Project)

 

Further in the script I converted the output to a string and then utilized the os.path.join which worked as intended.

 

SubBoundaryFC_Name = 'SubdivisionBoundary'
SubdivisionFC = os.path.join(str(Project_GDB), SubBoundaryFC_Name)
SubBoundary = arcpy.management.FeatureToPolygon(FCs_forSubdivisionBoundary, SubdivisionFC)
arcpy.AddMessage('{} has been created.'.format(SubBoundaryFC_Name))

 

So this simple solution was really all I needed. Here is the message result that it generated.

SubdivisionBoundary has been created.

View solution in original post

9 Replies
David_Brooks
MVP Regular Contributor

@RPGIS you haven't declared this variable:

FCs_forSubdivisionBoundary

 


David
..Maps with no limits..
RPGIS
by
Frequent Contributor

I accidently left out of the snippet for this little piece here by mistake. 

FCs_forSubdivisionBoundary = []

It is derived from using walk to append each feature class in the database based on matching names.

0 Kudos
BlakeTerhune
MVP Regular Contributor

Are you getting a particular error message? If so, please paste the entire traceback if you can. Is everything in FCs_forSubdivisionBoundary a line or polygon feature class? Also, try using SubdivisionFC as the out_feature_class for FeatureToPolygon() instead of SubBoundaryFC_Name (because no workspace is specified).

0 Kudos
RPGIS
by
Frequent Contributor

Hi @BlakeTerhune,

Here is the error message that I keep getting when it runs.

Failed script Import Test...
Traceback (most recent call last):
  File "*:\CAD Exporting Test\Default.tbx#ImportTest_Default.py", line 207, in <module>
  File "*:\CAD Exporting Test\Default.tbx#ImportTest_Default.py", line 183, in ScriptTool
  File "*:\''\''\AppData\Local\Programs\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\ntpath.py", line 76, in join
    path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not Result
Failed to execute (ImportTest).

Not sure what this error refers to but I can't seem to bypass it for some reason. I am thinking that the issue is either a syntax issue or an output error.

0 Kudos
BlakeTerhune
MVP Regular Contributor

That error means you're taking a Result object (output from a geoprocessing tool) and inputting it directly into something else without unpacking it.

I can't say exactly where it's happening since we don't have the whole script but look on line 183. Something there is getting a result object when it should be getting something more specific (like a string path to a feature class).

RPGIS
by
Frequent Contributor

Hi @BlakeTerhune,

So I figured out the issue with part of the script that was giving me issues.

 

#Check to see if the following project already exists. If project exists, overwrite the existing project.
        Project_GDB = ''
        for root, directory, files in os.walk(folder_location):
                GDB = os.path.split(root)[-1]

                if GDB == ProjectGDB:
                        Project_GDB  = root
                else:
                        Project_GDB = arcpy.management.CreateFileGDB(folder_location, Project)

 

Further in the script I converted the output to a string and then utilized the os.path.join which worked as intended.

 

SubBoundaryFC_Name = 'SubdivisionBoundary'
SubdivisionFC = os.path.join(str(Project_GDB), SubBoundaryFC_Name)
SubBoundary = arcpy.management.FeatureToPolygon(FCs_forSubdivisionBoundary, SubdivisionFC)
arcpy.AddMessage('{} has been created.'.format(SubBoundaryFC_Name))

 

So this simple solution was really all I needed. Here is the message result that it generated.

SubdivisionBoundary has been created.
by Anonymous User
Not applicable

It can be tricky trying to troubleshoot script tools but there are some ways you can get error messages.

import arcpy

Project_GDB = 'input random gdb'
try:
    SubBoundaryFC_Name = 'SubdivisionBoundary'
    SubdivisionFC = os.path.join(Project_GDB, SubBoundaryFC_Name)
    # As Brooks mentioned, should SubdivisionFC be FCs_forSubdivisionBoundary below?
    SubBoundary = arcpy.management.FeatureToPolygon(FCs_forSubdivisionBoundary, SubBoundaryFC_Name)
    arcpy.AddMessage('{} has been created.'.format(SubBoundary))
except:
    arcpy.AddError(arcpy.GetMessages(2))

or if you want to test the script in an IDE as a standalone/script tool script and use env.workspace:

import arcpy
Project_GDB = arcpy.GetParametersAsText(0) # value assigned when ran as script tool
if Project_GDB == '' or not Project_GDB:
    Project_GDB = 'input random gdb' # default value for standalone

SubBoundaryFC_Name = arcpy.GetParametersAsText(1)
if SubBoundaryFC_Name == '' or not SubBoundaryFC_Name:
    SubBoundaryFC_Name = 'SubdivisionBoundary'

try:
    arcpy.env.workspace = Project_GDB
    # if the target fc's are in the project_gdb, you should be able to reference them by name and not have to use os.path.join if you set your env.workspace
    SubBoundary = arcpy.management.FeatureToPolygon("FCs_forSubdivisionBoundary", SubBoundaryFC_Name)
    arcpy.AddMessage('{} has been created.'.format(SubBoundary))
except:
    arcpy.AddError(arcpy.GetMessages(2))

 

BlakeTerhune
MVP Regular Contributor

@Anonymous User has some good suggestions for error handling. I want to point out that the except like that will catch all exceptions but will only attempt to output specifically arcpy geoprocessing errors. A more well rounded solution would be a specific except and a general except for everything else.

 

import arcpy

Project_GDB = 'input random gdb'
try:
    SubBoundaryFC_Name = 'SubdivisionBoundary'
    SubdivisionFC = os.path.join(Project_GDB, SubBoundaryFC_Name)
    # As Brooks mentioned, should SubdivisionFC be FCs_forSubdivisionBoundary below?
    SubBoundary = arcpy.management.FeatureToPolygon(FCs_forSubdivisionBoundary, SubBoundaryFC_Name)
    arcpy.AddMessage('{} has been created.'.format(SubBoundary))
except arcpy.ExecuteError:
    arcpy.AddError(arcpy.GetMessages(2))
except Exception as e:
    arcpy.AddError(e)

 

Error handling with Python—ArcGIS Pro | Documentation

MichaelVolz
Esteemed Contributor

I would suggest adding print statements for variables to ensure that they are being populated as expected.