JTS Geometry用例分析

JTS Geometry用例分析拓扑关系 GeometryTest vividsolutio jts geom Coordinate importcom vividsolutio jts geom Geometry importcom vividsolutio jts geom GeometryColl importcom vividsoluti

微信搜索:“二十同学” 公众号,欢迎关注一条不一样的成长之路

拓扑关系

JTS Geometry用例分析
      
GeometryTest




 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; public class GeometryTest { private GeometryFactory geometryFactory = new GeometryFactory(); / * create a point * @return */ public Point createPoint(){ Coordinate coord = new Coordinate(109.013388, 32.); 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.)"); 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.,119.32488 31.)"); 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); } } }

Geometry之间的关系有下述几种:

JTS Geometry用例分析

 import com.vividsolutions.jts.geom.*; import com.vividsolutions.jts.io.ParseException; import com.vividsolutions.jts.io.WKTReader; / * gemotry之间的关系使用 */ public class GeometryTest { private GeometryFactory geometryFactory = new GeometryFactory(); / * 两个几何对象是否是重叠的 * @return * @throws ParseException */ public boolean equalsGeo() throws ParseException{ WKTReader reader = new WKTReader( geometryFactory ); LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)"); LineString geometry2 = (LineString) reader.read("LINESTRING(5 0, 0 0)"); return geometry1.equals(geometry2);//true } / * 几何对象没有交点(相邻) * @return * @throws ParseException */ public boolean disjointGeo() throws ParseException{ WKTReader reader = new WKTReader( geometryFactory ); LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)"); LineString geometry2 = (LineString) reader.read("LINESTRING(0 1, 0 2)"); return geometry1.disjoint(geometry2); } / * 至少一个公共点(相交) * @return * @throws ParseException */ public boolean intersectsGeo() throws ParseException{ WKTReader reader = new WKTReader( geometryFactory ); LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)"); LineString geometry2 = (LineString) reader.read("LINESTRING(0 0, 0 2)"); Geometry interPoint = geometry1.intersection(geometry2);//相交点 System.out.println(interPoint.toText());//输出 POINT (0 0) return geometry1.intersects(geometry2); } / * 判断以x,y为坐标的点point(x,y)是否在geometry表示的Polygon中 * @param x * @param y * @param geometry wkt格式 * @return */ public boolean withinGeo(double x,double y,String geometry) throws ParseException { Coordinate coord = new Coordinate(x,y); Point point = geometryFactory.createPoint( coord ); WKTReader reader = new WKTReader( geometryFactory ); Polygon polygon = (Polygon) reader.read(geometry); return point.within(polygon); } / * @param args * @throws ParseException */ public static void main(String[] args) throws ParseException { GeometryRelated gr = new GeometryRelated(); System.out.println(gr.equalsGeo()); System.out.println(gr.disjointGeo()); System.out.println(gr.intersectsGeo()); System.out.println(gr.withinGeo(5,5,"POLYGON((0 0, 10 0, 10 10, 0 10,0 0))")); } }

 

