Extract the data of contour lines in the contour plot.


int ExtractContourData(vector& vX, vector& vY, vector<int>& vCounts, vector& vLevels)


[output] X values of the extracted contour data.
[output] Y values of the extracted contour data.
[output] the number of the extracted contour data points for each level.
[output] the value of each level.


If success, return the number of levels, otherwise return 0.



This example shows how to use this method to extract contour lines from a contour plot, and then put the data to a worksheet. Also, the function ocmath_d_polygon_area is used to calculate the area of the extracted contour lines.

// To run this example, import the data "<Origin Installation Directory>\Samples
// \Matrix Conversion and Gridding\3D XYZ.dat" first. Then set column C as Z column
// and highlight it to make a contour graph by selecting menu "Plot: Contour: XYZ Contour".
// Activate this graph.
#include <ocmath.h>  // need to include this header file for using ocmath_d_polygon_area
void DataPlot_ExtractContourData_ex1()
	GraphLayer gl = Project.ActiveLayer();  // get the active graph
		out_str("Get no graph!");
	DataPlot dp = gl.DataPlots(0);  // get the data plot
	vector vX, vY, vLevels;
	vector<int> vCounts;
	int nRet = dp.ExtractContourData(vX, vY, vCounts, vLevels);  // extract contour data of the plot
	// if success, put contour data to worksheet
		WorksheetPage wp;  // create workbook
		Worksheet wks = wp.Layers(0);  // get worksheet
			out_str("Get no worksheet!");
		// loop all levels 
		for(int iLevel=0; iLevel<vLevels.GetSize(); iLevel++)
			vector vOutputX, vOutputY;
			int nSize = vCounts[iLevel];
			// get specific level data
			if(get_specific_level(iLevel, vX, vY, vCounts, vLevels, vOutputX, vOutputY, nSize))
				// calculate area, use fabs function to avoid negative area
				double dArea = fabs(ocmath_d_polygon_area(vOutputX, vOutputY, nSize));
				// set data to worksheet
				set_specific_level_to_worksheet(wks, vOutputX, vOutputY, vLevels, iLevel, dArea);

// get data of specific level from the extracted contour data
// iLevel: int, specific level
// vInputXs, vInputYs: vector, the extracted contour data
// vCounts: vector, nubmer of points in each level
// vLevels: vector, Z values of each level
// vOutputX, vOutputY: vector&, data of the specific level
// nSize: int, number of data in the specific level
bool get_specific_level(int iLevel, vector vInputXs, vector vInputYs, vector vCounts, vector vLevels, vector& vOutputX, vector& vOutputY, int nSize)
	if(iLevel>=vLevels.GetSize() || iLevel<0)  // invalid level index, return false
		out_str("Invalid iLevel!");
		return false;
	int nBeginIndex = 0;  // begin index of the specific level in the extracted contour data
	if(iLevel != 0)  // set begin index according to the specific level
		for(int iCount=0; iCount<iLevel; iCount++)
			nBeginIndex = nBeginIndex + vCounts[iCount];
	for(int iIndex=0; iIndex<nSize; iIndex++)  // get data of the specific level
		vOutputX[iIndex] = vInputXs[nBeginIndex+iIndex];
		vOutputY[iIndex] = vInputYs[nBeginIndex+iIndex];
	return true;

// set the data of specific level to worksheet, including data points, Z values of the levels, and areas
// wks: Worksheet, data will be put to this worksheet
// vX, vY: vector, data points of specific level
// vLevels: vector, Z values of all levels
// iLevel: int, the specific level index
// dArea: double, the area of the specific level
void set_specific_level_to_worksheet(Worksheet wks, vector vX, vector vY, vector vLevels, int iLevel, double dArea)
	// add two columns for spcific level data
	int nColX = wks.AddCol();
	int nColY = wks.AddCol();
	bool bLabel = false;  // use for checking whether exist Z-Level & Area label
	Grid grid;  // grid of the worksheet
	vector<string> vsLabelNames;
	if(grid.GetUserDefinedLabelNames(vsLabelNames))  // get all user-defined label names
		for(int iName=0; iName<vsLabelNames.GetSize(); iName++)
			// if exist Z-Level or Area label
			if(0 == vsLabelNames[iName].CompareNoCase("Z-Level") || 0 == vsLabelNames[iName].CompareNoCase("Area"))
				bLabel = true;
	if(!bLabel)  // not exist Z-Level and Area label, create them
		vector<int> vnTypes = {RCLT_UDL, RCLT_UDL+1};
		vector<string> vsLabel = {"Z-Level", "Area"};
	// set data to created two columns
	XYRange xyDr;
	xyDr.Add(wks, nColX, "X");
	xyDr.Add(wks, nColY, "Y");
	xyDr.SetData(&vY, &vX);
	Column colX = wks.Columns(nColX);  // X column
	Column colY = wks.Columns(nColY);  // Y column
	colX.SetType(OKDATAOBJ_DESIGNATION_X);  // set type to X
	colX.SetExtendedLabel((string)vLevels[iLevel], RCLT_UDL);  // set Z-Level value
	colX.SetExtendedLabel((string)dArea, RCLT_UDL+1);  // set area
	colY.SetExtendedLabel((string)vLevels[iLevel], RCLT_UDL);  // set Z-Level value
	colY.SetExtendedLabel((string)dArea, RCLT_UDL+1);  // set area


See Also

Header to Include