I'm trying to add a new grid layer by importing it from a CVS file. In the CSV, each line represents a grid, and the coordinates of the four corners are given. Moreover, the grid contains its own information, e.g. population.

Sample enter image description here

Lat1        Long1       Lat2        Long2       Lat3        Long3       Lat4         Long4      Population_Density
14.696832   120.921856  14.696832   120.932096  14.707072   120.932096  14.707072   120.921856  158.8909091
14.676352   120.932096  14.676352   120.942336  14.686592   120.942336  14.686592   120.932096  2224.472727
14.676352   120.939264  14.676352   120.949504  14.686592   120.949504  14.686592   120.939264  577.2429508
14.686592   120.932096  14.686592   120.942336  14.696832   120.942336  14.696832   120.932096  953.3454545
14.686592   120.939264  14.686592   120.949504  14.696832   120.949504  14.696832   120.939264  329.8531148
14.696832   120.929024  14.696832   120.939264  14.707072   120.939264  14.707072   120.929024  82.46327869
14.726528   120.939264  14.726528   120.949504  14.736768   120.949504  14.736768   120.939264  16.24056872
14.45312    120.96896   14.45312    120.9792    14.46336    120.9792    14.46336    120.96896   1116.113345
14.46336    120.96896   14.46336    120.9792    14.4736 120.9792    14.4736 120.96896   634.6526863

I'm aware of how to create a grid layer using the Create Grid in Vector Creation. However, this approach seems not working for this case. So how can I add a new grid given the four corner coordinates?


A very fast solution using, using the Field Calculator is:

  1. Import the CSV file as a point layer using LAT1 and LONG1 as X and Y;

  2. Save the shapefile as temporary (to be able to add a new field in the Attribute table) (we call here, for the example, VPOINTS);

  3. Open Field Calculator and create a new Field (string, field length 250) called "polygon" using the expression

   concat("Lat1", ' ' ,"Long1", ', ', "Lat2", ' ', "Long2", ',', "Lat3", ' ', "Long3", ',', "Lat4" ,' ', "Long4" )
  1. With this new field in VPOINTS, in Processing you can use the algorithm Geometry by expression to create a new vector layer with the expression
geom_from_wkt( 'POLYGON(('|| 

Use VPOINTS as Input Layer and choose Polygon as Output Geometry Type.

The grid is ready and in his attribute table you have all the original attributes preserved.

  • Thank you for the approach! Strangely, I tried and the polygon layer was generated, but not shown in the map
    – HanRF
    Feb 21 '20 at 3:23
  • 1
    It should be added to the map straight away. Check if in the algorithm the option to add the layer is selected. Hope that you have found this way more simple and direct than the others.
    – Val P
    Feb 21 '20 at 6:33

I run some tests with rasterizing the points etc., but while your grid is regular, it's distribution is not, hence those methods didn't work (that's also why the Create Grid won't work perfectly).

Here is a modified version of my previous answer that will work regardless of the fact that the population values are not unique.

1) Once again, this is our starting point: enter image description here

2) Add two columns between each pair of coordinates, and in the first column (group) for each of the coordinate pairs put in ascending, unique values (1,2,3,...,n). Second one (order) has to contain a number - 1 for the first pair of coordinates, 2 - for the second, etc. You should now have something like this.

enter image description here

3) Calculate average latitude and longitude for each of the row, and put it in two additional column at the end. Those coordinates indicate the center of each grid cell.

enter image description here

4) Put all the data apart from population and average lat long, into first 4 columns of the file. enter image description here

5) Load the data into QGIS, do it twice. First time, use Lat1 and Long1 as coordinate columns, second time, use LatAvg and LongAvg, name both imports with different names so you don't get confused. I'll name them 'Cornets' and 'Centers'. After import, it should look like this: enter image description here

6) First, let's create a grid. Use points to path tool - order field should be your "order" column, group field should be your "group" column. enter image description here

You should get this as a result:

enter image description here

7) Now, use the Lines to Polygons tool on the line layer you just generated. The output should look like this (Note that because there is some overlap in your grid, some polygons will be in part hidden behind others): enter image description here

8) So our grid is there, we are just missing the population values. We can get them using "Join attributes by location" tool and joining out "Centers" point layer with the "Polygons" layer we just created. "Polygons" should be the input layer, "Centers" the Join layer, as below:

enter image description here

This will produce a "Joined layer". Essentially it's your grid with population density values. You can of course remove all the unnecessary columns.

enter image description here

I hope this helps, although I know it requires some work.

  • It would definitely help. I will dive deeper into your approach. Thank you for the efforts!
    – HanRF
    Feb 20 '20 at 1:33

I have a workaround for you, note that it will require some work to prepare the data and will only work if the population stats are unique.

1) So our starting point is something like this: enter image description here

2) First thing that you want to do is to add two additional columns between each pair of the coordinates. One of them has to contain the population stat (pop), second one (order) has to contain a number - 1 for the first pair of coordinates, 2 - for the second, etc. You should now have somthing like this.

enter image description here

3) Now, bring all the data into one column like this:

enter image description here

4) Import the CSV file as a delimited text layer, you should have your points that make up the grid:

enter image description here

5) Use points to path tool to create lines between your points. Your order field should be the column that we called "order" earlier, your group field should be population stat ("pop"):

enter image description here

You'll end up with something like this, one of the outlines might be missing but it's ok:

enter image description here

6) Lastly, use Lines to Polygons tool on the lines you got, and your result should be this:

enter image description here

Important note: I'm assuming here, that your coordinates go something like this:

enter image description here

If they do not, figure out which is the correct number to put in in the order column beside each pair of coordinates.

  • The order is actually clockwise, but I understand our point. However, the population figures are actually not unique. Can I ask why your prerequisite is that it needs to be unique?
    – HanRF
    Feb 19 '20 at 11:06
  • Clockwise should be fine with the same values. It needs to be unique for the points to path to work correctly. If two pop numbers have the same value it'll create lines between not 4 but 8 points, which will be destructive for the grid. If there are only a few non-unique values, you should be able to edit the grid manually in qgis. Other solution would be to add 0.000001 to one of the non-unique values. It wouldn't introduce much error I suppose. Also, you could always substract that value after creating the grid.
    – Pawel
    Feb 19 '20 at 11:16
  • I see your point. Actually there are quite a number of fields that each grid is containing, and the grid count is a bit massive. It is going to be painful to do the amendments that you proposed. Let's see whether others have any alternative approaches
    – HanRF
    Feb 19 '20 at 11:44
  • Is your grid regular (all the cells have the same height and length? I have another solution for you, but it's a bit more work if the grid is not regular. Could you actually provide a few more lines of the file, so I could work off your original data?
    – Pawel
    Feb 19 '20 at 11:59
  • Thanks for the reply! Yes, the grids are to be the same height and length, and the coordinates are in lat and long. I will update a few lines of the data in the question
    – HanRF
    Feb 19 '20 at 12:48

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.