Tuesday, June 22, 2010

Geometry Plotting

I often have found myself looking for a quick and easy way to visualize a geometry object. Maybe I have a handful of geometry objects and I want to visualize some relationship (for example intersection) that exists between them.

Recently added to geoscript python is the ability to create an xy plot from a set of geometry objects. This functionality comes courtesy of the plot function that is located in the viewer module.


from geoscript.viewer import plot
from geoscript import geom

g = geom.LineString((0,0), (10,10))
plot(g)




Nothing all that earth shattering but something can be utilized in a variety of different ways. For instance visualizing an intersection as mentioned above:


g1 = geom.readWKT('POLYGON((-87.1875 49.5703125, -124.453125 -29.8828125, -92.8125 -58.7109375, -43.59375 -69.9609375, -4.21875 -60.1171875, 16.171875 -29.8828125, 16.171875 -5.2734375, 0 27.7734375, -20.390625 48.8671875, -55.546875 53.7890625, -78.046875 54.4921875, -87.1875 49.5703125))')

g2 = geom.readWKT('POLYGON((24.609375 51.6796875, -28.125 24.9609375, -25.3125 -20.7421875, 24.609375 -61.5234375, 74.53125 -51.6796875, 108.984375 -23.5546875, 92.109375 27.0703125, 61.171875 55.1953125, 40.078125 55.1953125, 24.609375 51.6796875))')

plot([g1.intersection(g2), g1,g2])




The plotting functionality makes use of the JFreeChart library, a popular open source Java framework for creating diagrams and charts. Another example of one of the benefits of the marriage of Java and Python that is Jython.

A recent use I made of this new functionality was with regard to geometry simplification. I wanted to quickly visualize a simplified geometry at different distance tolerances. Using everybody's favourite layer as an example:


from geoscript.layer import Shapefile
from geoscript.geom import simplify

shp = Shapefile('tests/work/states.shp')

texas = [f for f in shp.features("STATE_NAME = 'Texas'")][0].geom
plot(texas)

texas_simple = simplify(texas, 0.1)
plot(texas_simple)

texas_simple2 = simplify(texas, 0.5)
plot(texas_simple2)




The simplification routine is the Douglas-Peucker algorithm provided out of the box by JTS.

2 comments:

Introducing GeoScript

GeoScript adds geo capabilities to dynamic scripting languages such as JavaScript, Python, Scala and Groovy.