
What You Will Need • ArcGIS 8.2 or higher (ArcView, ArcEditor, or ArcInfo license) • Sample data downloaded from ArcUser Online Using VBScript to Build Complex Labels in ArcGIS By Chip Hankley, RMT, Inc. The ability to dynamically retrieve attribute in- formation associated with geographic features sets GIS apart from other mapping technolo- gies. Labels can be automatically generated, reducing the risk of human error associated with typing and transcription. While simple labeling in GIS is fairly routine, creating com- plex labels has traditionally been much more difficult. In ArcMap, the Expression Builder allows scripting (either JavaScript or VBScript) to be used for preprocessing and building labels on the fly. Because both JavaScript and VBScript are extremely powerful languages, this fea- ture goes a long way toward simplifying the process of creating complex labels. Leveraging the power of these languages to generate labels opens many new opportunities for streamlining the labeling process. Although either language could be used, the examples in this article will be limited to VBScript. Data for the Exercise This article uses the DTL_ST shapefile that comes in the sample data for ArcGIS on the ESRI Data & Maps Media Kit CD–ROMs. This shapefile is on the disc with United States data and represents all 50 U.S. states and the District of Columbia. Shapefile attributes con- Use the Expression Builder to generate a simple label. tain general state-by-state census information from the 2000 census. To more easily and Florida 15341185 ly described and click the Expression button. clearly illustrate the examples described in 5. Click OK twice to dismiss the Properties Change the expression so it looks like the code this article, a shapefile consisting of just four dialog box. Right-click the MidAtl layer and in Figure 1. states called MidAtl.shp was included with the click Label Features to display the labels using When typing this code, note that in to sample dataset. This shapefile will be used for the expression just created. make the text more readable in the Expression these labeling exercises. Builder dialog box, the Visual Basic (VB) line Using VBScript and Formatting Tags continuation character, an underscore (_), was Simple Expression Builder Example VBScript and ArcMap text formatting tags can used. Make sure the expression contains no This basic example shows how scripting works be used to format the label. The stateʼs name carriage returns. ArcMap text formatting tags in the Expression Builder. will be capitalized and displayed using the follow the basic rules of XML so each expres- 1. Start an ArcMap session and add the DTL_ Arial font at 12 points. The population figure sion must be well formed. Pay attention to the ST and MidAtl shapefiles. will be printed on a new line using the Luc- following three things when using formatting 2. Right-click the MidAtl layer and choose ida Console font at 10 points with a thousands tags. Properties from the context menu to access the separator and will be preceded by the label • When typing starting and closing tags, you Layer Properties dialog box. Click the Labels YEAR 2000 POPULATION. All text in the must use the same case for both. For instance tab, then click the Expression button. label will be capitalized. To do this, access the “<ACP>” must be closed by “</ACP>”, not 3. Delete anything in the Expression section Properties dialog box for STL_ST as previous- “</acp>”. of the dialog box. Under the Layer Fields sec- tion, double-click STATE_NAME, highlight “<ACP><FNT name=’Arial’ size=’12’>” & [STATE_NAME] & _ POP2000, and click the Append button. The “</FNT>” & vbnewline & _ contents of the dialog box should read: “<FNT name=’Lucida Console’ size=’10’>Year 2000 Population: “ & _ [STATE_NAME] & “ “ & [POP2000] formatnumber([POP2000],0,-1,-1,-1) & “</FNT></ACP>” 4. Click the Verify button. A box should pop up with an example of the label contents dis- Figure 1: Sample expression code for simple example played in the following format: 50 ArcUser October–December 2004 www.esri.com Hands On The Verify button in the Label Expression dialog box is used by ArcMap to validate the label expression. ArcMap will generate an er- ror message in response to scripting errors. If no error is found, the message box indicates the expression is valid and provides an exam- ple of the label text. Any text formatting tags are rendered as raw text in the sample label dialog box. The expression used for these labels was built using mostly text formatting tags. The only VBScript component is the “formatnumber” function, which suppresses the decimal places and adds a thousands separator. • Tags must be nested properly, and inner tags FormatNumber(Expression [,NumDigitsAfterDecimal [,Include- must be closed before outer tags are closed. LeadingDigit [,UseParensForNegativeNumbers [,GroupDigits]]]]) • The ampersand (&), greater than (>), and less than (<) characters are illegal in text for- Figure 2: Syntax for the FormatNumber function matting tags when used as part of a field value in an expression or as part of the label to be rendered. (Note that this does not refer to the Function FindLabel([field1],[field2],…,[fieldx]) “&” used to join the various parts of the expres- …some code sion.) For instance, the state field for a polygon FindLabel = …something could have a value of “Delaware & Maryland,” End Function or a results field on a chemistry dataset could have a value of “< 0.01 mg/kg.” These charac- Figure 3: Performing more advanced labeling ters can be used by replacing them with the fol- lowing HTML escape characters—&amp; for the ampersand, &gt; for the greater than sign, out decimal places and uses a thousands sepa- something different each time based on the and &lt; for the less than sign. rator. To do this, a “0” is passed for the second condition of the data. In such cases, use the Errors in text formatting tag syntax will not argument and a “-1” is passed for the fifth ar- Advanced Expression Builder. To access the cause ArcMap to generate an error; however, gument. Arguments were passed for the third Advanced Expression Builder, check the Ad- when labels are rendered in ArcMap, the for- and fourth elements, but that is irrelevant for vanced check box in the Expression section of matting tags will not be interpreted and will be this dataset because all the values are greater the Label Expression dialog box. The contents rendered as part of the label. Clicking the Ver- than 1. Clicking the Help button below the Ex- of the Expression section will change to a func- ify button will display the raw text that is gen- pression section in the dialog box will bring up tion written in the language of the parser that is erated by the ArcMap labeling engine, which the ArcGIS Help topic “About building label selected at the bottom of the dialog box. The means the formatting tags will be visible. To expressions,” which contains a link to the Mi- format of the function will always looks like see the labels in final form, view the labels in crosoft VBScript Language Reference. This the code shown in Figure 3. ArcMap. reference is invaluable for learning what is Note that in the first line, where the function The expression in Figure 1 is built mostly available in VBScript and for looking up syn- is declared, the field name(s) that will be used using the text formatting tags. The only VB- tax. in the label as arguments need to be passed. Script component is the FormatNumber func- Field names need to be placed in square brack- tion. The syntax for the FormatNumber func- Advanced Labeling With Logic ets, separated by commas. The value of the tion is shown in Figure 2. Sometimes more complex labeling requires variable FindLabel is what is displayed on the In this case, the number is formatted with- logic that will allow the labeling routine to do Continued on page 52 www.esri.com ArcUser October–December 2004 51 Using VBScript to Build Complex Labels in ArcGIS Continued from page 51 Dynamically generating complex labels has tradi- tionally been a complex task. Leveraging the power of VBScript or JavaScript within the Label Expression dialog box opens virtually unlimited opportunities for new label types. As a final step, the Layer Properties dialog box will be used to format the labels with a solid background and a leader line. map so the value of the variable FindLabel will the stateʼs population. States that have a what is possible using a scripting language to need to be set inside the function. By using a 2000 census population greater than 10 million develop labels. If needed, subroutines can be function, the full power of the scripting lan- will have red text. All others will have black included in the expression code. For example, guage can be leveraged by building real-time text. a subroutine could be written that would check logic (in the form of a small program) into the 1. Access the Advanced Expression Builder, for the illegal characters <, >, and & and re- label rendering process. delete any existing text, and type or cut and place them with the appropriate escape char- paste the code in Figure 4 into the Label Ex- acters. An Advanced Example—Stacked Labels pression dialog box. Alternatively, click the Developing a script that creates advanced The script in Figure 4 shows the VBScript rou- Load button, navigate to the sample data, and labels can be a complex task, but once created, tine that was developed to build a multilined select AdvancedLabel.lxp, a saved copy of this such a script provides a scalable and reusable (i.e., stacked) label that will have the following script.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages4 Page
-
File Size-