ArcGIS 10.1 ModelBuilder - ValidateTableName - Calculate Value - Expression

2599
5
Jump to solution
01-31-2013 07:21 PM
by Anonymous User
Not applicable
Original User: Playa

I need assistance in constructing a python expression using calculate value to validate the new Feature Classes that I'm creating from selecting CAD Polyline Features based on their Layer Names. The problem that I have is that there are invalid characters within the CAD Layer Names. I need to use a combination of the CAD Layers Name and the Output Location (File Geodatabase) to use ArcGIS ValidateTableName to remove the invalid characters automatically. I'm stumped in how to construct the python expression to accomplish this. Any assistance will be appreciated. I've attached print screens of my model and the model toolbox.

Regards
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
could [you] assist me constructing a python expression using calculate value to solve a similar problem. I've attached two print screens of my current model. I'm selecting CAD Features based on their layer name and then using Feature Class to Feature Class to write out the CAD Features to a File Geodatabase based on the Layer Name. The problem that I have is that I need to remove invalid characters from the Layers Name.


Here's some code for using Calculate Value to do this:

Expression:
ValidateName(r"%Layer name%",r"%Output geodatabase%")

Code:
def ValidateName(fc,wks):   import arcpy   vName = arcpy.ValidateTableName(fc,wks)   return vName


The way to connect this up is to make the inputs preconditions (to ensure the input values are ready to go when you run Calculate Value [CV]) and then you can connect the output directly to the Feature Class to Feature Class tool.  Depending on the required parameter type for the next tool you may have to set the output Data Type in Calculate Value. In this case, Feature Class to Feature class was happy to accept the default type (Any Value).

In some situations (say, the name is part of a pathname or the tool validation doesn't accept a parameter type that CV supports) you cannot easiliy connect the CV output to the tool. In those cases, you connect the CV output with a precondition and use the CV output variable in the tool dialog parameter box (e.g. "MyOutput_%Validated_name%").  Calculate Value, as you have seen by now, is key to make the most of ModelBuilder.

[ATTACH=CONFIG]21322[/ATTACH]
[ATTACH=CONFIG]21319[/ATTACH]



Another approach to your problem is the Feature Class to Geodatabase (Multiple) tool, which does all this iteration and validation for you. (BTW, its source code really helped me get the hang of validation and iteration in Python when I was first learning.)

View solution in original post

0 Kudos
5 Replies
curtvprice
MVP Esteemed Contributor
could [you] assist me constructing a python expression using calculate value to solve a similar problem. I've attached two print screens of my current model. I'm selecting CAD Features based on their layer name and then using Feature Class to Feature Class to write out the CAD Features to a File Geodatabase based on the Layer Name. The problem that I have is that I need to remove invalid characters from the Layers Name.


Here's some code for using Calculate Value to do this:

Expression:
ValidateName(r"%Layer name%",r"%Output geodatabase%")

Code:
def ValidateName(fc,wks):   import arcpy   vName = arcpy.ValidateTableName(fc,wks)   return vName


The way to connect this up is to make the inputs preconditions (to ensure the input values are ready to go when you run Calculate Value [CV]) and then you can connect the output directly to the Feature Class to Feature Class tool.  Depending on the required parameter type for the next tool you may have to set the output Data Type in Calculate Value. In this case, Feature Class to Feature class was happy to accept the default type (Any Value).

In some situations (say, the name is part of a pathname or the tool validation doesn't accept a parameter type that CV supports) you cannot easiliy connect the CV output to the tool. In those cases, you connect the CV output with a precondition and use the CV output variable in the tool dialog parameter box (e.g. "MyOutput_%Validated_name%").  Calculate Value, as you have seen by now, is key to make the most of ModelBuilder.

[ATTACH=CONFIG]21322[/ATTACH]
[ATTACH=CONFIG]21319[/ATTACH]



Another approach to your problem is the Feature Class to Geodatabase (Multiple) tool, which does all this iteration and validation for you. (BTW, its source code really helped me get the hang of validation and iteration in Python when I was first learning.)
0 Kudos
by Anonymous User
Not applicable
Original User: Playa

Hi Curtis

Thanks so much for assisting me in understanding how to construct the following expression. I�??ve been using ModelBuilder ever since it was released and it has helped me understand how the tools work in ArcGIS and being able to automate a large amount of my daily workflows. I�??ve been working within the Engineering field as a GIS Technologist for more 10 years and constantly have to convert data between GIS and CAD.  Normally I would identify the drawing layer\layers that I�??m interested in converting to GIS and would individually export them to a Feature Class by turning on and off the appropriate drawing layers from with the properties within ArcMap. When ESRI released CAD to Geodatabase I was stumped by the fact that they exported all the features into a single feature class based on the three basic types of feature class (i.e. Point, Line and Polygon). I felt that the tool was of no use to our environment and couldn�??t understand how this would help anyone. I then started looking into developing the following model. After getting the model to run and valuating my results I realised I had landed up with over 128 Feature Class (i.e. 128 Layers) and 128 Annotation Feature Classes. I then realised that although I needed to work with the Features based on their entity types that they represented that they didn�??t have to be split as I was able to distinguish between the different entities based on the Layer Field. What I am considering is creating a model that uses CAD to Geodatabase and then creates a Subtype based on the Layer Field.

The one thing that I�??ve realised more and more over the last while is that I urgently need to learn to script using Python. I�??ve been putting it off for too long, on the basis that it�??s too difficult.

Regards
0 Kudos
curtvprice
MVP Esteemed Contributor
Good deal.

One tool you haven't mentioned that you may want to investigate for this process is the Pivot Table tool, which can be used to simplify complex datasets (like the  kind that come out a CAD import). Just thought I'd mention it.
0 Kudos
by Anonymous User
Not applicable
Original User: Playa

Hi Curtis

Thanks for the helpful advice its greatly appreciated. I have a look at the "Pivot Table Tool". I'll post my final model, hopefully it will help someone else that is trying to accomplish something simaler.

Regards
0 Kudos
KarenHodge
Esri Contributor
You may want to look into the "Mapping Specification for CAD" and the free plug in, ArcGIS for AutoCAD.

The Mapping Specification for CAD defines a standard for creating feature classes inside a CAD file.  They can be as simple as "All the features on the ROADs layer belong to a feature class called Roads", or much more complicated.    It also defines a method of storing attributes on CAD features--just like GIS.  This standard, however, is just a specification--you'll need to use ArcGIS for AutoCAD or similar tool to actually create the feature classes.

The ArcGIS for AutoCAD tool helps you define these feature classes and edit any attributes.  Even if you don't want all your CAD editors to use the plug in, you can predefine a template file that includes the feature class definitions.  That way, when the drafter using plain AutoCAD to edit the CAD file, they are also populating feature classes.  It just makes it easier when you want to bring the resultant data into ArcGIS.
0 Kudos