QGIS Python (PyQGIS) - Create a new shapefile

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 ม.ค. 2025

ความคิดเห็น • 11

  • @lesterhorton4807
    @lesterhorton4807 4 ปีที่แล้ว +7

    The Qgis python code in this Video:
    from qgis.core import QgsFields, QgsFeature
    fn = r'C:/Users/wgy/Desktop/fire/test/newpoints.shp'
    layerFields = QgsFields()
    layerFields.append(QgsField('id', QVariant.Int))
    layerFields.append(QgsField('value', QVariant.Double))
    layerFields.append(QgsField('name', QVariant.String))
    writer = QgsVectorFileWriter(fn, 'UTF-8', layerFields, QgsWkbTypes.Point,\
    QgsCoordinateReferenceSystem('EPSG:26912'), 'ESRI Shapefile')
    feat = QgsFeature()
    feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(455618, 463221)))
    feat.setAttributes([1, 1.1, 'name'])
    writer.addFeature(feat)
    iface.addVectorLayer(fn, 'points', 'ogr')
    del(writer)

  • @danielkiss3067
    @danielkiss3067 2 ปีที่แล้ว

    Thanks for the tutorial. It's very useful! What should i write to the 'fn'= if i want to use the directory of the project file?

  • @GUSTAVO82027
    @GUSTAVO82027 3 ปีที่แล้ว

    Thanks for the tutorial, I want to know how I can add more characteristics or rows to the new shapefile please

    • @geospatialschool
      @geospatialschool  3 ปีที่แล้ว

      You should check out this course and see if it covers the topics you are interested in. opensourceoptions.com/course/pyqgis-learn-the-qgis-python-api/

  • @smyleGF03
    @smyleGF03 2 ปีที่แล้ว

    Hi. I run this script in Jupyter. I can create a new shapefile, but it does give me error: DeprecationWarning: QgsVectorFileWriter constructor is deprecated.
    So problem arise when I try to create multiple new shapefiles trough looping. It still give me shapefiles, but all shapefile except the first one (the one created at i=1, let say) are errors. no buffer zone inside

  • @marylappp
    @marylappp 3 ปีที่แล้ว

    I tried to execute this code in smaller parts and I have a question:
    Why after executing writer the layer is completly empty? It has no fields but we added them as a layerfields paremater inside the writer.
    I mean, executing first12 lines of code results with a compelty empty layer without any fields. Fields apeear after executing the rest of code begining from 13th line - when adding feature. But why... Isn't it supposed to have fields before excecuting this part of the code?

    • @geospatialschool
      @geospatialschool  3 ปีที่แล้ว

      It could be the way QGIS displays the layer. Just because the fields don't appear doesn't mean the field definitions aren't there. You could check this by querying the fields from the layer before you write any features to it.

    • @marylappp
      @marylappp 3 ปีที่แล้ว

      ​@@geospatialschool gosh, I was creating a layer and adding features over and over again but they never displayed in this layer added by the script to current open project in QGIS. I opened a new project, added this created layer and everything was there.
      Now it seems stupid but I was struggling a lot with creating and adding fetaures.
      Anyway, thanks for the answer! I find your tutorials very useful.

    • @geospatialschool
      @geospatialschool  3 ปีที่แล้ว

      @@marylappp You're welcome. Glad they're helpful!

  • @АгентСмит-к9ъ
    @АгентСмит-к9ъ 5 ปีที่แล้ว +1

    Worried that after executing the script the point will not be, glad that it is not ))