Turning a map into a globe.
in
Core Library Questions
•
1 years ago
I've been working with plotting KML files (essentially an XML format for maps) recently. The long and short of my issue is that I've been able to get the map to work well, but the globes render with mangled geometry and big holes in the continents. I am using the same data, and parsing code, but the transition to 3D introduces rendering issues I can't pin down.
If anyone could have a look at this code and lend a hand, I would be extremely grateful.
Archive with data posted here:
http://feltron.com/downloads/XML_Globe_112011d.zip
Processing code below.
- // LIBRARIES
- import processing.opengl.*;
- // GLOBAL VARIABLES
- String source = "110m_land.kml";
- String path = "Document/Folder/Placemark/Polygon/outerBoundaryIs/LinearRing/coordinates";
- float sphereRadius = 200;
- XMLElement kml;
- XMLElement[] polygons;
- String[] polygon;
- MapShape myMapShape[];
- int n = 1;
- // SETUP
- void setup() {
- size(1200, 600, OPENGL);
- background(0);
- lights();
- smooth();
- noStroke();
- parseKML();
- // Instantiate MapShape Array
- myMapShape = new MapShape[polygons.length];
- for (int i = 0; i<polygons.length; i++) {
- myMapShape[i] = new MapShape(polygon[i]);
- int n = i+1;
- println("polygon " + n + " of " + polygon.length);
- }
- }
- // DRAW LOOP
- void draw() {
- background(0);
- fill(102);
- for (int i = 0; i<polygons.length; i++) {
- myMapShape[i].display();
- }
- n++;
- println(n);
- }
- // KML PARSING FUNCTION
- void parseKML() {
- kml = new XMLElement(this, source);
- polygons = kml.getChildren(path);
- polygon = new String[polygons.length];
- for (int i = 0; i<polygons.length; i++) {
- polygon[i] = polygons[i].getContent();
- }
- }
- // MAPSHAPE CLASS
- class MapShape {
- String PolyString;
- String[][] PolyLatLong;
- // The Constructor is defined with arguments.
- MapShape(String TempPolyString) {
- PolyString = TempPolyString;
- String[] PolyStringClean = split(PolyString, ' ');
- PolyLatLong = new String[PolyStringClean.length][2];
- for (int i=0; i<PolyStringClean.length; i++) {
- String[] PolyStringCleanSplit = split(PolyStringClean[i], ',');
- PolyLatLong[i][0] = PolyStringCleanSplit[0];
- PolyLatLong[i][1] = PolyStringCleanSplit[1];
- // println(PolyLatLong[i][0] + ", " + PolyLatLong[i][1]);
- }
- }
- // 3D Render the Object
- void display() {
- pushMatrix();
- translate(width/2, height/2);
- rotateY(radians(n));
- rotateX(HALF_PI);
- beginShape();
- for (int i=0; i < PolyLatLong.length; i++) {
- float Ypos = sphereRadius * cos(radians(float(PolyLatLong[i][1]))) * cos(radians(float(PolyLatLong[i][0])));
- float Xpos = sphereRadius * cos(radians(float(PolyLatLong[i][1]))) * sin(radians(float(PolyLatLong[i][0])));
- float Zpos = sphereRadius * sin(radians(float(PolyLatLong[i][1])));
- vertex(Xpos, Ypos, Zpos);
- }
- endShape(CLOSE);
- popMatrix();
- }
- /*
- // 2D Render the Object
- void display() {
- beginShape();
- for (int i=0; i < PolyLatLong.length; i++) {
- vertex(map(float(PolyLatLong[i][0]), -180, 180, 0, width), map(float(PolyLatLong[i][1]), 90, -90, 0, height));
- }
- endShape(CLOSE);
- }
- */
- }
1