As part of a Spatial Model I had performed image segmentation on a CIR airphoto to produce area geometry features. Those features had then been classified using Machine Learning into discrete classes (e.g. "forest", "wheat", etc) stored in an attribute field called Name (such as by using the techniques shown in this Spatial Recipe). Styling was applied using the unique occurrences of Name attributes to form the data displayed in the left-hand 2D View of the screenshot below.
The final problem was how to combine segments which touched, and which shared the same Name attribute value, to form larger, contiguous area geometries (the results being shown in the right-hand 2D View of the screenshot below).
|Input shapefile of classified segments is shown in the left 2D View, results of running Merge_Features.gmdx on the right|
The screenshot below shows the basic Merge Features operator. The features to be merged would be fed into the FeaturesIn port.
|Merge Features operator|
To perform the desired merge of area geometries the following parameters were set:
The Criteria port was double-clicked and the Merge Touching Geometries and Matching Attributes option selected.
The MatchAttributeNames port was double-clicked and the Name field was selected.
Leaving the rest of the Operator set to its defaults would result in all area geometries which touch and which share a common value for the Name attribute to be merged into features with a single area geometry each. However the resulting features would only contain the geometry attribute, not any of the other attribute information from the FeaturesIn data stream. Consequently we wouldn't know what class (Name) each merged area geometry represented.
This is where the SummaryAttributen ports come in. If at runtime the port has not been explicitly set, then it defaults to internally creating a functional attribute with a field name of "Geometry" and an expression of "MERGE(<geom>)" where <geom> represents the name of the primary geometry field of the input features. Hence FeaturesOut would contain the desired geometry attribute containing merged geometries, but no other attributes. We need to add more than a single functional attribute if we want additional attribute fields populated and passed to the FeaturesOut.
To do this, right click on the Merge Attributes operator and select the Add Port option (Merge Features is an expandable operator). This adds an additional port called SummaryAttribute2. More such ports can be added if additional attributes are desired on the FeatureOut by right-clicking and selecting Add Port again.
We need two SummaryAttributen ports because, once we define a functional attribute to carry across the Name attribute we need to explicitly tell the Operator to create the merged geometries.
So the SummaryAttribute1 port was double-clicked and in the Functional Attribute dialog we defined an output field name of Geometry and an Expression as MERGE(FeaturesIn.Geometry). This functional attribute thus applies what otherwise would be the default behavior of merging.
|Generating Geometry attribute using Functional Attributes|
The SummaryAttribute2 port was double-clicked and in the Functional Attribute dialog we defined an output field name of Class and an Expression as FIRST(FeaturesIn.Name). Since we are merging multiple area polygons into single areas based on the common attribute value of the Name field it is acceptable to simply use the Name attribute value of the first area that was merged to assign to the output area Class field.
|Generating Class attribute using Functional Attributes|
Then it's a simple case of feeding FeaturesOut to the subsequent steps of the Model. In this case going straight out to a Preview or a Features Output operator to create a shapefile.
Obviously you would embed this into your larger Spatial Model rather than just running it file to file.