18.104.22.168. Using the field calculator and adding geometry columns
As the communes in this example have different areal extents, one could also ask for the commune area in relation to the number of honey bee colonies. With GIS software, this kind of problem is easy to solve. Also the extraction of statistical information about geometries like point coordinates, line length, perimeter or area from the features of a vector layer can be efficiently processed.
Let’s demonstrate this with the polygon layer already containing the attribute “bee_colony” created in the previous section:
- Open the Attribute table.
- Again toggle on the Edit mode.
- Open the Field calculator.
- Activate the checkbox to create a New field.
- Set the output field name to something like “area_bee” and the other parameters as follows: field type (decimal number), width (10) and precision (3).
- Search now for “area” within the Function List > geometry and select the function with the name $area by a double click.
- Click the appropriate button for a division operation and select the field containing the number of honey bee colonies.
- Confirm with OK, if the
resulting Expression corresponds to
“$area / bee_colony” (Fig. 89, “bee_colony” represents the column name,
selectable by Function List > Fields
The values in the field “bee_area” (to the right within the Attribute table, ratio area per bee colonies) could be a first indicator about the occurrence of honey bees (of course with strong limitations, as the polygon layer only represents administrative boundaries): The minimum value belongs to the community of “Granges-Paccot” with the “BFSNR” 2198.
We learned in this section to calculate the area of a polygon layer and write these values into an attribute field. Another possibility to add standard geometry information (e.g. point coordinates, line length, perimeter or area) is the function Export/Add geometry columns. This function is of particular use in combination with different coordinate reference systems (CRS), which can easily be demonstrated with the KML file created in section 22.214.171.124. (e.g. bees_colony_locations_WGS84.kml):
- Go to Vector.
- Select Geometry Tools (Fig. 90).
- Choose Export/Add geometry columns.
- Select bees_colony_locations_WGS84 as Input vector layer.
- Choose to calculate the geometry columns using the Layer CRS (e.g. WGS84).
- Specify the path and filename for the destination file that will contain the geometry columns (it is the best to convert it back to a shapefile again).
- Click OK and wait until the progress bar is at 100%.
- Confirm to import this new shapefile by clicking Yes on the next dialog and close the Export/Add geometry columns window.
- Open the Attribute table of
the new shapefile.
Like its origin (the KML file), the shapefile is referenced in WGS84
but because of the “on-the-fly” CRS transformation, the points representing the
honey bee colonies are still congruent with those referenced in the projected
coordinate system (which is defined as CH1903 / LV03). Within the Attribute table two new fields have been
created that show the latitude and longitude in degrees (according to WGS84).
Fig. 89. Calculate with attributes and geometrical statistics in the Field calculator.
Fig. 90. Add geometry columns to an existing or a new shapefile.