Skip to main content

How to Convert Linestring to Polygon

Explore methods for transforming linestring features into closed polygon geometries

Rifqi Sentosa avatar
Written by Rifqi Sentosa
Updated over 7 months ago

This section provides a comprehensive documentation to importing KML files into QGIS for further processing, specifically focusing on how to handle files that contain linestrings.

KML (Keyhole Markup Language) is commonly used for geographic data representation, and this step explains how to bring this data into QGIS, a popular open-source GIS tool.

This steps also works if you are importing raw data in other file formats such as shapefile and GeoJSON.


What is KML?

KML is an XML-based file format used to display geographic data in Earth browsers like Google Earth. KML files can represent points, lines, and polygons, making them highly versatile for different mapping and geographic tasks. Figure below shows example of KML files.

Common uses of KML file include:

  • Google Earth Visualization: KML is often used to display locations and map data on Google Earth.

  • GIS Integration: Many GIS software programs, including QGIS, support KML files, allowing for advanced geospatial analysis.

Common Problem with KML Files

A common issue when working with KML files is that they sometimes contain linestrings instead of polygons. This can cause problems if your objective is to visualize or analyze enclosed areas rather than just paths or lines.

This step will documentation you through importing such KML files into QGIS to begin the process of converting these linestrings into polygons.

Check KML Geometry

Several ways are available to check whether a KML file contains linestring, polygon, or other geometry types. One of third-party-independent ways to check is to open the KML file in its XML file format using text editor such as Notepad.

To open KML file in text editor, right-click on the KML file > Open with > Choose another app

A prompt window to select an app will be shown.

  1. Find and select Notepad from the list.

  2. Click Just Once

The Notepad window will show the specified KML file in its XML format.

  1. Type CTRL + F and type “Placemark” to find the text in the XML content.

  2. The geometry type can be indicated inside the Placemark tag. In the picture below, it is shown that the KML file contains LineString.


Import KML File(s) into QGIS

Launch QGIS from your Start menu or desktop icon. To start the import process, go to Layer > Add Layer > Add Vector Layer

A Data Source Manager window will be opened.

  1. Select File as Source Type.

  2. Click on the button to browse the KML file.

Navigate into your data folder.

  1. Select the KML file(s) to import.

  2. Click on Open button.

After selecting the files, click Add button to finish the import.

The Data Source Manager window will close to show the imported KML file(s) on the map in QGIS main window at the center, as well as in the Layers pane on bottom-left of QGIS window as KML group containing the vector layer.

If the imported KML file(s) does not shown on the map, in the Layers pane, right-click on the imported KML file group > Zoom to Group

Below is an example of the imported KML files being successfully shown on the map in the QGIS main page.

Another way to check the geometry contained in the KML file is by checking the imported KML file’s properties.

  1. Right-click on the vector file inside the KML file group.

  2. Click Information

  3. Check Geometry information on the shown page. In this documentation, the geometry of the specified KML file is shown to be LineString.


Visualizing the Linestring

This section provides a detailed documentation on how to visualize linestrings in QGIS, which is essential for understanding their path and structure. The visualization process allows you to inspect the geometry of the imported KML linestring data, ensuring that the next steps in processing (such as conversion to polygons) are accurately performed.

Linestrings represent paths or sequences of points connected by straight lines, typically used in geographic data to indicate roads, boundaries, or routes. Before converting these lines into polygons, it is important to visualize them properly in QGIS to ensure the geometry is accurate.

This step focuses on showing how to visually inspect linestrings after importing a KML file into QGIS.

Modify the Visualization

To modify the visualization of the linestring, right-click on the Double-click > Symbology > Single Symbol

To add vertices visualization with the linestring:

  1. Click + button to add new symbol layer within the Line

  2. Click Symbol layer type

  3. Change the new symbol layer type to Marker Line

To change the symbol visualization:

  1. Click on Simple Marker inside the Line symbol

  2. Click Fill color to start modifying the color

  3. Configure the color wheel based on preference

  4. Click OK to select the color on the color wheel

  5. Click OK to finish changing the symbol visualization

The picture below shows that the visualization of field_001 linestring has been modified. The vertices symbols make it easier to observe the path of the linestring.


Converting Linestring to Polygon

This step explains how to convert linestring data into polygons. We provide two approaches: using QGIS, a robust open-source GIS tool.

In geospatial data, linestrings are a series of connected points, typically used to represent linear features such as roads, rivers, or boundaries.

However, some applications require polygons, which represent enclosed areas. For example, to display field boundaries on a map, you would need polygons rather than linestrings. This section walks through converting linestrings to polygons.

To open the conversion window in QGIS, go to Vector > Geometry Tools > Lines to Polygon.

To start linestring to polygon conversion in the shown page:

  1. Click Input Layer button and select the KML file to convert

  2. Ensure Open output file after running algorithm to show the resulted data on the map

  3. Click Run to run the conversion

The picture below shows the result of conversion. The result is shown as “Polygons” in the Layers pane.

The geometry property of the resulted layer shows that it is of Polygon type.


Exporting the Polygon

This documentation provides a step-by-step documentation on how to export polygon data from QGIS after converting linestrings into polygons. The exported polygons can be saved in various formats, including KML, GeoJSON, Shapefiles, and more.

Exporting the Polygon in QGIS

Conversion in QGIS resulted in temporary layer, meaning that the result was not immediately saved as a new file. From QGIS, there are multiple file format that can be chosen to export the resulted polygon, such as GeoJSON, SHP, and KML.

To export the resulted layer into KML (or any desired file format), right-click the layer > Export > Save Feature As.

To start the export process:

  1. Click and select the desired format. In this documentation, KML is chosen.

  2. Browse for the destination directory and type in the file name. Layer name automatically follows the file name, but it can be modified separately.

  3. Leave everything else as default, then click OK

The picture below shows format options that can be chosen for export.

The picture below shows a new KML file containing the polygon after being exported from QGIS.


Conclusion

In summary, converting linestrings to polygons is a crucial process when working with geographic data that requires representing enclosed areas rather than linear paths.

Whether you’re visualizing property boundaries, city limits, or other spatial features, transforming linestrings into polygons enables accurate area analysis, mapping, and integration with tools like Google Earth.

By using tools such as QGIS, you can handle this conversion efficiently and export the results in formats suitable for a wide range of applications, including KML, GeoJSON, or Shapefiles.

These conversions open up a variety of use cases across fields like urban planning, environmental monitoring, and GIS analysis. We encourage users to experiment with different KML files and geospatial tools to enhance their workflows.

Trying out various methods, datasets, and formats will deepen your understanding of geospatial data processing and may reveal new opportunities for visualizing and analyzing geographic information.


Additional Resource

Resource Documentation

Did this answer your question?