From 362049c9e343e1301916a5b4507747e1dbe29581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E5=81=A5?= Date: Mon, 16 Aug 2021 15:48:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AD=A6=E6=A0=A1=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=BB=8F=E7=BA=AC=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../temp/shenjian/poi/SchoolLatLng.groovy | 65 +++++++++++++++++++ .../temp/shenjian/util/ExcelUtils.groovy | 28 ++++---- 2 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/poi/SchoolLatLng.groovy diff --git a/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/poi/SchoolLatLng.groovy b/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/poi/SchoolLatLng.groovy new file mode 100644 index 000000000..219dd491c --- /dev/null +++ b/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/poi/SchoolLatLng.groovy @@ -0,0 +1,65 @@ +package com.fangjia.script.temp.shenjian.poi + +import com.alibaba.excel.EasyExcel +import com.fangjia.script.temp.shenjian.util.ExcelUtils +import com.fangjia.script.temp.shenjian.util.GaoDeUtils + +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.CountDownLatch +import java.util.concurrent.ExecutorService +import java.util.concurrent.Executors + +/** + * 学校经纬度获取 + * + * @author shenjian + * @since 2021/8/16 + */ +class SchoolLatLng { + + static ExecutorService executorService = Executors.newFixedThreadPool(30) + + static String name = "大连市义务教育学校名录test"; + + static void main(String[] args) { + List> schools = ExcelUtils.read(new FileInputStream("D:\\Users\\Domi\\Desktop\\房价网\\项目\\模型\\学校\\" + name + ".xlsx")); + Map> result = new ConcurrentHashMap<>(); + CountDownLatch latch = new CountDownLatch(schools.size()); + + for (List schoolTemp : schools) { + final List school = schoolTemp + executorService.submit(new Runnable() { + @Override + void run() { + try { + def city = "大连" + def list = new ArrayList(); + def address = school.get(4) + def location = GaoDeUtils.parserByGaode(city, address); + if (location && location.lng) { + def lng = Double.valueOf(location.lng) + def lat = Double.valueOf(location.lat) + list.add(lng) + list.add(lat) + result.put(address, list) + } + println("已处理:" + address) + } catch(Exception ex) { + ex.printStackTrace() + } finally { + latch.countDown() + } + } + }) + } + latch.await() + + List> head = new ArrayList<>() + for (List school : schools) { + def address = school.get(4) + school.addAll(result.get(address)) + } + EasyExcel.write("D:\\Users\\Domi\\Desktop\\房价网\\项目\\模型\\学校\\" + name + "结果.xlsx").head(head).sheet("小学").doWrite(schools) + executorService.shutdown() + } +} diff --git a/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/ExcelUtils.groovy b/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/ExcelUtils.groovy index 229499b80..fa0927eee 100644 --- a/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/ExcelUtils.groovy +++ b/projects/fangjia-script/src/main/groovy/com/fangjia/script/temp/shenjian/util/ExcelUtils.groovy @@ -13,36 +13,36 @@ import com.alibaba.excel.event.AnalysisEventListener */ class ExcelUtils { - static List> read(String filePath) { - SimpleExcelListener listener = new SimpleExcelListener(filePath); - ExcelReader excelReader = EasyExcelFactory.read(filePath, null, listener).headRowNumber(0).build(); - excelReader.readAll() + public static List> read(InputStream inputStream) { + SimpleExcelListener listener = new SimpleExcelListener(); + ExcelReader excelReader = EasyExcelFactory.read(inputStream, null, listener) + .headRowNumber(1).build(); + excelReader.readAll(); List> dataList = listener.getDataList(); - excelReader.finish() + excelReader.finish(); return dataList; } - class SimpleExcelListener extends AnalysisEventListener { - - private List> dataList = new ArrayList<>() + public static class SimpleExcelListener extends AnalysisEventListener { + private List> dataList = new ArrayList<>(); @Override - void invoke(Object o, AnalysisContext analysisContext) { - Map map = o; - dataList.add(new ArrayList(map.values())) + public void invoke(Object o, AnalysisContext analysisContext) { + Map map = (Map) o; + dataList.add(new ArrayList<>(map.values())); } @Override - void doAfterAllAnalysed(AnalysisContext analysisContext) { + public void doAfterAllAnalysed(AnalysisContext analysisContext) { } List> getDataList() { - return dataList + return dataList; } void setDataList(List> dataList) { - this.dataList = dataList + this.dataList = dataList; } } }