MapInfo Pro

 View Only

MapInfo Monday: Showing Areas instead of Counts in the Legend

  • 1.  MapInfo Monday: Showing Areas instead of Counts in the Legend

    Employee
    Posted 10-09-2023 06:03

    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.
    The second legend tells you how many records or fields has each of the specific crops. The first legend goes a bit deeper and gives you the total size of the fields in hectares for each of the crop types.
    Let's see how we can show the hectares for each crop type in the legend. If you prefer a different area unit, that can of course also be done.

    In Theory

    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.
    If I select one of the fields from the resulting query in the map by clicking on it, you will see that I in fact selected several. That's because the query we ran earlier merged the polygons into one record.
    From the Map tab, I click on Add Theme.
    In the Create Thematic Map Step - Step 1 of 3 dialog, I  select Individual and the same thematic template as I used for the base table from the Template list.
    In the Create Thematic Map - Step 2 of 3 dialog, I select the aggregated output result, and from the Field list, I select Expressions...
    In the Expression dialog, I write the expression I want to appear in the legend:
    Crop + " " + FormatNumber$(Round(CartesianArea(Object, "hectare"), 0.01)) + " ha"
    As mentioned earlier, you can change the area unit if you prefer a different unit but "hectare". This is where you would do this. I have chosen to round the areas to two decimals using this expression: Round(..., 0.01). This can also be adobted to a different round.
    In the Create Thematic Map - Step 3 of 3 dialog, you can see the final thematic map and the values used for the thematic You can see how each value only appears once and that it does include the area for each crop type too.
    I chose to change the title and the font for my legend and I also removed the count from the legend as that doesn't make sense. You control these settings from the Customize Legend dialog.
    The resulting map with the embedded legend looks like this, very similar to what I showed at the beginning of the article.
    As you can see it is possible to get the legend to show other values besides the actual value. It does however require a bit of prework for this to work.
    I decided to summarize the size of the polygons. You could also have summarized values from a column instead and then shown this value in the legend.

    Known Issue

    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.
    If a new crop has been added, the legend will be missing that or one of the other crops. It will stick to the initial number of crops in the legend.
    The recommendation is therefore to not save this customized thematic map to a workspace. This is something you would create for an output when you need it. It could even get wrapped into a small application.


    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------