Exporting selected features to shapefile PyQGIS
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am trying to develop a script to export selected features on a layer to enable some automation.
So far I have used this formula to import a layer and run a query. This ends up with features being selected on the layer.
See result:
All good so far.
I now try to use the below script, found in a previous similar question:
QGIS Export Shapefile using PyQgis
It worked for the users in the post, however this returns the following error for me:
I am quite new to Python so am not quite sure what's wrong. It is saying name 'i' is not defined, but I'm not sure what 'i' is trying to refer to?
I'm using QGIS 2.18.20.
qgis pyqgis
add a comment |Â
up vote
1
down vote
favorite
I am trying to develop a script to export selected features on a layer to enable some automation.
So far I have used this formula to import a layer and run a query. This ends up with features being selected on the layer.
See result:
All good so far.
I now try to use the below script, found in a previous similar question:
QGIS Export Shapefile using PyQgis
It worked for the users in the post, however this returns the following error for me:
I am quite new to Python so am not quite sure what's wrong. It is saying name 'i' is not defined, but I'm not sure what 'i' is trying to refer to?
I'm using QGIS 2.18.20.
qgis pyqgis
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to develop a script to export selected features on a layer to enable some automation.
So far I have used this formula to import a layer and run a query. This ends up with features being selected on the layer.
See result:
All good so far.
I now try to use the below script, found in a previous similar question:
QGIS Export Shapefile using PyQgis
It worked for the users in the post, however this returns the following error for me:
I am quite new to Python so am not quite sure what's wrong. It is saying name 'i' is not defined, but I'm not sure what 'i' is trying to refer to?
I'm using QGIS 2.18.20.
qgis pyqgis
I am trying to develop a script to export selected features on a layer to enable some automation.
So far I have used this formula to import a layer and run a query. This ends up with features being selected on the layer.
See result:
All good so far.
I now try to use the below script, found in a previous similar question:
QGIS Export Shapefile using PyQgis
It worked for the users in the post, however this returns the following error for me:
I am quite new to Python so am not quite sure what's wrong. It is saying name 'i' is not defined, but I'm not sure what 'i' is trying to refer to?
I'm using QGIS 2.18.20.
qgis pyqgis
qgis pyqgis
asked 45 mins ago
JClarkson
205
205
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
The i
in the post you linked to was referencing all layers that were loaded. So the code was iterating through each layer and saving their selected features.
Since you're only interested in one layer, replace i
with layer
and add the boolean selection parameter onlySelected
:
_writer = QgsVectorFileWriter.writeAsVectorFormat(layer, 'C:/Datasets/South Oxfordshire Clipped LR INSPIRE.shp', "utf-8", None, "ESRI Shapefile", onlySelected=True)
Brilliant thank you. My next task is to create a loop for this to run through 348 individually named shapefiles. However I will try this myself first before asking for help!
– JClarkson
6 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
The i
in the post you linked to was referencing all layers that were loaded. So the code was iterating through each layer and saving their selected features.
Since you're only interested in one layer, replace i
with layer
and add the boolean selection parameter onlySelected
:
_writer = QgsVectorFileWriter.writeAsVectorFormat(layer, 'C:/Datasets/South Oxfordshire Clipped LR INSPIRE.shp', "utf-8", None, "ESRI Shapefile", onlySelected=True)
Brilliant thank you. My next task is to create a loop for this to run through 348 individually named shapefiles. However I will try this myself first before asking for help!
– JClarkson
6 mins ago
add a comment |Â
up vote
3
down vote
accepted
The i
in the post you linked to was referencing all layers that were loaded. So the code was iterating through each layer and saving their selected features.
Since you're only interested in one layer, replace i
with layer
and add the boolean selection parameter onlySelected
:
_writer = QgsVectorFileWriter.writeAsVectorFormat(layer, 'C:/Datasets/South Oxfordshire Clipped LR INSPIRE.shp', "utf-8", None, "ESRI Shapefile", onlySelected=True)
Brilliant thank you. My next task is to create a loop for this to run through 348 individually named shapefiles. However I will try this myself first before asking for help!
– JClarkson
6 mins ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
The i
in the post you linked to was referencing all layers that were loaded. So the code was iterating through each layer and saving their selected features.
Since you're only interested in one layer, replace i
with layer
and add the boolean selection parameter onlySelected
:
_writer = QgsVectorFileWriter.writeAsVectorFormat(layer, 'C:/Datasets/South Oxfordshire Clipped LR INSPIRE.shp', "utf-8", None, "ESRI Shapefile", onlySelected=True)
The i
in the post you linked to was referencing all layers that were loaded. So the code was iterating through each layer and saving their selected features.
Since you're only interested in one layer, replace i
with layer
and add the boolean selection parameter onlySelected
:
_writer = QgsVectorFileWriter.writeAsVectorFormat(layer, 'C:/Datasets/South Oxfordshire Clipped LR INSPIRE.shp', "utf-8", None, "ESRI Shapefile", onlySelected=True)
answered 31 mins ago
Joseph
54.9k787179
54.9k787179
Brilliant thank you. My next task is to create a loop for this to run through 348 individually named shapefiles. However I will try this myself first before asking for help!
– JClarkson
6 mins ago
add a comment |Â
Brilliant thank you. My next task is to create a loop for this to run through 348 individually named shapefiles. However I will try this myself first before asking for help!
– JClarkson
6 mins ago
Brilliant thank you. My next task is to create a loop for this to run through 348 individually named shapefiles. However I will try this myself first before asking for help!
– JClarkson
6 mins ago
Brilliant thank you. My next task is to create a loop for this to run through 348 individually named shapefiles. However I will try this myself first before asking for help!
– JClarkson
6 mins ago
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f298430%2fexporting-selected-features-to-shapefile-pyqgis%23new-answer', 'question_page');
);
Post as a guest
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
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
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