QGIS Python Programming Cookbook: HOW do I create a vector layer in memory?
So for Christmas I got the QGIS Python Programming Cookbook Second Edition, in hopes of improving my abilities around QGIS as well as making me confident in using Python. Keep in mind that my programming abilities are virtually nil, but I figured if I followed the directions in the book I could at least begin to grasp the basics.
Well, the first real exercise has me landing straight on my face! In it, you are supposed to create a vector layer in memory to display a point. The following is the code provided:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' , "memory")
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPoint(20,20)
pt.setGeometry(QgsGeometry.fromPoint(point1))
pr.addFeatures([pt])
layer.updateExtents()
QgsMapLayerRegistry.instance().addMapLayers([layer])
So when I enter it one line at a time, when I get to the pt.setGeometry(QgsGeometry.fromPoint(point1)) line I get the following error:
Traceback (most recent call last):
File "C:OSGEO4~1appsPython37libcode.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
AttributeError: type object 'QgsGeometry' has no attribute 'fromPoint'
If I continue on, after inputting the last line I get the following error:
Traceback (most recent call last):
File "C:OSGEO4~1appsPython37libcode.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
NameError: name 'QgsMapLayerRegistry' is not defined
If I run it altogether through the script editor, the first error appears. I also considered that they meant fromPointXY, but I get the following error:
Traceback (most recent call last):
File "C:OSGEO4~1appsPython37libcode.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
TypeError: QgsGeometry.fromPointXY(): argument 1 has unexpected type 'QgsPoint'
So yeah, I am out of ideas here. Really since this is supposed to be a book to learn PyQGIS, I really shouldn't be in this predicament in the first place! Either there is a major error on the FIRST exercise, or I am doing something incredibly stupid. The only other thing I can think of is that I am not using QGIS 2.18 as the book was written for, but 3.0.3 and 3.4.3 (I also tried upgrading to see if that would help, it didn't). I really don't want to downgrade just to learn PyQGIS, but if you think it would work I may just do so.
PS: Here is a link to the page in question via Google Books
qgis python pyqgis qgsvectorlayer
add a comment |
So for Christmas I got the QGIS Python Programming Cookbook Second Edition, in hopes of improving my abilities around QGIS as well as making me confident in using Python. Keep in mind that my programming abilities are virtually nil, but I figured if I followed the directions in the book I could at least begin to grasp the basics.
Well, the first real exercise has me landing straight on my face! In it, you are supposed to create a vector layer in memory to display a point. The following is the code provided:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' , "memory")
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPoint(20,20)
pt.setGeometry(QgsGeometry.fromPoint(point1))
pr.addFeatures([pt])
layer.updateExtents()
QgsMapLayerRegistry.instance().addMapLayers([layer])
So when I enter it one line at a time, when I get to the pt.setGeometry(QgsGeometry.fromPoint(point1)) line I get the following error:
Traceback (most recent call last):
File "C:OSGEO4~1appsPython37libcode.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
AttributeError: type object 'QgsGeometry' has no attribute 'fromPoint'
If I continue on, after inputting the last line I get the following error:
Traceback (most recent call last):
File "C:OSGEO4~1appsPython37libcode.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
NameError: name 'QgsMapLayerRegistry' is not defined
If I run it altogether through the script editor, the first error appears. I also considered that they meant fromPointXY, but I get the following error:
Traceback (most recent call last):
File "C:OSGEO4~1appsPython37libcode.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
TypeError: QgsGeometry.fromPointXY(): argument 1 has unexpected type 'QgsPoint'
So yeah, I am out of ideas here. Really since this is supposed to be a book to learn PyQGIS, I really shouldn't be in this predicament in the first place! Either there is a major error on the FIRST exercise, or I am doing something incredibly stupid. The only other thing I can think of is that I am not using QGIS 2.18 as the book was written for, but 3.0.3 and 3.4.3 (I also tried upgrading to see if that would help, it didn't). I really don't want to downgrade just to learn PyQGIS, but if you think it would work I may just do so.
PS: Here is a link to the page in question via Google Books
qgis python pyqgis qgsvectorlayer
add a comment |
So for Christmas I got the QGIS Python Programming Cookbook Second Edition, in hopes of improving my abilities around QGIS as well as making me confident in using Python. Keep in mind that my programming abilities are virtually nil, but I figured if I followed the directions in the book I could at least begin to grasp the basics.
Well, the first real exercise has me landing straight on my face! In it, you are supposed to create a vector layer in memory to display a point. The following is the code provided:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' , "memory")
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPoint(20,20)
pt.setGeometry(QgsGeometry.fromPoint(point1))
pr.addFeatures([pt])
layer.updateExtents()
QgsMapLayerRegistry.instance().addMapLayers([layer])
So when I enter it one line at a time, when I get to the pt.setGeometry(QgsGeometry.fromPoint(point1)) line I get the following error:
Traceback (most recent call last):
File "C:OSGEO4~1appsPython37libcode.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
AttributeError: type object 'QgsGeometry' has no attribute 'fromPoint'
If I continue on, after inputting the last line I get the following error:
Traceback (most recent call last):
File "C:OSGEO4~1appsPython37libcode.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
NameError: name 'QgsMapLayerRegistry' is not defined
If I run it altogether through the script editor, the first error appears. I also considered that they meant fromPointXY, but I get the following error:
Traceback (most recent call last):
File "C:OSGEO4~1appsPython37libcode.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
TypeError: QgsGeometry.fromPointXY(): argument 1 has unexpected type 'QgsPoint'
So yeah, I am out of ideas here. Really since this is supposed to be a book to learn PyQGIS, I really shouldn't be in this predicament in the first place! Either there is a major error on the FIRST exercise, or I am doing something incredibly stupid. The only other thing I can think of is that I am not using QGIS 2.18 as the book was written for, but 3.0.3 and 3.4.3 (I also tried upgrading to see if that would help, it didn't). I really don't want to downgrade just to learn PyQGIS, but if you think it would work I may just do so.
PS: Here is a link to the page in question via Google Books
qgis python pyqgis qgsvectorlayer
So for Christmas I got the QGIS Python Programming Cookbook Second Edition, in hopes of improving my abilities around QGIS as well as making me confident in using Python. Keep in mind that my programming abilities are virtually nil, but I figured if I followed the directions in the book I could at least begin to grasp the basics.
Well, the first real exercise has me landing straight on my face! In it, you are supposed to create a vector layer in memory to display a point. The following is the code provided:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' , "memory")
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPoint(20,20)
pt.setGeometry(QgsGeometry.fromPoint(point1))
pr.addFeatures([pt])
layer.updateExtents()
QgsMapLayerRegistry.instance().addMapLayers([layer])
So when I enter it one line at a time, when I get to the pt.setGeometry(QgsGeometry.fromPoint(point1)) line I get the following error:
Traceback (most recent call last):
File "C:OSGEO4~1appsPython37libcode.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
AttributeError: type object 'QgsGeometry' has no attribute 'fromPoint'
If I continue on, after inputting the last line I get the following error:
Traceback (most recent call last):
File "C:OSGEO4~1appsPython37libcode.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
NameError: name 'QgsMapLayerRegistry' is not defined
If I run it altogether through the script editor, the first error appears. I also considered that they meant fromPointXY, but I get the following error:
Traceback (most recent call last):
File "C:OSGEO4~1appsPython37libcode.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
TypeError: QgsGeometry.fromPointXY(): argument 1 has unexpected type 'QgsPoint'
So yeah, I am out of ideas here. Really since this is supposed to be a book to learn PyQGIS, I really shouldn't be in this predicament in the first place! Either there is a major error on the FIRST exercise, or I am doing something incredibly stupid. The only other thing I can think of is that I am not using QGIS 2.18 as the book was written for, but 3.0.3 and 3.4.3 (I also tried upgrading to see if that would help, it didn't). I really don't want to downgrade just to learn PyQGIS, but if you think it would work I may just do so.
PS: Here is a link to the page in question via Google Books
qgis python pyqgis qgsvectorlayer
qgis python pyqgis qgsvectorlayer
asked 2 hours ago
Benjamin SmithBenjamin Smith
111
111
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
If you are using QGIS 3 that book is outdated in hopes of improving your abilities around PyQGIS 3. Following code has 3 necessary corrections to run as expected:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' , "memory")
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPointXY(20,20) #1 correction
pt.setGeometry(QgsGeometry.fromPointXY(point1)) #2 correction
pr.addFeatures([pt])
layer.updateExtents()
QgsProject.instance().addMapLayers([layer]) #3 correction
After running it at Python Console I got expected point layer:

