problems with mapping lat/long coordinates to map
in
Programming Questions
•
2 years ago
Hi all!
I have a list of latitude/longitude coordinates that I'd like to plot to a US Map (the one used in Ben Fry's Visualizing book). I followed most of the steps he used in Chapter 6, including the Albers equal-area projection. Unfortunately, what i get out of it is the image below (not as squished though). I'm not sure how to fix it....thanks so much in advance! And sorry there's so much code here, I wanted to include all of it.
- Table data;
- import processing.xml.*;
- PImage img;
- float mapX1 = 0;
- float mapY1 = 0;
- float mapX2 = 640;
- float mapY2 =400;
- float[][] coords = new float[3000][2];
- float minX = 1;
- float maxX = -1;
- float minY = 1;
- float maxY = -1;
- void setup() {
- size(640,400);
- // Make a new instance of a PImage by loading an image file
- img = loadImage("map.png");
- data = new Table ("UFO-data.csv");
- for (int i=1; i<data.getRowCount(); i++) {
- float lat = data.getFloat(i,3);
- float lon = data.getFloat(i,4);
- // Albers equal-area conic projection.
- // USGS uses standard parallels at 45.5°N and 29.5°N
- // with a central meridian value of 96°W.
- // Latitude value is phi, longitude is lambda.
- float phi0 = 0;
- float lambda0 = radians(-96);
- float phi1 = radians(29.5f);
- float phi2 = radians(45.5f);
- float phi = radians(lat);
- float lambda = radians(lon);
- float n = 0.5f * (sin(phi1) + sin(phi2));
- float theta = n * (lambda - lambda0); //radians(lon - lambda0);
- float c = sq(cos(phi1)) + 2*n*sin(phi1);
- float rho = sqrt(c - 2*n*sin(phi)) / n;
- float rho0 = sqrt(c - 2*n*sin(phi0)) / n;
- float x = rho * sin(theta);
- float y = rho0 - rho*cos(theta);
- //store updated values in coordinates array
- coords[i][0] = x;
- coords[i][1] = y;
- print(" x is--"+x +" y is--"+y+"\n");
- if (x > maxX) maxX = x;
- if (x < minX) minX = x;
- if (y > maxY) maxY = y;
- if (y < minY) minY = y;
- }
- }
- public void draw() {
- background(0);
- // Draw the background image to the screen at coordinate (0,0)
- image(img,0,0);
- for (int j=1; j<data.getRowCount(); j++) {
- int xx = (int) TX(coords[j][0]);
- int yy = (int) TY(coords[j][1]);
- set(xx,yy,#008000);
- }
- }
- float TX(float x) {
- return map(x, minX, maxX, mapX1, mapX2);
- }
- float TY(float y) {
- return map(y, minY, maxY, mapY2, mapY1);
- }
1