rcelltovpointgeoref.sml

  Download

More scripts: Convert

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# SML created 10 Jan 2003 by Dan Glasser and Toshi Waza
# For each raster cell in the input raster,
# it creates a point in the output (3D) vector.
# The raster cell value is stored as the z-value
# The vector is then georeferenced to match the raster.
# Updated 15 April 2014 by Brett Colombe
clear();
class RASTER R;
GetInputRaster(R);
class GEOREF geoOrg = GetLastUsedGeorefObject(R);
if (!geoOrg) {
	PopupMessage("This raster does not have a default georeference");
	Exit();
}
class REGION2D extents = GetObjectExtentsRegion(R, geoOrg);
# Get the 3D output vector that we will add the points to
class VECTOR V;
GetOutputVector(V, "VectorToolkit, 3DVector");
# Loop through each cell in the raster, 
# getting the cell value and adding points.
numeric row, col;
foreach R[row, col]
{
	# Get the cell value
	numeric cellVal = R[row, col];
	#print(NumToStr(col) + " " + NumToStr(-row) + " " + NumToStr(cellVal));
	if (!IsNull(cellVal)) {
		VectorAddPoint(V, col, -row, cellVal);
		}
}
numeric cols=NumCols(R);
numeric lins=NumLins(R);
numeric xUL, yUL, xUR, yUR, xLL, yLL, xLR, yLR;
ObjectToMap(R, 0.5, 0.5, geoOrg, xUL, yUL);
ObjectToMap(R, cols+0.5, 0.5, geoOrg, xUR, yUR);
ObjectToMap(R, 0.5, lins+0.5, geoOrg, xLL, yLL);
ObjectToMap(R, cols+0.5, lins+0.5, geoOrg, xLR, yLR);
# Set number of control points
numeric numpoints = 4;
array numeric xsrc[numpoints], ysrc[numpoints], zsrc[numpoints];
array numeric xdest[numpoints], ydest[numpoints], zdest[numpoints];
# Control point 1 - upper left corner
xsrc[1] = 1;
ysrc[1] = -1;
zsrc[1] = 0;
xdest[1] = xUL;
ydest[1] = yUL;
zdest[1] = 0;
# Control point 2 - lower right corner
xsrc[2] = cols;
ysrc[2] = -lins;
zsrc[2] = 0;
xdest[2] = xLR;
ydest[2] = yLR;
zdest[2] = 0;
# Control point 3 - upper right corner
xsrc[3] = cols;
ysrc[3] = -1;
zsrc[3] = 0;
xdest[3] = xUR;
ydest[3] = yUR;
zdest[3] = 0;
# Control point 4 - lower left corner
xsrc[4] = 1;
ysrc[4] = -lins;
zsrc[4] = 0;
xdest[4] = xLL;
ydest[4] = yLL;
zdest[4] = 0;
# Now create the georef without dialog
class GEOREF geoNew = CreateControlPointGeorefFromGeoref(V, geoOrg, numpoints, xsrc, ysrc, zsrc, xdest, ydest, zdest);
GeorefFree(geoOrg);
GeorefFree(geoNew);
CloseVector(V);
CloseRaster(R);