add a comment |
You're using QGIS 2.x API in QGIS 3.x. You'll hit plenty of issues like this. You need to either:
- use QGIS 2.18
- wait for a third edition of the book, with updated code
- port the code to 3.x yourself (tricky!)
add a comment |
You are faced with some of many incompatibilities between the APIs between QGIS 2 and 3 versions. The first issue you have already found, for the second change the last line as follows:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' , "memory")
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPoint(20,20) # or QgsPointXY(20, 20)
pt.setGeometry(point1) # assign QgsPoint directly to QgsFeature
# or:
# pt.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(20, 20)))
pr.addFeatures([pt])
layer.updateExtents()
QgsProject.instance().addMapLayer(layer) # API change
For a detailed listing of API changes have a look at https://qgis.org/api/api_break.html
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "79"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f308586%2fqgis-python-programming-cookbook-how-do-i-create-a-vector-layer-in-memory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you are using QGIS 3 that book is outdated in hopes of improving your abilities around PyQGIS 3. Following code has 3 necessary corrections to run as expected:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' , "memory")
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPointXY(20,20) #1 correction
pt.setGeometry(QgsGeometry.fromPointXY(point1)) #2 correction
pr.addFeatures([pt])
layer.updateExtents()
QgsProject.instance().addMapLayers([layer]) #3 correction
After running it at Python Console I got expected point layer:

