I have created a 1km * 1km grid in QGIS using the MMQGIS plugin and below is a sample of shp@data and the layer



Is there a way to do a spatial join on the grid created?

I assume some grids should have more individual points (lat/long) and some should have no point data within them. I want to be able to identify the "grid_id" and the points present in the polygon such that I can then join the individual point data like below for e.g. in the below-aggregated data, which grid number does the lat and long belong to head(b)

Lat                Long     Num.CT   Num.SuccessCT Num.FailedCT Failure.rate
49.91222         -6.302522      1             1            0       0     
49.91251         -6.299382      1             1            0       0      
49.91440         -6.315690      1             1            0       0    
49.91443         -6.309872      1             1            0       0   
49.91501         -6.311369      1             0            1       1  
49.91502         -6.310905      1             1            0       0 

I tried joining the shapefile with the aggregated data. However, I can't get my head around how to plot and how would leaflet package in R identify the square

Spatial_join_by_location shapefile join aggregated data


Output post joining the point layer and polygon layer

enter image description here

  • Hi, and welcome to GIS SE!
    – RafDouglas
    May 29 '19 at 7:18

As I understand it, you are requesting two separate issues:

  1. how to associate each point with the ID of the tile it falls in
  2. how to count/perform stats on the points contained in each tile

Associate each point with the ID of the tile it falls in

Toolbox -> Join attribute by location

  • Input layer: choose the point layer
  • Overlay layer: choose the grid (of course it must by a polygon grid, i.e. each tile must define an area)
  • select contained as Geometric predicate (you want to associate each point with the grid tile they are contained in)

Join attribute by location

A new point layer is created, with the containing tile fields (including the IDs, in this case id_2) as added attributes:

enter image description here

You can then use id_2 to do the joins and calculations in R.

Count/perform stats on the points contained in each tile

Toolbox -> Join attributes by location (summary)

In this case you want to count how many points are contained in each tile, and have the stats calculated for you:

Join attributes by location (summary)

  • 1
    Many thanks for this , But some how i don't get an id field post joining within point layer and polygon grid layer. I have attached a snapshot of join outcome Can you help me with interpreting it correctly. I want to be able to say grid 1 - points that fall in it. What i see at the moment ( correct me if wrong ) .. Each lat /long fall in which polygon however each polygon doesnt have an id. So similar left/bottom/right/top should be labelled as GRID -1 ....
    – EricA
    May 29 '19 at 8:31
  • I see, probably the original grid does not have an ID? In the second screenshot of your question there are only 4 fields (left bottom, right, top). If an unique ID field is missing, create it, and then re-run the join.
    – RafDouglas
    May 29 '19 at 8:37
  • @I created the orignal grid as - Upload the CSV file with my lat/long information - create a grid layer on top - Create MMQGIS grid - X=0.1Y=0.1 unit = layer_units , Layer extent the same as lat /long in my file and saved it as shape file. I check now and the grid file created has only left , right, bottom ,top and no id , Can you suggest any other way to create an id label in my custom aggregated dataset
    – EricA
    May 29 '19 at 8:42
  • Does each latitude and longitude gets a unique id ? I can label them in numeric column. But when there are multiple lat/long having the same co-ordinates how should i deal with labelling them
    – EricA
    May 29 '19 at 8:43
  • 2
    make a new field (integer data type) and fill it with @row_number
    – Taras
    May 29 '19 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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