Skip to content

Instantly share code, notes, and snippets.

@moradology
Created February 13, 2015 19:27
Show Gist options
  • Save moradology/a41f50d55de6865172ab to your computer and use it in GitHub Desktop.
Save moradology/a41f50d55de6865172ab to your computer and use it in GitHub Desktop.

#geotrellis.vector ##Features and Geometries

In addition to working with raster data, Geotrellis provides a number of facilities for the creation, representation, and modification of vector data. The data types central to this functionality (geotrellis.vector.Feature and geotrellis.vector.Geometry) correspond - and not by accident - to certain objects found in the GeoJson spec. Features correspond to the objects listed under features in a geojson FeatureCollection. Geometrys, to geometries in a geojson Feature.

##Geometries The base geometry class can be found in Geometry.scala. Concrete geometries include:

  • geotrellis.vector.Point
  • geotrellis.vector.MultiPoint
  • geotrellis.vector.Line
  • geotrellis.vector.MultiLine
  • geotrellis.vector.Polygon
  • geotrellis.vector.MultiPolygon
  • geotrellis.vector.GeometryCollection

Working with these geometries is a relatively straightforward affair. Let's take a look:

import geotrellis.vector._
/*
 * First, let's create a Point. Then, we'll use its intersection method.
 * We should also use intersection's alias '&' for good measure.
 */
val myPoint = Point(1.0, 1.1) // Create a point
val selfIntersection = myPoint intersection Point(1.0, 1.1) // Intersection method
val nonIntersection = myPoint & Point(200, 300) // Intersection alias

At this point, the values selfIntersection and nonIntersection are GeometryResult containers. These containers are what many JTS operations on Geometry objects will wrap their results in. To idiomatically destructure these wrappers, we do the following:

def unwrapPoint(res: PointGeometryIntersectionResult): Option[Point] =
  res match {
    case PointResult(point) => Some(point)
    case _ => None
  }
// Note:
assert(unwrapPoint(selfIntersection) == Some(myPoint))
assert(unwrapPoint(nonIntersection) == None)

Beyond the methods which come with any Geometry object there are implicits in many geotrellis modules which will extend Geometry capabilities. For instance, after importing geotrellis.vector.json._, it becomes possible to call the toGeoJson method on any Geometry:

import geotrellis.vector.json._
assert(Point(1,1).toGeoJson == """{"type":"Point","coordinates":[1.0,1.0]}""")

Methods which are specific to certain subclasses of Geometry exist too. For example, geotrellis.vector.MultiLine is extended by geotrellis.vector.op such that this becomes possible:

import geotrellis.vector.op._
val myML = MultiLine.EMPTY
myML.unionGeometries

The following packages extend Geometry capabilities: geotrellis.vector.op geotrellis.vector.io.WKB geotrellis.vector.io.WKT geotrellis.vector.io.json geotrellis.vector.reproject

##Features

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment