1

What I'm trying to do is collect two .csv links and two layer identifications from the user, geocode the addresses in the .csv, and then change the data source on the identified layer in an open map to the geocoded results. The geocoding works fine, but replacing the data source doesn't work. It results in no error messages either, so I am having great difficulty tracking down the problem.

Here is a snippet of code:

mapdoc = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mapdoc, "")[0]
lyrlist = arcpy.mapping.ListLayers(mapdoc, "*", df)
for lyr in lyrlist:
    if lyr.supports("DATASOURCE"):
        if lyr.dataSource == cup_lyr_file:
            lyr.replaceDataSource(where_to_save, "SHAPEFILE_WORKSPACE", cup_bname, "")
        elif lyr.dataSource == reg_lyr_file:
            lyr.replaceDataSource(where_to_save, "SHAPEFILE_WORKSPACE", reg_bname, "")

where_to_save is the workspace collected from the user as to where the geocoded files should be saved. cup_bname and reg_bname are the baseNames of the geocoded shapefiles.

I am just now learning Python. With no feedback at all from the program, I'm lost as to what could be wrong.

1 Answer 1

1

One suggestion: where you have IF...ELIF statements, add an ELSE statement to your code to let you know if none of your IF conditions are met. For example:

mapdoc = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mapdoc, "")[0]
lyrlist = arcpy.mapping.ListLayers(mapdoc, "*", df)
for lyr in lyrlist:
    if lyr.supports("DATASOURCE"):
        if lyr.dataSource == cup_lyr_file:
            lyr.replaceDataSource(where_to_save, "SHAPEFILE_WORKSPACE", cup_bname, "")
        elif lyr.dataSource == reg_lyr_file:
            lyr.replaceDataSource(where_to_save, "SHAPEFILE_WORKSPACE", reg_bname, "")
        else print("dataSource is not cup_ or reg_ lyr_file.")
    else print("Layer does not support DATASOURCE.")

You would probably want to include the name of the layer or datasource in the PRINT function.

5
  • Where does the script print the message? I tried putting in print commands in various places in the script (including one at the very beginning that should print 100% of the time) and never saw the message.
    – Paulie_net
    Commented Feb 27, 2018 at 21:48
  • It should print them to the console window of whatever software you're using to run the script. I usually run mine in Pyscripter, any print commands show up in the "Python Interpreter" tab. You may need to turn on (or make active) the appropriate window/tab in your Python IDE.
    – Dan C
    Commented Feb 27, 2018 at 21:54
  • 1
    Use arcpy.AddMessage() instead of print() to get the messages to appear in the Results tab in ArcMap. So print("dataSource is not cup_ or reg_ lyr_file.") would become arcpy.AddMessage("dataSource is not cup_ or reg_ lyr_file.")
    – Dan
    Commented Feb 27, 2018 at 23:38
  • Thank you very much! This was so useful! I was able to query my variables and figure out that cup_lyr_file and reg_lyr_file did not have the values I expected them to have. So that's why the script wasn't working. I had no idea because it was running silently.
    – Paulie_net
    Commented Feb 28, 2018 at 13:49
  • I also did add in an else statement for each if. Thank you so much for your kindness and assistance.
    – Paulie_net
    Commented Feb 28, 2018 at 13:52

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.