conLabQry.qry

  Download

More scripts: Style By Script

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# ConLablQry
# This is a sample script to draw contour lines and
# label major contours with their elevation.  Labels
# are placed to avoid highly curved parts of lines, and
# to appear upright relative to the bottom of the map.
# Script from Using CartoScripts tutorial.
################### Set Parameters #########################
# Read elevation values from database table and assign to
# variable.  Use modulo operator (%) to identify major contours
# (200 foot interval) and minor contours (40 foot interval).
elev = Elevations.Elevation_Feet;
rem = elev % 200;						# rem = 0 for major contours
# red, green, blue variables define the color of the lines and labels
red = 170;	green = 85;		blue = 0;
# This variable defines the denominator of the intended map scale.
# It is used as the basis for defining line width and symbol size 
# and spacing.
# Example: for 1:24,000 map scale, Scale = 24000
scale = 5000;
# These variables control the width of the contour lines.
# widthMap is the desired map width of minor contour lines in mm, 
# assuming vector coordinates are in meters.  Major contour lines
# are drawn wider than minor contours.
widthMap = 0.35;
width = widthMap * scale / 1000;
widthBold = width * 3;
# Number of labels per line that has average length.
# Average line length should be expressed in object coordinates.
averageLineLength = 2000;
labelPerLine = 2;
minDistBetweenLabels = averageLineLength / labelPerLine;
# These variables control the size and positioning of labels.
# heightMap is the desired height of labels in mm at the intended
# map scale, assuming vector coordinates are in meters.
heightMap = 5;		labelSize = heightMap * scale / 1000;
halfSize = labelSize * 0.5;
# Set label font
LineStyleSetFont("ARIALBD.TTF");
######################## Process ###########################
# Set line and text color
LineStyleSetColor(red, green, blue);
LineStyleSetTextColor(red, green, blue);
if (rem <> 0) {						# draw minor contours
	LineStyleSetLineWidth(width);
	LineStyleDrawLine();
	}
else {									# draw and label major contours
	LineStyleSetLineWidth(widthBold);
	str$ = sprintf("%d", elev);		# read elevation to string variable
	# find length of contour label
	LineStyleTextNextPosition(str$,labelSize,0,1,nextx,nexty,length);
	begShift = 5 * length;			# offset from beginning of line
	stopLength = begShift;		# min label distance from end of line
	spLength = 1.5 * length;			# label length plus spaces
	LineStyleRollPen(begShift);		# draw beginning of contour line
	while ((LineStyleGetMaxDistance(spLength,drawAngle,maxDist)) <> 1) {
		LineStyleGetDirection(0.1 * labelSize,minAngle,maxAngle);
		# find change in line direction over length of label 
		devAngle = drawAngle - minAngle;
		# find distance to end of line and compare to stopLength
		remLength = LineStyleGetDistanceTo(3);
		if (remLength < stopLength) break;
		# check deviation distance and angle of line element
		if (maxDist < 1.5 * labelSize and abs(devAngle) < 15) {
			LineStyleRoll(0.25 * length);		# space before drawing label
			# Check if label needs to be inverted to be readable
			isInverse = 0;
			if (abs(drawAngle) > 90) isInverse = 1;
			if (isInverse) {
				LineStyleMoveTo(devAngle, length);
				LineStyleMoveTo(devAngle + 90, halfSize);
				LineStyleDrawText(str$,labelSize,drawAngle+180,1);
				}
			else {
				LineStyleMoveTo(-90,halfSize);
				LineStyleDrawText(str$,labelSize,drawAngle,1);
				}
			LineStyleRoll(1.2 * length);
			LineStyleMoveTo(0,0);
			LineStyleSetLineWidth(widthBold);
			LineStyleRollPen(minDistBetweenLabels);
			}
		else LineStyleRollPen(length);
		}
	# Draw remainder of line
	LineStyleRollPen(remLength);
	}