Exporting selected features to shapefile PyQGIS

The name of the pictureThe name of the pictureThe name of the pictureClash 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.



import script



See result:



selected features



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:



export error



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.










share|improve this question

























    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.



    import script



    See result:



    selected features



    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:



    export error



    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.










    share|improve this question























      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.



      import script



      See result:



      selected features



      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:



      export error



      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.










      share|improve this question













      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.



      import script



      See result:



      selected features



      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:



      export error



      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 45 mins ago









      JClarkson

      205




      205




















          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)





          share|improve this answer




















          • 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










          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          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)





          share|improve this answer




















          • 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














          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)





          share|improve this answer




















          • 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












          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)





          share|improve this answer












          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)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
















          • 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

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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













































































          Comments

          Popular posts from this blog

          List of Gilmore Girls characters

          What does second last employer means? [closed]

          One-line joke