It will be calculated which unit type is mostly in the town. At first it will be calculated if the city is a defensive or offensive town. Then the specific type (land, navy or fly) is calculated.
For example, if the attack power of land units is greater than the attack power of naval units, it gets a sword icon. (for land offensive)
If it is a manticore, griffin or harpy town it gets a fly offensive icon. If its a pegasus town it gets a fly defensive icon.
Outside units can't be detected by a script, so the icon is either "outside" or "empty".