2

I am trying to interpolate lines using Interpolation plugin in QGIS. There is an option for exporting triangulation file. I don't want to create raster interpolation. In this case, I want only triangulation file. My code:

from qgis.core import QgsRasterLayer, QgsVectorLayer, edit, QgsFeatureRequest, QgsMapLayerRegistry
import qgis.analysis


resolution = 0.1
q5_edited = QgsVectorLayer("C:\\Users\\testuser\\Documents\\exp_q5_23.shp", "CONTOUR", "ogr")
q20_edited = QgsVectorLayer("C:\\Users\\testuser\\Documents\\exp_q20_23.shp", "CONTOUR", "ogr")

q5_index = q5_edited.fieldNameIndex('intpol')
q20_index = q20_edited.fieldNameIndex('intpol')

ld_q20 = qgis.analysis.QgsInterpolator.LayerData()
ld_q5 = qgis.analysis.QgsInterpolator.LayerData()

ld_q20.vectorLayer = q20_edited
ld_q20.zCoordInterpolation=False
ld_q20.InterpolationAttribute = q20_index
ld_q20.mInputType = 1 #1 = structureLines, 2 = breakLines

ld_q5.vectorLayer = q5_edited
ld_q5.zCoordInterpolation = False
ld_q5.interpolationAttribute = q5_index
ld_q5.mInputType = 1

itp = qgis.analysis.QgsTINInterpolator([ld_q20, ld_q5])
itp.setExportTriangulationToFile(True)
itp.setTriangulationFilePath('C:\\Users\\testuser\\Documents\\dualedge.shp')

rect = q20_edited.extent()
ncol = int((rect.xMaximum() - rect.xMinimum())/resolution)
nrow = int((rect.yMaximum() - rect.yMinimum())/resolution)

test = qgis.analysis.QgsGridFileWriter(itp,"C:\\Users\\testuser\\Documents\\dualedge_raster.tif",rect,ncol,nrow,resolution,resolution)
test.writeFile(True)

I set setExportTriangulationToFile to True and set the setTriangulationFilePath to path where I want to create triangulation file. But the triangulation file is created only when the last line is run test.writeFile(True). This command creates triangulation file (.shp) and raster file (.tif). I checked the code of writeFile function (https://qgis.org/api/qgsgridfilewriter_8cpp_source.html) but I can't find the place where triangulation file is created I found only the writing to a raster file. I hoped there will be some function which creates triangulation file before creating the raster file.

How the triangulation file is created and is it possible to create triangulation file without creating raster? I need it because we interpolate a very big vector data which makes raster really big and we don't need a raster, just a triangulation.

1

I found it, it is a function interpolatePoint()(code). This function is called and then in initialized is created triangulation:

if ( mExportTriangulationToFile )
{
  dualEdgeTriangulation->saveAsShapefile( mTriangulationFilePath );
}

This how you can get only triangulation file from Interpolation Qgis plugin:

from qgis.analysis import QgsInterpolator, QgsTINInterpolator
from qgis.core import QgsVectorLayer
from paths import *

resolution = 100
q5_edited = QgsVectorLayer(path_q5_layer, "CONTOUR", "ogr")
q20_edited = QgsVectorLayer(path_q20_layer, "CONTOUR", "ogr")

q5_index = q5_edited.fieldNameIndex('intpol')
q20_index = q20_edited.fieldNameIndex('intpol')

ld_q20 = QgsInterpolator.LayerData()
ld_q5 = QgsInterpolator.LayerData()

ld_q20.vectorLayer = q20_edited
ld_q20.zCoordInterpolation=False
ld_q20.InterpolationAttribute = q20_index
ld_q20.mInputType = 1 #1 = structureLines, 2 = breakLines

ld_q5.vectorLayer = q5_edited
ld_q5.zCoordInterpolation = False
ld_q5.interpolationAttribute = q5_index
ld_q5.mInputType = 1

itp = QgsTINInterpolator([ld_q20, ld_q5])
itp.setExportTriangulationToFile(True)

#path for TIN, not clipped
itp.setTriangulationFilePath(path_tin)

rect = q20_edited.extent()
ncol = int((rect.xMaximum() - rect.xMinimum())/resolution)
nrow = int((rect.yMaximum() - rect.yMinimum())/resolution)

# this is what you need only for dualedge triangulation file
currentYValue = rect.yMaximum() - resolution / 2.0
currentXValue = rect.xMinimum() + resolution / 2.0
itp.interpolatePoint(currentXValue, currentYValue)

The last three rows call is needed for triangulation file without writing interpolation raster. It generates only triangulation file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.