add a comment |
If you are using QGIS 3 that book is outdated in hopes of improving your abilities around PyQGIS 3. Following code has 3 necessary corrections to run as expected:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' , "memory")
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPointXY(20,20) #1 correction
pt.setGeometry(QgsGeometry.fromPointXY(point1)) #2 correction
pr.addFeatures([pt])
layer.updateExtents()
QgsProject.instance().addMapLayers([layer]) #3 correction
After running it at Python Console I got expected point layer:

add a comment |
If you are using QGIS 3 that book is outdated in hopes of improving your abilities around PyQGIS 3. Following code has 3 necessary corrections to run as expected:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' , "memory")
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPointXY(20,20) #1 correction
pt.setGeometry(QgsGeometry.fromPointXY(point1)) #2 correction
pr.addFeatures([pt])
layer.updateExtents()
QgsProject.instance().addMapLayers([layer]) #3 correction
After running it at Python Console I got expected point layer:

If you are using QGIS 3 that book is outdated in hopes of improving your abilities around PyQGIS 3. Following code has 3 necessary corrections to run as expected:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' , "memory")
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPointXY(20,20) #1 correction
pt.setGeometry(QgsGeometry.fromPointXY(point1)) #2 correction
pr.addFeatures([pt])
layer.updateExtents()
QgsProject.instance().addMapLayers([layer]) #3 correction
After running it at Python Console I got expected point layer:

answered 1 hour ago
xunilkxunilk
14k31742
14k31742
add a comment |
add a comment |
You're using QGIS 2.x API in QGIS 3.x. You'll hit plenty of issues like this. You need to either:
- use QGIS 2.18
- wait for a third edition of the book, with updated code
- port the code to 3.x yourself (tricky!)
add a comment |
You're using QGIS 2.x API in QGIS 3.x. You'll hit plenty of issues like this. You need to either:
- use QGIS 2.18
- wait for a third edition of the book, with updated code
- port the code to 3.x yourself (tricky!)
add a comment |
You're using QGIS 2.x API in QGIS 3.x. You'll hit plenty of issues like this. You need to either:
- use QGIS 2.18
- wait for a third edition of the book, with updated code
- port the code to 3.x yourself (tricky!)
You're using QGIS 2.x API in QGIS 3.x. You'll hit plenty of issues like this. You need to either:
- use QGIS 2.18
- wait for a third edition of the book, with updated code
- port the code to 3.x yourself (tricky!)
answered 1 hour ago
ndawsonndawson
18.3k22641
18.3k22641
add a comment |
add a comment |
You are faced with some of many incompatibilities between the APIs between QGIS 2 and 3 versions. The first issue you have already found, for the second change the last line as follows:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' , "memory")
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPoint(20,20) # or QgsPointXY(20, 20)
pt.setGeometry(point1) # assign QgsPoint directly to QgsFeature
# or:
# pt.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(20, 20)))
pr.addFeatures([pt])
layer.updateExtents()
QgsProject.instance().addMapLayer(layer) # API change
For a detailed listing of API changes have a look at https://qgis.org/api/api_break.html
add a comment |
You are faced with some of many incompatibilities between the APIs between QGIS 2 and 3 versions. The first issue you have already found, for the second change the last line as follows:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' , "memory")
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPoint(20,20) # or QgsPointXY(20, 20)
pt.setGeometry(point1) # assign QgsPoint directly to QgsFeature
# or:
# pt.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(20, 20)))
pr.addFeatures([pt])
layer.updateExtents()
QgsProject.instance().addMapLayer(layer) # API change
For a detailed listing of API changes have a look at https://qgis.org/api/api_break.html
add a comment |
You are faced with some of many incompatibilities between the APIs between QGIS 2 and 3 versions. The first issue you have already found, for the second change the last line as follows:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' , "memory")
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPoint(20,20) # or QgsPointXY(20, 20)
pt.setGeometry(point1) # assign QgsPoint directly to QgsFeature
# or:
# pt.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(20, 20)))
pr.addFeatures([pt])
layer.updateExtents()
QgsProject.instance().addMapLayer(layer) # API change
For a detailed listing of API changes have a look at https://qgis.org/api/api_break.html
You are faced with some of many incompatibilities between the APIs between QGIS 2 and 3 versions. The first issue you have already found, for the second change the last line as follows:
layer = QgsVectorLayer('Point?crs=epsg:4326', 'MyPoint' , "memory")
pr = layer.dataProvider()
pt = QgsFeature()
point1 = QgsPoint(20,20) # or QgsPointXY(20, 20)
pt.setGeometry(point1) # assign QgsPoint directly to QgsFeature
# or:
# pt.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(20, 20)))
pr.addFeatures([pt])
layer.updateExtents()
QgsProject.instance().addMapLayer(layer) # API change
For a detailed listing of API changes have a look at https://qgis.org/api/api_break.html
answered 1 hour ago
DetlevDetlev
3,352618
3,352618
add a comment |
add a comment |
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f308586%2fqgis-python-programming-cookbook-how-do-i-create-a-vector-layer-in-memory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown