Hi Martin, > thank you very much for your example !! It really helped me a lot! Thank to > you displaying Linestrings is now easy!
It's always nice to hear the sound of a satisfied customer :) Since posting the demo I realized that GeoTools can do more of the work for you, specifically the getScreenCoords method can be replaced by the LiteShape class which is described here... http://docs.codehaus.org/display/GEOTDOC/13+Converting+JTS+Geometry+to+a+Java+Shape > But I wonder why it is necessary to "transform" the JTS Line to an arraylist > (Like you did)? There's nothing special about using an ArrayList, it was just to have the demo draw more than one line. If the panel was only to display a single LineString you could have that as a field directly. > And how can I draw a JTS Polygon? It's very similar to drawing a LineString. You can think of a Polygon as a set of closed LineStrings: one for the outer boundary and, optionally, one or more for "holes" in the polygon. See the demo below which I've modified to handle line and polygons. It should draw something like this... http://imagebin.org/78707 > The JTS Homepages (http://www.vividsolutions.com/jts/discussion.htm) show a > lot of nice example pictures but no hint how to draw them to screen! If you need fancy drawing it might be easier to use GeoTools to create features (SimpleFeature class) from your JTS objects and then use the GeoTools rendering system to display the resulting collection of features. This gives you access to a wide array of styling options (e.g. line and fill colours, opacity, labels). The following examples will help with some of the concepts (these examples work with shapefiles but you can easily modify them to work directly with an in-memory collection of features instead)... http://geotools.org/examples/csv2shp.html http://geotools.org/examples/stylelab.html > The reason why I am so interested in JTSis are the methods like, creating > Buffers, computing intersection matrix etc. Yep - we all rely on JTS :) Michael package org.geotools.demo; import com.vividsolutions.jts.geom.Envelope; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.LineString; import com.vividsolutions.jts.geom.Polygon; import com.vividsolutions.jts.io.WKTReader; import java.awt.Color; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Paint; import java.awt.Rectangle; import java.awt.geom.AffineTransform; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; import org.geotools.geometry.jts.Geometries; import org.geotools.geometry.jts.LiteShape; public class JTSDrawingPanel extends JPanel { private static final int MARGIN = 5; private List<Geometry> geometries = new ArrayList<Geometry>(); private AffineTransform geomToScreen; public void addGeometry(Geometry geom) { geometries.add(geom); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (!geometries.isEmpty()) { setTransform(); Graphics2D g2d = (Graphics2D) g; Paint polyPaint = new GradientPaint(0, 0, Color.CYAN, 100, 100, Color.MAGENTA, true); Paint defaultPaint = Color.BLUE; for (Geometry geom : geometries) { LiteShape shape = new LiteShape(geom, geomToScreen, false); if (Geometries.get(geom) == Geometries.POLYGON) { g2d.setPaint(polyPaint); g2d.fill(shape); } else { g2d.setPaint(defaultPaint); g2d.draw(shape); } } } } private void setTransform() { Envelope env = getGeometryBounds(); Rectangle visRect = getVisibleRect(); Rectangle drawingRect = new Rectangle( visRect.x + MARGIN, visRect.y + MARGIN, visRect.width - 2*MARGIN, visRect.height - 2*MARGIN); double scale = Math.min(drawingRect.getWidth() / env.getWidth(), drawingRect.getHeight() / env.getHeight()); double xoff = MARGIN - scale * env.getMinX(); double yoff = MARGIN + env.getMaxY() * scale; geomToScreen = new AffineTransform(scale, 0, 0, -scale, xoff, yoff); } private Envelope getGeometryBounds() { Envelope env = new Envelope(); for (Geometry geom : geometries) { Envelope geomEnv = geom.getEnvelopeInternal(); env.expandToInclude(geomEnv); } return env; } public static void main(String[] args) throws Exception { JTSDrawingPanel panel = new JTSDrawingPanel(); JFrame frame = new JFrame("Draw geometries"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel); frame.setSize(500, 500); WKTReader reader = new WKTReader(); LineString line = (LineString) reader.read( "LINESTRING(20 20, 20 25, 25 25, " + "25 15, 15 15, 15 30, 30 30, 30 10, " + "10 10, 10 35, 35 35, 35 5)"); panel.addGeometry(line); line = (LineString) reader.read("LINESTRING(-10 40, 5 50, 20 40, 35 50, 50 40)"); panel.addGeometry(line); Polygon poly = (Polygon) reader.read( "POLYGON((-10 -10, 0 0, 40 0, 50 -10, 40 -20, 0 -20, -10 -10), " + "(0 -10, 5 -5, 10 -10, 5 -15, 0 -10), " + "(30 -10, 35 -5, 40 -10, 35 -15, 30 -10))"); panel.addGeometry(poly); frame.setVisible(true); } } ------------------------------------------------------------------------------ This SF.Net email is sponsored by the Verizon Developer Community Take advantage of Verizon's best-in-class app development support A streamlined, 14 day to market process makes app distribution fast and easy Join now and get one step closer to millions of Verizon customers http://p.sf.net/sfu/verizon-dev2dev _______________________________________________ Geotools-gt2-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
