Point coordinates to polygon – part II

In my previous article, I showed how you can convert point coordinates into a polygon vector layer in QGIS. So how about GRASS GIS? Like in my previous post, let’s assume you have a text file with two columns with the coordinates. With the v.in.ascii function you can import this text file as a point layer. Next, you can use v.hull or v.delaunay to create polygon layers.

With a little bit more work, you can convert the above mentioned text file into a ASCII vector file. A vector map in GRASS native vector format is a simple text file which may contain a mix of primitives including points, lines, boundaries, centroids, areas, faces, and kernels. The text file may contain also a header with various metadata. Below I will show you the minimum input needed to create a polygon map with one polygon. For more options, you can consult the help file.

Figure 1. The v.in.ascii dialogue. Check the text for a further discussion of the marked items.
Figure 1. The v.in.ascii dialogue. Check the text for a further discussion of the marked items.

In GRASS GIS, a polygon vector consists of a boundary and a centroid. The example above defines the boundary as the line through the four points and a centroid within the boundary.

A) The header contains meta-data information and is optional. See the help file about what meta-data you can include in the header
B) This is the first line of the body and is obligatory
C) The letter indicates the type of ‘primitive’ (type of vector). The ‘B’ stands for ‘boundary’. This is followed by the number of elements (points).
D) The coordinates of the four boundary points
E) To close the boundary, you need to repeat the coordinates of the first point
F) The letter indicates the type of ‘primitive’ (type of vector). The ‘C’ stands for ‘centroid’. Here, we have one centroid in layer one.
G) The coordinates of the centroid. I used the average of the coordinates of the four boundary points.
H) This defines the category of the centroid (category 1 in layer 1).

Note that you can also create the boundary only, and use the v.centroids function to add the missing centroid later. This might be a better option if you are working with highly irregular shaped polygons and you want to be sure the centroid lies within the boundaries.

In the next tab, you need to tell GRASS that you are using a ASCII vector map as input (Input file format = standard). Last step, click ‘Run’ to create the polygon layer.

Figure 2. Second tab of the v.in.ascii dialogue
Figure 2. Second tab of the v.in.ascii dialogue

Your vector layer does not contain an attribute table yet. If you need one, you can add one using the v.db.addtable command. In the example below, I am adding a table with a column ‘Description’. I then fill this column using db.execute.

v.db.addtable map=OutputFile
v.db.addcolumn map=OutputFile layer=1 columns='Description VARCHAR'
echo "update OutputFile set Citation='Test'" | db.execute input=-

Although these steps are easy enough, I wanted to make it a bit easier. I therefore created a small spreadsheet template. It will take the coordinates of your boundary points and generate the GRASS GIS code to create a polygon vector layer and export this to a kml file.

Figure 3. Spreadsheet to create GRASS code to create kml file with bounary of a polygon based on list of coordinates of boundary points.
Figure 3. Spreadsheet to create GRASS code to create kml file with boundary of a polygon based on a list of coordinates of points. You just need to fill in the coordinates of the boundary points. In the output part, you’ll see the GRASS GIS code needed to create the polygon and export it as a kml file. Remove the empty lines first and then copy the code and paste it on the command line console from where you are running GRASS GIS. The kml file (or optionally the shapefile) will be saved in your home folder.

I just created this as a quick aid for myself and it only lets you create one polygon (that is all I needed it for). But it is easy enough to adapt it, e.g., if you want to create one vector layer with multiple polygons. Or even better, write a small script.

2 thoughts on “Point coordinates to polygon – part II

    1. pvanb

      Looks like a great tool for python programmers. I am not, although now with GRASS moving completely to Python it starts to make more and more sense to get at least some grasp of Python programming / scripting.

Leave a comment