JTS(Geometry)

9 篇文章 6 订阅
空间数据模型
(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包)

重点理解JTS Geometry model
(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: Chenll@mapbar.com

 * 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);
		}
	}
}


  • 8
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
如果你需要在Java中使用MySQL的Geometry类型字段,可以使用MySQL提供的Java SDK - MySQL Connector/J,它是Java访问MySQL数据库的官方驱动。 MySQL Connector/J提供了对Geometry类型字段的支持,可以将Geometry类型的数据转换为Java中的对象,例如Point、LineString、Polygon等等。对于Geometry类型的数据的操作,可以使用JTS Topology Suite库进行处理,也可以使用MySQL提供的Spatial Extensions中的函数来进行处理。 以下是使用MySQL Connector/J和JTS Topology Suite来读取Geometry类型字段的示例代码: ```java import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.io.ParseException; import com.vividsolutions.jts.io.WKTReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class GeometryExample { public static void main(String[] args) { try { // 加载MySQL驱动 Class.forName("com.mysql.cj.jdbc.Driver"); // 连接MySQL数据库 Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mydb?useSSL=false", "root", "password"); // 执行查询语句,查询Geometry类型字段 Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT geom FROM mytable WHERE id=1"); // 读取查询结果 if (rs.next()) { // 将Geometry类型的数据转换为JTS Geometry对象 String wkt = rs.getString(1); Geometry geom = new WKTReader().read(wkt); // 对Geometry对象进行操作 double area = geom.getArea(); System.out.println("Geometry area: " + area); } // 关闭数据库连接 rs.close(); stmt.close(); conn.close(); } catch (ClassNotFoundException | SQLException | ParseException e) { e.printStackTrace(); } } } ``` 以上示例代码演示了如何使用MySQL Connector/J和JTS Topology Suite来读取Geometry类型字段并进行操作。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值