Reputation: 631
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
Reputation: 652
For example,I have a csv with two columns "Id" and "geom" that geom have your POLYGON example,
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
The result is:
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
Reputation: 1
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).
Upvotes: 0
Reputation: 6716
You copy your text into the clipboard.
And then:
And you are done.
It is as simple as that.
Upvotes: 2