Add fields from a feature to a feature in another layer Planned maintenance scheduled April...
Is there a (better) way to access $wpdb results?
Identify plant with long narrow paired leaves and reddish stems
Can a non-EU citizen traveling with me come with me through the EU passport line?
Echoing a tail command produces unexpected output?
How widely used is the term Treppenwitz? Is it something that most Germans know?
How to tell that you are a giant?
Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?
Sci-Fi book where patients in a coma ward all live in a subconscious world linked together
English words in a non-english sci-fi novel
Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?
Is the Standard Deduction better than Itemized when both are the same amount?
Extract all GPU name, model and GPU ram
Denied boarding although I have proper visa and documentation. To whom should I make a complaint?
How to react to hostile behavior from a senior developer?
How to run gsettings for another user Ubuntu 18.04.2 LTS
Do I really need recursive chmod to restrict access to a folder?
Why do people hide their license plates in the EU?
What is a non-alternating simple group with big order, but relatively few conjugacy classes?
If a contract sometimes uses the wrong name, is it still valid?
A coin, having probability p of landing heads and probability of q=(1-p) of landing on heads.
Are two submodules (where one is contained in the other) isomorphic if their quotientmodules are isomorphic?
How to answer "Have you ever been terminated?"
How would the world control an invulnerable immortal mass murderer?
Resolving to minmaj7
Add fields from a feature to a feature in another layer
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Deleting all fields from feature class with ArcPy?rename feature layer fieldsPython Script to Add Fields to Feature ClassesDeleting many fields from many feature classes using ArcPy?How can my PyQGIS script copy specified attributes from one layer to another?Add attributes to layerPyQGIS - Adding Fields to a Feature LayerCan't see the Fields I'm trying to add in pyQgis?Automatically set attribute values for a layer based on proximity relations to another layer in QGISDefining fields and adding features to output layer using QGIS?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I have a criteria to select the features from a layer that overlap the features of another layer, but i do not know how to copy the fields at the same time:
Tyhe code does:
- two layers (1 and 2) of polygons
- if one feature of polygon2 overlap more than 80% one of polygon1, it selects it.
How to also copy the fields of polygon1 to add them to the fields of the selected polygon2?
from qgis.core import *
import processing
import os
mapcanvas = iface.mapCanvas()
layers = mapcanvas.layers()
#polygon1
feats_lyr1 = [ feat for feat in layers[0].getFeatures() ]
#polygon2
feats_lyr2 = [ feat for feat in layers[1].getFeatures() ]
selected_feats = []
for i, feat1 in enumerate(feats_lyr1):
for j, feat2 in enumerate(feats_lyr2):
if feat1.geometry().intersects(feat2.geometry()):
area1 = feat1.geometry().intersection(feat2.geometry()).area()
area2 = feat2.geometry().area()
crit =area1/area2
if crit > 0.8:
selected_feats.append(feat2)
epsg = layers[0].crs().postgisSrid()
uri = "Polygon?crs=epsg:" + str(epsg) + "&index=yes"
mem_layer = QgsVectorLayer(uri,
"mem_layer",
"memory")
for i, feat in enumerate(selected_feats):
feat.setAttributes(selected_feats[i].attributes())
prov = mem_layer.dataProvider()
attr = layers[1].dataProvider().fields().toList()
prov.addAttributes(attr)
mem_layer.updateFields()
prov.addFeatures(selected_feats)
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
pyqgis fields-attributes
bumped to the homepage by Community♦ 8 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I have a criteria to select the features from a layer that overlap the features of another layer, but i do not know how to copy the fields at the same time:
Tyhe code does:
- two layers (1 and 2) of polygons
- if one feature of polygon2 overlap more than 80% one of polygon1, it selects it.
How to also copy the fields of polygon1 to add them to the fields of the selected polygon2?
from qgis.core import *
import processing
import os
mapcanvas = iface.mapCanvas()
layers = mapcanvas.layers()
#polygon1
feats_lyr1 = [ feat for feat in layers[0].getFeatures() ]
#polygon2
feats_lyr2 = [ feat for feat in layers[1].getFeatures() ]
selected_feats = []
for i, feat1 in enumerate(feats_lyr1):
for j, feat2 in enumerate(feats_lyr2):
if feat1.geometry().intersects(feat2.geometry()):
area1 = feat1.geometry().intersection(feat2.geometry()).area()
area2 = feat2.geometry().area()
crit =area1/area2
if crit > 0.8:
selected_feats.append(feat2)
epsg = layers[0].crs().postgisSrid()
uri = "Polygon?crs=epsg:" + str(epsg) + "&index=yes"
mem_layer = QgsVectorLayer(uri,
"mem_layer",
"memory")
for i, feat in enumerate(selected_feats):
feat.setAttributes(selected_feats[i].attributes())
prov = mem_layer.dataProvider()
attr = layers[1].dataProvider().fields().toList()
prov.addAttributes(attr)
mem_layer.updateFields()
prov.addFeatures(selected_feats)
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
pyqgis fields-attributes
bumped to the homepage by Community♦ 8 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I have a criteria to select the features from a layer that overlap the features of another layer, but i do not know how to copy the fields at the same time:
Tyhe code does:
- two layers (1 and 2) of polygons
- if one feature of polygon2 overlap more than 80% one of polygon1, it selects it.
How to also copy the fields of polygon1 to add them to the fields of the selected polygon2?
from qgis.core import *
import processing
import os
mapcanvas = iface.mapCanvas()
layers = mapcanvas.layers()
#polygon1
feats_lyr1 = [ feat for feat in layers[0].getFeatures() ]
#polygon2
feats_lyr2 = [ feat for feat in layers[1].getFeatures() ]
selected_feats = []
for i, feat1 in enumerate(feats_lyr1):
for j, feat2 in enumerate(feats_lyr2):
if feat1.geometry().intersects(feat2.geometry()):
area1 = feat1.geometry().intersection(feat2.geometry()).area()
area2 = feat2.geometry().area()
crit =area1/area2
if crit > 0.8:
selected_feats.append(feat2)
epsg = layers[0].crs().postgisSrid()
uri = "Polygon?crs=epsg:" + str(epsg) + "&index=yes"
mem_layer = QgsVectorLayer(uri,
"mem_layer",
"memory")
for i, feat in enumerate(selected_feats):
feat.setAttributes(selected_feats[i].attributes())
prov = mem_layer.dataProvider()
attr = layers[1].dataProvider().fields().toList()
prov.addAttributes(attr)
mem_layer.updateFields()
prov.addFeatures(selected_feats)
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
pyqgis fields-attributes
I have a criteria to select the features from a layer that overlap the features of another layer, but i do not know how to copy the fields at the same time:
Tyhe code does:
- two layers (1 and 2) of polygons
- if one feature of polygon2 overlap more than 80% one of polygon1, it selects it.
How to also copy the fields of polygon1 to add them to the fields of the selected polygon2?
from qgis.core import *
import processing
import os
mapcanvas = iface.mapCanvas()
layers = mapcanvas.layers()
#polygon1
feats_lyr1 = [ feat for feat in layers[0].getFeatures() ]
#polygon2
feats_lyr2 = [ feat for feat in layers[1].getFeatures() ]
selected_feats = []
for i, feat1 in enumerate(feats_lyr1):
for j, feat2 in enumerate(feats_lyr2):
if feat1.geometry().intersects(feat2.geometry()):
area1 = feat1.geometry().intersection(feat2.geometry()).area()
area2 = feat2.geometry().area()
crit =area1/area2
if crit > 0.8:
selected_feats.append(feat2)
epsg = layers[0].crs().postgisSrid()
uri = "Polygon?crs=epsg:" + str(epsg) + "&index=yes"
mem_layer = QgsVectorLayer(uri,
"mem_layer",
"memory")
for i, feat in enumerate(selected_feats):
feat.setAttributes(selected_feats[i].attributes())
prov = mem_layer.dataProvider()
attr = layers[1].dataProvider().fields().toList()
prov.addAttributes(attr)
mem_layer.updateFields()
prov.addFeatures(selected_feats)
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
pyqgis fields-attributes
pyqgis fields-attributes
edited Jun 27 '18 at 13:17
francois
asked Jun 26 '18 at 14:40
francoisfrancois
826
826
bumped to the homepage by Community♦ 8 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 8 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Just add the source layer fields to a list, use addAttributes(), and updateFields() methods to add the fields:
prov = mem_layer.dataProvider()
attr = layers[0].dataProvider().fields().toList()
prov.addAttributes(attr)
mem_layer.updateFields()
prov.addFeatures(selected_feats)
Thanks for the answer! But how to get the correspondant fields of the other layer, in other word, in the loop, to add the fields of the feat1 to those of feat2?
– francois
Jun 27 '18 at 13:19
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%2f287559%2fadd-fields-from-a-feature-to-a-feature-in-another-layer%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
Just add the source layer fields to a list, use addAttributes(), and updateFields() methods to add the fields:
prov = mem_layer.dataProvider()
attr = layers[0].dataProvider().fields().toList()
prov.addAttributes(attr)
mem_layer.updateFields()
prov.addFeatures(selected_feats)
Thanks for the answer! But how to get the correspondant fields of the other layer, in other word, in the loop, to add the fields of the feat1 to those of feat2?
– francois
Jun 27 '18 at 13:19
add a comment |
Just add the source layer fields to a list, use addAttributes(), and updateFields() methods to add the fields:
prov = mem_layer.dataProvider()
attr = layers[0].dataProvider().fields().toList()
prov.addAttributes(attr)
mem_layer.updateFields()
prov.addFeatures(selected_feats)
Thanks for the answer! But how to get the correspondant fields of the other layer, in other word, in the loop, to add the fields of the feat1 to those of feat2?
– francois
Jun 27 '18 at 13:19
add a comment |
Just add the source layer fields to a list, use addAttributes(), and updateFields() methods to add the fields:
prov = mem_layer.dataProvider()
attr = layers[0].dataProvider().fields().toList()
prov.addAttributes(attr)
mem_layer.updateFields()
prov.addFeatures(selected_feats)
Just add the source layer fields to a list, use addAttributes(), and updateFields() methods to add the fields:
prov = mem_layer.dataProvider()
attr = layers[0].dataProvider().fields().toList()
prov.addAttributes(attr)
mem_layer.updateFields()
prov.addFeatures(selected_feats)
answered Jun 27 '18 at 12:26
artwork21artwork21
31.1k555120
31.1k555120
Thanks for the answer! But how to get the correspondant fields of the other layer, in other word, in the loop, to add the fields of the feat1 to those of feat2?
– francois
Jun 27 '18 at 13:19
add a comment |
Thanks for the answer! But how to get the correspondant fields of the other layer, in other word, in the loop, to add the fields of the feat1 to those of feat2?
– francois
Jun 27 '18 at 13:19
Thanks for the answer! But how to get the correspondant fields of the other layer, in other word, in the loop, to add the fields of the feat1 to those of feat2?
– francois
Jun 27 '18 at 13:19
Thanks for the answer! But how to get the correspondant fields of the other layer, in other word, in the loop, to add the fields of the feat1 to those of feat2?
– francois
Jun 27 '18 at 13:19
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%2f287559%2fadd-fields-from-a-feature-to-a-feature-in-another-layer%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