6

Can I do it in Qgis, that there are two layers, a target layer and an input layer, and I ascertain with an algorithm so that the closest points on the input layer are on the target layer in what direction they are?

Measured in degrees, with designation east, west, north, south, north east, south east, northwest, southwest?

Creating a table that contains this data?

1
  • Welcome to gis.stackexchange! Please note that a good question on this site is expected to show some degree of research on your part, i.e. what you have tried and - if applicable - code so far. For more info, you can check our faq.
    – underdark
    Jun 5, 2017 at 12:23

2 Answers 2

6

Not that long ago, I did something similar using two point layers: one containing the source; the other containing destinations.

Example layers:

Example layers


What I did was:

  1. Use the Distance to nearest hub algorithm with the following parameters to create a line layer showing the nearest destination point to the source point:

    Distance to nearest hub

    You can find the tool from:

    Processing Toolbox > QGIS geoalgorithms > Vector analysis tools > Distance to nearest hub
    

  1. Create a real field (e.g. "Direction") on the resulting line layer using the Field Calculator to calculate the direction of the lines using the expression below (as described in this post):

    (atan((xat(-1)-xat(0))/(yat(-1)-yat(0)))) * 180/3.14159
    + (180 *(((yat(-1)-yat(0)) < 0) + (((xat(-1)-xat(0)) < 0 
    AND (yat(-1) - yat(0)) >0)*2)))
    

    Field calculator direction


  1. Create a string field (e.g. "Principal") in the same line layer to calculate the principal direction of the line (i.e. north, north-east etc) using the expression below:

    CASE
    WHEN "Direction" >= 348.75 OR "Direction" < 33.75 THEN 'N'
    WHEN "Direction" >= 33.75 AND "Direction" < 78.75 THEN 'NE'
    WHEN "Direction" >= 78.75 AND "Direction" < 123.75 THEN 'E'
    WHEN "Direction" >= 123.75 AND "Direction" < 168.75 THEN 'SE'
    WHEN "Direction" >= 168.75 AND "Direction" < 213.75 THEN 'S'
    WHEN "Direction" >= 213.75 AND "Direction" < 258.75 THEN 'SW'
    WHEN "Direction" >= 258.75 AND "Direction" < 303.75 THEN 'W'
    WHEN "Direction" >= 303.75 AND "Direction" < 348.75 THEN 'NW'
    END
    

    Field Calculator principal


  1. You should (hopefully) have the degree values and their respective principal directions in the Attribute Table. You could then, if you wanted, add some style and labelling to the line layer to showcase the attributes.

    I used an arrow style:

    Arrow style

    And labelled it:

    label

    To get this result:

    Result

3

Probably refFunctions plugin is useful. It will give you additional functionalities in the Field Calculator. When installation of refFunctions is done, open the attribute table of your input_layer. Create 3 new fields in the following order:

Field [pt] ......Text(String) length=40

geomnearest('target_layer', '$geometry')

Field [az] ......Decimal number (real) length=12.8

degrees(azimuth($geometry,  geom_from_wkt("pt")))

Field [dir] ......Text(String) length=10

CASE 
WHEN  0 <= "az" AND "az" <= 45 THEN 'N'
WHEN 45 <  "az" AND "az" <= 135 THEN 'E'
WHEN 135 < "az" AND "az" <= 225 THEN 'S'
ELSE 'W'
END

Then you will get something like below:

Sorry I was lazy and skipped NE, SE, SW, NW but you will get an idea.

3
  • Well that's awfully a lot easier than what I had done in the past! Nicely done :)
    – Joseph
    Jun 5, 2017 at 13:17
  • 1
    Thanks @Joseph! To be 100% honest I learned about this plugin from your answer to this question. Rendering based on layer overlap qgis?
    – Kazuhito
    Jun 5, 2017 at 13:22
  • Oh,... my CASE WHEN part for dir field is definitely wrong! Stupid me :/
    – Kazuhito
    Jun 5, 2017 at 22:31

Your Answer

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

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