accessing GIS data from Processing
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;
}
See also:
projecting GIS Data on a sphere using Processing
sketch experiment 7 - osc events
nested cubes in processing
processing phaseflower
Time Perception
sketch experiment 7 - osc events
nested cubes in processing
processing phaseflower
Time Perception
Trackbacks
- Very usefull info. Thanks!
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?
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,
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)
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
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!
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...
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....
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.