accessing GIS data from Processing

Nikolaus Gradwohl2009-05-03T19:50:00+00:00

I wrote a processing sketch to show how to access a wms service for showing a map. i use geoserver as a wms provider and the map shown in the screenshots is from http://thematicmapping.org/.

the map can be dragged with the mouse and zoomed with the scroll wheel

worldmap worldmap zoomed

code:

PImage map;

float minlon = -180;
float minlat = -90;
float maxlon = 180;
float maxlat = 90;

void setup() {
  addMouseWheelListener(new java.awt.event.MouseWheelListener() { 
    public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) { 
      mouseWheel(evt.getWheelRotation());
  }}); 

  size(800,400);
  map = getWMS( "http://localhost:8080/geoserver/wms", 
    "topp:TM_WORLD_BORDERS-0", minlon, minlat, maxlon, maxlat, width, height );
}

void draw() {
  image( map, 0,0,width, height );
}

float startx;
float starty;

void mousePressed() {
  startx = mouseX;
  starty = mouseY;
}

void mouseReleased() {

  float aX = map( startx, 0, width, minlon, maxlon );
  float aY = map( height - starty, 0, height, minlat, maxlat );

  float bX = map( mouseX, 0, width, minlon, maxlon );
  float bY = map( height - mouseY, 0, height, minlat, maxlat );


  float dlon = maxlon - minlon;
  float dlat = maxlat - minlat;

  minlon = minlon + aX - bX;
  maxlon = maxlon + aX - bX;
  minlat = minlat + aY - bY;
  maxlat = maxlat + aY - bY;

  map = getWMS( "http://localhost:8080/geoserver/wms", 
    "topp:TM_WORLD_BORDERS-0", minlon, minlat, maxlon, maxlat, width, height );          
}

void mouseWheel(int delta) {
  float dlon = (maxlon - minlon)/2;
  float dlat = (maxlat - minlat)/2;

  float centerX = minlon + dlon;
  float centerY = minlat + dlat;

  if ( delta > 0 ) {
    minlon = centerX - dlon*1.25;
    maxlon = centerX + dlon*1.25;
    minlat = centerY - dlat*1.25;
    maxlat = centerY + dlat*1.25;
  } else { 
    minlon = centerX - dlon/1.25;
    maxlon = centerX + dlon/1.25;
    minlat = centerY - dlat/1.25;
    maxlat = centerY + dlat/1.25;

  }
  map = getWMS( "http://localhost:8080/geoserver/wms", 
    "topp:TM_WORLD_BORDERS-0", minlon, minlat, maxlon, maxlat, width, height );          
}

PImage getWMS( String url, String layer, float minlon, float minlat, float maxlon, float maxlat, int width, int height ) {
  PImage res = loadImage( url + "?request=GetMap&layers=" + layer 
    + "&bbox=" + minlon + "," + minlat + "," + maxlon + "," + maxlat  
    + "&format=image/png&width="+width+"&height="+height, "png");
  return res;
}
Tweet This! submit to reddit Digg! Tags: | 8 comments | 1 trackbacks

See also:

projecting GIS Data on a sphere using Processing
sketch experiment 7 - osc events
nested cubes in processing
processing phaseflower
Time Perception

Trackbacks

  1. http://superdrupermegapuper54321.com/ 2009-07-21T14:50:53+00:00
    Very usefull info. Thanks!

Comments

