Repository navigation
2a) Random Forest Classification
A set of features is calculated per line capturing its geometry and its spatial context. Table 1 shows all features being calculated, of which the first two are not used for the classification. Together with a boundary label, the features are used to train a Random Forest (RF) classifier. The boundary label refers to a line being a boundary (1) or not boundary (0). This label is assigned manually or derived from reference data. The trained classifier predicts boundary likelihoods for unseen testing data without a boundary label (Figure 1). The classification is based on the open-source Scikit-learn RF implementation .
Table 1: Features calculated per line used by the Random Forest (RF) classifier for boundary classification.
Figure 1: Boundary line classification based on Random Forest (RF) to derive boundary likelihoods for MCG lines.
- image segmentation lines without attributes (
.shp) - cadastral reference lines (
.shp) (optional) - RGB orthoimage raster (
.tif) - DSM orthoimage raster (
.tif) (optional)
- image segmentation lines with boundary likelihood as attribute (
.shp)
To assign a value of 0 or 1 for each line as boundary attribute in the training data.
- boundary = 1 : line is useful for boundary delineation
- boundary = 0 : lines is not useful for boundary delineation
The boundary value can be assigned either
- manually by selecting lines and setting their value accordingly or
- semi-automatically by comparing the overlap of the image segmentation lines with cadastral reference lines
- image segmentation lines without boundary label (
.shp) - cadastral reference lines (
.shp) (optional)
- image segmentation lines with boundary label (
.shp)
-
Create the boundary attribute for your input lines.
Open Attribute Table|New Field
-
Select lines to be labelled with boundary= 1 by clicking on features while pressing Ctrl
-
Open Attribute Table|Toggle editing mode| set boundary = 1 for all selected features withUpdate Selected
-
Save edits|Toggle editing mode
Semi-automatic line labelling (part 1/2)
Note: This requires cadastral reference data. An additional attribute (bound_ref) is calculated that captures the overlap between each line and the buffered cadastral reference.
-
Buffer the cadastral reference by a radius distance of e.g. 0.4m
-
Convert the cadastral reference data to a raster format. The raster should have the value 1 at locations of the cadastral reference buffer. Make sure that the resolution (pixel size) of your output raster is smaller than the buffer size you have chosen.
-
Save the raster to
\share\input
-
Set
"Ref_RasterFile"inconfig\config.jsonto the name of your raster file
-
Continue with 2. Calculate attributes and label lines thereafter
To calculate features required to train the classifier and to save them as attributes for the image segmentation lines.
- image segmentation lines without attributes (
.shp) - RGB orthoimage raster (
.tif) - DSM orthoimage raster (
.tif) (optional)
- image segmentation lines with attributes (
.shp)
-
Download all folders from GitHub
-
Save your input data to
\share\input
-
Set the following parameters in
config\config.jsonaccording to your input data file names :-
"SegmentShapeFile": name of your image segmentation lines without attributes (.shp) -
"RGB_RasterFile": name of your RGB orthoimage raster (.tif) -
"DSM_RasterFile": name of your DSM orthoimage raster (.tif) (optional)
-
-
Open Command Prompt
- Select the Windows start button > type
cmd
- Select the Windows start button > type
-
Change the command promt's working directory to
\scripts- copy the path to the
\scriptsfolder -
cd path_to_folder\scripts
- copy the path to the
-
Start
main.pywith Python-
python main.py
-
-
Start
testmodus-
--test
-
-
Calculate attributes
-
-a
-
If you now open the input shapefile (.shp) you saved to \share\input in a GIS and check out the attribute table. Each line should have new attributes.

Attribute description:
- ID - dsm_grad: see Table 1
- cad_sum: sum of cadastral reference pixels being within buffer around line
- cad_count: sum of pixels being within buffer around line
-
bound_ref: value between 0 and 1 showing overlap between line's buffer and cadastral reference buffer (cad_sum divided by bound_ref):
- bound_ref = 0 : no overlap between line and cadastral reference buffer
- bound_ref = 1 : 100% overlap between line and cadastral reference buffer
Semi-automatic line labelling (part 2/2):
-
Set the boundary value for all lines having bound_ref > 0.5 to 1
Open Attribute Table|Toggle editing mode|Select features using an expression|"bound_ref">0.5|Select Features
-
Set boundary = 1 for all selected features with
Update Selected
-
Invert selectionand Set boundary = 0 for all selected features withUpdate Selected
-
Save edits|Toggle editing mode
To create a classifier model based on training data to be applied later on testing data.
- image segmentation lines with attributes and boundary label (
.shp)
- trained classifier model (
.pkl)
-
Set
"TrainingLayer"inconfig\config.jsonto the name of your training lines
-
Create classifier
-
-c
-
The classifier model classifierModel.pkl is now saved in \share\output
To apply trained classifier on testing data in order to predict boundary likelihoods.
- image segmentation lines with attributes and without boundary label (
.shp)
- image segmentation lines with attributes and with predicted boundary label (
.shp)
-
Copy
classifierModel.pklfrom\share\outputto\share\input -
Set
"ValidationLayer"inconfig\config.jsonto the name of your testing lines
-
Predict boundary likelihoods
-
-p
-
The boundary attribute in your training data is now updated with a boundary value between 0 and 1.

- A log file containing information about the data processing is saved as
processing.txtin\share\temp - If not further specified, the term lines refers to the input segmentation lines
- The created classifier can be reused and applied to more testing data. In that case, steps 1 and 3 are not needed.
- The GIS used for this wiki is the open source QGIS.
