001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2006, Adrian Custer, assigned to the PMC.
006: *
007: * This file is hereby placed into the Public Domain. This means anyone is
008: * free to do whatever they wish with this file. Use it well and enjoy!
009: */
010: package org.geotools.demo.libraryJTS;
011:
012: //JTS imports
013: import com.vividsolutions.jts.geom.Coordinate;
014: import com.vividsolutions.jts.geom.Envelope;
015: import com.vividsolutions.jts.geom.Geometry;
016: import com.vividsolutions.jts.geom.GeometryFactory;
017: import com.vividsolutions.jts.geom.LineString;
018: import com.vividsolutions.jts.geom.LinearRing;
019: import com.vividsolutions.jts.geom.Point;
020: import com.vividsolutions.jts.geom.Polygon;
021: import com.vividsolutions.jts.io.ParseException;
022: import com.vividsolutions.jts.io.WKTReader;
023: import com.vividsolutions.jts.io.WKTWriter;
024:
025: /**
026: * This is DemoJTS.java a class to examine the JTS Geometric model of the
027: * GeoTools code base.
028: *
029: *
030: * This tutorial was written in April 2006 against the Geotools 2.2RC2 release.
031: * It was updated in November 2006 against the 1.7.2 JTS release.
032: *
033: *
034: * The Geotools Users' Manual:
035: * ---------------------------
036: *
037: * This tutorial is written as part of the Geotools Users' Manual which
038: * will be available on the Geotools web site. The tutorial and manual aim to
039: * get new programmers started in using the Geotools library. Programmers who
040: * wish to extend the library should look at the Developpers' Guide instead.
041: *
042: *
043: * The Geotools Geometric Model:
044: * -----------------------------
045: *
046: * This tutorial introduces one of the two geometric models used by Geotools.
047: * The JTS model is used to describe the spatial definition of the 'Features' in
048: * the GIS.
049: *
050: * The JTS model is a strictly cartesian, two dimensional, orthogonal axis
051: * model; while a third, 'z', coordinate is allowed in each coordinate tuple,
052: * these 'z' coordinates are all ignored in the geometric operators. Similarly,
053: * while the geometry objects can hold a spatial reference identification (SRID)
054: * number, the library does not take that identification into account.
055: *
056: *
057: * Tutorial Outline:
058: * -----------------
059: *
060: * The tutorial starts by building geometries from scratch. This section uses
061: * the Java primitive double data type to create coordinates, then assembles
062: * these coordinates into arrays, and uses those arrays to make JTS Geometries.
063: *
064: * The tutorial continues by building geometries from Well Known Text (WKT)
065: * String elements.
066: *
067: *
068: * Note on Code Structure:
069: * -----------------------
070: *
071: * For reading simplicity, this tutorial does not use any Java method or object
072: * structure; it consists of only a single long main(.) method.
073: *
074: *
075: * Further Reading:
076: * ---------------
077: *
078: * Read the Geotools Users' Manual
079: * Read the tutorials in the JTS distribution.
080: *
081: * Material for this tutorial was taken from:
082: * http://www.geotools.org/CreateAGeometry
083: * retrived on 24 Sept 2005
084: *
085: * @author Adrian Custer, (c) assigned to the Geotools Project Management Committee.
086: * @version 0.0.1 April 2006
087: *
088: */
089:
090: public class DemoJTS {
091:
092: public static void main(String[] args) {
093:
094: System.out
095: .println("Start of the output for the tutorial: DemoJTS.\n");
096:
097: // PART I: Create Coordinates and Coordinate Arrays
098: System.out.println("PART I: Coordinates from scratch");
099:
100: // Create a coordinate for a point
101: Coordinate ptc = new Coordinate(14.0d, 14.0d);
102: System.out.println("The Coordinate is: " + ptc);
103:
104: // Create an array and add the coordinates for the line
105: Coordinate[] lsc = new Coordinate[8];
106: lsc[0] = new Coordinate(5.0d, 5.0d);
107: lsc[1] = new Coordinate(6.0d, 5.0d);
108: lsc[2] = new Coordinate(6.0d, 6.0d);
109: lsc[3] = new Coordinate(7.0d, 6.0d);
110: lsc[4] = new Coordinate(7.0d, 7.0d);
111: lsc[5] = new Coordinate(8.0d, 7.0d);
112: lsc[6] = new Coordinate(8.0d, 8.0d);
113: lsc[7] = new Coordinate(9.0d, 9.0d);
114:
115: // Create an array and add the coordinates for the polygon
116: // Note that the last coordinate is the same as the first
117: Coordinate[] pgc = new Coordinate[10];
118: pgc[0] = new Coordinate(7, 7);
119: pgc[1] = new Coordinate(6, 9);
120: pgc[2] = new Coordinate(6, 11);
121: pgc[3] = new Coordinate(7, 12);
122: pgc[4] = new Coordinate(9, 11);
123: pgc[5] = new Coordinate(11, 12);
124: pgc[6] = new Coordinate(13, 11);
125: pgc[7] = new Coordinate(13, 9);
126: pgc[8] = new Coordinate(11, 7);
127: pgc[9] = new Coordinate(7, 7);
128:
129: // PART II: Create Envelopes and operations
130: System.out
131: .println("\nPART II: Envelopes from scratch and operations");
132:
133: // Construct with doubles x1,x2,y1,y2
134: Envelope e1 = new Envelope(8.0d, 20.0d, 4.0d, 12.0d);
135: // Construct with Coordinates lower left, upper right
136: Envelope e2 = new Envelope(new Coordinate(12.0d, 6.0d),
137: new Coordinate(16.0d, 16.0d));
138: System.out.println("The first Envelope is: " + e1);
139: System.out.println("It contains the coordinate? "
140: + e1.contains(ptc));
141: System.out.println("Does the second? "
142: + e2.contains(ptc));
143: Envelope e1prime = new Envelope(e1);
144: e1prime.expandToInclude(ptc);
145: System.out.println("The first Envelope expanded " + e1prime);
146: System.out.println("Two Envelopes intersected "
147: + e1.intersection(e2));
148:
149: // PART III: Creating Geometry objects from doubles
150: System.out.println("\nPART III: Geometries from Coordinates");
151:
152: // Create a com.vividsolutions.jts.geom.GeometryFactory
153: //
154: // Geotools uses the idea of factories a lot. It's known as a 'pattern,'
155: // a common setup that gives a particular kind of flexibility. The idea
156: // is: you make the factory, change the settings of the factory, and
157: // then make a new object based on those settings. Factories make it
158: // easy to create lots of objects with similar settings.
159: //
160: // The JTS Geometry factory provides different public methods from which
161: // the various geometry types can be instantiated.
162: //
163: GeometryFactory geomFac = new GeometryFactory();
164:
165: // TODO: HOW? "Here if we wanted to, we could tweak the factory"
166:
167: // Use the factory to make the jts geometries
168: Point ptG = geomFac.createPoint(ptc);
169: LineString lnG = geomFac.createLineString(lsc);
170: LinearRing rgG = geomFac.createLinearRing(pgc);
171: Polygon pgG = geomFac.createPolygon(rgG, null);
172:
173: // TODO: add some query stuff.
174:
175: // Just to see how far we have gotten
176: System.out.println("The point is: " + ptG);
177: System.out.println("The line string is: " + lnG);
178: System.out.println("The linear ring is: " + rgG);
179: System.out.println("The polygon is: " + pgG);
180: // Note that the JTS Geometry Objects' toString() method outputs the
181: // Coordinates in the Well Known Text (WKT) String format.
182:
183: // PART IV: Creating Geometry objects from Well Known Text
184: System.out.println("\nPART IV: Geometry Creation from WKT");
185: //
186: // The Well Known Text (WKT) format is a String format used to define
187: // geometry Objects. The WKT format is defined in ...TODO
188:
189: // Create another LineString from Well Known Text. The LineString is
190: // created, not as a LineString object directly, but as a more generic
191: // Geometry object.
192: Geometry lnGwkt = null;
193: try {
194: lnGwkt = new WKTReader()
195: .read("LINESTRING (0 0, 30 30, 0 7, 5 10)");
196: } catch (ParseException pe) {
197: System.out.println("Couldn't parse the linestring");
198: }
199:
200: // The geometry can also be output as well known text, allowing
201: // for output of all geometry types
202: System.out.print("The WKT line string is: ");
203: WKTWriter wrt = new WKTWriter();
204: String str = wrt.write(lnGwkt);
205: System.out.println(str);
206: // A more compact version of the same would be.
207: System.out.println(" or with one statement: "
208: + (new WKTWriter()).write(lnGwkt));
209:
210: // PART V: Compare Geometry Objects
211: System.out.println("\nPART V: Geometry Comparisons");
212:
213: // Geometry equality. (Note that the 'z' coordinate is ignored.)
214: Coordinate cNew = new Coordinate(14.000001, 14.0, 3.0);
215: Point ptNew = geomFac.createPoint(cNew);
216: System.out.println("Are the pt geometries equal? "
217: + ptG.equals(ptNew));
218: System.out.println(" exactly equal? "
219: + ptG.equalsExact(ptNew));
220: System.out.println(" near to exactly equal? "
221: + ptG.equalsExact(ptNew, 0.0000000001));
222: System.out.println(" inexactly equal? "
223: + ptG.equalsExact(ptNew, 0.001));
224:
225: Geometry g_one = null;
226: Geometry g_two = null;
227: try {
228: g_one = new WKTReader()
229: .read("LINESTRING (0 0, 10 10, 0 20, 0 0)");
230: g_two = new WKTReader()
231: .read("LINESTRING (10 10, 0 20, 0 0, 10 10)");
232: } catch (ParseException pe) {
233: System.out.println("Couldn't parse the linestring");
234: }
235: System.out.println("Are the triangles equal? "
236: + g_one.equals(g_two));
237:
238: // Geometry overlap: envelopes, convex hulls, geometries
239: // TODO: why don't the geoms overlap?
240: System.out.println("Do the envelopes overalp? "
241: + lnG.getEnvelope().overlaps(pgG.getEnvelope()));
242: System.out.println("Do the convex hulls overalp? "
243: + lnG.convexHull().overlaps(pgG.convexHull()));
244: System.out.println("Do the geometries overalp? "
245: + lnG.overlaps(pgG));
246:
247: // PART VI: Operations on Geometry objects
248: System.out.println("\nPART VI: Geometry Operations");
249:
250: // Intersection
251: // Test the line string for self intersection.
252: System.out.println("Does it self intersect? "
253: + lnGwkt.isSimple());
254: // LineString intersection.
255: // Note that the result is a complex geometry.
256: System.out.println("Line strings intersection: "
257: + lnGwkt.intersection(lnG));
258: System.out.println("Line, polygon intersection: "
259: + lnG.intersects(pgG));
260:
261: // Union
262: System.out.println("Union of two LineStrings: "
263: + lnG.union(lnGwkt));
264:
265: // PART VII: Complex Operations on Geometry sets
266: System.out.println("\nPART VII: Operations on Geometry Sets");
267: System.out.println("...TODO...");
268:
269: System.out.println("\nEnd of the tutorial output.");
270:
271: System.out.println("Testing...");
272:
273: }
274:
275: }
|