【GIS】JTS com.vividsolutions.jts.geom

JTS (Java Topology Suite)简介:JTS是加拿大的 Vivid Solutions公司做的一套开放源码的 Java API。它提供了一套空间数据操作的核心算法。为在兼容OGC标准的空间对象模型中进行基础的几何操作提供2D空间API。
官网链接
下载

空间数据模型
在这里插入图片描述

Geometry之间的关系
支持的空间操作包括
在这里插入图片描述

使用Demo

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

    public LineString createLine(int a, int b, int c, int d){
        Coordinate[] coords  = new Coordinate[] {new Coordinate(a, b), new Coordinate(c, d)};
        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);
        }

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


    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
com.vividsolutions.jts.geom jar是一个用于处理地理空间数据的Java库。该库提供了许多用于几何计算和空间分析的类和方法。 首先,com.vividsolutions.jts.geom jar提供了各种几何对象的表示,如点(Point)、线(Line)、多边形(Polygon)、多点(MultiPoint)、多线(MultiLine)和多边形集合(MultiPolygon)。这些几何对象具有坐标信息和相关的属性,可以用于描述和存储地理空间数据。 其次,该库还提供了许多几何计算和分析的方法。例如,可以使用该库中的方法来计算两个几何对象之间的距离、计算几何对象的面积和长度、判断点是否在几何对象内部等。这些方法可以在地理空间分析和地图应用程序中使用,使得开发者可以轻松处理空间数据的计算和分析任务。 此外,com.vividsolutions.jts.geom jar还支持对几何对象进行空间关系和拓扑分析。开发者可以使用该库中的方法来判断两个几何对象之间的关系,如相等、相交、包含等,或者判断几何对象是否具有拓扑关系,如相邻、相连等。这些方法可以帮助开发者进行复杂的空间数据查询和分析。 总之,com.vividsolutions.jts.geom jar是一个功能强大的Java库,用于处理地理空间数据。它提供了各种几何对象的表示和处理方法,方便开发者进行几何计算、空间分析和拓扑处理。该库在各种地理信息系统和地图应用程序中广泛应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值