# Uncertainty of a line between two coordinates with uncertainties in QGIS

I am trying to plot the uncertainty of a line between two coordinates with uncertainties. I have created polygons around 2 coordinates to represent the uncertainty region around each point. I then create random points in each polygon, and then join them using the points to paths function. Looks something like this: I want to plot a region that represents the probability of the lines, and also takes the uncertainty region of the original endpoints into account - should look like this; The uncertainty region needs to be at the 95% confidence level. I am new to QGIS, I thought maybe by creating a grid and populating cells within the grid I could somehow come up with a polygon that represents a probability density area? I am using QGIS Desktop 3.0.1 with GRASS 7.4.0. I believe I will need large numbers of random points/lines to achieve what I want which may make processing any algorithms slow?

That was a fun problem! I solved it using QGIS 2.18 but I don't think the tools have drastically changed in 3.0.

## 1. Generate the lines

I won't give much details here since you already have the lines you need. I have written a script to generate these lines, but it works only up to QGIS 2.18. Needless to say, the more lines the better your estimation.

## 2. Create a grid

Go to Vector > Research Tools > Vector Grid (QGIS 2.18). In QGIS 3.0, it seems that you have to open the processing toolbox (Processing > Toolbox) then look for the grid tool.

For the grid extent, choose "Use layer/canvas extent" (3 dots on the right). Choose whatever cell size you want, but be prepared to face long computation times if your grid is too thin. Choose "Output grid as polygons" in "grid type".

## 3. Count lines by grid cell

This is a little tricky. You first have to cut your lines so that each line is split across the cells. To achieve that, use the intersection tool (Vector > Geoprocessing Tools > Intersection in both QGIS 2.18 and 3.0). Choose the line layer as "Input layer", and the grid as "Intersection layer".

To prepare the next step, we have to add a field to the layer we just obtained. Select this layer and open the field calculator; check the "create a new field option", set 'newid' as name, keep 'numeric' as type. In the "Expression" box, write `\$rownum`, then click OK. You will get a new field with a unique id for each line segment.

Then, we'll use the mean coordinates tool (Vector > Analysis tools > Mean coordinates in QGIS 2.18, again in the toolbox for QGIS 3.0). Set the intersected layer (from the previous operation) in the "input layer" field, and 'newid' as "Unique ID field". This gives you a point layer, each point corresponding to a line segment.

Finally, use the "Count points in polygon tool" (in Vector > Analysis tools for both QGIS 2.18 and 3.0). Set the grid in the "Polygons" field and the newly created point layer in the "Points" field. This will create a new field in the grid with the number of line segments in each grid cell. You can divide this number by the total number of lines in the field calculator if you want, if you need an estimation of line probability.

## 4. Example results

Obtained with 2000 lines and a uniform point repartition inside the uncertainty regions (you might want to try a gaussian repartition too). Same uncertainty parameters, with 5000 lines and a thinner grid (took several hours to run): • How did you get the 'heat map effect' of the colours, i.e. colours showing the density of the intersected line/mean coordinate location. Is that from the 'uniform point repartition'? I don't quite understand how you got to the final output, or how to do the 'uniform point repartition. – Ben Blackburn May 23 '18 at 8:48
• @BenBlackburn The "heatmap effect" is only a display styling of the number of line segments in each grid cell (see section 3 of my answer). What I am referring as "uniform point repartition" is simply the repartition of endpoints inside the uncertainty regions. I can add the script to generate them in my answer if you want. – ArMoraer May 23 '18 at 11:31

@ArMoraer I have been off this topic for a while, finally got to trial it. I am having issues with my lines and mean coordinates from the intersected lines.

My lines have been created from random points then points to paths, I believe the issue is that all the lines created this way are treated as 1 object? For example, when I do the intersections and enquire on one all intersected lines within a grid cell are highlighted as below. I tried exploding the intersection layer, but unfortunately this explodes the lines to any intersection vertex not to the ends of lines at grid intersection as I intend. This has a similar result with paths layer prior to intersection with the grid.

I then thought the exploded lines would still be valid statistically for what I want to achieve, however when I try to obtain mean coordinates with the rownum as the unique field, I only get 1 mean coordinate for each grid cell, not each line segment. See below. (red line show 1 exploded line) Am I right in assuming that each line has to be unique in some way for this to work, and that is tied into my method of creating random points then points to paths? Do you have any suggestions to work around this issue?

• @Armoraer any suggestions to above - not sure if you were automatically notified? – Ben Blackburn Sep 10 '18 at 10:46