2

I have used an R script to calculate the distance from a point within a polygon to the edge of the polygon at 1-degree increments. This has been saved as a CSV file. I am now wanting to create a shapefile of vector data to represent this information in QGIS. Is there a way to draw vectors of a given length, angle (0-360) and starting point using a function or script rather than drawing each line manually? For context this is the distance of a buoy in a lake to the lake edges.

enter image description here

The first 5 lines of the CSV are below:

line.distance thetas
4669.310681 1
4644.783120 2
4630.030523 3
4614.497676 4
4621.2902638 5

@Spacedman your answer was what I needed. Could you help me with trying to correctly centre the line? when using the script I end up with the below image. The difficulty I'm having is correctly identifying the coordinates between lat and long for the R script and my QGIS project projection NZTM 2000 EPSG:2193.

enter image description here

1

1 Answer 1

2

Loads of ways of doing this. Easiest may be to compute the X and Y coords of the start and finish points in R and create a GeoPackage and load that into QGIS instead - use the sf package in R to do this. You can compute the X and Y coords given start coordinate, distance, and angle using trigonometry.

Or to do in QGIS from the CSV, create a virtual layer which creates a line geometry using trigonometry.

I loaded a test CSV into a layer using "Add Layer" "Delimited Text Layer" and got a layer with no geometry called d. This has two columns, line.distance and thetas. Then "Create Layer" "New Virtual Layer" and use some SQL like this:

select makeline(
 st_point(1234, 5678),
 st_point(
  1234+"line.distance"*sin(thetas*3.14/180),
  5678+"line.distance"*cos(thetas*3.14/180)
  )
) as geometry from d

Note the central point is coordinate (1234, 5678) in whatever coordinate system you are using, and the numbers appear twice here. The conversion from degrees to radians uses pi=3.14 as an approximation, maybe put in another couple of decimal places for pi. The resulting virtual layer should have line geometry and for my test data looks like this:

enter image description here

since it was created with increasing line.distance with thetas from north (thetas=0) going clockwise round.

You may also need to set a CRS on the layer to get it in the right place, my test data is in the ocean only a few km from (0,0) ("Null Island") in Web Mercator coordinates.

1
  • Hi Spacedman, thank you very much this worked well in QGIS. The only issue I am currently having is that I can't get the correct projection and centre point coordinate from my R script (long, lat) in this code (the 1234, 5678 is off centre to my project and vector shapefile for the lake outline). When I run it the line geometry is slightly of the centre of where the distances were created. Do you have any suggestions for how to get the correct CRS/projection? Sep 4, 2023 at 1:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.