Search in sources :

Example 1 with ExcelListener

use of com.summit.coordinates.config.ExcelListener in project springboot by lbshold.

the class CoordinateExcelController method convertCoordinate.

/**
 * 1, 读取Excel中的坐标数据
 * 2. 遍历坐标数据,
 * a. 坐标数据格式转换,度分秒、度分转度
 * b. 坐标数据经纬度格式转换,WGS84转GCJ02
 */
private List<MyCoordinate> convertCoordinate(@RequestParam("file") MultipartFile file) throws IOException {
    // 读取Excel中的坐标数据
    ExcelListener listener = new ExcelListener();
    ExcelReader excelReader = new ExcelReader(file.getInputStream(), ExcelTypeEnum.XLSX, null, listener);
    excelReader.read(new Sheet(1, 1, MyCoordinate.class));
    List<MyCoordinate> datas = listener.getDatas();
    Pattern pattern = Pattern.compile(REGEX);
    Pattern pattern2 = Pattern.compile(REGEX2);
    Pattern pattern3 = Pattern.compile(REGEX3);
    List<MyCoordinate> result = new ArrayList<>();
    String lat;
    String lng;
    // 遍历坐标数据
    for (MyCoordinate myCoordinate : datas) {
        lat = myCoordinate.getLatitude();
        lng = myCoordinate.getLongitude();
        if (StringUtils.isEmpty(lat) || StringUtils.isEmpty(lng) || StringUtils.isEmpty(lat.trim()) || StringUtils.isEmpty(lng.trim())) {
            myCoordinate.setRemark("数据不能为空");
            myCoordinate.setIsSucceeded(FALSE);
            result.add(myCoordinate);
            continue;
        }
        lat = lat.trim();
        lng = lng.trim();
        if (pattern.matcher(lat).find() && pattern2.matcher(lng).find()) {
            myCoordinate.setLongitude(CoordinateFormatUtil.DmsTurnDD(lat));
            myCoordinate.setLatitude(CoordinateFormatUtil.DmsTurnDD(lng));
            myCoordinate.setIsSucceeded(TRUE);
            result.add(MyUitls.wgs84ToGcj02Copy(myCoordinate));
            continue;
        }
        if (pattern2.matcher(lat).find() && pattern2.matcher(lng).find()) {
            myCoordinate.setLongitude(CoordinateFormatUtil.DmTurnDD(lat));
            myCoordinate.setLatitude(CoordinateFormatUtil.DmTurnDD(lng));
            myCoordinate.setIsSucceeded(TRUE);
            result.add(MyUitls.wgs84ToGcj02Copy(myCoordinate));
            continue;
        }
        if (pattern3.matcher(lat).find() && pattern3.matcher(lng).find()) {
            myCoordinate.setIsSucceeded(TRUE);
            result.add(MyUitls.wgs84ToGcj02Copy(myCoordinate));
            continue;
        }
        myCoordinate.setIsSucceeded(FALSE);
        myCoordinate.setRemark("数据格式有误");
        result.add(myCoordinate);
    }
    return result;
}
Also used : ExcelReader(com.alibaba.excel.ExcelReader) MyCoordinate(com.summit.coordinates.entity.MyCoordinate) Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) ExcelListener(com.summit.coordinates.config.ExcelListener) Sheet(com.alibaba.excel.metadata.Sheet)

Aggregations

ExcelReader (com.alibaba.excel.ExcelReader)1 Sheet (com.alibaba.excel.metadata.Sheet)1 ExcelListener (com.summit.coordinates.config.ExcelListener)1 MyCoordinate (com.summit.coordinates.entity.MyCoordinate)1 ArrayList (java.util.ArrayList)1 Pattern (java.util.regex.Pattern)1