1

QGIS 3.0.1

I wrote a code to create a vector layer and save it to disk as shapefile . Is there a way I can save its geometry property as 'Point' and SHPT as 'Point'

from qgis.core import *
import qgis.utils

QgsApplication.setPrefixPath("C:/Program Files/QGIS 3.0", True)
qgs = QgsApplication([], False) ###
qgs.initQgis() ###

_layer1 = QgsVectorLayer("C:/Arundhati/Automation Test Script/Radio Signal/MasterLine.shp", "MASTERLINE", "ogr")

if not _layer1.isValid():
  print("Layer failed to load!")

else:
    print("Layer was loaded successfully!")

_writer1 = QgsVectorFileWriter.writeAsVectorFormat(_layer1,"C:/Arundhati/Automation Test Script/Radio Signal/MasterLine12.shp",'utf-8',QgsCoordinateReferenceSystem(),'ESRI Shapefile')

qgs.exitQgis()

please suggest.

1

As described here, you can set the WkbType upon QgsVectorFileWriter instatiation, Here's an example:

from qgis.core import QgsWKBTypes

# Creates the file using giving settings
vector_writer = QgsVectorFileWriter("path/to/your/file", 'utf-8', _layer1.fields(), QgsWKBTypes.Point, QgsCoordinateReferenceSystem(4326L))

# Add your features to the created Shapefile
for _feature in features:
    vector_writer.addFeature(_feature)

# Close the writer to avoid concurrency
del vector_writer

Following this method, you do not need to use writeAsVectorFormat(), The file will be created during instantiation

12
  • I am getting following error :- NameError: name 'QgsWKBTypes' is not defined. What could be the reason? Is this version specific? Traceback (most recent call last): File "C:\PROGRA~1\QGIS3~1.0\apps\Python36\lib\code.py", line 91, in runcode exec(code, self.locals) File "<input>", line 1, in <module> File "<string>", line 42, in <module> NameError: name 'QgsWKBTypes' is not defined
    – Jay
    May 15 '18 at 18:01
  • Sorry, my mistake, I fix the code to import QgsWKBTypes, instead of QgsWkbTypes
    – xlDias
    May 15 '18 at 18:53
  • Umm No. It now gives me import error:- Traceback (most recent call last): File "C:\PROGRA~1\QGIS3~1.0\apps\Python36\lib\code.py", line 91, in runcode exec(code, self.locals) File "<input>", line 1, in <module> File "<string>", line 3, in <module> ImportError: cannot import name 'QgsWKBTypes'
    – Jay
    May 15 '18 at 18:59
  • I think it is not a version problem, as QGis 3.0 seems to use QgsWKBTypes. I just notice you've used from qgis.core import *, since you have this, try removing from qgis.core import QgsWKBTypes
    – xlDias
    May 15 '18 at 19:06
  • Hmm. Ok. Still the same import error. I am trying to find a solution for similar problems but no luck
    – Jay
    May 15 '18 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.