GeoTools使用之JTSFactoryFinder接口
GIS项目开发中用到的一个工具类
(依赖jar文件为 gt-opengis-8.0-M0.jar
、 jsr-275-1.0-beta-2.jar
更高版本不限定)
package com.giscafer.geometry;import java.util.ArrayList;
import java.util.List;import org.geotools.factory.Hints;
import org.geotools.geometry.GeneralDirectPosition;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.ReferencingFactoryFinder;
import org.opengis.geometry.DirectPosition;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CRSFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.CoordinateOperation;
import org.opengis.referencing.operation.CoordinateOperationFactory;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
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.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;public class GeometryUtil {private static GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null ); private static CRSFactory crsFactory = ReferencingFactoryFinder.getCRSFactory(null); //WGS2000经纬度坐标系private static String GCS_China_2000 = "GEOGCS[\"GCS_China_Geodetic_Coordinate_System_2000\",DATUM[\"D_China_2000\",SPHEROID[\"CGCS2000\",6378137.0,298.257222101]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]";//墨卡托平面坐标系,用距离计算、面积计算private static String Mercator_XY_WKT = "PROJCS[\"WGS_1984_Web_Mercator\",GEOGCS[\"GCS_WGS_1984_Major_Auxiliary_Sphere\",DATUM[\"D_WGS_1984_Major_Auxiliary_Sphere\",SPHEROID[\"WGS_1984_Major_Auxiliary_Sphere\",6378137.0,0.0]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Mercator\"],PARAMETER[\"False_Easting\",0.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",0.0],PARAMETER[\"Standard_Parallel_1\",0.0],UNIT[\"Meter\",1.0]]";//坐标转换对象/** * create a Point * @param x * @param y * @return */ public static Coordinate createPoint(double x,double y){ return new Coordinate(x,y); } /*** create a point* @return Geometry*/public static Point createGeoPoint(double x,double y){Coordinate coord = new Coordinate(x, y);Point point = geometryFactory.createPoint( coord );return point;}/*** 通过读取WKT字符串创建空间对象 * @param geomWKT* @return* @throws ParseException*/public Geometry createPointByWKT(String geomWKT) throws ParseException{ WKTReader reader = new WKTReader( geometryFactory ); Geometry geom =reader.read(geomWKT); return geom; } /** * create a line * @return */ public static LineString createLine(List<Coordinate> points){if(points.size()<2){points.add(points.get(0));}Coordinate[] coords = (Coordinate[]) points.toArray(new Coordinate[points.size()]); LineString line = geometryFactory.createLineString(coords); return line; }/*** 根据点串创建多边形* @param points* @return*/public static Polygon createPolygon(List<Coordinate> points){Coordinate[] coords = (Coordinate[]) points.toArray(new Coordinate[points.size()]); Polygon polygon=geometryFactory.createPolygon(coords);return polygon;}/** * 返回(A)与(B)中距离最近的两个点的距离 * @param a * @param b * @return */ public static double distanceGeo(Geometry a,Geometry b){ return a.distance(b); } /** * 两个几何对象的交集 * @param a * @param b * @return */ public static Geometry intersectionGeo(Geometry a,Geometry b){ return a.intersection(b); } /** * 几何对象合并 * @param a * @param b * @return */ public static Geometry unionGeo(Geometry a,Geometry b){ return a.union(b); } /** * 在A几何对象中有的,但是B几何对象中没有 * @param a * @param b * @return */ public static Geometry differenceGeo(Geometry a,Geometry b){ return a.difference(b); } /*** 获取线的长度,默认坐标为:坐标值为北京2000坐标系* @param line* @return* @throws TransformException * @throws FactoryException * @throws MismatchedDimensionException */public static double getLengthLonLat(LineString line) throws MismatchedDimensionException, FactoryException, TransformException{Coordinate []points=line.getCoordinates();List<Coordinate> pointsList=new ArrayList<Coordinate>();for(int i=0;i<points.length;i++){Coordinate point=lonLatToXY(points[i]);pointsList.add(point);}LineString lineXY=createLine(pointsList);return lineXY.getLength();}/*** 获取线的长度,默认坐标为平面坐标系* @param line* @return*/public static double getLengthXY(LineString line){return line.getLength();}/*** 获取多边形面积,输入坐标系为北京2000经纬度坐标系* @param polygon* @return* @throws MismatchedDimensionException* @throws FactoryException* @throws TransformException*/public static double getAreaLonLat(Polygon polygon) throws MismatchedDimensionException, FactoryException, TransformException{Coordinate []points=polygon.getCoordinates();List<Coordinate> pointsList=new ArrayList<Coordinate>();for(int i=0;i<points.length;i++){Coordinate point=lonLatToXY(points[i]);pointsList.add(point);}Polygon lineXY=createPolygon(pointsList);return lineXY.getArea();}/*** create a Circle 创建一个圆,圆心(x,y) 半径RADIUS* @param x* @param y* @param RADIUS 单位(米)* @return*/public static Polygon createCircle(double x, double y, final double RADIUS){final int SIDES = 32;//圆上面的点个数//距离转度double degree = RADIUS / (2 * Math.PI * 6378137.0) * 360;
// System.out.println("度:"+degree);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 ) * degree;double dy = Math.sin( angle ) * degree;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 point* @return* @throws FactoryException * @throws TransformException * @throws MismatchedDimensionException */public static Coordinate lonLatToXY(Coordinate coordinate) throws FactoryException, MismatchedDimensionException, TransformException{Hints hint = new Hints(Hints.LENIENT_DATUM_SHIFT, true);CoordinateReferenceSystem sourceCRS = crsFactory.createFromWKT(GCS_China_2000);CoordinateReferenceSystem targetCRS = crsFactory.createFromWKT(Mercator_XY_WKT);CoordinateOperationFactory coFactory = ReferencingFactoryFinder.getCoordinateOperationFactory(hint);CoordinateOperation co = coFactory.createOperation(sourceCRS,targetCRS);MathTransform transform = co.getMathTransform();double x = coordinate.x;double y = coordinate.y;DirectPosition point = new GeneralDirectPosition(x, y);point = transform.transform(point, point);double lat = point.getOrdinate(0);double lon = point.getOrdinate(1);return createPoint(lat,lon);}/*** @param args* @throws FactoryException * @throws TransformException * @throws MismatchedDimensionException */public static void main(String[] args) throws FactoryException, MismatchedDimensionException, TransformException {// TODO Auto-generated method stubCoordinate point1=GeometryUtil.createPoint(116.252189, 39.9065632);Coordinate point2=GeometryUtil.createPoint(116.251977, 39.9068492);Coordinate point3=GeometryUtil.createPoint(116.252168, 39.9065392);Coordinate point4=GeometryUtil.createPoint(116.255095, 39.9066112);List<Coordinate> points=new ArrayList<Coordinate>();List<Coordinate> points1=new ArrayList<Coordinate>();points.add(point1);points.add(point2);points.add(point3);points.add(point4);points.add(point1);points1.add(point1);//创建面Polygon polygon=GeometryUtil.createPolygon(points);//创建线LineString line1=GeometryUtil.createLine(points1);LineString line =GeometryUtil.createLine(points);//获取面积和长度System.out.println("line-changdu-lonlat:"+line.getLength());System.out.println("line-changdu-xy:"+GeometryUtil.getLengthLonLat(line));System.out.println("polygon-mianji-lonlat:"+polygon.getArea());System.out.println("line-changdu-xy:"+GeometryUtil.getAreaLonLat(polygon));System.out.println("toString()---"+line.toString());System.out.println("toString()---"+line1.toString());//判断某个点是否在圆内Polygon p = GeometryUtil.createCircle(116.1851444, 39.9269055, 200000);Point point=GeometryUtil.createPoint(119.185144, 39.9269);Boolean isOk=p.contains(point);System.out.println(isOk);}}
GeoTools使用之JTSFactoryFinder接口相关推荐
- geotools应用-JTS生产四叉树索引和R树索引
微信搜索:"二十同学" 公众号,欢迎关注一条不一样的成长之路 geotools介绍 geotools官网https://geotools.org/ Geotools是一个java类 ...
- 解析FeatureCollection(Geotools对geojson操作出现的问题)
微信搜索:"二十同学" 公众号,欢迎关注一条不一样的成长之路 GeoJSON格式示例 {"type":"FeatureCollection" ...
- GeoTools 笔记
概述 GeoTools 是一个基于开放地理空间联盟(OGC)数据结构规范的 Java 开源地理空间数据操作库. 参考资料:官方教程 | 官方文档 | EPSP 简介 核心功能 定义关键空间概念 ...
- geotools读取shp文件及shp文件操作工具类代码
geotools读取shp文件及shp文件操作工具类代码.pdf 完整文档下载地址 https://download.csdn.net/download/a772304419/17468931 imp ...
- GeoTools应用-JTS(Geometry之间的关系)
几何信息和拓扑关系是地理信息系统中描述地理要素的空间位置和空间关系的不可缺少的基本信息.其中几何信息主要涉及几何目标的坐标位置.方向.角度.距离和面积等信息,它通常用解析几何的方法来分析.而空间关系信 ...
- 使用geotools构建特殊的多边形
geotools介绍 Geotools是一个基于Java的开源地理信息系统(GIS)工具包,可以用于处理和分析地理数据. 构建图形 我们地球上所说的两点之间的距离基本上都是弧长,所以要有弧长转换为弧度 ...
- GeoJson的生成与解析,JSON解析,Java读写geojson,geotools读取shp文件,Geotools中Geometry对象与GeoJson的相互转换
GeoJson的生成与解析 一.wkt格式的geometry转成json格式 二.json格式转wkt格式 三.json格式的数据进行解析 四.Java读写geojson 五.geotools读取sh ...
- Java使用geotools将Geometry(地图要素)导出为shp文件
做图形方面的功能,往往会遇到将地图要素导出成shap文件的需求,现整理记录一下方便后期直接使用. 1.需要用到的依赖 <geotools.version>20.5</geotools ...
- Geotools中蜂巢的实现
概述 本文讲述如何在geotools中实现蜂巢效果. 效果 实现 1.扩展类IntersectionBuilder import com.vividsolutions.jts.geom.Coordin ...
最新文章
- PC微信逆向:使用HOOK拦截二维码
- clojure学习记录
- SpringMVC的XML配置解析
- c语言判断一个点在长方体内部_21个入门练手项目,让你轻松玩转C语言
- 从网游策划谈《梦幻西游》的成功之道
- root cause of exception single deletion failure
- ABAP的语法高亮是如何在浏览器里显示的
- 网易严选Java开发三面面经:java读文件内容
- how to install tensorflow-gpu==1.12.0
- vs下C# WinForm 解决方案里面生成的文件都是什么作用?干什么的?
- kuangbin 基础DP1
- 课程设计 - 运动控制卡(云服务器)
- 创建数据库,库名为考生姓名拼音的缩写,例如考生姓名为张三
- java日志系统框架整理(转载)
- 图片底部留白怎么处理
- 黑苹果引导工具 Clover 配置详解
- Flutter环境搭建、运行gallary项目
- 计算机应用和教学,《计算机应用基础》教学方法浅析
- Unity Hair 毛发系统 初体验
- 网络安全下用c语言写蠕虫病毒,神经网络在计算机网络安全管理中的应用
热门文章
- 独立站可以一个人做吗?
- 经典排序算法总结与Python实现(上)
- 数学建模 TOPSIS法
- 吴恩达机器学习【第三天】线性代数基础知识
- 计算机网络设备配置遇到的问题,网络设备使用与维护
- SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“.解决方法
- 解决SQLServer占用80端口问题
- 第12章[12.4] 鼠标移入移除时弹出和关闭窗口
- Web API 开发入门--基于Visual Studio
- [概率论与数理统计] 常用定义与公式