New features in JTS 1.11

JTS最近发布了1.11版本,新增了:

计算Delaunay三角网和Voronoi多边形:

import java.util.ArrayList;
import java.util.Collection;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.triangulate.DelaunayTriangulationBuilder;
import com.vividsolutions.jts.triangulate.VoronoiDiagramBuilder;

/**
 *
 * @author Sun Ning/SNDA
 * @since 2010-3-3
 */

public class DelaunayAndVoronoiApp {

    /**
     * create some predefined sites
     * @return
     */

    public static Collection<coordinate> getPredefinedSites(){
        double[][] coords = {{100,27},{28, 50},{29, 40},{32, 90}, {12, 26}};
        ArrayList<coordinate> coordinates = new ArrayList<coordinate>(coords.length);

        for(int i=0; i<coords.length; i++){
            coordinates.add(new Coordinate(coords[i][0], coords[i][1]));
        }

        return coordinates;
    }

    /**
     *
     * @param coords
     * @return a geometry collection of triangulations
     */

    public static Geometry buildDelaunayTriangulation(Collection<coordinate> coords){
        DelaunayTriangulationBuilder builder = new DelaunayTriangulationBuilder();
        builder.setSites(coords);
        return builder.getTriangles(new GeometryFactory());
    }
    /**
     *
     * @param coords
     * @return a collection of polygons
     */

    public static Geometry buildVoronoiDiagram(Collection<coordinate> coords){
        VoronoiDiagramBuilder builder = new VoronoiDiagramBuilder();
        builder.setSites(coords);
        return builder.getDiagram(new GeometryFactory());
    }

    /**
     *
     * @param args
     */

    public static void main(String[] args){
        Collection<coordinate> coordinates = getPredefinedSites();

        /**
         * Delauny
         */

        GeometryCollection triangulations
                = (GeometryCollection)buildDelaunayTriangulation(coordinates);

        int total = triangulations.getNumGeometries();
        System.out.printf("Total triangulations: %d\n", total);
        for(int i=0; i<total; i++){
            Geometry g = triangulations.getGeometryN(i);
            Coordinate[] coords = g.getCoordinates();
            System.out.printf("Triangulation %d: ", i);
            for(Coordinate c : coords){
                System.out.printf("(%.3f, %.3f) ", c.x, c.y);
            }
            System.out.println();
        }

        /**
         * Voronoi
         */

        GeometryCollection diagram = (GeometryCollection)
                buildVoronoiDiagram(coordinates);
        int totalDia = diagram.getNumGeometries();
        for(int i=0; i<totalDia; i++){
            Geometry g = diagram.getGeometryN(i);
            Coordinate[] coords = g.getCoordinates();
            System.out.printf("Diagram %d: ", i);
            for(Coordinate c : coords){
                System.out.printf("(%.3f, %.3f) ", c.x, c.y);
            }
            System.out.println();
        }
    }

}

输出:

Total triangulations: 5
Triangulation 0: (32.000, 90.000) (12.000, 26.000) (28.000, 50.000) (32.000, 90.000)
Triangulation 1: (32.000, 90.000) (28.000, 50.000) (100.000, 27.000) (32.000, 90.000)
Triangulation 2: (100.000, 27.000) (28.000, 50.000) (29.000, 40.000) (100.000, 27.000)
Triangulation 3: (100.000, 27.000) (29.000, 40.000) (12.000, 26.000) (100.000, 27.000)
Triangulation 4: (12.000, 26.000) (29.000, 40.000) (28.000, 50.000) (12.000, 26.000)
Diagram 0: (-76.000, 88.625) (-76.000, 178.000) (176.713, 178.000) (72.699, 65.730) (-38.235, 76.824) (-76.000, 88.625)
Diagram 1: (-76.000, -62.000) (-76.000, 88.625) (-38.235, 76.824) (11.978, 43.348) (56.422, -10.619) (57.006, -62.000) (-76.000, -62.000)
Diagram 2: (11.978, 43.348) (-38.235, 76.824) (72.699, 65.730) (67.316, 48.882) (11.978, 43.348)
Diagram 3: (176.713, 178.000) (188.000, 178.000) (188.000, -62.000) (57.006, -62.000) (56.422, -10.619) (67.316, 48.882) (72.699, 65.730) (176.713, 178.000)
Diagram 4: (11.978, 43.348) (67.316, 48.882) (56.422, -10.619) (11.978, 43.348)

将Geometry对象转换成Shape对象,绘制在JPanel上:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.util.Collection;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.vividsolutions.jts.awt.ShapeWriter;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import java.util.Random;

/**
 *
 * @author Sun Ning/SNDA
 * @since 2010-3-3
 */

public class JTS2Awt {

    public static void showUI(final Shape... shape){
        JFrame jframe = new JFrame("JTS Geometry to AWT Shape");

        JPanel jp = new JPanel(){
            @Override
            public void paint(Graphics g){
                super.paint(g);
                Graphics2D g2d = (Graphics2D)g;
                if(shape != null){
                    for(Shape s: shape){
                        g2d.setColor(new Color(Color.HSBtoRGB(new Random().nextFloat(), 1f, 0.6f)));
                        g2d.draw(s);
                    }
                }
            }
        };
        jp.setPreferredSize(new Dimension(150, 150));



        jframe.getContentPane().add(jp);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.pack();
        jframe.setVisible(true);
    }

    public static Shape toShape(Geometry geom){
        ShapeWriter writer = new ShapeWriter();
        return writer.toShape(geom);
    }

    public static void main(String[] args){
        Collection<coordinate> coords = DelaunayAndVoronoiApp.getPredefinedSites();
        Geometry geomT = DelaunayAndVoronoiApp.buildDelaunayTriangulation(coords);
        Geometry geomD = DelaunayAndVoronoiApp.buildVoronoiDiagram(coords);

        showUI(toShape(geomT), toShape(geomD));
    }

}

jts

This entry was written by Sunng , posted on Wednesday March 03 2010at 01:03 pm , filed under 装备 and tagged , . Bookmark the permalink . Post a comment below or leave a trackback: Trackback URL.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word