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?
3 Answers 3
A very fast solution using, using the Field Calculator is:
Import the CSV file as a point layer using LAT1 and LONG1 as X and Y;
Save the shapefile as temporary (to be able to add a new field in the Attribute table) (we call here, for the example, VPOINTS);
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" )
- 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(('||
"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 mapHanRF– HanRF2020年02月21日 03:23:44 +00:00Commented Feb 21, 2020 at 3:23
-
1It 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– Val P2020年02月21日 06:33:45 +00:00Commented Feb 21, 2020 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.
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.
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:
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:
This will produce a "Joined layer". Essentially it's your grid with population density values. You can of course remove all the unnecessary columns.
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– HanRF2020年02月20日 01:33:42 +00:00Commented Feb 20, 2020 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.
3) Now, bring all the data into one column like this:
4) Import the CSV file as a delimited text layer, you should have your points that make up the grid:
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"):
You'll end up with something like this, one of the outlines might be missing but it's ok:
6) Lastly, use Lines to Polygons tool on the lines you got, and your result should be this:
Important note: I'm assuming here, that your coordinates go something like this:
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– HanRF2020年02月19日 11:06:38 +00:00Commented Feb 19, 2020 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– Pawel2020年02月19日 11:16:36 +00:00Commented Feb 19, 2020 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 approachesHanRF– HanRF2020年02月19日 11:44:09 +00:00Commented Feb 19, 2020 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– Pawel2020年02月19日 11:59:19 +00:00Commented Feb 19, 2020 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 questionHanRF– HanRF2020年02月19日 12:48:31 +00:00Commented Feb 19, 2020 at 12:48