Програмний модуль написаний для автоматизації процесу створення КML файлів. Всі об'єкти з обраного шару будуть перетворені в окремі KML файли і збереженні в папці "C:/kml_out". (буде створена автоматично)
Блок для QGIS 2
from qgis.core import *
from qgis.utils import iface
from qgis.analysis import QgsGeometryAnalyzer
from PyQt4.QtGui import QMessageBox
from PyQt4.QtCore import QVariant
import os
#Author: Ursuliak P.P. upp8888@gmail.com
#Date: 2019-06-13
# ПРОГРАМА РОЗБИВАЄ АКТИВНИЙ ШАР НА ОКРЕМІ KML файли
dest_crs = QgsCoordinateReferenceSystem(4326)
print "Program will save object of selected layer to separete *.kml files"
# create forlder for saving
if not os.path.exists('C:/kml_out'): # if directory exists
os.makedirs(os.path.join('C:/', 'kml_out'))
#pole = QgsVectorLayer(r'd:\Dropbox\work2019\a20190614_emft\t\temp.shp',"polia","ogr",)
#pole.isValid()
#selection = pole.getFeatures(QgsFeatureRequest().setFilterExpression(u'"ZIP"=10002'))
#QgsMapLayerRegistry.instance().addMapLayers([pole])
#select active layer
pole = iface.activeLayer()
if pole.fieldNameIndex("Name") ==-1: #check if field name doesn't exist in layer,
vpr = pole.dataProvider()
fields = vpr.fields()
vpr.addAttributes([QgsField("Name", QVariant.String)]) #add field to layer
pole.updateFields()
iface.showAttributeTable(iface.activeLayer())
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText("Fill column Name with data! \n \n Filenames error! \n File name will be features ID's ")
msg.show()
fts = pole.getFeatures() #
i = 0
for x in fts:
i = i+1
print x.id(), x.attributes()[pole.fieldNameIndex("Name")]
pole.setSelectedFeatures([x.id()])
iface.mapCanvas().zoomToSelected()
if pole.fieldNameIndex("Name") ==-1:
name = r'C:\kml_out\%s.kml' % x.id()
else:
name = r'C:\kml_out\%s.kml' % x.attributes()[pole.fieldNameIndex("Name")]
QgsVectorFileWriter.writeAsVectorFormat(pole,name, "utf-8", dest_crs, "KML", True)
print "%s file(s) was saved C:\kml_out\ \n \n send 'Thanks' to upp8888@gmail.com " % i
msgFin = QMessageBox()
msgFin.setText("%s file(s) was saved C:\kml_out\ \n \n send 'Thanks' to upp8888@gmail.com " % i)
msgFin.setIcon(QMessageBox.Information)
msgFin.show()
Код для QGIS 3
from qgis.core import *
from qgis.utils import iface
#from PyQt4.QtGui import QMessageBox
#from PyQt4.QtCore import QVariant
import os
#Author: Ursuliak P.P. upp8888@gmail.com
#Date: 2019-06-13
dest_crs = QgsCoordinateReferenceSystem(4326)
print( "Program will save object of selected layer to separete *.kml files")
# create forlder for saving
if not os.path.exists('C:/kml_out'): # if directory exists
os.makedirs(os.path.join('C:/', 'kml_out'))
#pole = QgsVectorLayer(r'd:\Dropbox\work2019\a20190614_emft\t\temp.shp',"polia","ogr",)
#pole.isValid()
#selection = pole.getFeatures(QgsFeatureRequest().setFilterExpression(u'"ZIP"=10002'))
#QgsMapLayerRegistry.instance().addMapLayers([pole])
#select active layer
pole = iface.activeLayer()
if pole.fields().indexFromName("Name") ==-1: #check if field name doesn't exist in layer,
vpr = pole.dataProvider()
fields = vpr.fields()
vpr.addAttributes([QgsField("Name", QVariant.String)]) #add field to layer
pole.updateFields()
iface.showAttributeTable(iface.activeLayer())
#msg = QMessageBox()
#msg.setIcon(QMessageBox.Warning)
print("Fill column Name with data! \n \n Filenames error! \n File name will be features ID's \n ")
#msg.show()
fts = pole.getFeatures() #
i = 0
for x in fts:
i = i+1
print( x.id(), x.attributes()[pole.fields().indexFromName("Name")])
pole.selectByIds([x.id()])
iface.mapCanvas().zoomToSelected()
if pole.fields().indexFromName("Name") ==-1:
name = r'C:\kml_out\%s.kml' % x.id()
elif x.attributes()[pole.fields().indexFromName("Name")] == NULL:
name = r'C:\kml_out\%s.kml' % x.id()
else:
name = r'C:\kml_out\%s.kml' % x.attributes()[pole.fields().indexFromName("Name")]
QgsVectorFileWriter.writeAsVectorFormat(pole,name, "utf-8", dest_crs, "KML", True)
if x.attributes()[pole.fields().indexFromName("Name")] == NULL:
print(u"\n Заповни колонку Name данними! \n \nFilenames error! \n \n Файли будуть названі порядковими номерами \n File name will be features ID's \n ")
print( "%s file(s) was saved C:\kml_out\ \n \n send 'Thanks' to upp8888@gmail.com " % i)
pass