open goverment data with processing

little test-app to load a map (request getMap) from the http://data.wien.gv.at using http://processing.org

it loads a map of the vienna destricts and overlays a map with all schools (the school-map is set transparent inside the code!)

WARINING: it is very slow – use the mousewheel to zoom an drag to move the map

PImage map;
PImage map_over;
PFont font;

float minlon = -11179;
float minlat = 331050;
float maxlon = 18258;
float maxlat = 353820;

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

  size(800,800);

  // Load the font. Fonts must be placed within the data
  // directory of your sketch. A font must first be created
  // using the ‚Create Font…‘ option in the Tools menu.
  font = loadFont(„CourierNew36.vlw“);
  textAlign(LEFT);
  fill(0);

  // Set the font and its size (in units of pixels)
  textFont(font, 20);

  map = getWMS( „http://data.wien.gv.at/daten/wms“,
    „BEZIRKSGRENZEOGD“, minlon, minlat, maxlon, maxlat, width, height );

  map_over = getWMS( „http://data.wien.gv.at/daten/wms“,
    „SCHULEOGD“, minlon, minlat, maxlon, maxlat, width, height );
}

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

  map_over = setTransparent(map_over);
  image( map_over, 0,0,width, height );
  text(„bbox “ + minlon + „, “  + minlat + „, “  + maxlon + „, “  + maxlat, 5,20);
}

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://data.wien.gv.at/daten/wms“,
    „BEZIRKSGRENZEOGD“, minlon, minlat, maxlon, maxlat, width, height);

  map_over = getWMS( „http://data.wien.gv.at/daten/wms“,
    „SCHULEOGD“, minlon, minlat, maxlon, maxlat, width, height );

  println(„minlon “ + startx + “ X “ + starty + “ X “ + minlon + “ X “ + minlat + “ X “ + maxlon + “ X “ + maxlat);
}

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://data.wien.gv.at/daten/wms“,
    „BEZIRKSGRENZEOGD“, minlon, minlat, maxlon, maxlat, width, height);

  map_over = getWMS( „http://data.wien.gv.at/daten/wms“,
    „SCHULEOGD“, minlon, minlat, maxlon, maxlat, width, height );

  println(„minlon “ + startx + “ X “ + starty + “ X “ + minlon + “ X “ + minlat + “ X “ + maxlon + “ X “ + maxlat);
}

PImage getWMS( String url, String layer, float minlon, float minlat, float maxlon, float maxlat, int width, int height) {
  PImage res = loadImage(
    url
    +“?request=GetMap&version=1.1.1&width=“
    +width
    +“&height=“
    +height
    +“&layers=“
    +layer
    +“&styles=&format=image/gif&bbox=“
    +minlon
    +“,“
    +minlat
    +“,“
    +maxlon
    +“,“
    +maxlat
    +“&srs=EPSG:31256″, „gif“
  );
  return res;
}

PImage setTransparent (PImage img)
{
  for (int i = 0; i < img.width * img.height; i++)
  {
    if ((img.pixels[i] & 0x00FFFFFF) >= 0x00EFFFFF)
    {
      img.pixels[i] = 0;
    }
  }
  img.format = ARGB;
  img.updatePixels();
  return img;
}

Advertisements

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s