Adding new field and calculating in PyqgisAdding a new field and calculating area using PythonAdding field...
How do researchers send unsolicited emails asking for feedback on their works?
Is it okay for a cleric of life to use spells like Animate Dead and/or Contagion?
Exposing a company lying about themselves in a tightly knit industry: Is my career at risk on the long run?
Is VPN a layer 3 concept?
Print last inputted byte
How to understand 「僕は誰より彼女が好きなんだ。」
Could any one tell what PN is this Chip? Thanks~
What will the Frenchman say?
Is xar preinstalled on macOS?
Jem'Hadar, something strange about their life expectancy
Why are there no stars visible in cislunar space?
PTIJ: Why do we make a Lulav holder?
PTIJ: Where did Achashverosh's years wander off to?
Does the Shadow Magic sorcerer's Eyes of the Dark feature work on all Darkness spells or just his/her own?
Symbolism of 18 Journeyers
I got the following comment from a reputed math journal. What does it mean?
Weird lines in Microsoft Word
Why do I have a large white artefact on the rendered image?
What favor did Moody owe Dumbledore?
Exit shell with shortcut (not typing exit) that closes session properly
Do native speakers use "ultima" and "proxima" frequently in spoken English?
Why doesn't the fusion process of the sun speed up?
If I cast the Enlarge/Reduce spell on an arrow, what weapon could it count as?
Error in master's thesis, I do not know what to do
Adding new field and calculating in Pyqgis
Adding a new field and calculating area using PythonAdding field and calculating expression with PyQGIS?How accessing a specific feature and comparing it with the next one?Adding field in PyQGIS?Adding Google Maps with PyQGIS?PyQGIS: Multiply fields and populate new fieldMaking output shapefile taking fields of input, and adding new field populated with list using PyQGIS?Defining fields and adding features to output layer using QGIS?Adding new field with expression in pyqgisCreate Widgets for specific fields of a feature
I'm trying to write a plugin using QGIS 3.4. What I want to achieve is an excel-style calculation where I can create a new field (field2) and calculate the difference between current and previous row of an existing field (field1).
However, I'm stuck at the first step which is creating the new field. Attached my sample code below. The first part is how I generated my layer from an interpolation process.
# Interpolate
mem_layer = QgsVectorLayer("Point?crs="+vlayer.sourceCrs().authid(), 'point', 'memory')
mem_layer.startEditing()
fields = [field for field in csvlayer.fields()]
prov = mem_layer.dataProvider()
prov.addAttributes(fields)
mem_layer.updateFields()
chainageIdx = csvlayer.fields().indexFromName(chainage_field)
for line in vlayer.getFeatures():
for feat in csvlayer.getFeatures():
new_feat = QgsFeature()
attrs = feat.attributes()
new_feat.setGeometry(line.geometry().interpolate(float(attrs[chainageIdx])))
new_feat.setAttributes(attrs)
mem_layer.addFeatures([new_feat])
mem_layer.commitChanges()
# Calc 1
mem_layer.startEditing()
datetimeField = QgsField('DateTime', QVariant.Double)
prov = mem_layer.dataProvider()
prov.addAttributes([datetimeField])
mem_layer.updateFields()
fields.append(datetimeField)
newdatetimeIdx = mem_layer.fields().indexFromName('DateTime')
for feat in mem_layer.getFeatures():
#mem_layer.changeAttributeValue(feat.id(),newdatetimeIdx, 1)
feat.setAttribute(newdatetimeIdx, 1.0)
mem_layer.updateFeature(feat)
mem_layer.commitChanges()
# Output
(sink_points, dest_id_points) = self.parameterAsSink(
parameters,
self.OUTPOINTS,
context,
fields,
QgsWkbTypes.Point,
vlayer.sourceCrs()
)
features = mem_layer.getFeatures()
for current, feature in enumerate(features):
# Stop the algorithm if cancel button has been clicked
if feedback.isCanceled():
break
# Add a feature in the sink
sink_points.addFeature(feature, QgsFeatureSink.FastInsert)
# Update the progress bar
feedback.setProgress(int(current * total))
When I try to run this, I get a KeyError: '2'
message which suggests that the field was not created hence it couldn't write the test value.
python pyqgis qgis-3 plugins
add a comment |
I'm trying to write a plugin using QGIS 3.4. What I want to achieve is an excel-style calculation where I can create a new field (field2) and calculate the difference between current and previous row of an existing field (field1).
However, I'm stuck at the first step which is creating the new field. Attached my sample code below. The first part is how I generated my layer from an interpolation process.
# Interpolate
mem_layer = QgsVectorLayer("Point?crs="+vlayer.sourceCrs().authid(), 'point', 'memory')
mem_layer.startEditing()
fields = [field for field in csvlayer.fields()]
prov = mem_layer.dataProvider()
prov.addAttributes(fields)
mem_layer.updateFields()
chainageIdx = csvlayer.fields().indexFromName(chainage_field)
for line in vlayer.getFeatures():
for feat in csvlayer.getFeatures():
new_feat = QgsFeature()
attrs = feat.attributes()
new_feat.setGeometry(line.geometry().interpolate(float(attrs[chainageIdx])))
new_feat.setAttributes(attrs)
mem_layer.addFeatures([new_feat])
mem_layer.commitChanges()
# Calc 1
mem_layer.startEditing()
datetimeField = QgsField('DateTime', QVariant.Double)
prov = mem_layer.dataProvider()
prov.addAttributes([datetimeField])
mem_layer.updateFields()
fields.append(datetimeField)
newdatetimeIdx = mem_layer.fields().indexFromName('DateTime')
for feat in mem_layer.getFeatures():
#mem_layer.changeAttributeValue(feat.id(),newdatetimeIdx, 1)
feat.setAttribute(newdatetimeIdx, 1.0)
mem_layer.updateFeature(feat)
mem_layer.commitChanges()
# Output
(sink_points, dest_id_points) = self.parameterAsSink(
parameters,
self.OUTPOINTS,
context,
fields,
QgsWkbTypes.Point,
vlayer.sourceCrs()
)
features = mem_layer.getFeatures()
for current, feature in enumerate(features):
# Stop the algorithm if cancel button has been clicked
if feedback.isCanceled():
break
# Add a feature in the sink
sink_points.addFeature(feature, QgsFeatureSink.FastInsert)
# Update the progress bar
feedback.setProgress(int(current * total))
When I try to run this, I get a KeyError: '2'
message which suggests that the field was not created hence it couldn't write the test value.
python pyqgis qgis-3 plugins
add a comment |
I'm trying to write a plugin using QGIS 3.4. What I want to achieve is an excel-style calculation where I can create a new field (field2) and calculate the difference between current and previous row of an existing field (field1).
However, I'm stuck at the first step which is creating the new field. Attached my sample code below. The first part is how I generated my layer from an interpolation process.
# Interpolate
mem_layer = QgsVectorLayer("Point?crs="+vlayer.sourceCrs().authid(), 'point', 'memory')
mem_layer.startEditing()
fields = [field for field in csvlayer.fields()]
prov = mem_layer.dataProvider()
prov.addAttributes(fields)
mem_layer.updateFields()
chainageIdx = csvlayer.fields().indexFromName(chainage_field)
for line in vlayer.getFeatures():
for feat in csvlayer.getFeatures():
new_feat = QgsFeature()
attrs = feat.attributes()
new_feat.setGeometry(line.geometry().interpolate(float(attrs[chainageIdx])))
new_feat.setAttributes(attrs)
mem_layer.addFeatures([new_feat])
mem_layer.commitChanges()
# Calc 1
mem_layer.startEditing()
datetimeField = QgsField('DateTime', QVariant.Double)
prov = mem_layer.dataProvider()
prov.addAttributes([datetimeField])
mem_layer.updateFields()
fields.append(datetimeField)
newdatetimeIdx = mem_layer.fields().indexFromName('DateTime')
for feat in mem_layer.getFeatures():
#mem_layer.changeAttributeValue(feat.id(),newdatetimeIdx, 1)
feat.setAttribute(newdatetimeIdx, 1.0)
mem_layer.updateFeature(feat)
mem_layer.commitChanges()
# Output
(sink_points, dest_id_points) = self.parameterAsSink(
parameters,
self.OUTPOINTS,
context,
fields,
QgsWkbTypes.Point,
vlayer.sourceCrs()
)
features = mem_layer.getFeatures()
for current, feature in enumerate(features):
# Stop the algorithm if cancel button has been clicked
if feedback.isCanceled():
break
# Add a feature in the sink
sink_points.addFeature(feature, QgsFeatureSink.FastInsert)
# Update the progress bar
feedback.setProgress(int(current * total))
When I try to run this, I get a KeyError: '2'
message which suggests that the field was not created hence it couldn't write the test value.
python pyqgis qgis-3 plugins
I'm trying to write a plugin using QGIS 3.4. What I want to achieve is an excel-style calculation where I can create a new field (field2) and calculate the difference between current and previous row of an existing field (field1).
However, I'm stuck at the first step which is creating the new field. Attached my sample code below. The first part is how I generated my layer from an interpolation process.
# Interpolate
mem_layer = QgsVectorLayer("Point?crs="+vlayer.sourceCrs().authid(), 'point', 'memory')
mem_layer.startEditing()
fields = [field for field in csvlayer.fields()]
prov = mem_layer.dataProvider()
prov.addAttributes(fields)
mem_layer.updateFields()
chainageIdx = csvlayer.fields().indexFromName(chainage_field)
for line in vlayer.getFeatures():
for feat in csvlayer.getFeatures():
new_feat = QgsFeature()
attrs = feat.attributes()
new_feat.setGeometry(line.geometry().interpolate(float(attrs[chainageIdx])))
new_feat.setAttributes(attrs)
mem_layer.addFeatures([new_feat])
mem_layer.commitChanges()
# Calc 1
mem_layer.startEditing()
datetimeField = QgsField('DateTime', QVariant.Double)
prov = mem_layer.dataProvider()
prov.addAttributes([datetimeField])
mem_layer.updateFields()
fields.append(datetimeField)
newdatetimeIdx = mem_layer.fields().indexFromName('DateTime')
for feat in mem_layer.getFeatures():
#mem_layer.changeAttributeValue(feat.id(),newdatetimeIdx, 1)
feat.setAttribute(newdatetimeIdx, 1.0)
mem_layer.updateFeature(feat)
mem_layer.commitChanges()
# Output
(sink_points, dest_id_points) = self.parameterAsSink(
parameters,
self.OUTPOINTS,
context,
fields,
QgsWkbTypes.Point,
vlayer.sourceCrs()
)
features = mem_layer.getFeatures()
for current, feature in enumerate(features):
# Stop the algorithm if cancel button has been clicked
if feedback.isCanceled():
break
# Add a feature in the sink
sink_points.addFeature(feature, QgsFeatureSink.FastInsert)
# Update the progress bar
feedback.setProgress(int(current * total))
When I try to run this, I get a KeyError: '2'
message which suggests that the field was not created hence it couldn't write the test value.
python pyqgis qgis-3 plugins
python pyqgis qgis-3 plugins
asked 7 mins ago
strythestrythe
587
587
add a comment |
add a comment |
0
active
oldest
votes
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%2f315926%2fadding-new-field-and-calculating-in-pyqgis%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f315926%2fadding-new-field-and-calculating-in-pyqgis%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