1

I have

  • a grid (10 x 10 km) where each cell is identified by a code
  • a shapefile, a point layer, with the year when each point was sampled

as shown in the figure below.

example1

As you can see from the image below, the year is identified by a color and in each cell I have many points of different years.

example2

What I want to obtain is an intersection between the two layers, specifically, I want the same grid with a new column showing the year(s) of the points within each cell. I show you below an example

Cell_ID       Year_of_sampling
10kmE275N271  2014-2015
10kmE276N271  2015
10kmE277N271  2013-2016
10kmE278N271  2013-2015-2016

It seems an easy task, but with Vector -> Geoprocessing -> Intersection is not possible. Any ideas?

Taras
  • 32,823
  • 4
  • 66
  • 137
Franza
  • 173
  • 1
  • 7

1 Answers1

1

You can use the Field Calculator and the array variable to create a new field in the grid layer attribute table.

This is the expression that you should use

array_to_string(
array_sort(aggregate(
 layer:= 'point_layer',
 aggregate:='array_agg',
 expression:=field_name,
 filter:=contains(geometry(@parent), $geometry))),
 delimiter:='-'
 )

changing your point_layer name and your field_name according with your data.

in the image below an example of the result using a layer called point and a field called id.

enter image description here

NB The array_sort variable is not available in all the QGIS versions. To use it you should install before the plugin ArrayPlus.

Val P
  • 3,858
  • 1
  • 8
  • 33
  • Looking at your results it is what I wanted, unfortunately I hadn't the array_sort, and also I was not able to install the plugin (I use QGIS 2.18 for Mac) – Franza Apr 04 '20 at 16:30
  • Highly suggested updating it to the most recent LTD, i.e. QGIS 3.10 – Taras Apr 04 '20 at 16:35
  • I agree with Taras suggestion. Anyway, array_sort order your attribute. You can always apply the expression removing the array_sort and sort the result in another way. – Val P Apr 04 '20 at 16:42
  • Shame on me should be LTR not LTD – Taras Apr 04 '20 at 16:44