1

I have updated my script in ArcGIS 10.3 to include the copyFeatures_management geoprocessing tool because I want to only copy the features that already have the same projection, not reproject them;

#Loop through shapfiles in folder and reproject
 for fc in fcList:
     fcspatialRef = arcpy.Describe(fc).spatialReference.name
     if fcspatialRef != spatialRef:
         arcpy.Project_management(fc, outFolder + "\\" + fc, template) 
     else:
         arcpy.CopyFeatures_management(fc, outFolder + "\\" + fc)

# Print shapefile Project results
     arcpy.AddMessage(fc)

But when I do, I loss what I have been trying to achieve with the shapefile list in the dialog window;

![enter image description here][1]

The script works fine without the copyFeatures_management included;

#Loop through shapfiles in folder and reproject
for fc in fcList:
    fcspatialRef = arcpy.Describe(fc).spatialReference.name
    if fcspatialRef != spatialRef:
        arcpy.Project_management(fc, outFolder + "\\" + fc, template) 

# Print shapefile Project results
    arcpy.AddMessage(fc)

Any ideas please?


Indentation fixed, code working for me, but still printing both the projected and copied shapefiles names in list, when I only want the projected shapefile names printed.

@fluidmotion, the;

arcpy.AddMessage('{} was projected'.format(fc)) # add if you want projected fc printed to screen

isn't working at all, and,

arcpy.AddMessage('{} was copied only'.format(fc)) #add if you want copied fc printed

is printing both the projected and copied shapefile names in the loop.

6
  • 1
    your indenting is off. if fcspatialRef != spatialRef: needs to be indented... also spatial references can't be equated. Try using the factoryCode instead (it's an int) or alias (string) for the !=. As it stands by the time it gets to arcpy.AddMessage the value of fc is nothing. Commented Apr 15, 2015 at 0:43
  • 1
    If you try to run that code snippet then it looks like it will throw indentation errors. Can you always test your code snippets before posting them so that we will know what error/symptom to focus on, please?
    – PolyGeo
    Commented Apr 15, 2015 at 0:44
  • noooo, my code was just working.
    – Mark W
    Commented Apr 15, 2015 at 0:56
  • 1
    I recommend keeping your code separate from your code snippets. That way, as you get each code snippet working (separate from your code), you will learn how it works and what it achieves, before incorporating it back into your code. Here we are not so interested in your code - that is yours - but happy to help with code snippets.
    – PolyGeo
    Commented Apr 15, 2015 at 1:13
  • 1
    @MichaelMiles-Stimson I checked some of the OP's code from a previous question and he's doing a string not an SR Object comparison. Still I agree that there are better ways to test for SRS equality.
    – user2856
    Commented Apr 15, 2015 at 2:43

3 Answers 3

3

i may be way off, but i don't see where the issue is that the script does not actually copy the correct features - it simply doesn't print them to the dialogue as you would expect (?). As others have mentioned - this is difficult to assist as we can't tell if the issue is simply indentation problems. Here's what we could assume the indentation should be:

#Loop through shapfiles in folder and reproject
for fc in fcList:
    fcspatialRef = arcpy.Describe(fc).spatialReference.name
    if fcspatialRef != spatialRef:
        arcpy.Project_management(fc, outFolder + "\\" + fc, template) 
        arcpy.AddMessage('{} was projected'.format(fc)) # add if you want projected fc printed to screen
    else:
        arcpy.CopyFeatures_management(fc, outFolder + "\\" + fc)
        # Print shapefile Project results
        arcpy.AddMessage('{} was copied only'.format(fc)) #add if you want copied fc printed

depending on what you want printed to the dialogue - i've added statements above that would hopefully help.

3
  • 1
    +1 especially for adding "AddMessage('{} was projected/copied'" message!
    – user2856
    Commented Apr 15, 2015 at 2:39
  • Correct fluidmotion, I can't get the correct print in the window dialog. I hopefully have fixed my original question with the correct indentation as well. Unfortunately I only required the shapefiles names in my image.
    – Mark W
    Commented Apr 15, 2015 at 4:54
  • multiple ways to accomplish this - and i think many have made correct suggestions. in the code above, there are 2 AddMessage statements that should print the shape file names to the dialogue box. You could use either or both. Commented Apr 15, 2015 at 15:22
3

Your indentation is incorrect. Your first code sample loops through all fcs, sets the fcspatialRef each time and does nothing else. It then exits the loop, compares the last fcspatialRef to spatialRef and projects or copies the last fc only, then calls arcpy.AddMessage(fc) only if the last fc is copied, not projected.

Correct indentation:

#Loop through shapfiles in folder and reproject
for fc in fcList:
    fcspatialRef = arcpy.Describe(fc).spatialReference.name

    if fcspatialRef != spatialRef: 
        arcpy.Project_management(fc, outFolder + "\\" + fc, template) 
    else:
        arcpy.CopyFeatures_management(fc, outFolder + "\\" + fc)

    # Print shapefile Project results
    arcpy.AddMessage(fc)
0

I solved it;

arcpy.AddMessage(fc)
arcpy.Project_management(fc, outFolder + "\\" + fc, template)

not

arcpy.Project_management(fc, outFolder + "\\" + fc, template)
arcpy.AddMessage(fc)
2
  • 1
    I cannot see how reversing the order of these two particular lines of code would make any difference to what shows up in the progress dialog or what gets output and where.
    – PolyGeo
    Commented Apr 16, 2015 at 10:29
  • agreed. perhaps it would change the order of some output - depending on what project_management prints to the screen by default - but they should both work basically the same way. Commented Apr 17, 2015 at 12:28

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.