1. Intriduction to Java Programming for Beginners, Novices
Download
Report
Transcript 1. Intriduction to Java Programming for Beginners, Novices
Programming for
Geographical Information Analysis:
Advanced Skills
Lecture 7: Libraries I: Visualisation
Dr Andy Evans
Libraries
Graphing
Geographical data
Processing
Libraries
Code put together for others to use.
Usually in the form of jar files.
May contain native dynamically linked libraries (dll) and
others, but more usually just jar files.
Consult notes, as dlls etc. sometimes needed, but more
usually these are libraries for other languages.
Managing Libraries
Download the library.
Make sure it is in your CLASSPATH when compiling and running.
java -classpath ".;c:\libraries\lib.jar" YourClass
[any problems, see http://en.wikipedia.org/wiki/Classpath_(Java) ]
Alternatively, you can use a class management system.
Eclipse
Build automation tools
GNU Make:
Old UNIX system for installing complex programs on UNIX.
Not pleasant to use.
Apache Maven:
XML based.
Downloads required libraries.
Organises compilation and creation of jar files.
Demands a specific project structure.
Eclipse : File → New → Project → Maven
Apache Ant:
XML version of Make.
More flexible / harder to get a grip on.
Libraries
Graphing
Geographical data
Processing
Graphing
JFreeChart
http://www.jfree.org/jfreechart/
Setting up a Graph
Set up a dataset.
JFreeChart chart = ChartFactory.createXChart(dataset);
XPlot plot = (XPlot) chart.getPlot();
Adjust form, colour, etc. of plot if necessary.
ChartPanel chartPanel = new ChartPanel(chart);
Add chartPanel to Frame or similar
Example
DefaultPieDataset data = new DefaultPieDataset();
data.setValue("Cherry", 30);
data.setValue("Steak and Kidney", 25);
data.setValue("Stargazy", 45);
JFreeChart chart =
ChartFactory.createPieChart3D
("Favourite Pies", data, true, true, false);
PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setForegroundAlpha(0.5f);
ChartPanel chartPanel = new ChartPanel(chart);
Other graphing packages
InfoVis (Javascript + JSON)
http://philogb.github.io/jit/
D3 (DOM styling)
http://d3js.org/
Raphael
http://raphaeljs.com/
Libraries
Graphing
Geographical data
KML
Free mapping services
GeoTools
Cartograms
Processing
Geographical data
Keyhole Markup Language (KML)
http://www.opengeospatial.org/standards/kml/
https://developers.google.com/kml/documentation/
Google
KML can be combined with GoogleMaps.
Can be used to overlay information on GoogleEarth.
Test suite:
https://kmlsamples.googlecode.com/svn/trunk/interactive/index.html
GoogleMaps can also pull in GeoRSS streams.
KML
OGS specification.
At simplest, a little like the XML we looked at last lecture:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
10,10,1
10,20,1
20,20,1
20,10,1
10,10,1
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>
Other free mapping services
Google WebGL Globe
http://www.chromeexperiments.com
/globe
PhiloGL
http://www.senchalabs.org/
philogl/
GeoTools (Leeds et al.):
http://geotools.org/
Geographical data
Features
Mapping
Includes simple Swing components for showing maps.
GML/XML conversion
Web service setup
Interaction with databases
Shapefile read and write
Graph and Network tools
Spatial queries
Data formats
Raster formats and data access:
arcsde, arcgrid, geotiff, grassraster, gtopo30, image (JPEG, TIFF, GIF,
PNG), imageio-ext-gdal, imagemoasaic, imagepyramid, JP2K,
matlab
Database jdbc support:
db2, h2, mysql, oracle, postgis, spatialite, sqlserver
Vector formats and data access:
app-schema, arcsde, csv, dxf, edigeo, excel, geojson, org, property,
shapefile, wfs
XML Bindings:
Java data structures and bindings provided for the following: xsdcore (xml simple types), fes, filter, gml2, gml3, kml, ows, sld, wcs,
wfs, wms, wps, vpf.
Additional Geometry, Filter and Style parser/encoders available for
DOM and SAX applications.
Example
FileDataStore store =
FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource featureSource =
store.getFeatureSource();
MapContent map = new MapContent();
map.setTitle("Quickstart");
Style style =
SLD.createSimpleStyle(featureSource.getSchema());
Layer layer = new FeatureLayer(featureSource, style);
map.addLayer(layer);
JMapFrame.showMap(map);
GML
http://docs.geotools.org/latest/userguide/library/xml/geometry.html
http://docs.geotools.org/latest/userguide/library/xml/faq.html
See org.geotools.gml in docs
Also parses/produces KML.
Cartograms
WorldMapper (Sheffield Geography):
http://www.worldmapper.org/about.
html
Cartogram Geoprocessing Tool (Tom
Gross):
http://arcscripts.esri.com/details.asp?
dbid=15638
Cartograms
ScapeToad (Java: Chôros Laboratory)
http://scapetoad.choros.ch/
Takes in shapefiles; exports shapefiles or SVG.
You can download the src here:
http://sourceforge.net/projects/scapetoad/files/scapetoad/
ScapeToad%201.1/
Nothing to stop you using it in other GNU General Public
License, version 2, code.
Libraries
Graphing
Geographical data
Processing
Processing
http://processing.org/
MIT Media Lab
Libraries for visualisation.
Wraps a simple visualisation
scripting language inside java
classes.
Also javascript /Android / iPhone
javascript versions.
Excellent community with a
positive attitude to open sourcing
good code.
Processing Development Environment
Small put perfectly formed IDE.
Saves as Applications, MPEG, SVG and PDFs.
Bundles everything (data, libraries) into jars.
Constructs executables.
Coding
Two important methods:
void setup(){ }
Runs once at start.
void draw(){}
Runs each frame until stopped
(60 frames s-1; but can be changed).
Coding
float xCoor = 0;
float yCoor = 100;
float speed = 1;
void setup() {
size(200,200);
}
// Size has to be first thing in setup
void draw() {
background(255); // Wipe the background white
xCoor = xCoor + speed;
if (xCoor > width) {
xCoor = 0;
}
// Change x location
// Reset if it runs of the screen.
fill(0);
// Paint a rectangle
rect(xCoor,yCoor,30,10);
}
Objects
In the PDE you can put code in different tabs. However, you
can also just put multiple classes in one tab.
Processing will wrap them all inside its own class, as ‘inner
classes’.
Java development
Extend the Processing wrapper PApplet.
Add in your Processing code.
If you want it to run as an application, add:
public static void main(String args[]) {
PApplet.main(new String[] {
"--present", "MyProcessingSketch“
});
}
For multiple classes you need to get a reference to the
Papplet class into your class then call, e.g.
papplet.draw();
Eclipse
There is an Eclipse plugin:
http://wiki.processing.org/w/Eclipse_Plug_In
However, you can write just as well without it:
http://processing.org/learning/eclipse/
If you run as an applet in Eclipse and want to run as an
application, go into File -> Properties and delete the Launch
Configuration or use the arrow on the run button to “Run As”.
Processing tips
Processing creates a project ‘sketchbook’ directory called after
the main file for each project. It expects to find all data in a data
directory in here. Libraries should also be in a ‘libraries’
directory, though better to just ‘Add File’ them.
Avoid variable names it might already use, like pixels, x, y, and z.
http://forum.processing.org/topic/reserved-keywords-wiki-page
If you separate classes into other files, call them, e.g. “Car”
rather than “Car.java” – the latter signals that Processing should
treat it as Java, and you’ll need to import the classes etc.
Other Processing
Libraries for:
XML / GPS interaction / Network communications
Mapping
Connections with Ruby/Python/Javascript
Video / Sound
Communications with hardware
Webcam and motion recognition
Spinoff projects:
Wiring/ Arduino –
chipboards for
hardware development
Other versions
1.5 rather nicely generates a webpage for you with the source
code and an applet version embedded in it.
The preferred option now is to
generate a javascript version of the
code. You can do this by adding the
javascript mode to version 2 and
changing to it (need to reboot).
Unfortunately this is doesn’t work well
with libraries, so you may want 1.5 for
web work.
Also an Android option.
Book
Great book on processing,
but also modelling real
systems more generally, is:
Visualisation inspiration
http://visual.ly/
http://www.visualisingdata.com/
Books:
JFreeChart developer guide.
Processor books (see website)
Visualisation books by Edward Tufte
Next Lecture
Libraries II: Scientific libraries
Practical
JFreeChart
Processing