网站空间支持功能,学网页设计的课程,模板网站 动易,购物软件哪个更好更便宜常用可以用于GIS数据处理和空间计算的java包有geotool和jts。
相对来说#xff0c;geotool功能更全面#xff0c;还可以用于数据转换、瓦片地图发布、栅格影像分析等#xff0c;jts只能进行基本的数据处理和空间计算。
但大多数情况下jts就完全够用了。
geotool的官网geotool功能更全面还可以用于数据转换、瓦片地图发布、栅格影像分析等jts只能进行基本的数据处理和空间计算。
但大多数情况下jts就完全够用了。
geotool的官网https://www.geotools.org/
本例只讲jts的用法
maven依赖
dependencygroupIdcom.vividsolutions/groupIdartifactIdjts/artifactIdversion1.13/version
/dependencyimport com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.index.strtree.STRtree;import java.util.List;public class RTreeDemo {public static void main(String[] args){//声明STRtreeSTRtree strTree new STRtree();GeometryFactory geometryFactory new GeometryFactory();for (int i0;i50;i){for (int j0;j50;j){//新建一个点Geometry pgeometryFactory.createPoint(new Coordinate(i,j));//以点为中心取一个半径为1.0的圆插入RTree
//insert(Envelope itemEnv, Object item)strTree.insert(p.buffer(1.0).getEnvelopeInternal(),p.buffer(1.0));}}//构建RTreestrTree.build();//查询点[1,1]落在哪些圆中List querysstrTree.query(geometryFactory.createPoint(new Coordinate(1,1)).getEnvelopeInternal());for (Object obj:querys) {System.out.println(obj);}}
}线裁切面如图所示用红色线去裁切蓝色面结果会生成3个面。 import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import com.vividsolutions.jts.operation.polygonize.Polygonizer;import java.util.ArrayList;
import java.util.Collection;
import java.util.List;public class PolygonDemo {public static void main(String[] args){// wkt工具将wkt文本转为geometry对象WKTReader wktReadernew WKTReader();try {//读被裁切面Geometry polygonwktReader.read(POLYGON ((220 350, 400 440, 635 249, 380 80, 174 164, 179 265, 220 350)));//读裁切线Geometry polylinewktReader.read(LINESTRING (570 400, 392 315, 299 215, 430 140, 530 240, 450 360, 460 480));//取面的边线Geometry boundarypolygon.getBoundary();//将裁切线与面的边线联合交点会被打断polylinepolyline.union(boundary);ListGeometry clipPolygon new ArrayList();ListGeometry lineListnew ArrayList();for(int i0;ipolyline.getNumGeometries();i){lineList.add(polyline.getGeometryN(i));}// 构造面生成器Polygonizer p new Polygonizer();p.add(lineList);//取构面结果CollectionGeometry polys p.getPolygons();//取buffer以免因精度损失遗漏构面结果Geometry bufferpolygon.buffer(1);for(Geometry geometry:polys){//如果包含在buffer中则添加if(buffer.contains(geometry)){clipPolygon.add(geometry);}}for (Geometry presult:clipPolygon) {System.out.println(presult);}} catch (ParseException e) {e.printStackTrace();}}
}将裁切线换成LINESTRING (320 330, 500 280, 400 150, 290 200, 400 360)结果如下 结果可以在JTS TestBuilder中查验。
在控制台input的文本框中输入wkt文本点击load geometrys就可以在面板中展示图形。