Happy #MapInfoMonday!
In the article today we will investigate a way to show the total area for each value in a thematic legend as you can see below in the embedded legend.
As you know, MapInfo Pro can give you the count of records with a given value in the legend. You can see an example of this below where the legend again has been embedded in the map.
When creating an Individual Theme, MapInfo Pro needs to create a list of all the possible values in the selected column or using the specified expression. The obvious way to do that is through a Select
statement that groups the table using the column or expression. Using my Field
table and Crop
column, the Select statement could look like this:
Select Crop
From Fields
Group By Crop
The count that you currently can see in the legend is using one of the most basic aggregate functions available in MapInfo: Count(*)
. This will give you the number of records.
Select Crop, Count(*)
From Fields
Group By Crop
That is however the only aggregate, MapInfo Pro supports for legends. We want to use another aggregate: Sum()
which will summarize values, in our case the areas of the polygons.
This means that we need to preprocess the table before creating the thematic map. We want to end up with one record for each Crop Type so that we can include the size of the polygon(s) for that one record in the expression for the thematic map too.
We need to run a query on our table which will group the result by the expression for our thematic map. As we need to maintain the spatial objects too, we will need to use a Spatial Aggregate for the spatial objects. This capability was added to MapInfo Pro in version 2019 where we did a lot of work on improving SQL. We will use AggregateCombine()
which will combine the spatial objects for all the records with the same value.
If we want to keep the count of records, we also need to include this in the query. In our example, we won't need the count so I'll leave that out for now.
The basic query will look like this:
Select Crop, AggregateCombine(Obj)
From Fields
Group By Crop
We may want to adjust this slightly when we move into the practical world.
Now that's the theory. Let's try to implement this.
Implementation
When implementing this, there are two stages: 1) creating the grouped table and 2) creating the thematic map.
I'll use the SQL Window to create the grouped table. The final implementation of the Select statement looks like this:
Select f.Crop, AggregateCombine(f.Obj)
From Fields As "f"
Group By f.Crop
Into _Crops_Aggreg NoSelect
Do note that I only bring over the value from the Crop
column and the spatial object. It wouldn't make sense to include other columns as they would have different values in the original table and so the value brought over would be random.
You can specify a different name for the resulting query. Just make sure it meets the conditions for a table name in MapInfo Pro. It should start with a letter and not hold any special characters (besides the underscore, "_") or spaces.
When I run this query, the resulting table _Crops_Aggreg
will get added to my map window allowing me to create a thematic map for this layer. I have turned off the thematic map for the original Field table and set an override style for the aggregated table. This style will be hidden by the thematic map we are about to add.
This works fine for a here-and-now analysis. It is however more complicated if you try to save this to a workspace. The MapInfo Pro will capture the actual values used for the thematic map and write these to the workspace. The thematic map is stored in an expression like this:
shade 1 with Crop + ", " + FormatNumber$(Round(CartesianArea(Object, "hectare"), 0.01)) + " ha"
Ignore ""
Values
"Barley, 14,48 ha" Brush (2,16752800,16777215),
"Corn, 33,34 ha" Brush (2,14680064,16777215),
"Potatoes, 33,62 ha" Brush (2,7381247,16777215),
"Rye, 16,17 ha" Brush (2,19424,16777215)
This will work fine the next time you open the workspace - unless your data has changed. If the crop type has changed on a single field or if the size of a single field has changed, there will be one or more fields that no longer match the static text shown under the values. You will typically see these as a black polygon on the map.
If it's just that your fields have changed in size or that you have changed the crop to an already used crop, you can use the Recalculate Thema option on the Theme tab to refresh it.
------------------------------
Peter Horsbøll Møller
Principal Presales Consultant | Distinguished Engineer
Precisely | Trust in Data
------------------------------