16

I have a polygon, imported from a shapefile in QGIS. I would like transform it to a linestring instead so I can save it as a GPX file later.

How can I transform a polygon to a linestring in QGIS?

Taras
  • 32,823
  • 4
  • 66
  • 137
Jonas
  • 2,251
  • 6
  • 28
  • 37
  • 3
    simply cutting a polygonal object won't transform it to line. you would need to do a poly to line conversion. I can tell you how to do that in arcmap. I'm sure it is just as easy in qgis. – Brad Nesom Oct 17 '11 at 14:45

4 Answers4

25

Assuming you're using QGIS >= 1.6.0. There is a geoalgorithm called "Polygons to lines" accessible via Vector > Geometry Tools > Polygons to Lines (otherwise search in the Processing Toolbox Ctrl+Alt+T), which will create a new shapefile with all the attributes of the original.

Vector_Menu

For PyQGIS it is "native:polygonstolines"

>>> processing.algorithmHelp("native:polygonstolines")
Polygons to lines (native:polygonstolines)

Converts polygons to lines.

Converts polygons to lines


Input parameters

INPUT: Input layer

Parameter type: QgsProcessingParameterFeatureSource

Accepted data types:
    - str: layer ID
    - str: layer name
    - str: layer source
    - QgsProcessingFeatureSourceDefinition
    - QgsProperty
    - QgsVectorLayer

OUTPUT: Lines

Parameter type: QgsProcessingParameterFeatureSink

Accepted data types:
    - str: destination vector file, e.g. 'd:/test.shp'
    - str: 'memory:' to store result in temporary memory layer
    - str: using vector provider ID prefix and destination URI, e.g. 'postgres:…' to store result in PostGIS table
    - QgsProcessingOutputLayerDefinition
    - QgsProperty


Outputs

OUTPUT: <QgsProcessingOutputVectorLayer> Lines

Taras
  • 32,823
  • 4
  • 66
  • 137
MerseyViking
  • 14,543
  • 1
  • 41
  • 75
4

Another option is the SAGA's "Convert Polygons to Lines" module (search in the Processing Toolbox Ctrl+Alt+T).

Input:
input

Working window:
window

Output:
output

For PyQGIS it is "saga:convertpolygonstolines"

>>> processing.algorithmHelp("saga:convertpolygonstolines")
Convert polygons to lines (saga:convertpolygonstolines)

Input parameters

POLYGONS: Polygons

Parameter type: QgsProcessingParameterFeatureSource

Accepted data types:
    - str: layer ID
    - str: layer name
    - str: layer source
    - QgsProcessingFeatureSourceDefinition
    - QgsProperty
    - QgsVectorLayer

LINES: Lines

Parameter type: QgsProcessingParameterVectorDestination

Accepted data types:
    - str
    - QgsProperty
    - QgsProcessingOutputLayerDefinition


Outputs

LINES: <QgsProcessingOutputVectorLayer> Lines

Taras
  • 32,823
  • 4
  • 66
  • 137
2

Another option is the GRASS's v.to.lines (search in the Processing Toolbox Ctrl+Alt+T).

Input:
input

Working window:
window

Output:
output

For PyQGIS it is "grass7:v.to.lines"

>>> processing.algorithmHelp("grass7:v.to.lines")
v.to.lines (grass7:v.to.lines)

Converts vector polygons or points to lines.


Input parameters

input: Name of input vector map

Parameter type: QgsProcessingParameterFeatureSource

Accepted data types:
    - str: layer ID
    - str: layer name
    - str: layer source
    - QgsProcessingFeatureSourceDefinition
    - QgsProperty
    - QgsVectorLayer

method: Method used for point interpolation

Parameter type: QgsProcessingParameterEnum

Available values:
    - 0: delaunay

Accepted data types:
    - int
    - str: as string representation of int, e.g. '1'
    - QgsProperty

output: Lines

Parameter type: QgsProcessingParameterVectorDestination

Accepted data types:
    - str
    - QgsProperty
    - QgsProcessingOutputLayerDefinition

GRASS_REGION_PARAMETER: GRASS GIS 7 region extent

Parameter type: QgsProcessingParameterExtent

