RealMapServiceImpl.java 15.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
package com.bsth.service.realcontrol.impl;

import com.bsth.common.ResponseCode;
import com.bsth.controller.realcontrol.dto.StationSpatialData;
import com.bsth.data.gpsdata.arrival.utils.GeoUtils;
import com.bsth.entity.CarPark;
import com.bsth.service.realcontrol.RealMapService;
import com.bsth.service.realcontrol.dto.SectionRouteCoords;
import com.bsth.util.TransGPS;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.ArrayListMultimap;
import com.vividsolutions.jts.geom.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by panzhao on 2016/11/23.
 */
@Service
public class RealMapServiceImpl implements RealMapService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Map<String, Object> stationSpatialData(String idx) {
        Map<String, Object> rs = new HashMap();

        try {
            List<String> idArray = Splitter.on(",").splitToList(idx);
            //拼接in语句
            String inStr = "";
            for (String code : idArray) {
                inStr += (",'" + code + "'");
            }
            inStr = " (" + inStr.substring(1) + ")";

            String sql = "select r.LINE_CODE,r.STATION_NAME,r.STATION_CODE,r.STATION_MARK,r.DIRECTIONS,r.DISTANCES,r.TO_TIME, r.VERSIONS,s.G_LONX,s.G_LATY,s.RADIUS,s.SHAPES_TYPE,ST_AsText(s.G_POLYGON_GRID) as G_POLYGON_GRID, r.STATION_ROUTE_CODE from bsth_c_stationroute r inner join bsth_c_station s on r.station=s.id where r.line_code in " + inStr + " and r.destroy=0";

            List<StationSpatialData> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper(StationSpatialData.class));
            rs.put("status", ResponseCode.SUCCESS);
            rs.put("list", list);
        } catch (Exception e) {
            logger.error("", e);
            rs.put("status", ResponseCode.ERROR);
            rs.put("msg", "查询站点空间数据出现异常!");
        }

        return rs;
    }

    @Override
    public Map<String, Object> carParkSpatialData() {
        Map<String, Object> rs = new HashMap();

        try {
            String sql = "select ID, AREA,PARK_CODE,PARK_NAME,ST_AsText(G_PARK_POINT) as G_PARK_POINT,G_CENTER_POINT,RADIUS,SHAPES_TYPE from bsth_c_car_park WHERE destroy=0 and shapes_type='d'";

            List<CarPark> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper(CarPark.class));
            rs.put("status", ResponseCode.SUCCESS);
            rs.put("list", list);
        } catch (Exception e) {
            logger.error("", e);
            rs.put("status", ResponseCode.ERROR);
            rs.put("msg", "查询停车场空间数据出现异常!");
        }
        return rs;
    }

    @Override
    public Map<String, Object> findRouteByLine(String idx) {
        Map<String, Object> rs = new HashMap<>();
        StringBuilder inCond = new StringBuilder("(");
        List<String> codeList = Splitter.on(",").splitToList(idx);
        for(String lineCode : codeList){
            inCond.append("'" + lineCode + "',");
        }
        inCond.deleteCharAt(inCond.length() - 1).append(")");

        String sql = "SELECT r.ID,r.LINE_CODE,r.SECTION_CODE,r.SECTIONROUTE_CODE,r.DIRECTIONS,s.SECTION_NAME,ST_AsText(s.GSECTION_VECTOR) GSECTION_VECTOR,s.SECTION_DISTANCE,s.SECTION_TIME FROM bsth_c_sectionroute r INNER JOIN bsth_c_section s on r.section_code=s.section_code WHERE r.line_code in "+inCond.toString()+" and r.destroy=0 order by sectionroute_code";
        List<SectionRouteCoords> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper(SectionRouteCoords.class));

        //排序
        Collections.sort(list, new Comparator<SectionRouteCoords>() {
            @Override
            public int compare(SectionRouteCoords o1, SectionRouteCoords o2) {
                return Integer.parseInt(o1.getSectionrouteCode()) - Integer.parseInt(o2.getSectionrouteCode());
            }
        });

        ArrayListMultimap<String, SectionRouteCoords> listMultimap = ArrayListMultimap.create();
        for (SectionRouteCoords sr : list) {
            //按lineCode 分组
            listMultimap.put(sr.getLineCode(), sr);
        }
        //坐标转换
        Map<String, Object> subMap;
        Set<String> ks = listMultimap.keySet();
        List<SectionRouteCoords> sublist;
        List<String> upList,downList;
        String vectorStr = "";
        for(String k : ks){
            subMap = new HashMap<>();
            sublist = listMultimap.get(k);
            upList = new ArrayList<>();
            downList = new ArrayList<>();
            for(SectionRouteCoords sr : sublist){
                vectorStr = sr.getGsectionVector();
                vectorStr = vectorStr.substring(11, vectorStr.length() - 2);
                if (sr.getDirections() == 0)
                    upList.add(vectorStr);
                else
                    downList.add(vectorStr);
            }
            subMap.put("up", upList);
            //subMap.put("upJoins", jointCoords(upList));
            subMap.put("down", downList);
            subMap.put("up_bd", multiWgsToBd(upList));
            subMap.put("down_bd", multiWgsToBd(downList));
            subMap.put("up_gcj", multiWgsToGcj(upList));
            subMap.put("down_gcj", multiWgsToGcj(downList));

            rs.put(k, subMap);
        }
        return rs;
    }

    @Override
    public Map<String, Object> findRouteAndStationByLine(String lineCode) {
        Map<String, Object> rs = new HashMap<>();
        try {
            //查询路段信息
            String sql = "select r.LINE_CODE,r.SECTION_CODE,r.SECTIONROUTE_CODE,s.SECTION_NAME,ST_AsText(s.GSECTION_VECTOR) as GSECTION_VECTOR, r.DIRECTIONS from bsth_c_sectionroute r INNER JOIN bsth_c_section s on r.section=s.id where r.line_code=? and r.destroy=0";
            List<Map<String, Object>> secList = jdbcTemplate.queryForList(sql, lineCode);
            rs.put("section", secList);

            //查询站点信息
            sql = "select r.STATION_NAME,r.STATION_ROUTE_CODE,r.LINE_CODE,r.STATION_CODE,r.STATION_MARK,s.G_LONX,s.G_LATY, r.DIRECTIONS from bsth_c_stationroute r INNER JOIN bsth_c_station s on r.station=s.id and r.line_code=? and r.destroy=0";
            List<Map<String, Object>> stationList = jdbcTemplate.queryForList(sql, lineCode);
            rs.put("station", stationList);

            rs.put("status", ResponseCode.SUCCESS);
        } catch (DataAccessException e) {
            logger.error("", e);
            rs.put("status", ResponseCode.ERROR);
        }
        return rs;
    }

    @Override
    public Map<String, Object> multiSectionRoute(String codeIdx) {
        Map<String, Object> rs = new HashMap<>();
        try {
            List<String> idArray = Splitter.on(",").splitToList(codeIdx);
            //拼接in语句
            String inStr = "";
            for (String code : idArray) {
                inStr += (",'" + code + "'");
            }
            inStr = " (" + inStr.substring(1) + ")";

            String sql = "SELECT r.LINE_CODE,r.SECTION_CODE,r.SECTIONROUTE_CODE,s.SECTION_NAME,ST_AsText (s.GSECTION_VECTOR) AS GSECTION_VECTOR,r.DIRECTIONS FROM bsth_c_sectionroute r INNER JOIN bsth_c_section s ON r.section = s.id WHERE r.line_code in " + inStr + " AND r.destroy = 0 order by r.line_code, r.directions,r.sectionroute_code";

            List<Map<String, Object>> secList = jdbcTemplate.queryForList(sql);
            rs.put("section", secList);
            rs.put("status", ResponseCode.SUCCESS);
        } catch (DataAccessException e) {
            logger.error("", e);
            rs.put("status", ResponseCode.ERROR);
        }

        return rs;
    }

    @Override
    public Map<String, Object> multiRouteByLine(String codeStr) {
        List<String> codeArray = Splitter.on(",").splitToList(codeStr);
        Map<String, Object> rs = new HashMap<>();

        for(String code : codeArray){
            rs.put(code + "_route" ,findRouteByLine(code));
        }
        return rs;
    }

    /**
     * wgs 坐标数组转 百度
     *
     * @param list
     * @return
     */
    private List<String> multiWgsToBd(List<String> list) {
        List<String> bdList = new ArrayList<>();

        StringBuilder itemStr;
        String[] subArr, cds;
        TransGPS.Location location;
        for (String item : list) {
            subArr = item.split(",");

            itemStr = new StringBuilder();
            for (String coord : subArr) {
                cds = coord.split(" ");
                //城建转经纬度
                //Map<String, Double> map = JWDUtil.ConvertSHToJW(Double.parseDouble(cds[0]), Double.parseDouble(cds[1]));


                location = TransGPS.bd_encrypt(TransGPS.transformFromWGSToGCJ(TransGPS.LocationMake(Double.parseDouble(cds[0]), Double.parseDouble(cds[1]))));
                itemStr.append(location.getLng() + " " + location.getLat() + ",");
            }

            bdList.add(itemStr.substring(0, itemStr.length() - 1));
        }
        return bdList;
    }

    /**
     * wgs 坐标数组转 Gcj
     *
     * @param list
     * @return
     */
    private List<String> multiWgsToGcj(List<String> list) {
        List<String> gcjList = new ArrayList<>();

        StringBuilder itemStr;
        String[] subArr, cds;
        TransGPS.Location location;
        for (String item : list) {
            subArr = item.split(",");

            itemStr = new StringBuilder();
            for (String coord : subArr) {
                cds = coord.split(" ");
                location = TransGPS.transformFromWGSToGCJ(TransGPS.LocationMake(Double.parseDouble(cds[0]), Double.parseDouble(cds[1])));
                itemStr.append(location.getLng() + " " + location.getLat() + ",");
            }

            gcjList.add(itemStr.substring(0, itemStr.length() - 1));
        }
        return gcjList;
    }

    /**
     * 将相连的路段拼接起来,去掉接口覆盖区域。
     * 主要因为前端地图绘制时,过多的路段会带来性能消耗。在这里提前拼接好以减少路段数量
     * @param list
     * @return
     */
    private List<String> jointCoords(List<String> list){
        List<String> rs = new ArrayList<>();
        int len = list.size();
        if(len == 0)
            return rs;

        String joinstr = list.get(0);
        String str;
        for(int i = 1; i < len; i ++){
            str = jointCoords(joinstr.toString(), list.get(i));
            if(str != null)
                joinstr = str;
            else{
                rs.add(joinstr.toString());
                joinstr = list.get(i);
            }
        }
        rs.add(joinstr);
        return rs;
    }

    static GeometryFactory geometryFactory = new GeometryFactory();
    private String jointCoords(String c1, String c2){
        String rs=null;
        LineString s1 = createLineString(c1);
        LineString s2 = createLineString(c2);


        /*if(s1.intersects(s2)){
            //2条线相交
            Geometry g = s1.intersection(s2);
            Point inPoint = geometryFactory.createPoint(g.getCoordinates()[0]);
            //s1 = replacePoint(s1, s1.getCoordinates().length - 1, inPoint);
            //s2 = replacePoint(s2, 0, inPoint);

            //rs = toString(s1);
            //rs += ("," + toString(s2));
            rs = replacePoint(c1, s1.getCoordinates().length - 1, inPoint);
            rs += ("," + replacePoint(c2, 0, null));
        }
        else {*/
            //距离10米内的点直接连起来
            double distance = GeoUtils.getDistanceLineToLine(s1.getStartPoint(), s1.getEndPoint(), s2.getStartPoint(), s2.getEndPoint());
            if(distance < 10){
                //s2 = replacePoint(s2, 0, s1.getEndPoint());
                rs = c1;

                //获取c1终点到 c2 的垂直交点
                //s1.getEndPoint()
                //rs += ("," + replacePoint(c2, 0, null));
                //rs = toString(s1);
                //rs += ("," + toString(s2));
            }
        //}
        return rs;
    }

    /**
     * 替换线段中指定的一个点位
     * @param index
     * @param p

    private LineString replacePoint(LineString line, int index, Point p){
        Coordinate[] array = line.getCoordinates();
        array[index] = p.getCoordinate();

        return geometryFactory.createLineString(array);
    }*/

    private String replacePoint(String lineStr, int index, Point p){
        List<String> list = new ArrayList<>(Splitter.on(",").splitToList(lineStr));
        list.remove(index);
        if(p != null){
            list.add(index, p.getX() + " " + p.getY());
        }
        return Joiner.on(",").join(list);
    }

    private LineString createLineString(String c){
        List<String> list = Splitter.on(",").splitToList(c);

        Coordinate[] cds = new Coordinate[list.size()];
        String[] strs;
        for(int i = 0; i < list.size(); i ++){
            strs = list.get(i).split(" ");
            cds[i] = new Coordinate(Float.parseFloat(strs[1]), Float.parseFloat(strs[0]));
        }
        return geometryFactory.createLineString(cds);
    }

    private String toString(LineString line){
        Coordinate[] all = line.getCoordinates();

        Coordinate[] cs;
        StringBuilder sb = new StringBuilder("");
        for(int i = 0; i < all.length - 1; i ++){
            cs = new Coordinate[2];
            cs[0] = all[i];
            cs[1] = all[i + 1];

            sb.append("," + cs[0].y + " " + cs[0].x);
            sb.append("," + cs[1].y + " " + cs[1].x);
        }
        sb.deleteCharAt(0);
        return sb.toString();
    }

