We have 100's of dgn files. We have successfully "assigned" coordinate system to those dgn files in batch by using "on the fly" + custom CRS. Now we have to save each dgn's geometry (Point, Linestring, Polygon) in WGS84. Manually we can do that. Right Click -> Save Us -> Projection: WGS84 & add suffix "Point, Lines, Areas" to each layer. But this method will take a lot of time to do that. What i would like to do is
1) Based on the File name, the layers has to be batch saved to a respective folder. I have read this and tried. How to batch "Layer save as"-process in QGIS? But it takes the layer name (here: elementsPoints, elementsLineString) instead of the original file name (042641hd). I would like to have 042641hd_Points, 042641hd_Lines etc..
2) While doing so, how to save all the files in WGS84 ?
This code works perfectly. Reading the file name, not the layer name.
from qgis.core import *
import os
pathToFile = "D:\\Dummy\\"
trs = QgsCoordinateReferenceSystem()
trs.createFromId(4326)
layers = iface.legendInterface().layers()
for layer in layers:
dgn_pn = layer.dataProvider().dataSourceUri().split('|')[0]
dgn_fn = os.path.basename(dgn_pn)
dgn_fn_wo_ext = os.path.splitext(dgn_fn)[0]
geom_name = {0: 'Points', 1: 'Lines', 2: 'Polygons'}
geom_type = layer.geometryType()
newName = dgn_fn_wo_ext + '_' + geom_name[geom_type]
ret = QgsVectorFileWriter.writeAsVectorFormat(layer,pathToFile + newName,'utf-8',trs,'ESRI Shapefile')
if ret == QgsVectorFileWriter.NoError:
print newName + " saved to " + pathToFile + "!"