Python Scripting in QGIS to add features and select them? Planned maintenance scheduled April 23, 2019 at 23:30UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?PyQGIS, features geometry type changes to unknown after copying to another layerqgis processing not add layerDeleting all Features of a Vector Layer in pyQGISSelect features and zoom to selection on standalone QGis applicationHow to get the BBOX of selected features in QGIS using the UI?Run QGIS layer Action on all selected featuresFeature Selection Using Custom Python Expression Functions in QGIS 2.18No “committedFeaturesAdded” signal after dataProvider().addFeatures() - QGiS 2.14.8Run QGIS Model on selected features only?Using file path in QgsVectorLayer?
C's equality operator on converted pointers
How to report t statistic from R
Semigroups with no morphisms between them
Would it be easier to apply for a UK visa if there is a host family to sponsor for you in going there?
How often does castling occur in grandmaster games?
An adverb for when you're not exaggerating
Why does it sometimes sound good to play a grace note as a lead in to a note in a melody?
Why are my pictures showing a dark band on one edge?
Does the Mueller report show a conspiracy between Russia and the Trump Campaign?
Antipodal Land Area Calculation
Intuitive explanation of the rank-nullity theorem
Lagrange four-squares theorem --- deterministic complexity
How many morphisms from 1 to 1+1 can there be?
What initially awakened the Balrog?
How does the math work when buying airline miles?
A term for a woman complaining about things/begging in a cute/childish way
Karn the great creator - 'card from outside the game' in sealed
Crossing US/Canada Border for less than 24 hours
Flash light on something
What does this say in Elvish?
How can I prevent/balance waiting and turtling as a response to cooldown mechanics
What would you call this weird metallic apparatus that allows you to lift people?
Drawing spherical mirrors
Why are vacuum tubes still used in amateur radios?
Python Scripting in QGIS to add features and select them?
Planned maintenance scheduled April 23, 2019 at 23:30UTC (7:30pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?PyQGIS, features geometry type changes to unknown after copying to another layerqgis processing not add layerDeleting all Features of a Vector Layer in pyQGISSelect features and zoom to selection on standalone QGis applicationHow to get the BBOX of selected features in QGIS using the UI?Run QGIS layer Action on all selected featuresFeature Selection Using Custom Python Expression Functions in QGIS 2.18No “committedFeaturesAdded” signal after dataProvider().addFeatures() - QGiS 2.14.8Run QGIS Model on selected features only?Using file path in QgsVectorLayer?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm trying to migrate a python plugin I wrote from QGIS 2.18 to QGIS 3.4. In the plugin, I'm creating new vector features and eventually adding them to an existing vector layer. I'm trying to have the added features selected automatically in QGIS after running the code described below.
For QGIS 2.18, the QgsVectorLayer
class had an addFeatures
method that contained a very convenient makeSelected
flag. When the flag was set to true, the added features were selected.
In QGIS 3.4 the addFeatures
method still exists, but the flag is gone.
I also tried to select the features using some of the class's selection methods but with no success. It seems that after adding the features to the layer, their id's change and I don't have access to these specific features and can not select them using their old ids. The only way that somehow works is assigning some sentinel value to one of the fields in all the new features and then selecting them using a query. I'd like to avoid this way because I'm afraid that at some point it can mess up a user's data.
here's an example of how I created features and added them to the current selected layer in Qgis. it can be run in the Qgis Python terminal:
layer = iface.activeLayer()
layer.startEditing()
feat = QgsFeature(layer.fields())
point_list = [QgsPointXY(0, 0), QgsPointXY(0,1), QgsPointXY(1,1), QgsPointXY(1,0), QgsPointXY(0,0)]
feat.setGeometry(QgsGeometry.fromPolygonXY(point_list))
layer.addFeature(feat)
# I'd like to add feat to the layers selection at this point
self.layer.endEditCommand()
I'm using QGIS 3.4.1 (migrating from QGIS 2.18.26), and the pyqgis API of the above versions.
I'm aware that Qgis3 uses python 3 instead of python 2.
pyqgis vector-layer pyqgis-3 qgsvectorlayer
add a comment |
I'm trying to migrate a python plugin I wrote from QGIS 2.18 to QGIS 3.4. In the plugin, I'm creating new vector features and eventually adding them to an existing vector layer. I'm trying to have the added features selected automatically in QGIS after running the code described below.
For QGIS 2.18, the QgsVectorLayer
class had an addFeatures
method that contained a very convenient makeSelected
flag. When the flag was set to true, the added features were selected.
In QGIS 3.4 the addFeatures
method still exists, but the flag is gone.
I also tried to select the features using some of the class's selection methods but with no success. It seems that after adding the features to the layer, their id's change and I don't have access to these specific features and can not select them using their old ids. The only way that somehow works is assigning some sentinel value to one of the fields in all the new features and then selecting them using a query. I'd like to avoid this way because I'm afraid that at some point it can mess up a user's data.
here's an example of how I created features and added them to the current selected layer in Qgis. it can be run in the Qgis Python terminal:
layer = iface.activeLayer()
layer.startEditing()
feat = QgsFeature(layer.fields())
point_list = [QgsPointXY(0, 0), QgsPointXY(0,1), QgsPointXY(1,1), QgsPointXY(1,0), QgsPointXY(0,0)]
feat.setGeometry(QgsGeometry.fromPolygonXY(point_list))
layer.addFeature(feat)
# I'd like to add feat to the layers selection at this point
self.layer.endEditCommand()
I'm using QGIS 3.4.1 (migrating from QGIS 2.18.26), and the pyqgis API of the above versions.
I'm aware that Qgis3 uses python 3 instead of python 2.
pyqgis vector-layer pyqgis-3 qgsvectorlayer
This script does not works in the QGIS Python terminal,
– xunilk
Jan 22 at 7:52
Hi xunik. Why did you change your comment? Your comment from before showed that the script did work for you in the Python terminal.
– meir412
Jan 22 at 9:00
add a comment |
I'm trying to migrate a python plugin I wrote from QGIS 2.18 to QGIS 3.4. In the plugin, I'm creating new vector features and eventually adding them to an existing vector layer. I'm trying to have the added features selected automatically in QGIS after running the code described below.
For QGIS 2.18, the QgsVectorLayer
class had an addFeatures
method that contained a very convenient makeSelected
flag. When the flag was set to true, the added features were selected.
In QGIS 3.4 the addFeatures
method still exists, but the flag is gone.
I also tried to select the features using some of the class's selection methods but with no success. It seems that after adding the features to the layer, their id's change and I don't have access to these specific features and can not select them using their old ids. The only way that somehow works is assigning some sentinel value to one of the fields in all the new features and then selecting them using a query. I'd like to avoid this way because I'm afraid that at some point it can mess up a user's data.
here's an example of how I created features and added them to the current selected layer in Qgis. it can be run in the Qgis Python terminal:
layer = iface.activeLayer()
layer.startEditing()
feat = QgsFeature(layer.fields())
point_list = [QgsPointXY(0, 0), QgsPointXY(0,1), QgsPointXY(1,1), QgsPointXY(1,0), QgsPointXY(0,0)]
feat.setGeometry(QgsGeometry.fromPolygonXY(point_list))
layer.addFeature(feat)
# I'd like to add feat to the layers selection at this point
self.layer.endEditCommand()
I'm using QGIS 3.4.1 (migrating from QGIS 2.18.26), and the pyqgis API of the above versions.
I'm aware that Qgis3 uses python 3 instead of python 2.
pyqgis vector-layer pyqgis-3 qgsvectorlayer
I'm trying to migrate a python plugin I wrote from QGIS 2.18 to QGIS 3.4. In the plugin, I'm creating new vector features and eventually adding them to an existing vector layer. I'm trying to have the added features selected automatically in QGIS after running the code described below.
For QGIS 2.18, the QgsVectorLayer
class had an addFeatures
method that contained a very convenient makeSelected
flag. When the flag was set to true, the added features were selected.
In QGIS 3.4 the addFeatures
method still exists, but the flag is gone.
I also tried to select the features using some of the class's selection methods but with no success. It seems that after adding the features to the layer, their id's change and I don't have access to these specific features and can not select them using their old ids. The only way that somehow works is assigning some sentinel value to one of the fields in all the new features and then selecting them using a query. I'd like to avoid this way because I'm afraid that at some point it can mess up a user's data.
here's an example of how I created features and added them to the current selected layer in Qgis. it can be run in the Qgis Python terminal:
layer = iface.activeLayer()
layer.startEditing()
feat = QgsFeature(layer.fields())
point_list = [QgsPointXY(0, 0), QgsPointXY(0,1), QgsPointXY(1,1), QgsPointXY(1,0), QgsPointXY(0,0)]
feat.setGeometry(QgsGeometry.fromPolygonXY(point_list))
layer.addFeature(feat)
# I'd like to add feat to the layers selection at this point
self.layer.endEditCommand()
I'm using QGIS 3.4.1 (migrating from QGIS 2.18.26), and the pyqgis API of the above versions.
I'm aware that Qgis3 uses python 3 instead of python 2.
pyqgis vector-layer pyqgis-3 qgsvectorlayer
pyqgis vector-layer pyqgis-3 qgsvectorlayer
edited Jan 21 at 16:35
xunilk
14.9k31942
14.9k31942
asked Jan 20 at 15:02
meir412meir412
345
345
This script does not works in the QGIS Python terminal,
– xunilk
Jan 22 at 7:52
Hi xunik. Why did you change your comment? Your comment from before showed that the script did work for you in the Python terminal.
– meir412
Jan 22 at 9:00
add a comment |
This script does not works in the QGIS Python terminal,
– xunilk
Jan 22 at 7:52
Hi xunik. Why did you change your comment? Your comment from before showed that the script did work for you in the Python terminal.
– meir412
Jan 22 at 9:00
This script does not works in the QGIS Python terminal,
– xunilk
Jan 22 at 7:52
This script does not works in the QGIS Python terminal,
– xunilk
Jan 22 at 7:52
Hi xunik. Why did you change your comment? Your comment from before showed that the script did work for you in the Python terminal.
– meir412
Jan 22 at 9:00
Hi xunik. Why did you change your comment? Your comment from before showed that the script did work for you in the Python terminal.
– meir412
Jan 22 at 9:00
add a comment |
1 Answer
1
active
oldest
votes
I've managed to reach the functionality I desired, and I'd like to share my solution.
I haven't found an elegant way to use the PyQGIS API to select the features you are adding to your layer, but I used the feature's geometry to track them down and add them to the selection.
I'm generalizing my solution to a list of features that you'd like to add to your layer and have them selected.
I'm assuming a list called geom_list
which contains any number of QgsGeometry
objects which represent the geometries of the features you're adding to your layer.
# prepare layer for editing
layer = iface.activeLayer()
layer.startEditing()
feature_list = []
# create all features with geometries from geom_list and fields from our layer and store in list
for index in range(len(geom_list)):
feat = QgsFeature(layer.fields())
feat.setGeometry(geom_list[index]]))
feature_list.append(feat)
# add new features to layer
layer.addFeatures(feature_list)
# Select all of the new added features
for general_feat in layer.getFeatures():
for desired_feat in feature_list:
if str(desired_feat.geometry()) == str(general_feat.geometry()):
layer.select(general_feat.id())
#finish editing
self.layer.endEditCommand()
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%2f309269%2fpython-scripting-in-qgis-to-add-features-and-select-them%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I've managed to reach the functionality I desired, and I'd like to share my solution.
I haven't found an elegant way to use the PyQGIS API to select the features you are adding to your layer, but I used the feature's geometry to track them down and add them to the selection.
I'm generalizing my solution to a list of features that you'd like to add to your layer and have them selected.
I'm assuming a list called geom_list
which contains any number of QgsGeometry
objects which represent the geometries of the features you're adding to your layer.
# prepare layer for editing
layer = iface.activeLayer()
layer.startEditing()
feature_list = []
# create all features with geometries from geom_list and fields from our layer and store in list
for index in range(len(geom_list)):
feat = QgsFeature(layer.fields())
feat.setGeometry(geom_list[index]]))
feature_list.append(feat)
# add new features to layer
layer.addFeatures(feature_list)
# Select all of the new added features
for general_feat in layer.getFeatures():
for desired_feat in feature_list:
if str(desired_feat.geometry()) == str(general_feat.geometry()):
layer.select(general_feat.id())
#finish editing
self.layer.endEditCommand()
add a comment |
I've managed to reach the functionality I desired, and I'd like to share my solution.
I haven't found an elegant way to use the PyQGIS API to select the features you are adding to your layer, but I used the feature's geometry to track them down and add them to the selection.
I'm generalizing my solution to a list of features that you'd like to add to your layer and have them selected.
I'm assuming a list called geom_list
which contains any number of QgsGeometry
objects which represent the geometries of the features you're adding to your layer.
# prepare layer for editing
layer = iface.activeLayer()
layer.startEditing()
feature_list = []
# create all features with geometries from geom_list and fields from our layer and store in list
for index in range(len(geom_list)):
feat = QgsFeature(layer.fields())
feat.setGeometry(geom_list[index]]))
feature_list.append(feat)
# add new features to layer
layer.addFeatures(feature_list)
# Select all of the new added features
for general_feat in layer.getFeatures():
for desired_feat in feature_list:
if str(desired_feat.geometry()) == str(general_feat.geometry()):
layer.select(general_feat.id())
#finish editing
self.layer.endEditCommand()
add a comment |
I've managed to reach the functionality I desired, and I'd like to share my solution.
I haven't found an elegant way to use the PyQGIS API to select the features you are adding to your layer, but I used the feature's geometry to track them down and add them to the selection.
I'm generalizing my solution to a list of features that you'd like to add to your layer and have them selected.
I'm assuming a list called geom_list
which contains any number of QgsGeometry
objects which represent the geometries of the features you're adding to your layer.
# prepare layer for editing
layer = iface.activeLayer()
layer.startEditing()
feature_list = []
# create all features with geometries from geom_list and fields from our layer and store in list
for index in range(len(geom_list)):
feat = QgsFeature(layer.fields())
feat.setGeometry(geom_list[index]]))
feature_list.append(feat)
# add new features to layer
layer.addFeatures(feature_list)
# Select all of the new added features
for general_feat in layer.getFeatures():
for desired_feat in feature_list:
if str(desired_feat.geometry()) == str(general_feat.geometry()):
layer.select(general_feat.id())
#finish editing
self.layer.endEditCommand()
I've managed to reach the functionality I desired, and I'd like to share my solution.
I haven't found an elegant way to use the PyQGIS API to select the features you are adding to your layer, but I used the feature's geometry to track them down and add them to the selection.
I'm generalizing my solution to a list of features that you'd like to add to your layer and have them selected.
I'm assuming a list called geom_list
which contains any number of QgsGeometry
objects which represent the geometries of the features you're adding to your layer.
# prepare layer for editing
layer = iface.activeLayer()
layer.startEditing()
feature_list = []
# create all features with geometries from geom_list and fields from our layer and store in list
for index in range(len(geom_list)):
feat = QgsFeature(layer.fields())
feat.setGeometry(geom_list[index]]))
feature_list.append(feat)
# add new features to layer
layer.addFeatures(feature_list)
# Select all of the new added features
for general_feat in layer.getFeatures():
for desired_feat in feature_list:
if str(desired_feat.geometry()) == str(general_feat.geometry()):
layer.select(general_feat.id())
#finish editing
self.layer.endEditCommand()
edited Apr 11 at 19:10
answered Apr 11 at 15:21
meir412meir412
345
345
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%2f309269%2fpython-scripting-in-qgis-to-add-features-and-select-them%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
This script does not works in the QGIS Python terminal,
– xunilk
Jan 22 at 7:52
Hi xunik. Why did you change your comment? Your comment from before showed that the script did work for you in the Python terminal.
– meir412
Jan 22 at 9:00