0

I have python code that I want to turn into a script.

I want the end user to be able to navigate to the feature class the code should be run on.

The code I have currently speaks to a specific feature class and doesn't allow for picking what feature class the code is to be run on.

How do I change my code so I can have the parameters in the script allow the end user to pick what feature class the code should be run on?

This is the piece of code I need to change to allow the end user pick the feature class.. enter image description here

What do I need to change in my code to allow for the peramiters I set up in the script to replace this line of code? I need fc to equal the input the end user selects not a specific feature class as the code currently stands

1
  • Please always include code as text (which you can format using the {} button) rather than as a picture.
    – PolyGeo
    Aug 20, 2015 at 22:15

2 Answers 2

1

That line of code simply has to be changed to:

fc = arcpy.GetParameterAsText(0)  # 0 assumes that it is the first parameter defined on your tool dialog

but you also need to configure the parameter on your tool dialog.

I recommend reviewing:

3
  • Thanks for the help Poly but my code doesn't work when I change my code. This is the error I get...ERROR 000714: Error in script Script1. Error in executing: cmd.exeC:\Users\tlathrop\Desktop\Python\LATLON~3 "C:\Users\tlathrop\Desktop\TL_GIS\Metadata\DomainWork\DomainTesting_1.gdb\hospital_pt"....it works fine w/out changing the code as you indicated...any ideas?
    – Nebula93
    Aug 21, 2015 at 15:41
  • Without seeing precise code and what you have configured your tool dialog to it is not possible to advise. I suggest thinking about how you could phrase a new question.
    – PolyGeo
    Aug 21, 2015 at 20:35
  • I apparently had bug in the software. I had to reinstall ArcMap and once I did the code worked fine.
    – Nebula93
    Aug 28, 2015 at 14:35
1

You want to make use of Python functions. This allows you to make dynamic tasks and allows for code re-usability.

If you are wanting to make an input optional, say related to your question Calculating geometry for feature class with x/y and Lat/Long values in different projections? you could do this:

import arcpy

def getLatLong(fc, lat_field, long_field):
    """add lat/long fields

    fc -- path to feature class or shapefile
    lat_field -- latitude field to populate
    long_field -- longtitude field to populate
    """

    # add fields if they don't exist
    fieldList = [f.name for f in arcpy.ListFields(fc)]
    for f in [lat_field, long_field]:
        if f not in fieldList:
            arcpy.management.AddField(fc, f, 'DOUBLE')

    # populate lat/long fields
    wgs = arcpy.SpatialReference(4326)
    fields = ['SHAPE@XY', lat_field, long_field]
    with arcpy.da.UpdateCursor(fc, fields, spatial_reference=wgs) as rows:
        for row in rows:
            row[1:] = row[0] 
            rows.updateRow(row)

if __name__ == '__main__':

    fc = r'C:\TEMP\UofM_cc.shp'
    getLatLong(fc, 'Latitude', 'Longitude')
    print 'done'

Because this is a Python function, you can pass any point feature class in you want and it will do the coordinate conversion.

This could also be used for a script tool where you could pass in parameters like this:

getLatLong(*[arcpy.GetParameterAsText(i) for i in range(arcpy.GetArgumentCount())])

And thanks to Paul for mentioning the spatial_reference option for the update cursor. I always forget about that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.