I want to create a ABCD-rectangle in QGIS using the coordinates for AB, BC,CD, DA in format (x,y) each.
What is the procedure to do this? Also is there any special formatting required for the CSV?
I want to create a ABCD-rectangle in QGIS using the coordinates for AB, BC,CD, DA in format (x,y) each.
What is the procedure to do this? Also is there any special formatting required for the CSV?
To automatically generate a polygon of all the points in your CSV, without manually listing every single vertex, you can use the following QGIS expression - either with Geometry generator (for visualization only) or Geometry by expression (for actual geometries - see here for details about these two options):
if (
$id = minimum ($id),
with_variable(
'points',
array_foreach(
array_agg( $id),
make_point (
attribute (get_feature_by_id (@layer, @element), 'x'),
attribute (get_feature_by_id (@layer, @element), 'y')
)
),
make_polygon(
make_line (
array_foreach (
generate_series (0, count ($id)-1),
array_get (@points,@element)
)
)
)
),
''
)
How it works
The expression aggregates all features (line 6) to an array, for each array-element (line 5) gets the x- and y-coordinate to create a point from it (lines 7-9). This array of points is defined as variable points (lines 3-4).
Then a polygon is created (line 12), based on a linestring (line 13) that is based on connecting the points from the variable points (addressed as @points, line 16). The points are listed in the order they appear in the attribute tabel of the CSV. To get each point in order, an array_foreach iteration is used (line 14) and runs as many times as there are features in the layer (from 0 to the number of counted features -1, line 15). For Each of these numbers n, the n-th point from the variable @points (line 16) is returned and connected to a line, then the line to a polygon (lines 12/13).
Finally, this expression should run just once and not for each feature in the layer, thus I adden an if statement (lines 1-2): only run if the id of the current feature corresponds to the smallest id in the dataset.
CSV loaded with points created from coordinates in the attribute table. The polygon is dynamically created from the coordinate values as well:
In your csv-file, make the polygon as a WKT-string, that is
POLYGON((x1 y1, x2 y2 , x3 y3 , x4 y4, x1 y1))
(Note that the last point has to be repeated at the end) and set the geometry definition as "Well Known Text (WKT)".
If this just a few polygons, I would recommend the extension QuickWKT. You type in your coordinates as a wkt-string. This will give you your polygon as a memory layer that can later be saved if you need that.
The plugin will assume that your coordinates are in the same CRS as your project. If that is not the case, you can type it in as EWKT that also has the CRS-info. (Just select the right type in the drop down and you will get an example).
coordinates_ab,coordinates_bc,coordinates_cd,coordinates_daEach column has one pair yes
– asiffarhankhan Nov 25 '21 at 12:06