Loading...
Logo
Processing Forum

CSV Files read from specific URL

in General Discussion  •  Other  •  6 months ago  
I am trying to make a data visualization that displays earthquake data around the world in real-time. Do do this I need the application to reach back to a URL that is a CSV file that gets updated every minute. I have done a bit of looking and digging around but I am not having much luck. How can I tell processing to reach back to read the online CSV file?

Replies(4)

Thanks for the speedy reply, I can't believe I didn't find that at all. Does the String command get put into the void Draw or elsewhere?
I guess you can create a separate thread() to keep checking on that remote CVS file's URL like 1 minute or so.
Just use delay(60 * 1000); to pause 1 minute for each updating when you make yours!  
Copy code
    /**
     http://forum.processing.org/topic/threading-background-threads-inherently-slower-than-animation-thread
     */
    
    PGraphics pg;
    boolean isThreading;
    
    final static String ENGINE = P2D; // JAVA2D, P2D, P3D, OPENGL
    final static byte   FONT = 050;
    final static short  FPS  = 0300;
    
    void setup() {
      size(0300, 0200, ENGINE);
      frameRate(FPS);
      noSmooth();
      textSize(FONT);
      fill(#00FFFF);
    
      pg = createGraphics(width, height, ENGINE);
    
      pg.beginDraw();
      pg.noSmooth();
      pg.textSize(FONT);
      pg.fill(#FFFF00);
      pg.endDraw();
    
      mousePressed();
    }
    
    void draw() {
      background(0);
      text(frameCount, 010, 050);
      image(pg, 0, 0);
    }
    
    void myThread() {
      int count = 0;
    
      while (isThreading) {
        pg.background(0, 0);
        pg.text(++count, 010, 0150);
        delay(1000);
      }
    }
    
    void mousePressed() {
      if (isThreading = !isThreading)  thread("myThread");
    }
    
    void keyPressed() {
      mousePressed();
    }
    

Thank you for the reference and the idea there. The trick for me is that I need to have animations running while we wait 1 minute to retrieve the data. Here is another timer version that may allow this to happen, but I also assume that there are lines in the code you gave above I may need, in specific the ones regarding PGraphics. I read through the forum you linked where that code came from and people seem to have issues. Anyway, here is a sample timer I am trying to look at. I just don't know where to plug the time in, and whether or not I need to throw this entire thing into its own special void, or where to put the loadStrings for the CSV file. Actually, I will just send you my entire code so far, if it isnt too much trouble.

Copy code
  1. PImage backgroundMap;

  2. float mapGeoLeft = -180;
  3. float mapGeoRight = 180;
  4. float mapGeoTop = 80;
  5. float mapGeoBottom = -80;

  6. float mapScreenWidth,mapScreenHeight;
  7. ********
  8. void setup()
  9. {
  10.   size(1680, 1050);
  11.   backgroundMap = loadImage("Map.jpg");
  12.   mapScreenWidth = width;
  13.   mapScreenHeight = height;
  14.   
  15.   String lines[] = loadStrings("http://earthquake.usgs.gov/earthquakes/feed/v0.1/summary/1.0_week.csv");
  16.   println("there are this many lines:" + lines.length);
  17.   for (int i = 0; i < lines.length; i++) {
  18.     println(lines[i]);
  19.   }
  20. }
  21. ********
  22. void draw()
  23. {
  24.   image(backgroundMap,0,0,mapScreenWidth,mapScreenHeight);
  25.   
  26.   fill(0,205,0);
  27.   
  28.   PVector p = geoToPixel(new PVector(-116.057, 32.828));
  29.   ellipse(p.x,p.y,50,50);
  30. }
  31. *********
  32. public PVector geoToPixel(PVector geoLocation)
  33. {
  34.   return new PVector(mapScreenWidth*(geoLocation.x-mapGeoLeft)/
  35.                     (mapGeoRight-mapGeoLeft), mapScreenHeight -
  36.                     mapScreenHeight*(geoLocation.y-mapGeoBottom)
  37.                     /(mapGeoTop-mapGeoBottom));
  38. }
  39. *********
  40. class Timer
  41. {
  42.   int savedTime;
  43.   boolean running = false;
  44.   int totalTime;
  45.   
  46.   Timer(int tempTotalTime) {
  47.     totalTime = tempTotalTime;
  48.   }
  49.   
  50.   void start() {
  51.     running = true;
  52.     savedTime = millis();
  53.   }
  54.   
  55.   boolean isFinished()
  56.   {
  57.     int passedTime = millis() - savedTime;
  58.     if (running && passedTime > totalTime) {
  59.       running = false;
  60.       return true;
  61.     } else {
  62.       return false;
  63.     }
  64.   }
  65.   
  66. }