引入相关pom依赖,我这边是引用全部的。利用jar包的方式引用过来的。
org.geotools gt-shapefile 19.2 system ${project.basedir}/src/main/resources/lib/gt-shapefile-19.2.jar org.ejml ejml-ddense 0.39 system ${project.basedir}/src/main/resources/lib/ejml-ddense-0.32.jar org.ejml ejml-core 0.39 system ${project.basedir}/src/main/resources/lib/ejml-core-0.39.jar org.geotools gt-opengis 19.2 system ${project.basedir}/src/main/resources/lib/gt-opengis-19.2.jar org.geotools gt-data 19.2 system ${project.basedir}/src/main/resources/lib/gt-data-19.2.jar org.geotools gt-api 19.2 system ${project.basedir}/src/main/resources/lib/gt-api-19.2.jar org.geotools gt-main 19.2 system ${project.basedir}/src/main/resources/lib/gt-main-19.2.jar org.geotools gt-metadata 19.2 system ${project.basedir}/src/main/resources/lib/gt-metadata-19.2.jar org.geotools gt-referencing 19.2 system ${project.basedir}/src/main/resources/lib/gt-referencing-19.2.jar org.geotools gt-geojson 19.2 system ${project.basedir}/src/main/resources/lib/gt-geojson-19.2.jar com.vividsolutions jts 1.13 system ${project.basedir}/src/main/resources/lib/jts-1.13.jar
相关资源包可到https://download.csdn.net/download/saygood999/86539069进行下载
1、判断点是否在面内
WKTReader2 wktReader = new WKTReader2(); Point point = new GeometryFactory().createPoint(new Coordinate(110.00741191345466,31.08890400396027)); Geometry geometry = wktReader.read("MULTIPOLYGON(((110.00741191345466 31.08890400396027,112.10423570721355 31.08890400396027,112.10423570721355 35.14276333856076,110.00741191345466 35.14276333856076,110.00741191345466 31.08890400396027)))"); StringWriter writer = new StringWriter(); System.out.println(geometry.contains(point));
如果单纯的contains其实并不能满足正确的判断。这个方法,我发现了一个问题,如果在面的最边缘上和面进行相交与一点。这个方法是返回false的。
所以就要加上另个判断 geometry.intersects(point)相交
System.out.println(geometry.contains(point) || geometry.intersects(point));
意思满足两者任何一个就行。
2、wkb格式数据转换成geojson
WKTReader2 wktReader = new WKTReader2(); Geometry geometry = wktReader.read("MULTIPOLYGON(((110.00741191345466 31.08890400396027,112.10423570721355 31.08890400396027,112.10423570721355 35.14276333856076,110.00741191345466 35.14276333856076,110.00741191345466 31.08890400396027)))"); StringWriter writer = new StringWriter(); GeometryJSON geometryJSON = new GeometryJSON(); geometryJSON.write(geometry,writer); System.out.println(writer.toString());
类似还有很多的方法。比较判断面积大小,其实搞过postgis的,知道函数的。在geotools上都能找到对应的方法。postgis函数推荐看这篇postgresql gis常用函数介绍_王威振的csdn的博客-CSDN博客_postgresql st函数
geotools学习文档