Hi folks,

In response to a question on the user list I unearthed some old code
for creating hexagonal grids (tesselation of hexagonal polygons) and
reworked it for GeoTools 2.7.

As I'm sure you know, hexagonal lattices have that advantage that each
cell's immediate neighbours are equidistant. I've used them in the
past for ecological simulations.

The code can create a lattice as a SimpleFeatureCollection where you
control hexagon size and orientation. Here's an example...
http://imagebin.org/98114

The code for that is below.

Would anyone have any objections to me placing this code in a new
unsupported module ?

cheers
Michael

public class HexagonalLattice {

    public static void main(String[] args) {
        SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
        typeBuilder.setName("hextype");
        typeBuilder.add("hexagon", Polygon.class,
(CoordinateReferenceSystem)null);
        typeBuilder.add("color", Color.class);
        final SimpleFeatureType TYPE = typeBuilder.buildFeatureType();

        final Envelope bounds = new Envelope(0, 100, 0, 100);

        HexagonAttributeSetter attributeSetter = new
HexagonAttributeSetter(TYPE) {
            public void setAttributes(Hexagon h, Map<String, Object>
attributes) {
                int g = (int) (255 * h.getCenter().x / bounds.getWidth());
                int b = (int) (255 * h.getCenter().y / bounds.getHeight());
                attributes.put("color", new Color(0, g, b));
            }
        };

        final double sideLen = 5.0;

        SimpleFeatureCollection lattice =
Hexagons.createLattice(bounds, sideLen, Orientation.FLAT,
attributeSetter);

        DefaultMapContext map = new DefaultMapContext();
        map.addLayer(lattice, createStyle("color"));
        JMapFrame.showMap(map);
    }

    private static Style createStyle(String propName) {
        FilterFactory2 ff2 = CommonFactoryFinder.getFilterFactory2(null);
        StyleFactory sf = CommonFactoryFinder.getStyleFactory(null);

        Stroke stroke = sf.createStroke(ff2.literal(Color.BLACK),
ff2.literal(1.0));
        Fill fill = sf.createFill(ff2.property(propName));
        PolygonSymbolizer sym = sf.createPolygonSymbolizer(stroke, fill, null);
        return SLD.wrapSymbolizers(sym);
    }
}

------------------------------------------------------------------------------

_______________________________________________
Geotools-devel mailing list
Geotools-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-devel

Reply via email to