The Java Topology Suite (needs to be updated to Python)
BlueSpray uses some portions of the GeoTools including the Java Topology Suite(JTS) from Vivid Solutions, Inc. This package contains the defintions for the Simple Features from the OpenGIS Consortium. These features represent the most commonly used geometric shapes for working with vector data and a number of valuable functions for working with the shapes. The one clarification we must make is that while the JTS contains some functions that perform topological operations, the JTS does not include topological data structures.It you are editing or processing topological data such as stream or road networks or polotical boundaries you'll want to use LayerVector objects instead of Simple Features.
BlueSpray provides special functions for working specifically with geometries. You can open and save geometries from shapefiles, query and update them in databases, and convert them to other layer types. Most of these functions were described on the Packages page. The Vivid Solutions library also provides a large number of functions for working with geometries. These are summarized below. We have included the JavaDocs for the geometries and the class names below are linked to them.
- Coordinate - coordinates define each of the points within a geometry
- Geometry - the base class for all geometry types and the one you'll probably use the most often
- Polygon - contains special functions to access the holes within a polygon
- Geometry Factory - class to create the geometry objects
- WKTReader - converts Well-Known Text format strings defining geometries into binary geometries
- WKTWriter - converts existing geometries into the Well-Known Text format
Below we've provided a selection of the most commonly used JTS packages, classes, and functions with examples of how they would be tyically used. The examples are in JavaScript syntax and, since JavaScript is a variant-type language, we've provided the type of the return value in the name. Some clues to the type of return value are:
- Flag - a boolean variable with a value of "true" or "false"
- TheGeomtry - an object created from one of the subclasses of "Geoemtry"
This is a very simple class that is used to hold 2 dimensional (x,y) coordinates and 3 dimensional (x,y,z) coordinates.
Attributes:
double x;
double y;
double z;
Geometry is a very powerful and flexible class. Geometries represent all types of SimpleFeatures including collections of SimpleFeatures. I've found that since the Geometry class contains functions to access the individual geometries in a collection I really don't need the GeometryCollection class.
Basics
var NewGeometry=TheGeomtry.clone();
var TypeString=TheGeomtry.getGeometryType(): - "MULTIPOLYGON", "POINT", etc.
Information about Geometries
var Flag=TheGeometry.isSimple(); - no lines intersect
var Flag=TheGeometry.isValid(); - true if the geometry is valid (i.e. no overlapping polygons)
var Flag=TheGeometry.isEmpty();
var Flag=TheGeometry.getDimension(); - returns 2 for 2d data, 3 for 3d
Working with collections of geometries
var NumGeometries=TheGeometry.getNumGeometries(); - returns the number of geometries in a geometry collection
Calculations
var TheDistance=TheGeometry.distance(Geometry);
var TheArea=TheGeometry.getArea();
var TheLength=TheGeometry.getLength();
Boundaries of Geometries
var NewGeometry=TheGeometry.getBoundary(); - ?
var NewGeometry=TheGeometry.getEnvelope(); - returns a bounding rectangle
var NewGeometry=TheGeometry.convexHull();
Getting Points
var ThePoint=TheGeometry.getCentroid(); - returns a POINT geometry at the centroid of the geometry
var ThePoint=TheGeometry.getInteriorPoint(); - returns a POINT in the interior if possible, otherwise on the boundary
Convertions
var TheWKTString=TheGeometry.toText() - Well-Known text (WKT) representation
var NumPoints=TheGeometry.getNumPoints();
var CoordinateArray=TheGeometry.getCoordinates();
Relationships with other Geometries
var Flag=Geoemtry1.disjoint(Geometry2); - true if the geometries do not touch at all
var Flag=Geoemtry1.touches(Geometry2); -
var Flag=Geoemtry1.intersects(Geometry2);
var Flag=Geoemtry1.crosses(Geoemtry2)
var Flag=Geoemtry1.within(Geoemtry2); - true if this geometry is within the specified geomegtry
var Flag=Geoemtry1.contains(Geoemtry2) - true if this geometry contains the specified geometry within it's boundary (i.e. does not touch the boundary)
var Flag=Geoemtry1.overlaps(Geoemtry2)
var Flag=Geoemtry1.covers(Geoemtry2) - true if this geometry completely covers the speciifed geometry (i.e. their boundaries may be shared)
var Flag=Geoemtry1.equals(Geoemtry2)
Operations to create new Geometries from the Relationships between two Geometries
var NewGeometry=Geoemtry1.intersection(Geometry2);
var NewGeometry=Geoemtry1.union(Geometry2);
var NewGeometry=Geoemtry1.difference(Geometry2);
Buffering
BufferOp.CAP_ROUND - (default) a semi-circle
BufferOp.CAP_BUTT - a straight line perpendicular to the end segment
BufferOp.CAP_SQUARE - a half-square
var NewGeometry=TheGeometry.buffer(TheDistance); - creates a buffered geometry based on the distance in map units
var NewGeometry=TheGeometry.buffer(TheDistance,NumQuatratSegements); - buffer with the number of segments used to create a circle specified
var NewGeometry=TheGeometry.buffer(TheDistance,NumQuatratSegements,TheEndCapStyle); - buffer with the end cap style added
var NumHoles=ThePolygon.getNumInteriorRing();
var TheLineString=ThePolygon.getInteriorRingN(index); - returns a LineString
var TheLineString=ThePolygon.getExteriorRing();
This class creates a number of functions to create geometries of all types.
var TheFactory=GeometryFactory(); - creates a new geometry factory
var TheGeometryCollection=createGeometryCollection(Geometry[] TheGeometries); - creates a geometry collection from an array of existing geometries
var ThePoint=createPoint(TheCoordinate); - creates a Point geometry from a single coordinate
var TheLinearRing=createLinearRing(TheCoordinates); - creates a linear ring from an array of coordinates
var TheLineString=createLineString(TheCoordinates); - creates a linear string from an array of coordinates
var TheMultiLineString=createMultiLineString(TheLineStrings)- creates a geometry with mulitple linear strings from an array of linear strings
var TheMultiPolygon=createMultiPolygon(ThePolygons); - creates a Multipolygon geometry from an array of polygons
var ThePolygon=createPolygon(TheLinearRingShell,TheLinearRingHoles); creates a polygon from a shell (the exterior) and an array of interior holes
These functions convert data to and from the SimpleFeature data types. This allow syou to get geomtries in the standard Well Known Text (WKT) format and the Well Known Binary (WKB) format. The text format is valuable because it both allows you to look at your spatial data and because spatial databases typically use this as the format to insert data into the database. The binary format
The example below shows how to create a polygon that covers the northern hemisphere in geographic coordinates.
var TheReader=new WKTReader();
var TheBox=TheReader.read("POLYGON((-180 90,180 90,180 0,-180 0,-180 90))");
Converting an existing SimpleFeature into a WKT string:
var TheWriter=new WKTWriter();
var TheWKTString=TheWriter.write(TheGeometry);
----------- not needed:
GeometryCollectionIterator(Geometry parent)
boolean hasNext();
Object next();
|