Friday, March 18, 2011

Polygon Buffering with JTS Topology and jython

Previously, I had done a post regarding polygon offset.  It turns out that JTS Topology has polygon buffering capability.  I wanted to try this out and compare it to my results from the polygon offset post.

As with most things with a library like JTS, the software does most of the work for you:

import sys
# path to jar into CLASSPATH
sys.path.append('/home/carl/Downloads/jts/lib/jts-1.11.jar')

from com.vividsolutions.jts.geom import Coordinate
from com.vividsolutions.jts.geom import GeometryFactory
# LinearRing is for the creation of the Polygon
from com.vividsolutions.jts.geom import LinearRing
# CoordinateSequenceArray is for the 

# creation of the LinearRing
from com.vividsolutions.jts.geom.impl import CoordinateArraySequence
# CAP_ROUND is for the Polygon.buffer operation
from com.vividsolutions.jts.operation.buffer.BufferOp import CAP_ROUND

# coordinates
             # PT 1
MONASTERY = [(1.1, 0.75),
             # PT 2
             (1.2, 1.95),
             # PT 3
             (1.9, 1.96),
                .

                .
                .
             # PT 21
             (1.1, 0.75)]

gf = GeometryFactory()
coords = [Coordinate(*coord) for coord in MONASTERY]
cas = CoordinateArraySequence(coords)
lr = LinearRing(cas, gf)
polyx = gf.createPolygon(lr, None)

# offset of 0.15 inward
# rounded edges
# 10 points per quarter circle of rounded edges
polyinset = polyx.buffer(-0.15, 10, CAP_ROUND)


The result is shown below:



The rounded edges give an entirely different look to the shape. My version is shown below:


I'm glad I did the egg the way I did it (it looks more realistic).  Nonetheless, the polygon buffering capability of JTS is a useful tool, particularly for calculating offsets and distances for scientific or geographic purposes.


Notes:  Shapely is a Python package (CPython) with much of the same functionality as JTS.

No comments:

Post a Comment