How to use WKT to draw polygon in qgis?
Asked Answered
R

3

6

I have a CSV file with a data field which contains data like bellow

POLYGON ((79.87749999947846 6.997500000409782, 79.88249999947845 6.997500000409782, 79.88249999947845 7.002500000409782, 79.87749999947846 7.002500000409782, 79.87749999947846 6.997500000409782))

I want to draw a polygon by using this data field in qgis. How can i do this?

Readjust answered 10/7, 2017 at 11:56 Comment(0)
B
14

For example,I have a csv with two columns "Id" and "geom" that geom have your POLYGON example,

enter image description here

Go to layer->Add Layer->Add delimited text Layer and browse your csv and the geometry field combobox select the column that have your wkt data,in my case is "geom" and Geometry definition select (WKT) option

enter image description here

The result is:

enter image description here

In another way, using Python:

uri ='file:///C://Users//fjraga//Desktop//test.csv?delimiter=%s&crs=epsg:4326&wktField=%s' % (",", "geom")
lyr = QgsVectorLayer(uri, 'Test','delimitedtext')
QgsMapLayerRegistry.instance().addMapLayer(lyr)

But if you only want load this WKT geometry using QGIS python console,try with this:

wkt = "POLYGON ((79.87749999947846 6.997500000409782, 79.88249999947845 6.997500000409782, 79.88249999947845 7.002500000409782, 79.87749999947846 7.002500000409782, 79.87749999947846 6.997500000409782))"

temp = QgsVectorLayer("Polygon?crs=epsg:4326", "result", "memory")
QgsMapLayerRegistry.instance().addMapLayer(temp)

temp.startEditing()
geom = QgsGeometry()
geom = QgsGeometry.fromWkt(wkt)
feat = QgsFeature()
feat.setGeometry(geom)
temp.dataProvider().addFeatures([feat])
temp.commitChanges()
Brettbretz answered 11/7, 2017 at 8:56 Comment(1)
Thanks - the python code worked in QGIS 3.x once I changed QgsMapLayerRegistry.instance to QgsProject.instanceIndonesian
U
2

You copy your text into the clipboard.

And then:

  1. Open QGIS
  2. Open the "Edit" Menu
  3. Enter the "Insert Objects as"/"Insert Features as" sub-menu
  4. Choose either vector or temporary layer
  5. Select the correct coordinate system

And you are done.

It is as simple as that.

Unconquerable answered 10/7, 2017 at 14:44 Comment(0)
N
0

Creating new layers from the clipboard using well-known text (WKT)

Features that are on the clipboard can be pasted into a new layer. To do this, Select some features, copy them to the clipboard, and then paste them into a new layer using Edit ‣ Paste Features as ‣ and choosing:

New Vector Layer…: the Save vector layer as… dialog appears (see Creating new layers from an existing layer for parameters)

or Temporary Scratch Layer…: you need to provide a name for the layer

A new layer, filled with selected features and their attributes is created (and added to map canvas).

Note

Creating layers from the clipboard is possible with features selected and copied within QGIS as well as features from another application, as long as their geometries are defined using well-known text (WKT).

Reference: https://docs.qgis.org/3.10/en/docs/user_manual/managing_data_source/create_layers.html#creating-new-layers-from-the-clipboard

Nicol answered 5/5, 2020 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.