1

my indented code without the try/except is below. Python reports no errors but after the script completes running, DefineProjection_management() doesn't show the correct location/points in a map.

My folder structure and code are below. Can somebody suggest a fix please?

import arcpy
from arcpy import env
import os

# set path where the shapefiles are located
arcpy.env.workspace = "Z:/temp1"
inWorkspace = arcpy.env.workspace
workspaces = arcpy.ListWorkspaces("*", "ALL")

for item in workspaces:
    print item
    env.workspace = item
    fcs = arcpy.ListFeatureClasses()

    for file in fcs:
        print '\t', file

 # set local variables
    for file in fcs:
         inData = file
         coordinateSystem = arcpy.SpatialReference(4283)
         arcpy.DefineProjection_management(inData, coordinateSystem)

enter image description here enter image description here

Midavalo
  • 29,696
  • 10
  • 48
  • 104
Tony Bonomo
  • 137
  • 1
  • 8
  • 1
    Please [edit] your question to include the full error message – Midavalo Aug 17 '16 at 01:35
  • 1
    does print arcpy.env.workspace, file output anything? – Midavalo Aug 17 '16 at 01:36
  • 1
    When presenting code snippets I think it is always best to remove any try/except statements because these can often mask otherwise useful error messages provided by Python. – PolyGeo Aug 17 '16 at 02:06
  • 2
    Please edit the code block to retain the indent scheme found in the actual code. Did you mean ".gdb" in the title? – Vince Aug 17 '16 at 02:28
  • Hi, full error msg is included at the top. (name 'inData' is not defined.) Yes print arcpy.env.workspace,file is printing the GDP filenames and their paths – Tony Bonomo Aug 17 '16 at 05:11
  • 1
    There is an indentation issue under the second for block. Make sure the 3 lines under the block are aligned and indented. – GISGe Aug 17 '16 at 05:53
  • 1
    To see the full error message (which will include a line number) you need to remove your try/except statements. name 'inData' is not defined is only part of the error message. – PolyGeo Aug 17 '16 at 23:26
  • 1
    I think you should be looking at the arcpy.da.Walk() tool. This is a better way to traverse feature classes in a workspace. – Fezter Aug 17 '16 at 23:29
  • 1
    Also, this post is probably relevant. – Fezter Aug 17 '16 at 23:30
  • Hi GISGe, I aligned and indented the 3 lines under the second for loop and I get no python error but no action performed by the script either – Tony Bonomo Aug 17 '16 at 23:56
  • Hi PolyGeo, when I removed try/except statements I get no error but no action performed by the script either – Tony Bonomo Aug 17 '16 at 23:58
  • 1
    @TonyBonomo please [edit] and update your Question to include the code without the try/except, so that we can see what you've got. When you paste your code make sure the indentation matches your script. – Midavalo Aug 18 '16 at 03:14

1 Answers1

2

The reason you're not getting errors is because it's not finding anything to change. You need to search for workspaces (gdb) before feature classes. Currently it's looking for feature classes directly in your workspace folder and not inside the GDBs.

While you could use arcpy.ListWorkspaces() before you use arcpy.ListFeatureClasses() I'd recommend you instead use arcpy.da.walk() to step through all the feature classes in all the databases.

Try the following:

import arcpy, os
workspace = r"Z:\temp"

arcpy.env.workspace = workspace
coordinateSystem = arcpy.SpatialReference(4283)
walk = arcpy.da.Walk(workspace, topdown=True, datatype="FeatureClass")

for dirpath, dirnames, filenames in walk:
    for fc in filenames:
        print dirpath, fc
        file = os.path.join(dirpath, fc)
        arcpy.DefineProjection_management(file, coordinateSystem)

(I have typed this on my phone on the bus, so if needed please fix any indentation issues in my code. I'll fix when I'm at a PC shortly)

Midavalo
  • 29,696
  • 10
  • 48
  • 104
  • Hi Midavalo, I copy/pasted your code and it listed the classes etc. thank you! However when I drag the newly created shp file into my map it shows the wrong points/locations in the map. It seems DefineProjection_management() isn't doing the conversion work? – Tony Bonomo Aug 18 '16 at 05:33
  • @TonyBonomo this sounds like a new question - if what I suggested is looping through the GDBs and folders as asked, then issues with your DefineProjection should probably be asked as a new separate question, stating what you're trying to achieve when you define the projection. – Midavalo Aug 18 '16 at 05:50
  • Hi, How can I mark this question as answered pls? – Tony Bonomo Aug 21 '16 at 22:18