/*    private String truncationEnd(LineString lineString, Point p){
        Coordinate[] all = lineString.getCoordinates();
        LineString sl;

        Coordinate[] cs;
        StringBuilder sb = new StringBuilder("");
        double threshold = 0.1,distance;
        for(int i = 0; i < all.length - 1; i ++){
            cs = new Coordinate[2];
            cs[0] = all[i];
            cs[1] = all[i + 1];
            sl = geometryFactory.createLineString(cs);

            sb.append("," + cs[0].y + " " + cs[0].x);
            distance = GeoUtils.getDistanceFromLine(sl, p);
            if(distance < threshold){
                sb.append("," + p.getY() + " " + p.getX());
                break;
            }
            else
                sb.append("," + cs[1].y + " " + cs[1].x);
        }
        sb.deleteCharAt(0);
        return sb.toString();
    }

    private String truncationStart(LineString lineString, Point p){
        Coordinate[] all = lineString.getCoordinates();
        LineString sl;

        Coordinate[] cs;
        StringBuilder sb = new StringBuilder("");
        double threshold = 0.1,distance;
        for(int i = 0; i < all.length - 1; i ++){
            cs = new Coordinate[2];
            cs[0] = all[i];
            cs[1] = all[i + 1];
            sl = geometryFactory.createLineString(cs);

            distance = GeoUtils.getDistanceFromLine(sl, p);
            if(distance < threshold){
                sb.append("," + p.getY() + " " + p.getX());
                sb.append("," + cs[1].y + " " + cs[1].x);
                break;
            }
            else{
                sb.append("," + cs[0].y + " " + cs[0].x);
                sb.append("," + cs[1].y + " " + cs[1].x);
            }
        }
        sb.deleteCharAt(0);
        return sb.toString();
    }*/
}