052] Global Mapper - Creating Attributes Using Calculations - Attribute Calculation Setup: Use Formula Calculation
052] Global Mapper - Creating Attributes Using Calculations - Attribute Calculation Setup: Use Formula Calculation
Today, we will learn how to transform attributes using "Use Formula Calculation" in the Attribute Calculation Setup.
[046] Parallel Line Offset: Create Parallel Lines Spaced from Selected Line Feature(s)
The Need for Attribute Transformation in 3D Simulations
For 3D simulations, you need to create 3D data by applying a planned elevation to each parcel. However, in actual CAD drawings you receive, the parcels and the planned elevations are often placed on separate layers. Of course, if there are only one or two, you could manually enter the elevation value for each parcel, but if there are hundreds of parcels, the story changes.
Take a look at the data below.
The Lot.shp layer contains the parcels, and Height.shp contains the planned elevation for each parcel.
If the Height.shp layer consisted of numbers only, you could simply copy them using the method described in "028] Global Mapper - Transferring Attributes: Copy Attributes Between Features." However, since "FH:" is attached in front of the numbers, it is difficult to use them directly as the elevation attribute for the parcels unless that string is removed.
Let's first look at the attributes of a single point.
The Feature Name is "FH: 118" and there is a "NAME" attribute which is identical to the Feature Name. I've confirmed there are no other attributes.
Step-by-Step: Removing "FH:" and Converting to Numbers
Now, I will show you how to delete "FH:", convert the remaining text into a number, and then save it as an attribute for each parcel in Lot.shp.
In the Control Center, right-click on the Height.shp layer and select Layer -> Calculate/Copy Attribute Value(s)...
A window will appear asking you to select the layer to operate on.
Since we need to transform the "FH:xxx" values in Height.shp to create the attribute, select Height.shp and click OK.
The Attribute Calculation Setup window will appear. This window only applies to the Height.shp layer selected in the previous step.
Type the new attribute name in the top box. Since we need to get the planned elevation, let's enter "elevation".
Now, the important part: check the "Use Formula Calculation" option. This allows you to enter a calculation formula in the Enter Formula box.
You can select elements to input from two lists:
The first is Available Attributes, which includes the attributes owned by the layer and the features within it.
In Height.shp, besides the default attributes, you can see the "NAME" attribute that the features originally possessed.
The second list is Available Functions.
If you have even a little experience with SQL, these are quite easy to understand.
Designing the Formula
The points in Height.shp all have the same "NAME" attribute format, such as "FH:xxx". We will design a formula using this "NAME" attribute value and then apply the functions.
What we need from "FH:xxx" is the "xxx" part. Simply put, we just need to take the value to the right of the ":" and convert it into a numeric format.
To do this, we can use the right(string, number) function. This function takes a string and returns a string of the length specified by 'number', starting from the right end.
Here is the logic for the formula:
- Receive the entire string.
- Find the length of that string.
- Find the position (index) of the ":".
- Subtract the index of ":" from the total length to find the length of the remaining string on the right.
- Since
findis based on an index, we subtract 1 more to get only the numeric part. - Wrap this value with
num()to convert it into a number.
Applying the Formula
Let's look at the actual formula:
NUM(RIGHT(%NAME%, LEN(%NAME%) - FIND(%NAME%, ":") - 1))
Breakdown:
If %NAME% is "FH:108":
LEN(%NAME%) = 6
FIND(%NAME%, ":") = 2 (The third character is index 2)
6 - 2 - 1 = 3 (If we don't subtract 1, the ":" would be included)
RIGHT("FH:108", 3) = "108"
NUM("108") = 108
As I always say, there is no single right answer. Remember that you can achieve the same result in different ways depending on how you develop your logic.
Now, enter this formula into the Enter Formula box.
[img 10 : Formula entered into the box with Sample result showing 118]After entering it, look at the Status / Sample result: section. it shows whether there are errors in the function, what the result is, and what sample value was used.
If "Formula OK" appears, there are no syntax errors. You just need to verify that the value used in Parameters is correctly transformed in the Sample result.
You can see that "FH:118" has been correctly transformed to "118". Now, click the Calculate button to get the final results.
[img 11 : Clicking the Calculate button]A window appears telling you how many features had their attributes changed. In this case, 6 features were modified. Now, if we check the point information again:
[img 13 : Updated Feature Info window showing the new elevation attribute with value 118]The "elevation" attribute and its value have been correctly added.
Wrapping Up
The remaining process is to copy the attribute values between the two layers. You can refer to "028] Global Mapper - Transferring Attributes: Copy Attributes Between Features" for instructions on how to do that.
That’s all for today!
Comments
Post a Comment