Transcript
Page 1: Processing 2.0 + Open Data

Processing 2.0+ Open Data

@stevebattlestevebattle.me

blog.stevebattle.me

processing.freeforums.org

Page 2: Processing 2.0 + Open Data

Here’s the plan

1.Access Open Data (weather forecast)

2.Read JSON (JavaScript Object Notation)

3.Graphical widgets (ControlP5)

4.Upload App to mobile device

Page 5: Processing 2.0 + Open Data

JSON http://www.json.org

• Objects {} containing string:value pairs

• Arrays [] of values

• Values can be objects, arrays, strings, numbers, booleans, or null

{"Locations": {"Location": [ { "id": "3094", "name": "Rosehearty Samos", "longitude": "-2.121", "latitude": "57.698" }, ... { "id": "354552", "name": "Perranuthnoe (Perran Sands) (Beach)", "longitude": "-5.4411", "latitude": "50.1121" } ]}}

Page 6: Processing 2.0 + Open Data

• Use a loop to search JSON for an object named ‘Bristol’.

Page 7: Processing 2.0 + Open Data

Get the five day forecast

Page 8: Processing 2.0 + Open Data

{"SiteRep": { "Wx": {"Param": [ { "$": "Day Maximum Temperature", "name": "Dm", "units": "C" }, ... ]}, "DV": { "dataDate": "2013-08-02T21:00:00Z", "Location": { "Period": [ { "value": "2013-08-02Z", "type": "Day", "Rep": [ { "Dm": "24", ... } ] }, ... ] } "type": "Forecast" }}}

The five day forecast

Page 9: Processing 2.0 + Open Data

Method chaining

Page 10: Processing 2.0 + Open Data

Plan ahead with a sketch

Page 11: Processing 2.0 + Open Data

Graphical Widgets• Sketch > Import Library > Add Library

• Sketch > Import Library > ControlP5 import controlP5.*;

Page 12: Processing 2.0 + Open Data

ListBox Widget

public void draw() { background(255); fill(0);}

Page 13: Processing 2.0 + Open Data

•Define locationID as global:String locationID = "310004"; // Bristol

•Replace // Search for ‘Bristol’ code with:initList(locations);

Page 14: Processing 2.0 + Open Data
Page 15: Processing 2.0 + Open Data

•Detect the List selection event to assign locationID

Page 16: Processing 2.0 + Open Data

Tab Widgets• To make the siteList selection happen

before it gets the forecast we’ll put them on different tabs.

Page 17: Processing 2.0 + Open Data

• Move the forecasting code at the end of setup() into its own function...

Page 18: Processing 2.0 + Open Data

...and call the function when the ‘forecast’ tab is selected.

Page 19: Processing 2.0 + Open Data

Chart Widget• Instead of printing the forecast we’ll

plot the temperatures on a chart.

Chart chart;

Page 20: Processing 2.0 + Open Data

Modify initForecast()

Page 21: Processing 2.0 + Open Data

Upload AppA few minor tweaks are required to run this as an Android App.

• Switch to ‘Android’ mode.

• You have to give the App permission to

use the internet:

Android > Sketch Permisions > Check INTERNET

Page 22: Processing 2.0 + Open Data

App Mods• Set the size() for the device:

size(displayWidth,displayHeight);

• Optionally change the orientation:orientation(LANDSCAPE);

• The tab widgets are too small:cp5.getTab(“default”).setWidth(200).setHeight(40);cp5.getTab(“forecast”).setWidth(200).setHeight(40);

• The ListBox is too small. Double: ItemHeight, BarHeight, ScrollBarWidth.

• The font is too small:PFont pfont = createFont(“Ariel”,20,true);cp5.setFont(new ControlFont(pfont,24));

Page 23: Processing 2.0 + Open Data
Page 24: Processing 2.0 + Open Data

Summary• Decorating the Met Office weather

chart is left as an exercise for the reader. See controlP5 javadoc:<http://www.sojamo.de/libraries/controlP5/reference/>

• Try displaying other information in the forecast, such as the weather summary (field “W”). <http://www.metoffice.gov.uk/media/pdf/3/0/DataPoint_API_reference.pdf>

• You now have the technology to create great open data Apps using Processing 2.0


Top Related