Leave a response

  1. David 2009-07-14T06:55:33+00:00

    This is exactly what I've been trying to figure out, and seems much more sophisticated than the mucking around with SVGs that I've been doing so far.

    However, I get the following error:

    The file http://localhost:8080/geoserver/wms?request=GetMap&layers=topp:TM_WORLD_BORDERS-0&bbox=-180.0,-90.0,180.0,90.0&format=image/png&width=800&height=400 contains bad image data, or may not be an image.

    I have geoserver installed and running, and from what I can tell, it should be working happily on localhost:8080...

    Any thoughts?

  2. devrim baris acar 2009-07-22T16:03:03+00:00

    It seems that the geoserver does not have the topp:TMWORLDBORDERS-0 layer as map data, try this instead, or find the map data geoserver has and replace it,

    tiger:poly_landmarks,tiger:tiger_roads

    Best regards,

  3. Juanma SarriĆ³ 2009-11-26T16:16:08+00:00

    Hello...I'm testing it for my processing (vista)...no works!!!:(

    If necesary import processing.net.*; ??? o this sketch is for Eclipse???

    I'me newby!!!!

    Thanks!!!

    java.net.SocketException: Network is unreachable: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:525) at java.net.Socket.connect(Socket.java:475) at sun.net.NetworkClient.doConnect(NetworkClient.java:163) at sun.net.www.http.HttpClient.openServer(HttpClient.java:394) at sun.net.www.http.HttpClient.openServer(HttpClient.java:529) at sun.net.www.http.HttpClient.(HttpClient.java:233) at sun.net.www.http.HttpClient.New(HttpClient.java:306) at sun.net.www.http.HttpClient.New(HttpClient.java:323) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801) at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049) at java.net.URL.openStream(URL.java:1010) at processing.core.PApplet.createInputRaw(PApplet.java:4001) at processing.core.PApplet.createInput(PApplet.java:3970) at processing.core.PApplet.loadBytes(PApplet.java:4144) at processing.core.PApplet.loadImage(PApplet.java:3219) at sketch_nov26b.getWMS(sketch_nov26b.java:92) at sketch_nov26b.setup(sketch_nov26b.java:32) at processing.core.PApplet.handleDraw(PApplet.java:1402) at processing.core.PApplet.run(PApplet.java:1327) at java.lang.Thread.run(Thread.java:619) The file "http://localhost:8080/geoserver/wms?request=GetMap&layers=topp:TM_WORLD_BORDERS-0&bbox=-180.0,-90.0,180.0,90.0&format=image/png&width=800&height=400" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable. Exception in thread "Animation Thread" java.lang.NullPointerException at processing.core.PGraphics.image(PGraphics.java:2197) at processing.core.PApplet.image(PApplet.java:7208) at sketch_nov26b.draw(sketch_nov26b.java:36) at processing.core.PApplet.handleDraw(PApplet.java:1425) at processing.core.PApplet.run(PApplet.java:1327) at java.lang.Thread.run(Thread.java:619)

  4. Nikolaus Gradwohl 2009-11-26T17:23:04+00:00

    it looks like you have no geoserver running, make sure it is started and change the url in getWMS if you run it on another server or port

  5. JayB 2011-02-12T01:31:54+00:00

    Very neat! I'm going to attempt to link processing=>geoserver=>postgresql/postgis=>flickr/twitter API. We'll see how it works. Thanks for giving my brain a little direction!

  6. alex 2011-10-12T21:42:09+00:00

    Thank you for releasing this great code. I tried it and got this error:

    The file http://localhost:8080/geoserver/wms?request=GetMap&layers=topp:TM_WORLD_BORDERS-0&bbox=-180.0,-90.0,180.0,90.0&format=image/png&width=800&height=400 contains bad image data, or may not be an image.

    I have the geoserver running. Any idea what this means? Many thanks...

  7. Alex 2011-10-12T21:46:08+00:00

    I see David in no 1 above had the same mistake. But I don't understand what to do to get the topp:TMWORLDBORDERS-0 as the map layer....

  8. NEX-5 2011-11-16T15:30:21+00:00

    Vielen Dank für die sinnvolle Kritik. Me & my Nachbarn waren nur Vorbereitung, einige der Forschung zu unternehmen. Wir haben ein Grab ein Buch aus unserer Bibliothek vor Ort, aber ich glaube, ich lernte mehr von diesem Beitrag. Ich bin sehr froh, diese tolle Informationen, die frei heraus dort gemeinsam zu sehen.

Leave a comment