accessing GIS data from Processing
Posted by Nikolaus Gradwohl
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

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;
}


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:TMWORLDBORDERS-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?
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:polylandmarks,tiger:tigerroads
Best regards,
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 sketchnov26b.getWMS(sketchnov26b.java:92) at sketchnov26b.setup(sketchnov26b.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:TMWORLDBORDERS-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 sketchnov26b.draw(sketchnov26b.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)
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