From the Introduction to GeoTools workshop!
Are you new to GeoSpatial? This GeoTools session is back by popular demand with Java 8 examples. Offering a visual introduction for Java developers we will exploring how you can integrate GIS services into your next project.
For those new to the GeoSpatial scene we provide an introduction to spatial concepts and how to avoid common pitfalls.
The workshop offers a steady series of workbooks introducing:
Feature creation
Geometry, Coordinate Reference Systems and Re-projection
Spatial Queries
Handling large format rasters
Working with Style
Raster Operations
Covering both the concepts and the science of map making the workbooks serve as an excellent reference, but the focus is always on you and the code you need to get the job done.
2. Care and Feeding Instructions
1. Connect to GSI, Credentials: ticket846 / cm52n
2. Download links provided or USB being passed around
3. Coffee / water are over -->
4. Washrooms end of hall on the left
2
3. GeoSpatial for Java
• This is a quick introduction to Geospatial
concepts and ideas for the Java developer
• We will make use of number of Java projects
• However the focus is on the ideas
(we just happen to be using running code)
3
5. Welcome
5
Jody Garnett
Community Lead
jgarnett@boundlessgeo.com
@jodygarnett
Open Source Geospatial Foundation
Board Member
OSGeo Incubation Chair
GeoTools Project Officer
Eclipse Foundation
LocationTech Project Steering Committee
LocationTech Technology Project
Boundless
Boundless provides geospatial tools and
services for managing data and building
applications.
Open Source Projects
GeoTools
GeoServer
uDig
9. Distance
A common theme is representation of walking time rather
than distance. Allows map to be used as a planning tool.
9
10. Navigation and Problem Solving
Polynesian stick maps
Used for navigating in the pacific ocean.
Depicted islands, wave patterns and current direction
10
11. Problem Solving
1854 Broad Street cholera outbreak
Source of outbreak found by mapping cases of cholera
John Snow discovered that victims all used the same water
source
11
14. GeoTools 1
GeoTools 1 had more users then documentation
Solution was to fire the users
... and GeoTools 2 was formed
14
15. GeoTools 2
We learned our lesson!
GeoTools 2 leans on the OGC standards
OGC provides public standards anyone can download
Easier to follow standards than having to invent names
So the “best” docs are often the standards
Standards are not intended to as a limitation
Only a common starting point
Get it Done
15
16. GeoTools and Collaboration
The project practices “open development”
Easy to take part
However we are LGPL so we get lots of forks
(mostly buried in commercial apps)
In the spirit of “Get it Done”
We make use of existing projects early and often
This is the DRY “Do not repeat yourself” principle in action
16
17. GeoTools and Jars
We will introduce these other projects as we go
using code as often as possible
This does however mean that we will use a lot of jars
Maven is the tool we use to manage jars
Do you use maven for your project?
17
20. This is Hands On Programming
1. Pair up with a friend (or make a new friend)
2. There is a USB stick if needed
3. Copy the “GeospatialForJava” folder
4. We are going to begin with the “QuickStart” workbook
20
+
22. Quickstart
1. Start when ready
2. We have around 30 mins
3. Choose Dev Environment
22
Workbook provides download links;
or copy from the USB.
1. geotools-userguide.zip
2. navigate to tutorials
Optional: advanced maven magic:
1. cd home directory “.m2“ folder
2. unzip repository.zip
24. What is a Feature (English)
• Feature is a prominent or conspicuous part or
characteristic
–When drawing a person you will often draw the features
of a persons face (nose, ears, eyes, mouth)
–When drawing a map we draw the features that make
up the landscape
• When mapping a real world object
–We draw the shape (the Geometry)
–We also record where the object is
24
26. A Feature is an Object
• A Feature is an Object (ie “instance”)
• Works just like an Object (ie an “instance”) as a
representation of an entity in the real world
26
GeometryFactory geomFactory =
JTSFactoryFinder.getGeometryFactory();
Flag here = new Flag();
here.location = geomFactory.createPoint(new Coordinate(23.3,-37.2));
here.name = "Here";
here.classification = 3;
here.height = 2.0;
27. A Feature is an Object
• Record information about the real world entity
• Feature properties record information
• properties are either attribute or association
27
GeometryFactory geomFactory =
JTSFactoryFinder.getGeometryFactory();
SimpleFeatureBuilder f = new SimpleFeatureBuilder( type );
f.add( geomFactory.createPoint( new Coordinate(23.3,-37.2) ) );
f.add("here");
f.add(3);
f.add(2.0);
SimpleFeature feature = f.buildFeature("fid.1");
28. FeatureType
• Group several features that are similar into
classes and form these classes into a hierarchy
–Feature Type is a “class”
• Like Objects, features can be ideas or concepts
(like a “urban growth” or rainfall)
28
class Flag {
public Point location;
public String name;
public int classification;
public double height;
};
29. FeatureType
• Like Objects, features can be ideas or concepts
(like a “urban growth” or rainfall)
29
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
b.setName( "Flag" );
b.setCRS( DefaultGeographicCRS.WGS84 );
b.add( "location", Point.class );
b.add( "name", String.class );
b.add( "classification", Integer.class );
b.add( "height", Double.class );
SimpleFeatureType type = b.buildFeatureType();
31. Feature Model
31
• Lots of ways to access attributes
• “SimpleFeature” for flat record data structure
32. org.opengis
• Summary
–org.opengis.* defines the Interfaces
–org.geotools.* implements the Objects
• Win:
–javadocs are easier than reading standards
–Now an “OGC GeoAPI” working group
• Fail:
–“opengis” was an attempt to promote collaboration
between deegree and GeoTools projects
–Failed as both parties need funding at the same time
32
33. Feature Workbook
• 25 minutes
• Great example of
building features by hand
• Data Model
–Feature, FeatureType
• Data Acess
–DataStore
–FeatureSource
–FeatureStore
33
35. Fundamental Requirement of GIS
• We need both:
–The shape to draw (ie the Geometry)
–The location to draw the shape in
35
36. JTS Topology Suite
• Geometry is provided by the JTS project
–Implementation of OGC “Simple Features for SQL”
–Numerically stable, the rocket science of GIS
foundation of our open source spatial industry
• Team made up of a single Canadian developer
–Martin Davis
36
37. Geometry - Port
• In the beginning…
–Point
–A single coordinate of two to four dimensions
37
38. GeometryFactory and WKT
38
GeometryFactory geometryFactory =
JTSFactoryFinder.getGeometryFactory();
Coordinate coord = new Coordinate(151.211111, -33.859972 );
Point point = geometryFactory.createPoint( coord );
GeometryFactory geometryFactory =
JTSFactoryFinder.getGeometryFactory();
WKTReader reader = new WKTReader( geometryFactory );
String wkt = "POINT (151.211111, -33.859972)";
Point point = (Point) reader.read( wkt );
39. Geometry - LineString
39
• A set of two of more coordinates linear
interpretation of a path between coordinates
• Translation:
–Java String is a “string of characters”
–LineString is a string of straight line segments
40. Geometry - Polygon
40
• A set of one or more linestrings
–One ring defines the exterior boundary
–The remaining rings define the “holes” in the polygon
• Represents an Area
41. Coverage
• Coverage represent an area with a function:
–feature: used to record an attribute value for an area
–coverage: used to record an attribute whose value
changes across an area (Land use, Soil types, Zones)
41
42. What is the Point?
• Geometry is the shape of our “feature”
• We are still missing an important bit of
information
–“Where” the feature is!
• This is similar to the idea of “distance”
–Q: how far?
–A: 3
We really need to know 3 “what”
• The units of distance are important
• We really need the meaning of a shape
42
43. Coordinate
• Coordinate –ordered list of measurements
–X / Y
–Latitude / Longitude
–Longitude / Latitude
• An important part is defining what is being
measured
–Latitude and Longitude in degrees
–Distances measured in meters
43
Coordinate coord = new Coordinate(151.211111, -33.859972 );
44. Coordinate Reference System
• CRS is the definition of what coordinates are
measuring
–Positions are always in 3D space
–We often “cheat” and only write down two numbers
(the ordinates in co-ordinates)
• How is this possible?
–We treat the numbers as measurements against a
model of the earth
• Earth model? (ie Geodetic Datum)
–Ellipsoid – shape kind of like a sphere
–Geoid – gravity field – kind of like sea level
44
45. Where to measure from?
45
Easy to measure on a flat surface
Not as easy to measure on a curve
47. Warning: Axis Order Issue
47
• Map makers record northing/easting order
– Sextant allows you to measure the angle of north star
• Computer systems expect x/y order
• But the map makers never really agreed
–So there is confusion
–You need to check the data
48. Feature and Geometry Standards
48
Simple
Point
Line
Polygon
Standards
GML2
SFSQL
Implementation
JTS
Complex
Point
Curve
Surface
Standards
GML3
ISO19107
Implementation
data structure only
49. Geometry CRS Workbook
49
• 25 minutes
–A chance to work with
Geometry
• Use MathTransform
• Process Features
–EPSG:32751 EPSG:
32656
51. Filter used to Select information
• Filter is used as part of a Query
–to “select” information
–Think of it as the WHERE clause in an SQL statement
• You can run the resulting Filter object yourself
• Uses “Filter Expressions” used to access data
• Filter / Expressions work with Feature and
POJOs
51
if( filter.evaluate( feature ) ){
// the feature was "selected" by the filter
System.out.println( "Selected "+ feature.getId();
}
52. Filter is defined by an XML specification (for web standards).
SAX, DOM and dynamic parsing solutions available.
Filter as XML
52
<Filter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns="http://www.opengis.net/ogc"
xsi:schemaLocation="http://www.opengis.net/ogc filter.xsd">
<PropertyIsEqualTo>
<PropertyName>land_code</PropertyName>
<Literal>2</Literal>
</PropertyIsEqualTo>
</Filter>
53. Filter as CQL (text DSL)
Constraint Query Language (from library science actually).
Utility class provides: CQL.toFilter( cql )
53
Filter filter = CQL.toFilter("area( SHAPE ) BETWEEN 1000 AND 3000");
54. Filter as Java
Finally you can use a FilterFactory directly.
54
FilterFactory ff = CommonFactoryFinder.getFilterFactory();
Filter filter = ff.propertyLessThan( ff.property( "AGE"), ff.literal( 12 ) );
55. Query Tutorial
• This is where it gets cool
• Ad hoc query against
Shapefile, PostGIS, WFS
• Using the same code
55
57. Grid Coverage
• Representing a Coverage
–collection of features that “complete cover an area”
–grid coverage form a regular grid of features
57
Coverage
Feature
Coverage
Grid Coverage
58. Measurements not Pixels
• Grid Coverages used to record measurements
• The file metadata describes what the values are
–Height (called a Digital Elevation Model or DEM)
–Height and Depth – Bathymetry
–Vegetation Index
–Some have RGB measurements
58
59. Raster Formats
• GridCoverages look so much like “pixels” we end
up using the same file formats (jpeg, png, tiff,
etc..) with a text file for location
• Spatial formats record (geotiff, eww)
–resolution of the grid
–the measurements associated with each location
–the location of the grid in the real world
59
60. Image Tutorial
• 25 minutes
• Working with raster files
• Grayscale, Color, Multi-
band
60
62. Raster Operations
• Direct use of GridCoverage
–Extract set of records at a specific location
• Simple Operations
–Crop - trim to a geographic bounding box or envelope
–Scale - increase / decrease size
–Resample - ad-hoc sampling, such as reprojection
• Operations class
–Facade class to make this easier to program
–CoverageProcessor performs the operations internally
–Operations form a chain, lazy evaluation
62
65. Style
• Cartography is the practice of map making
• Focus on purpose
–What is the map trying to communicate?
–Who will be using it?
–What projection, scale and symbology are best?
65
66. Cartography - Scale
• Scale
–Appropriate for task at hand(floor plan vs interstate)
• Graphical Representation of Scale
–1:25000 is not sufficient
–graphical representation works when altered /printed
66
67. Cartography - Projection
• Choosing projection is hard
–Equal area to measure distance
–WGS84 to show entire world
–“google” projection for speed?
67
68. Cartography - Symbology
• Choose appropriate symbols for audience
• Is the audience trained to read the symbols?
–MIL2525B symbols for tactical response
–Happy Face for planning lunch
68
Surface and Hostile
Planned
-=Reduced
North Direction
East Direction
Velocity = Red
Task Force – No
Quantity – 9
Nuclear - Yes
Happy
69. Cartography - Colour
• Colour also is dependent on audience
• Device makes a difference
–Projector cannot display as much colour as screen
–Will the map be photocopied?
• People make a difference
–may be colourblind
–may be using the map in bright sunlight or at night
• ColorBrewer
–http://colorbrewer2.org/
–Color brewer palettes built-in toGeoTools
69
70. Colour - Qualitative
• Values represent qualities of your data
–Use colour to tell values apart
–The colour should have the same “impact”
• Each groups has the same
importance.
70
71. Colour - Sequential
• Values represent an ordered series
–Use colour to tell values apart
–Use colour to tell “how close” values are
• The amount of colour directly reflects the value
71
72. Colour - Diverging
• Values once again form a sequence
–Use colour to tell values apart
–Use colour to identify extreme values
• amount of colour emphasises low and high
values
72
73. MapContent and Style
• MapContent
–Layer – data and style used to draw
• StyleLayerDescriptor
–FeatureTypeStyle – instructions for how to draw
features
–Rule – choose which features
• PolygonSymbolizer
• PointSymbolizer
• LineSymbolizer
• TextSymbolizer
• RasterSymbolizer
73
74. Style Layer Descriptor (SLD)
• XML File format used to configure WMS servers
• We are mostly going to ignore this bit
74