RealMapServiceImpl.java 14.1 KB
package com.bsth.service.realcontrol.impl;

import com.bsth.common.ResponseCode;
import com.bsth.controller.realcontrol.dto.StationSpatialData;
import com.bsth.entity.CarPark;
import com.bsth.service.realcontrol.RealMapService;
import com.bsth.service.realcontrol.dto.SectionRouteCoords;
import com.bsth.util.CoordinateConverter;
import com.google.common.base.Splitter;
import com.google.common.collect.ArrayListMultimap;
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,ST_X(s.CENTER_POINT_WGS) G_LONX,ST_Y(s.CENTER_POINT_WGS) G_LATY,r.RADIUS,r.SHAPED_TYPE shapes_type,ST_AsText(r.BUFFER_POLYGON_WGS) 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> stationVersionSpatialData(String idx) {
        Map<String, Object> rs = new HashMap();
        try {
            //拼接in语句
            List<String> idArray = Splitter.on(",").splitToList(idx);
            StringBuffer inStr = new StringBuffer();
            for (String code : idArray) {
                inStr.append(",'");
                inStr.append(code);
                inStr.append("'");
            }
            String inStrs = " (" + inStr.toString().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,ST_X(s.CENTER_POINT_WGS) G_LONX,ST_Y(s.CENTER_POINT_WGS) G_LATY,r.RADIUS,r.SHAPED_TYPE shapes_type,ST_AsText(r.BUFFER_POLYGON_WGS) as G_POLYGON_GRID, r.STATION_ROUTE_CODE from bsth_c_ls_stationroute r inner join bsth_c_station s on r.station=s.id where r.line_code in " + inStrs + " 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,r.versions 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> findRouteAndVersionByLine(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,r.versions FROM bsth_c_ls_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.getVersions(), 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,ST_X(s.CENTER_POINT_WGS) G_LONX,ST_Y(s.CENTER_POINT_WGS) 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;
        CoordinateConverter.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 = CoordinateConverter.bd_encrypt(CoordinateConverter.transformFromWGSToGCJ(CoordinateConverter.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;
        CoordinateConverter.Location location;
        for (String item : list) {
            subArr = item.split(",");

            itemStr = new StringBuilder();
            for (String coord : subArr) {
                cds = coord.split(" ");
                location = CoordinateConverter.transformFromWGSToGCJ(CoordinateConverter.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;
    }
}