Hello All,
I have a script which I'm trying to implement to re-define the projections of all datasets in a directory and its subdirectories. I'm using the define projection tool from data management to do this. Unfortunately, when I run the script, I'm getting an error:
If I try to define the projection in one of these datasets directly through ArcToolbox, I get the following warning:
My guess is that I need to write some error handling into the script in order to dismiss this warning, but I can't find anything anywhere that explains clearly how I might implement this. Anyone have any ideas? Feedback is greatly appreciated!
My code so far:
import os import arcpy from arcpy import env arcpy.env.overwriteOutput = True workspace = r"C:\Scripts\Redefine Projection" newProjection = arcpy.SpatialReference(25832) # wkid for 'ETRS_1989_UTM_Zone_32N' def recursiveProjections(workspace): """ Function iterates through a workspace and its subfolders using arcpy.da.walk and returns a tuple containing two lists: 1) list of spatial reference objects of all geographic datasets (including those inside geodatabases) and 2) a list of filepaths for the geographic datasets. Parameter: target workspace """ describeDatasets = [] filesList = [] for dirpath, path, filenames in arcpy.da.Walk(workspace): for filename in filenames: desc = arcpy.Describe(os.path.join(dirpath, filename)) describeDatasets.append(desc) for path, path_names, data_names in arcpy.da.Walk(workspace): for data_name in data_names: filesList.append(os.path.join(path, data_name)) return describeDatasets, filesList def defineProjection(workspace): """ Function redefines the existing projection for datasets in a workspace. A log file is created listing the paths of all redefined datasets. Parameter: workspace """ # unpack tuple returned by def recursiveProjections into two lists descriptions, filenames = recursiveProjections(workspace) # iterate through features and redefine their projections correctly for feature in descriptions: arcpy.DefineProjection_management(feature, newProjection) def main(): defineProjection(workspace) if __name__ == '__main__': main()
Solved! Go to Solution.
The warning should pass on its own (Warning 000132.....), but the runtime error is the issue.
Looking at your code, you are running the Define Projection against the description object you made, shouldn't it be running it with the file names?
Why are you using a toolbox when you haven't defined any parameters to get values from the tools? Your script appears to be a standalone script, which you can edit and run. If you want to use a toolbox, then you have to get the parameters as variables, then define those parameters in the toolbox. And based on your script, I don't think you want to get a featureclass as the first input variable, you want a workspace (ie gdb or folder).
Can you show the link between your script and the parameters you defined in the toolbox?
Where are you in this process? A quick tour of creating tools with Python—Help | ArcGIS for Desktop
Hi Dan, thanks for your response. I'm just running a standalone script, I just ran the tool to demonstrate the error that I assume is being encountered when the script is run. I'm not trying to develop a toolbox, just a script.
You might want to look at the Define Projection Help, they give a good example on the standalone script for using try/except blocks to handle error messages.
Also just as always a reminder that Define Projection is only to be used when a a file has an unknown coordinate system or is currently using an incorrect coordinate system.
"The most common use for this tool is to assign a known coordinate system to a dataset with an unknown coordinate system (that is, the coordinate system is "Unknown" in the dataset properties). Another use is to assign the correct
coordinate system for a dataset that has an incorrect coordinate system defined (for example, the coordinates are in UTM meters but the coordinate system is defined as geographic)."
Also may I recommend that you do not put spaces in your filepaths anywhere, they have a bad habit of messing up some geoprocessing tools. You may want to rename your folder to remove the space.
Hope this helps
Hi Ian,
Thanks for the response. I'm doing this because the custom projection on these datasets needs to be renamed to an identical known projection.
As far as I can tell, the example on the Define Projection help page just prints the error I get, but I need something that will tell it to acknowledge the error but run anyway.
The warning should pass on its own (Warning 000132.....), but the runtime error is the issue.
Looking at your code, you are running the Define Projection against the description object you made, shouldn't it be running it with the file names?
Ah -- now that makes more sense. Thanks for pointing that out...so I guess I just need to pass the tool the file path string, yes? For some reason I thought I had to pass it the description.
Working better now or still getting errors?
Hi Ian, sorry, I'm on European time. Yup, looks good, thanks again...new code:
import os import arcpy from arcpy import env arcpy.env.overwriteOutput = True workspace = r"C:\Scripts\Redefine Projection" newProjection = arcpy.SpatialReference(25832) # wkid for 'ETRS_1989_UTM_Zone_32N' def recursiveDatasets(workspace): """ Function iterates through a workspace and its subfolders using arcpy.da.walk and returns a list of unicode filepaths for every geogrpahic dataset in wht workspace. Parameter: target workspace """ filesList = [] for path, path_names, data_names in arcpy.da.Walk(workspace): for data_name in data_names: filesList.append(os.path.join(path, data_name)) return filesList def defineProjection(workspace): """ Function redefines the existing projection for datasets in a workspace. A log file is created listing the paths of all redefined datasets. Parameter: workspace """ datasetPaths = recursiveDatasets(workspace) # iterate through features and redefine their projections correctly for feature in datasetPaths: arcpy.DefineProjection_management(feature, newProjection) ## # logging ## for filename in filenames: ## print filename def main(): defineProjection(workspace)
Also, it's worth noting that if a feature class is inside a feature dataset, the projection cannot be redefined -- it would have to be reprojected and with the destination set as a new feature dataset (which also has to be created beforehand, of course). That's a fair amount of code that I won't post here, but I did change the recursiveDatasets function to put any feature classes residing inside feature datasets in their own list:
def recursiveDatasets(workspace): """ Function iterates through a workspace and its subfolders using arcpy.da.walk and returns a tuple containing two lists containing: 1) paths for all feature classes contained in feature datasets inside geodatabases and 2) filepaths for feature classes not inside feature datasets. Parameter: target workspace """ describeDatasets = [] filesList = [] featureDatasetFeatureClasses = [] for dirpath, path, filenames in arcpy.da.Walk(workspace): for filename in filenames: desc = arcpy.Describe(os.path.join(dirpath, filename)) # create a desctription object for each feature if hasattr(desc, 'path'): descPth = arcpy.Describe(desc.path) if hasattr(descPth, 'dataType'): # if a feature class is part of a feature dataset, which cannot be reprojected, # add it to featureDatasetFeatureClasses list: if descPth.dataType == 'FeatureDataset': featureDatasetFeatureClasses.append(os.path.join(dirpath, filename)) # if not part of feature dataset, add it to filesList else: filesList.append(os.path.join(dirpath, filename)) return featureDatasetFeatureClasses, filesList