1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| from qgis.core import *
from processing.tools.vector import VectorWriter
Input_Table = 'path_to_the_csv/input.csv' # set the filepath for the input CSV
lon_field = 'point_longitude' # set the name for the field containing the longitude
lat_field = 'point_latitude' # set the name for the field containing the latitude
crs = 4326 # set the crs as needed
Output_Layer = 'path_to_the_output/output.shp' # set the filepath for the output shapefile
spatRef = QgsCoordinateReferenceSystem(crs, QgsCoordinateReferenceSystem.EpsgCrsId)
inp_tab = QgsVectorLayer(Input_Table, 'Input_Table', 'ogr')
prov = inp_tab.dataProvider()
fields = inp_tab.pendingFields()
outLayer = QgsVectorFileWriter(Output_Layer, None, fields, QGis.WKBPoint, spatRef)
pt = QgsPoint()
outFeature = QgsFeature()
for feat in inp_tab.getFeatures():
attrs = feat.attributes()
pt.setX(float(feat[lon_field]))
pt.setY(float(feat[lat_field]))
outFeature.setAttributes(attrs)
outFeature.setGeometry(QgsGeometry.fromPoint(pt))
outLayer.addFeature(outFeature)
del outLayer |
Partager