GeoCacheData.java 3.4 KB
package com.bsth.data.gpsdata.analyse;

import com.bsth.data.gpsdata.GpsEntity;
import com.google.common.collect.ArrayListMultimap;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by panzhao on 2016/12/23.
 */
@Component
public class GeoCacheData {

    //每辆车缓存最后50条gps
    private static final int CACHE_SIZE = 50;
    private static Map<String, CircleQueue<GpsEntity>> gpsCacheMap = new HashMap<>();

    //线路路段走向
    private static ArrayListMultimap<String, LineString> sectionCacheMap;

    //线路站点路由
    private static ArrayListMultimap<String, StationRoute> stationCacheMap;

    @Autowired
    JdbcTemplate jdbcTemplate;

    public void loadData(){
        final GeometryFactory geometryFactory = new GeometryFactory();
        //加载站点路由
        String sql = "select r.LINE_CODE,r.DIRECTIONS,r.STATION_CODE,r.STATION_MARK,s.SHAPES_TYPE,s.G_LONX,s.G_LATY,ST_AsText(s.G_POLYGON_GRID) as G_POLYGON_GRID,s.RADIUS from bsth_c_stationroute r left join bsth_c_station s on r.station=s.id";
        List<StationRoute> routeList = jdbcTemplate.query(sql, new RowMapper<StationRoute>() {
            @Override
            public StationRoute mapRow(ResultSet rs, int rowNum) throws SQLException {
                StationRoute sRoute = new StationRoute();
                sRoute.setCode(rs.getString("STATION_CODE"));
                sRoute.setLineCode(rs.getString("LINE_CODE"));
                sRoute.setDirections(rs.getInt("DIRECTIONS"));
                sRoute.setPoint(geometryFactory.createPoint(new Coordinate(rs.getFloat("G_LONX"), rs.getFloat("G_LATY"))));
                sRoute.setRadius(rs.getFloat("RADIUS"));

                String shapesType = rs.getString("SHAPES_TYPE");
                //多边形电子围栏
                if(StringUtils.isNotEmpty(shapesType) && shapesType.equals("d")){
                    geometryFactory.createPolygon(parsePolygon(rs.getString("G_POLYGON_GRID")));
                }
                return sRoute;
            }
        });
        //按线路和走向分组
        if(routeList.size() > 0){
            ArrayListMultimap<String, StationRoute> tempMap = ArrayListMultimap.create();
            for(StationRoute sr : routeList){
                tempMap.put(sr.getLineCode() + "_" + sr.getDirections(), sr);
            }
            stationCacheMap = tempMap;
        }

        System.out.println(stationCacheMap);
    }

    public Coordinate[] parsePolygon(String polygonStr){
        String[] coords = polygonStr.substring(11, polygonStr.length() - 2).split(",")
                ,temps;

        Coordinate[] cds = new Coordinate[coords.length];
        int len = coords.length;
        for(int i = 0; i < len; i++){
            temps = coords[i].split(" ");
            cds[i] = new Coordinate(Float.parseFloat(temps[0]), Float.parseFloat(temps[1]));
        }
        return cds;
    }
}