空間數據模型
空間數據模型
(1)、JTS Geometry model
(2)、ISO Geometry model (Geometry Plugin and JTS Wrapper Plugin)
GeoTools has two implementations of these interfaces:
Geometry Plugin a port of JTS 1.7 to the ISO Geometry interfaces
JTS Wrapper Plugin an implementation that delegates all the work to JTS
JTS包結構
系(linearref包)、計算交點(noding包)、幾何圖形操作(operation包)、平面圖(planargraph包)、多邊形化(polygnize包)、精度(precision)、工具(util包)
(1) JTS提供瞭如下的空間數據類型
Point
MultiPoint
LineString
LinearRing 封閉的線條
MultiLineString 多條線
Polygon
MultiPolygon
GeometryCollection 包括點,線,面
(2) 支持接口
Coordinate
Coordinate(座標)是用來存儲座標的輕便的類。它不同於點,點是Geometry的子類。不像模範Point的對象(包含額外的信息,例如一個信包,一個精確度模型和空間參考系統信息),Coordinate只包含縱座標值和存取方法。
Envelope(矩形)
一個具體的類,包含一個最大和最小的x 值和y 值。
GeometryFactory
GeometryFactory提供一系列的有效方法用來構造來自Coordinate類的Geometry對象。支持接口
- package com.mapbar.geo.jts;
- import org.geotools.geometry.jts.JTSFactoryFinder;
- 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.geom.LineString;
- import com.vividsolutions.jts.geom.LinearRing;
- import com.vividsolutions.jts.geom.Point;
- import com.vividsolutions.jts.geom.Polygon;
- import com.vividsolutions.jts.geom.MultiPolygon;
- import com.vividsolutions.jts.geom.MultiLineString;
- import com.vividsolutions.jts.geom.MultiPoint;
- import com.vividsolutions.jts.io.ParseException;
- import com.vividsolutions.jts.io.WKTReader;
- /**
- * Class GeometryDemo.java
- * Description Geometry 幾何實體的創建,讀取操作
- * Company mapbar
- * author Chenll E-mail: [email protected]
- * Version 1.0
- * Date 2012-2-17 上午11:08:50
- */
- public class GeometryDemo {
- private GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
- /**
- * create a point
- * @return
- */
- public Point createPoint(){
- Coordinate coord = new Coordinate(109.013388, 32.715519);
- Point point = geometryFactory.createPoint( coord );
- return point;
- }
- /**
- * create a point by WKT
- * @return
- * @throws ParseException
- */
- public Point createPointByWKT() throws ParseException{
- WKTReader reader = new WKTReader( geometryFactory );
- Point point = (Point) reader.read("POINT (109.013388 32.715519)");
- return point;
- }
- /**
- * create multiPoint by wkt
- * @return
- */
- public MultiPoint createMulPointByWKT()throws ParseException{
- WKTReader reader = new WKTReader( geometryFactory );
- MultiPoint mpoint = (MultiPoint) reader.read("MULTIPOINT(109.013388 32.715519,119.32488 31.435678)");
- return mpoint;
- }
- /**
- *
- * create a line
- * @return
- */
- public LineString createLine(){
- Coordinate[] coords = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
- LineString line = geometryFactory.createLineString(coords);
- return line;
- }
- /**
- * create a line by WKT
- * @return
- * @throws ParseException
- */
- public LineString createLineByWKT() throws ParseException{
- WKTReader reader = new WKTReader( geometryFactory );
- LineString line = (LineString) reader.read("LINESTRING(0 0, 2 0)");
- return line;
- }
- /**
- * create multiLine
- * @return
- */
- public MultiLineString createMLine(){
- Coordinate[] coords1 = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
- LineString line1 = geometryFactory.createLineString(coords1);
- Coordinate[] coords2 = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
- LineString line2 = geometryFactory.createLineString(coords2);
- LineString[] lineStrings = new LineString[2];
- lineStrings[0]= line1;
- lineStrings[1] = line2;
- MultiLineString ms = geometryFactory.createMultiLineString(lineStrings);
- return ms;
- }
- /**
- * create multiLine by WKT
- * @return
- * @throws ParseException
- */
- public MultiLineString createMLineByWKT()throws ParseException{
- WKTReader reader = new WKTReader( geometryFactory );
- MultiLineString line = (MultiLineString) reader.read("MULTILINESTRING((0 0, 2 0),(1 1,2 2))");
- return line;
- }
- /**
- * create a polygon(多邊形) by WKT
- * @return
- * @throws ParseException
- */
- public Polygon createPolygonByWKT() throws ParseException{
- WKTReader reader = new WKTReader( geometryFactory );
- Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");
- return polygon;
- }
- /**
- * create multi polygon by wkt
- * @return
- * @throws ParseException
- */
- public MultiPolygon createMulPolygonByWKT() throws ParseException{
- WKTReader reader = new WKTReader( 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)))");
- return mpolygon;
- }
- /**
- * create GeometryCollection contain point or multiPoint or line or multiLine or polygon or multiPolygon
- * @return
- * @throws ParseException
- */
- public GeometryCollection createGeoCollect() throws ParseException{
- LineString line = createLine();
- Polygon poly = createPolygonByWKT();
- Geometry g1 = geometryFactory.createGeometry(line);
- Geometry g2 = geometryFactory.createGeometry(poly);
- Geometry[] garray = new Geometry[]{g1,g2};
- GeometryCollection gc = geometryFactory.createGeometryCollection(garray);
- return gc;
- }
- /**
- * create a Circle 創建一個圓,圓心(x,y) 半徑RADIUS
- * @param x
- * @param y
- * @param RADIUS
- * @return
- */
- public Polygon createCircle(double x, double y, final double RADIUS){
- final int SIDES = 32;//圓上面的點個數
- Coordinate coords[] = new Coordinate[SIDES+1];
- for( int i = 0; i < SIDES; i++){
- double angle = ((double) i / (double) SIDES) * Math.PI * 2.0;
- double dx = Math.cos( angle ) * RADIUS;
- double dy = Math.sin( angle ) * RADIUS;
- coords[i] = new Coordinate( (double) x + dx, (double) y + dy );
- }
- coords[SIDES] = coords[0];
- LinearRing ring = geometryFactory.createLinearRing( coords );
- Polygon polygon = geometryFactory.createPolygon( ring, null );
- return polygon;
- }
- /**
- * @param args
- * @throws ParseException
- */
- public static void main(String[] args) throws ParseException {
- GeometryDemo gt = new GeometryDemo();
- Polygon p = gt.createCircle(0, 1, 2);
- //圓上所有的座標(32個)
- Coordinate coords[] = p.getCoordinates();
- for(Coordinate coord:coords){
- System.out.println(coord.x+","+coord.y);
- }
- }
- }