Spatial data Model
Determines whether the two geometry has a specified spatial relationship. Including:
Equal (equals), detach (disjoint), intersect (intersect), phase (touches), crossover (crosses), inclusive (within), inclusive (contains), Overwrite/overwrite (overlaps). The General relational (relate) operator is also supported.
Spatial relationships supported by JTS
Http://www.cnblogs.com/duanxingxing/p/5144257.html
Code
ImportOrg.geotools.geometry.jts.JTSFactoryFinder;Importcom.vividsolutions.jts.geom.Coordinate;ImportCom.vividsolutions.jts.geom.Geometry;Importcom.vividsolutions.jts.geom.GeometryCollection;Importcom.vividsolutions.jts.geom.GeometryFactory;Importcom.vividsolutions.jts.geom.LineString;Importcom.vividsolutions.jts.geom.LinearRing;ImportCom.vividsolutions.jts.geom.Point;ImportCom.vividsolutions.jts.geom.Polygon;ImportCom.vividsolutions.jts.geom.MultiPolygon;Importcom.vividsolutions.jts.geom.MultiLineString;ImportCom.vividsolutions.jts.geom.MultiPoint;Importcom.vividsolutions.jts.io.ParseException;ImportCom.vividsolutions.jts.io.WKTReader;/*** Class Geometrydemo.java * Description Geometry geometry entity creation, read operation * Company Mapbar * Author CHENLL e-mail: [Email protect ED] * Version 1.0 * Date 2012-2-17 a.m. 11:08:50*/ Public classGeometrydemo {PrivateGeometryfactory geometryfactory = Jtsfactoryfinder.getgeometryfactory (NULL ); /*** Create a point *@return */ PublicPoint Createpoint () {coordinate coord=NewCoordinate (109.013388, 32.715519); Point Point=Geometryfactory.createpoint (coord); returnPoint ; } /*** Create a point by WKT *@return * @throwsparseexception*/ PublicPoint Createpointbywkt ()throwsparseexception{Wktreader Reader=NewWktreader (geometryfactory); Point Point= (point) reader.read ("Point (109.013388 32.715519)"); returnPoint ; } /*** Create multiPoint by wkt *@return */ PublicMultiPoint createmulpointbywkt ()throwsparseexception{Wktreader Reader=NewWktreader (geometryfactory); MultiPoint Mpoint= (MultiPoint) reader.read ("MultiPoint (109.013388 32.715519,119.32488 31.435678)"); returnMpoint; } /*** * Create a line *@return */ PublicLineString Createline () {coordinate[] coords=NewCoordinate[] {NewCoordinate (2, 2),NewCoordinate (2, 2)}; LineString Line=geometryfactory.createlinestring (coords); returnLine ; } PublicLineString Createline (intAintBintCintd) {coordinate[] coords=NewCoordinate[] {NewCoordinate (A, B),Newcoordinate (c, D)}; LineString Line=geometryfactory.createlinestring (coords); returnLine ; } /*** Create a line by WKT *@return * @throwsparseexception*/ PublicLineString createlinebywkt ()throwsparseexception{Wktreader Reader=NewWktreader (geometryfactory); LineString Line= (LineString) reader.read ("LineString (0 0, 2 0)"); returnLine ; } /*** Create MultiLine *@return */ Publicmultilinestring Createmline () {coordinate[] coords1=NewCoordinate[] {NewCoordinate (2, 2),NewCoordinate (2, 2)}; LineString line1=geometryfactory.createlinestring (COORDS1); Coordinate[] Coords2=NewCoordinate[] {NewCoordinate (2, 2),NewCoordinate (2, 2)}; LineString line2=geometryfactory.createlinestring (COORDS2); Linestring[] Linestrings=NewLinestring[2]; linestrings[0]=line1; linestrings[1] =line2; Multilinestring Ms=geometryfactory.createmultilinestring (linestrings); returnMS; } /*** Create multiLine by WKT *@return * @throwsparseexception*/ PublicMultilinestring createmlinebywkt ()throwsparseexception{Wktreader Reader=NewWktreader (geometryfactory); Multilinestring Line= (multilinestring) reader.read ("Multilinestring ((0 0, 2 0), (1 2))"); returnLine ; } /*** Create a polygon (polygon) by WKT *@return * @throwsparseexception*/ PublicPolygon createpolygonbywkt ()throwsparseexception{Wktreader Reader=NewWktreader (geometryfactory); Polygon Polygon= (Polygon) reader.read ("Polygon ((20 10, 30 0, 40 10, 30 20, 20 10)"); returnPolygon; } /*** Create multi polygon by wkt *@return * @throwsparseexception*/ PublicMultipolygon createmulpolygonbywkt ()throwsparseexception{Wktreader Reader=NewWktreader (geometryfactory); Multipolygon Mpolygon= (Multipolygon) reader.read ("Multipolygon" (((40 10, 30 0, 40 10, 30 20, 40 10), (30 10, 30 0, 40 10, 30 20, 30 10)) "); returnMpolygon; } /*** Create geometrycollection contain point or multiPoint or line or multiLine or polygon or Multipolygon *
@return * @throwsparseexception*/ PublicGeometryCollection Creategeocollect ()throwsparseexception{LineString Line=Createline (); Polygon Poly=createpolygonbywkt (); Geometry G1=Geometryfactory.creategeometry (line); Geometry G2=geometryfactory.creategeometry (poly); Geometry[] Garray=NewGEOMETRY[]{G1,G2}; GeometryCollection GC=geometryfactory.creategeometrycollection (Garray); returnGC; } /*** Create a circle creates a circle, center (x, y) radii radius *@paramx *@paramy *@paramRADIUS *@return */ PublicPolygon Createcircle (DoubleXDoubleYFinal DoubleRADIUS) { Final intSIDES = 32;//number of points above the circleCoordinate coords[] =NewCoordinate[sides+1]; for(inti = 0; i < SIDES; i++){ DoubleAngle = ((Double) I/(Double) * SIDES) * Math.PI * 2.0; DoubleDX = Math.Cos (angle) *RADIUS; DoubleDY = Math.sin (angle) *RADIUS; Coords[i]=NewCoordinate ((Double) x + DX, (Double) y +dy); } Coords[sides]= Coords[0]; Linearring Ring=geometryfactory.createlinearring (coords); Polygon Polygon= Geometryfactory.createpolygon (Ring,NULL ); returnPolygon; } /** * @paramargs *@throwsparseexception*/ Public Static voidMain (string[] args)throwsparseexception {Geometrydemo GT=NewGeometrydemo (); Polygon P= Gt.createcircle (0, 1, 2); //all coordinates on a circle (32)Coordinate coords[] =p.getcoordinates (); for(coordinate coord:coords) {System.out.println (coord.x+","+coord.y); } Point PT=Gt.createpoint (); System.out.println (Pt.getx ()+ "," +pt.gety ()); Point Pt2=gt.createpointbywkt (); System.out.println (Pt2.getx ()+ "," +pt2.gety ()); LineString L_1= Gt.createline (20, 0, 30, 0); LineString l_2= Gt.createline (20, 0, 30, 10); LineString L_3= Gt.createline (30, 10, 30, 15); LineString L_4= Gt.createline (20, 10, 30, 0); Polygon Pol=gt.createpolygonbywkt (); System.out.println (POL); System.out.println (L_1.within (POL)); System.out.println (L_2.within (POL)); System.out.println (L_3.within (POL)); System.out.println (L_4.within (POL)); System.out.println (Pol.within (l_3)); }}
Java JTS & Spatial Data Model