Skip to main content
edited tags; edited title
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

Create a Creating shapefile in a standalone script using PyQGIS?

Removed tag prefix from question title as per Jeff Atwood style preference
Source Link
underdark
  • 84.5k
  • 22
  • 233
  • 415

QGIS 3.0.2 : create Create a shapefile in a standalone script

I'm trying to write a standalone script that writes a shapefile (using QGIS 3.0.2). I've tried something like :

from qgis.core import *
from qgis.PyQt.QtCore import QVariant
from qgis.utils import QGis


QgsApplication.setPrefixPath("/usr/bin/qgis", True)
qgs = QgsApplication([], False)

qgs.initQgis()

fields = QgsFields()
fields.append(QgsField("ID_HYD", QVariant.Int))
fields.append(QgsField("SBD_KM2", QVariant.Double))


writer = QgsVectorFileWriter("/home/sylvain/test.shp",
                             "CP1250",
                             fields,
                             QGis.WKBPoint,
                             None,
                             "ESRI Shapefile")
      
qgs.exitQgis()

The issue is as follow :

/usr/bin/python3.5 /home/sylvain/test.py
Must construct a QGuiApplication first.

Process finished with exit code 1

Is there a way to write a shapefile on drive without constructing a GUIApplication ?

If not, how should I do do create such an item ?

QGIS 3.0.2 : create a shapefile in a standalone script

I'm trying to write a standalone script that writes a shapefile. I've tried something like :

from qgis.core import *
from qgis.PyQt.QtCore import QVariant
from qgis.utils import QGis


QgsApplication.setPrefixPath("/usr/bin/qgis", True)
qgs = QgsApplication([], False)

qgs.initQgis()

fields = QgsFields()
fields.append(QgsField("ID_HYD", QVariant.Int))
fields.append(QgsField("SBD_KM2", QVariant.Double))


writer = QgsVectorFileWriter("/home/sylvain/test.shp",
                             "CP1250",
                             fields,
                             QGis.WKBPoint,
                             None,
                             "ESRI Shapefile")
      
qgs.exitQgis()

The issue is as follow :

/usr/bin/python3.5 /home/sylvain/test.py
Must construct a QGuiApplication first.

Process finished with exit code 1

Is there a way to write a shapefile on drive without constructing a GUIApplication ?

If not, how should I do do create such an item ?

Create a shapefile in a standalone script

I'm trying to write a standalone script that writes a shapefile (using QGIS 3.0.2). I've tried something like :

from qgis.core import *
from qgis.PyQt.QtCore import QVariant
from qgis.utils import QGis


QgsApplication.setPrefixPath("/usr/bin/qgis", True)
qgs = QgsApplication([], False)

qgs.initQgis()

fields = QgsFields()
fields.append(QgsField("ID_HYD", QVariant.Int))
fields.append(QgsField("SBD_KM2", QVariant.Double))


writer = QgsVectorFileWriter("/home/sylvain/test.shp",
                             "CP1250",
                             fields,
                             QGis.WKBPoint,
                             None,
                             "ESRI Shapefile")
      
qgs.exitQgis()

The issue is as follow :

/usr/bin/python3.5 /home/sylvain/test.py
Must construct a QGuiApplication first.

Process finished with exit code 1

Is there a way to write a shapefile on drive without constructing a GUIApplication ?

If not, how should I do do create such an item ?

deleted 51 characters in body
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

I'm trying to write a standalone script that writes a shapefile. I've tried something like :

from qgis.core import *
from qgis.PyQt.QtCore import QVariant
from qgis.utils import QGis


QgsApplication.setPrefixPath("/usr/bin/qgis", True)
qgs = QgsApplication([], False)

qgs.initQgis()

fields = QgsFields()
fields.append(QgsField("ID_HYD", QVariant.Int))
fields.append(QgsField("SBD_KM2", QVariant.Double))


writer = QgsVectorFileWriter("/home/sylvain/test.shp",
                             "CP1250",
                             fields,
                             QGis.WKBPoint,
                             None,
                             "ESRI Shapefile")
      
qgs.exitQgis()

The issue is as follow :

/usr/bin/python3.5 /home/sylvain/test.py
Must construct a QGuiApplication first.

Process finished with exit code 1

Is there a way to write a shapefile on drive without constructing a GUIApplication ? If

If not, how should I do do create such an item ?

Thanks for any information.

All the best, S.

I'm trying to write a standalone script that writes a shapefile. I've tried something like :

from qgis.core import *
from qgis.PyQt.QtCore import QVariant
from qgis.utils import QGis


QgsApplication.setPrefixPath("/usr/bin/qgis", True)
qgs = QgsApplication([], False)

qgs.initQgis()

fields = QgsFields()
fields.append(QgsField("ID_HYD", QVariant.Int))
fields.append(QgsField("SBD_KM2", QVariant.Double))


writer = QgsVectorFileWriter("/home/sylvain/test.shp",
                             "CP1250",
                             fields,
                             QGis.WKBPoint,
                             None,
                             "ESRI Shapefile")
      
qgs.exitQgis()

The issue is as follow :

/usr/bin/python3.5 /home/sylvain/test.py
Must construct a QGuiApplication first.

Process finished with exit code 1

Is there a way to write a shapefile on drive without constructing a GUIApplication ? If not, how should I do do create such an item ?

Thanks for any information.

All the best, S.

I'm trying to write a standalone script that writes a shapefile. I've tried something like :

from qgis.core import *
from qgis.PyQt.QtCore import QVariant
from qgis.utils import QGis


QgsApplication.setPrefixPath("/usr/bin/qgis", True)
qgs = QgsApplication([], False)

qgs.initQgis()

fields = QgsFields()
fields.append(QgsField("ID_HYD", QVariant.Int))
fields.append(QgsField("SBD_KM2", QVariant.Double))


writer = QgsVectorFileWriter("/home/sylvain/test.shp",
                             "CP1250",
                             fields,
                             QGis.WKBPoint,
                             None,
                             "ESRI Shapefile")
      
qgs.exitQgis()

The issue is as follow :

/usr/bin/python3.5 /home/sylvain/test.py
Must construct a QGuiApplication first.

Process finished with exit code 1

Is there a way to write a shapefile on drive without constructing a GUIApplication ?

If not, how should I do do create such an item ?

added 51 characters in body
Source Link
sylther
  • 61
  • 1
  • 5
Loading
Tweeted twitter.com/StackGIS/status/997469735041273862
removed thanks, added pyqgis tag, formatted code
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404
Loading
added 1 character in body
Source Link
sylther
  • 61
  • 1
  • 5
Loading
Source Link
sylther
  • 61
  • 1
  • 5
Loading