GihanDB
GihanDB

Reputation: 631

How to use WKT to draw polygon in qgis?

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?

Upvotes: 6

Views: 11765

Answers (3)

Fran Raga
Fran Raga

Reputation: 652

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")
# QgsProject.instance().addMapLayer(temp) #for qgis >3.0
QgsMapLayerRegistry.instance().addMapLayer(temp)
  
temp.startEditing()
geom = QgsGeometry()
geom = QgsGeometry.fromWkt(wkt)
feat = QgsFeature()
feat.setGeometry(geom)
temp.dataProvider().addFeatures([feat])
temp.commitChanges()

Upvotes: 14

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

Upvotes: 0

Nitram
Nitram

Reputation: 6716

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.

Upvotes: 2

Related Questions