/*==================================================== Creates a KML file showing pie charts of country names Author: Martin Davis Last Update: Nov 28 2008 ====================================================*/ ShapefileReader t file: "geocommons-world.shp"; //------- Extract largest polygon for each country tpoly1 = select String.trim(NAME) name, AREA, splitValue poly, Geom.area(splitValue) polyarea from t split by Geom.splitByMembers(GEOMETRY) order by polyarea desc; tpoly2 = select name, first(AREA) countryArea, first(poly) geom from tpoly1 group by name; //-------- duplicate each record, with a flag indicating which orthographic category tpoly = select *, splitValue statType from tpoly2 split by SplitBy.index(2); //------------------------------------------ // create pie chart slices at centroid of polygon // Pie radius is determined by interior point/boundary distance //------------------------------------------ tpie = select name, statType, nameLen, countryArea, vowelLen, consLen, Geom.createEllipticalArcPolygon(inPt, startAng, angSize, width, height, 40) pieSliceGeom with { nameLen = String.length(name); nameLow = String.toLowerCase(name); vowels = String.replaceAll(nameLow, "[^aeiou]", ""); consonants = String.replaceAll(nameLow, "[^bcdfghjklmnpqrstvwxyz]", ""); vowelLen = String.length(vowels); consLen = String.length(consonants); ang = Math.pi() / (vowelLen + consLen); splitAng = vowelLen * ang; inPt = Geom.interiorPoint(geom); radius = Geom.distance(inPt, Geom.boundary(geom)); width = 2 * radius; //--- decrease height to match decreasing size of degrees of longitude deflectionRad = Math.toRadians(Math.abs(Geom.y(inPt))); height = 2 * radius * Math.cos(deflectionRad); startAng = statType == 0 ? 0.0 : splitAng; angSize = statType == 0 ? splitAng : 2 * Math.pi() - splitAng; } from tpoly; //ShapefileWriter tcirc file: "namePie.shp"; //------------------------------------------ // Style and output KML // Styling is provided directly in the feature records, since it is relatively simple //------------------------------------------ tkml = select name as kmlname, name + " (" + vowelLen + " vowels, " + consLen + " consonants)" as kmlDescription, statType == 0 ? "style_vowel" : "style_cons" kmlStyleUrl, statType == 0 ? "Vowels" : "Consonants" kmlFolderId, nameLen * 50000 kmlAltitude, 0 polyStyleOutline, statType == 0 ? "bfffff00" : "bf0000ff" polyStyleColor, pieSliceGeom from tpie order by kmlFolderId; KMLWriter tkml comment: "Pie Charts of Country Name orthographic statistics" name: "Country Name Pie Charts" extrude: 1 altitudeMode: "relativeToGround" file: "namePie.kmz";