java jts_JTS基本概念和使用

这个Java程序演示了如何使用JTS库来创建各种几何对象,如点、线、多边形、多线和多边形等。还展示了如何通过WKT(Well-Known Text)格式读取和创建几何对象。
摘要由CSDN通过智能技术生成

packagecom.alibaba.autonavi;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;public classGeometryDemo {private GeometryFactory geometryFactory = newGeometryFactory();/*** create a point

*@return

*/

publicPoint createPoint(){

Coordinate coord= new Coordinate(109.013388, 32.715519);

Point point=geometryFactory.createPoint( coord );returnpoint;

}/*** create a point by WKT

*@return*@throwsParseException*/

public Point createPointByWKT() throwsParseException{

WKTReader reader= newWKTReader( geometryFactory );

Point point= (Point) reader.read("POINT (109.013388 32.715519)");returnpoint;

}/*** create multiPoint by wkt

*@return

*/

public MultiPoint 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= new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};

LineString line=geometryFactory.createLineString(coords);returnline;

}/*** create a line by WKT

*@return*@throwsParseException*/

public LineString createLineByWKT() throwsParseException{

WKTReader reader= newWKTReader( geometryFactory );

LineString line= (LineString) reader.read("LINESTRING(0 0, 2 0)");returnline;

}/*** create multiLine

*@return

*/

publicMultiLineString 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);returnms;

}/*** create multiLine by WKT

*@return*@throwsParseException*/

public MultiLineString createMLineByWKT()throwsParseException{

WKTReader reader= newWKTReader( geometryFactory );

MultiLineString line= (MultiLineString) reader.read("MULTILINESTRING((0 0, 2 0),(1 1,2 2))");returnline;

}/*** create a polygon(多边形) by WKT

*@return*@throwsParseException*/

public Polygon 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*/

public MultiPolygon 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*/

public GeometryCollection 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 创建一个圆,圆心(x,y) 半径RADIUS

*@paramx

*@paramy

*@paramRADIUS

*@return

*/

public Polygon createCircle(double x, double y, final doubleRADIUS){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);returnpolygon;

}/***@paramargs

*@throwsParseException*/

public static void main(String[] args) throwsParseException {

GeometryDemo gt= newGeometryDemo();

Polygon p= gt.createCircle(0, 1, 2);//圆上所有的坐标(32个)

Coordinate coords[] =p.getCoordinates();for(Coordinate coord:coords){

System.out.println(coord.x+","+coord.y);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值