• GisUtil工具类:将WKT(wellKnownText)文本转换为ElasticSearch识别的空间对象字符串形式


    ElasticSearch中的Geo Shape Type:
     
    从熟知文本或熟知二进制数据中生成的 ST_Geometry:
     
    package com.nihaorz.utils;
    
    import com.vividsolutions.jts.geom.*;
    import com.vividsolutions.jts.io.ParseException;
    import com.vividsolutions.jts.io.WKTReader;
    import org.geotools.geometry.jts.JTSFactoryFinder;
    
    /**
     * Created by Administrator on 2016/4/1.
     */
    public class GisUtil {
    
        private final static WKTReader wktReader = new WKTReader(JTSFactoryFinder.getGeometryFactory());
    
        /**
         * 将WKT文本字符串转换为ES中的GeoShape字符串格式
         * @param wkt
         * @return
         * @throws ParseException
         */
        public static String getESGeoTextFromWkt(String wkt) throws ParseException {
            String result = null;
            Geometry geom = wktReader.read(wkt);
            String type = geom.getClass().getSimpleName().toLowerCase();
            Point point;
            MultiPoint multiPoint;
            LineString lineString;
            MultiLineString multiLineString;
            Polygon polygon;
            MultiPolygon multiPolygon;
            if(type.equals("point")){
                point = (Point) geom;
                result = getESPointText(point);
            }else if(type.equals("linestring")){
                lineString = (LineString) geom;
                result = getESLineStringText(lineString);
            }else if(type.equals("polygon")){
                polygon = (Polygon) geom;
                result = getESPolygonText(polygon);
            }else if(type.equals("multipoint")){
                multiPoint = (MultiPoint) geom;
                result = getESMultiPointText(multiPoint);
            }else if(type.equals("multilinestring")){
                multiLineString = (MultiLineString) geom;
                result = getESMultiLineStringText(multiLineString);
            }else if(type.equals("multipolygon")){
                multiPolygon = (MultiPolygon) geom;
                result = getESMultiPolygonText(multiPolygon);
            }
            return result;
        }
    
        /**
         * 通过MultiPolygon对象拼接中括号表示的字符串
         * @param multiPolygon
         * @return
         */
        private static String getESMultiPolygonText(MultiPolygon multiPolygon) throws ParseException {
            Polygon polygon;
            int num = multiPolygon.getNumGeometries();
            StringBuffer sb = new StringBuffer("[");
            for(int i = 0; i < num; i++){
                polygon = (Polygon) multiPolygon.getGeometryN(i);
                sb.append(getESPolygonText(polygon)+",");
            }
            sb.setCharAt(sb.length() - 1, ']');
            return sb.toString();
        }
    
        /**
         * 通过MultiLineString对象拼接中括号表示的字符串
         * @param multiLineString
         * @return
         */
        private static String getESMultiLineStringText(MultiLineString multiLineString) {
            LineString lineString;
            int num = multiLineString.getNumGeometries();
            StringBuffer sb = new StringBuffer("[");
            for(int i = 0; i < num; i++){
                lineString = (LineString) multiLineString.getGeometryN(i);
                sb.append(getESLineStringText(lineString)+",");
            }
            sb.setCharAt(sb.length() - 1, ']');
            return sb.toString();
        }
    
        /**
         * 通过MultiPoint对象拼接中括号表示的字符串
         * @param multiPoint
         * @return
         */
        private static String getESMultiPointText(MultiPoint multiPoint) {
            Point point;
            int num = multiPoint.getNumGeometries();
            StringBuffer sb = new StringBuffer("[");
            for(int i = 0; i < num; i++){
                point = (Point) multiPoint.getGeometryN(i);
                sb.append(getESPointText(point)+",");
            }
            sb.setCharAt(sb.length() - 1, ']');
            return sb.toString();
        }
    
        /**
         * 通过Polygon对象拼接中括号表示的字符串
         * @param polygon
         * @return
         * @throws ParseException
         */
        private static String getESPolygonText(Polygon polygon){
            String result;
            /**
             * 内部闭合环形的数量
             */
            int num = polygon.getNumInteriorRing();
            if(num > 0){
                StringBuffer sb = new StringBuffer("["+getESLineStringText(polygon.getExteriorRing())+",");
                for(int i = 0; i < num; i++){
                    sb.append(getESLineStringText(polygon.getInteriorRingN(i))+",");
                }
                sb.setCharAt(sb.length() - 1, ']');
                result = sb.toString();
            }else{
                result = "["+getESLineStringText(polygon.getExteriorRing())+"]";
            }
            return result;
        }
    
        /**
         * 通过LineString对象拼接中括号表示的字符串
         * @param lineString
         * @return
         */
        public static String getESLineStringText(LineString lineString){
            Coordinate[] corrds = lineString.getCoordinates();
            StringBuffer sb = new StringBuffer("[");
            for(Coordinate corrd : corrds) {
                sb.append(getCoordinateText(corrd)+",");
            }
            sb.setCharAt(sb.length() - 1, ']');
            return sb.toString();
        }
    
        /**
         * 通过Point对象拼接中括号表示的字符串
         * @param point
         * @return
         */
        public static String getESPointText(Point point){
            return getCoordinateText(point.getCoordinate());
        }
    
        /**
         * 通过Coordinate对象拼接中括号表示的字符串
         * @param coord
         * @return
         */
        public static String getCoordinateText(Coordinate coord) {
            return "["+coord.x+","+coord.y+"]";
        }
        
    }
    

      

     

    附:

    空心面的WKT文本格式:

    POLYGON  (( 114.69311270 32.15375671, 114.69311270 30.38273703, 117.16663685 30.38273703, 117.16663685 32.15375671, 114.69311270 32.15375671),( 115.46055456 31.36860465, 115.96824687 31.67558140, 116.33425760 31.37450805, 115.81475850 31.01440072, 115.46055456 31.36860465))

    ArcMap中的预览效果:

  • 相关阅读:
    SpringMVC之使用ResponseEntity
    紧随时代的步伐--Java8特性之接口默认方法
    Executor多线程框架
    Jsoup入门
    Echart、Excel、highcharts、jfreechart对比
    JFreeChart入门
    Spring定时任务(@Scheduled)
    Java正则表达式入门基础篇
    Vue.js之入门
    springboot rabbitmq direct exchange和topic exchange 写法上关于路由键的区别
  • 原文地址:https://www.cnblogs.com/nihaorz/p/5344925.html
Copyright © 2020-2023  润新知