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

判断一个点是否在多边形上,GIS里经常要用的功能,但在平面几何上也是可用的。

 

这里借助大名鼎鼎的geotools实现

 

jar包依赖,pom.xml

 

Java代码  
  1. <?xml version=“1.0” encoding=“UTF-8”?>  
  2. <project xmlns=“http://maven.apache.org/POM/4.0.0”  
  3.          xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”  
  4.          xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>  
  5.     <modelVersion>4.0.0</modelVersion>  
  6.   
  7.     <groupId>com.wisely</groupId>  
  8.     <artifactId>point</artifactId>  
  9.     <version>1.0-SNAPSHOT</version>  
  10.     <properties>  
  11.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  12.         <geotools.version>13.1</geotools.version>  
  13.     </properties>  
  14.   
  15.     <dependencies>  
  16.         <dependency>  
  17.             <groupId>org.geotools</groupId>  
  18.             <artifactId>gt-geojson</artifactId>  
  19.             <version>${geotools.version}</version>  
  20.         </dependency>  
  21.         <dependency>  
  22.             <groupId>org.geotools</groupId>  
  23.             <artifactId>gt-geometry</artifactId>  
  24.             <version>${geotools.version}</version>  
  25.         </dependency>  
  26.         <dependency>  
  27.             <groupId>org.geotools</groupId>  
  28.             <artifactId>gt-epsg-hsql</artifactId>  
  29.             <version>${geotools.version}</version>  
  30.         </dependency>  
  31.         <dependency>  
  32.             <groupId>org.geotools</groupId>  
  33.             <artifactId>gt-jts-wrapper</artifactId>  
  34.             <version>${geotools.version}</version>  
  35.         </dependency>  
  36.     </dependencies>  
  37.   
  38.     <repositories>  
  39.         <repository>  
  40.             <id>maven2-repository.dev.java.net</id>  
  41.             <name>Java.net repository</name>  
  42.             <url>http://download.java.net/maven/2</url>  
  43.         </repository>  
  44.         <repository>  
  45.             <id>osgeo</id>  
  46.             <name>Open Source Geospatial Foundation Repository</name>  
  47.             <url>http://download.osgeo.org/webdav/geotools/</url>  
  48.         </repository>  
  49.     </repositories>  
  50.   
  51.   
  52. </project>  

 

 代码

Java代码  
  1.  String wktPoly = “POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))”//请自行搜素了解wkt格式  
  2.  String wktPoint = “POINT (30 30)”;    
  3. WKTReader reader = new WKTReader(JTSFactoryFinder.getGeometryFactory());  
  4.         GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);  
  5. Geometry point = reader.read(wktPoint);  
  6. Geometry poly = reader.read(wktPoly);  
  7. poly.contains(point); //返回true或false  

转载自:https://blog.csdn.net/qiu265843468/article/details/72726052

You may also like...

退出移动版