Accepted data types:
    - str: as comma delimited list of x min, x max, y min, y max. E.g. '4,10,101,105'
    - str: layer ID. Extent of layer is used.
    - str: layer name. Extent of layer is used.
    - str: layer source. Extent of layer is used.
    - QgsMapLayer: Extent of layer is used
    - QgsProcessingFeatureSourceDefinition: Extent of source is used
    - QgsProperty
    - QgsRectangle
    - QgsReferencedRectangle
    - QgsGeometry: bounding box of geometry is used

GRASS_SNAP_TOLERANCE_PARAMETER: v.in.ogr snap tolerance (-1 = no snap)

Parameter type: QgsProcessingParameterNumber

Accepted data types:
    - int
    - float
    - QgsProperty

GRASS_MIN_AREA_PARAMETER: v.in.ogr min area

Parameter type: QgsProcessingParameterNumber

Accepted data types:
    - int
    - float
    - QgsProperty

GRASS_OUTPUT_TYPE_PARAMETER: v.out.ogr output type

Parameter type: QgsProcessingParameterEnum

Available values:
    - 0: auto
    - 1: point
    - 2: line
    - 3: area

Accepted data types:
    - int
    - str: as string representation of int, e.g. '1'
    - QgsProperty

GRASS_VECTOR_DSCO: v.out.ogr output data source options (dsco)

Parameter type: QgsProcessingParameterString

Accepted data types:
    - str
    - QgsProperty

GRASS_VECTOR_LCO: v.out.ogr output layer options (lco)

Parameter type: QgsProcessingParameterString

Accepted data types:
    - str
    - QgsProperty

GRASS_VECTOR_EXPORT_NOCAT: Also export features without category (not labeled). Otherwise only features with category are exported

Parameter type: QgsProcessingParameterBoolean

Accepted data types:
    - bool
    - int
    - str
    - QgsProperty


Outputs

output: <QgsProcessingOutputVectorLayer> Lines

Taras
  • 32,823
  • 4
  • 66
  • 137
1

Another option is using the boundary($geometry) expression in the "Geometry by expression" tool (search in the Processing Toolbox Ctrl+Alt+T).

Input:
input

Working window:
window

Output:
output

For PyQGIS it is "native:geometrybyexpression"

>>> processing.algorithmHelp("native:geometrybyexpression")
Geometry by expression (native:geometrybyexpression)

This algorithm updates existing geometries (or creates new geometries) for input features by use of a QGIS expression. This allows complex geometry modifications which can utilize all the flexibility of the QGIS expression engine to manipulate and create geometries for output features.

For help with QGIS expression functions, see the inbuilt help for specific functions which is available in the expression builder.


Input parameters

INPUT: Input layer

Parameter type: QgsProcessingParameterFeatureSource

Accepted data types:
    - str: layer ID
    - str: layer name
    - str: layer source
    - QgsProcessingFeatureSourceDefinition
    - QgsProperty
    - QgsVectorLayer

OUTPUT_GEOMETRY: Output geometry type

Parameter type: QgsProcessingParameterEnum

Available values:
    - 0: Polygon
    - 1: Line
    - 2: Point

Accepted data types:
    - int
    - str: as string representation of int, e.g. '1'
    - QgsProperty

WITH_Z: Output geometry has z dimension

Parameter type: QgsProcessingParameterBoolean

Accepted data types:
    - bool
    - int
    - str
    - QgsProperty

WITH_M: Output geometry has m values

Parameter type: QgsProcessingParameterBoolean

Accepted data types:
    - bool
    - int
    - str
    - QgsProperty

EXPRESSION: Geometry expression

Parameter type: QgsProcessingParameterExpression

Accepted data types:
    - str
    - QgsProperty

OUTPUT: Modified geometry

Parameter type: QgsProcessingParameterFeatureSink

Accepted data types:
    - str: destination vector file, e.g. 'd:/test.shp'
    - str: 'memory:' to store result in temporary memory layer
    - str: using vector provider ID prefix and destination URI, e.g. 'postgres:…' to store result in PostGIS table
    - QgsProcessingOutputLayerDefinition
    - QgsProperty


Outputs

OUTPUT: <QgsProcessingOutputVectorLayer> Modified geometry

Taras
  • 32,823
  • 4
  • 66
  • 137