关系类型

 

  JTS Geometry用例分析

 import java.util.ArrayList; import java.util.List; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.LineString; public class GeometryTest { private GeometryFactory geometryFactory = new GeometryFactory(); / * create a Point * * @param x * @param y * @return */ public Coordinate point(double x, double y) { return new Coordinate(x, y); } / * create a line * * @return */ public LineString createLine(List 
  
    points) { Coordinate[] coords = (Coordinate[]) points.toArray(new Coordinate[points.size()]); LineString line = geometryFactory.createLineString(coords); return line; } / * 返回a指定距离内的多边形和多多边形 * * @param a * @param distance * @return */ public Geometry bufferGeo(Geometry a, double distance) { return a.buffer(distance); } / * 返回(A)与(B)中距离最近的两个点的距离 * * @param a * @param b * @return */ public double distanceGeo(Geometry a, Geometry b) { return a.distance(b); } / * 两个几何对象的交集 * * @param a * @param b * @return */ public Geometry intersectionGeo(Geometry a, Geometry b) { return a.intersection(b); } / * 几何对象合并 * * @param a * @param b * @return */ public Geometry unionGeo(Geometry a, Geometry b) { return a.union(b); } / * 在A几何对象中有的,但是B几何对象中没有 * * @param a * @param b * @return */ public Geometry differenceGeo(Geometry a, Geometry b) { return a.difference(b); } public static void main(String[] args) { Operation op = new Operation(); //创建一条线 List 
   
     points1 = new ArrayList 
    
      (); points1.add(op.point(0, 0)); points1.add(op.point(1, 3)); points1.add(op.point(2, 3)); LineString line1 = op.createLine(points1); //创建第二条线 List 
     
       points2 = new ArrayList 
      
        (); points2.add(op.point(3, 0)); points2.add(op.point(3, 3)); points2.add(op.point(5, 6)); LineString line2 = op.createLine(points2); System.out.println(op.distanceGeo(line1, line2));//out 1.0 System.out.println(op.intersectionGeo(line1, line2));//out GEOMETRYCOLLECTION EMPTY System.out.println(op.unionGeo(line1, line2)); //out MULTILINESTRING ((0 0, 1 3, 2 3), (3 0, 3 3, 5 6)) System.out.println(op.differenceGeo(line1, line2));//out LINESTRING (0 0, 1 3, 2 3) } } 
       
      
     
    
  

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/203951.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午9:13
下一篇 2026年3月19日 下午9:13


相关推荐

  • php代码执行函数_php代码如何运行

    php代码执行函数_php代码如何运行**php代码执行函数解析**​一、代码执行漏洞原理:用户输入的数据被当做后端代码进行执行<?php@eval($_REQUEST[8])?>//其实一句话木马的本质就是一个代码执行漏洞。用户输入的数据被当做代码进行执行。这里提一下RCE(remotecommand/codeexecute)远程命令或者代码执行。现在只要渗透的最终情况可以实现执行命令或者是代码都属于RCE,例如代码执行、文件包含、反序列化、命令执行,甚至是写文件Getshell都可以属于RCE在PHP存在诸多

    2026年4月16日
    5
  • DedeCMS实现自定义表单提交后发送指定QQ邮箱法

    DedeCMS实现自定义表单提交后发送指定QQ邮箱法

    2021年10月7日
    44
  • 10.MATLAB方差分析

    10.MATLAB方差分析方差分析是英国统计学家 R A Fisher 在 20 世纪 20 年代提出的一种统计方法 它有着非常广泛的应用 在生产实践和科学研究中 经验要研究生产条件或实验条件的改变对产品的质量或产量的影响 如在农业生产中 需要考虑品种 施肥量 种植密度等因素对农作物收获量的影响 又如某产品在不同的地区 不同的时期 采用不同的销售方式 其销售量是否有差异 在诸多影响因素中 哪些是主要的 哪些是次要的 以及主要因素处于

    2026年3月6日
    2
  • 案例:EVE和ENSP对接LLDP协议「建议收藏」

    案例:EVE和ENSP对接LLDP协议「建议收藏」1.EVE与ENSP使用cloud对接LLDP协议(拓扑)2.思科开启LLDP(EVE需使用2018年后的L2/L3IOU才支持LLDP功能)Switch(config)#lldprun//思科全局运行开启lldpSwitch(config)#inte0/1Switch(config-if)#lldptransmitSwitch(config-if)#lldpreceive//接口下开启lldp传送与接受华为开启LLDP[Huawei]lldpenableInfo:Glo

    2022年5月5日
    89
  • PhpStorm 2021.5.2 x64 简书 激活码【在线注册码/序列号/破解码】「建议收藏」

    PhpStorm 2021.5.2 x64 简书 激活码【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
    53
  • 二叉树性质及证明「建议收藏」

    二叉树性质及证明「建议收藏」二叉树性质及证明(1)规定根节点层次为0,则一棵非空二叉树的第i层上最多有2i个结点。(2)规定根节点层次为0,则深度为k的二叉树的最大结点数为2(k+1)-1。(3)具有n个结点的完全二叉树的深度k为不超过lb(n+1)-1的最大整数。(4)对于一棵非空二叉树,如果叶节点个数为n0,度为2的结点数为n2,则有n0=n2+1。(5)对于具有n个结点的完…

    2022年5月31日
    44

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号