GeoTools判断一个点是否在多边形上

2 篇文章 0 订阅
2 篇文章 0 订阅

https://repo.osgeo.org/service/rest/repository/browse/release/org/geotools
http://docs.geotools.org/latest/userguide/library/jts/index.html
https://sourceforge.net/projects/geotools/files/GeoTools%2023%20Releases/23.0/
https://github.com/geotools/geotools

        <dependency>
            <groupId>com.vividsolutions</groupId>
            <artifactId>jts</artifactId>
            <version>1.13</version>
        </dependency>
import com.vividsolutions.jts.algorithm.PointLocator;
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import com.vividsolutions.jts.operation.buffer.BufferOp;

	/**
	 * 判断以x,y为坐标的点point(x,y)是否在geometry表示的Polygon中
	 * @param x
	 * @param y
	 * @param geometry wkt格式 POLYGON((0 0, 10 0, 10 10, 0 10,0 0))
	 * @return
	 */
	public boolean withinGeo(double x,double y,String geometry) throws ParseException {
		GeometryFactory geometryFactory = new GeometryFactory();
		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);
	}
	
		public static void main(String[] args) throws IOException, ParseException {
		String wktPoly = "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"; //请自行搜素了解wkt格式
		String wktPoint = "POINT (30 30)";
		GeometryFactory geometryFactory = new GeometryFactory();
		WKTReader reader = new WKTReader(geometryFactory);
		Geometry point = reader.read(wktPoint);
		Geometry poly = reader.read(wktPoly);
		boolean ex = poly.contains(point);
		System.out.println("ex = " + ex);
		LineString geometry = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");
		LineString geometry2 = (LineString) reader.read("LINESTRING(5 0, 0 0)");
		boolean b = geometry.equals(geometry2);
		System.out.println("b = " + b);
		System.out.println(withinGeo(5,5,"POLYGON((0 0, 10 0, 10 10, 0 10,0 0))"));
	}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值