经纬度

This commit is contained in:
沈健
2021-08-18 14:09:39 +08:00
parent daf23e52c5
commit 347ef3f4db
3 changed files with 132 additions and 11 deletions
@@ -1,6 +1,8 @@
package com.fangjia.script.temp.shenjian.poi
import com.alibaba.excel.EasyExcel
import com.fangjia.script.coredata.util.CollectionUtils
import com.fangjia.script.statistics.poi.AmapPoi
import com.fangjia.script.temp.shenjian.util.ExcelUtils
import com.fangjia.script.temp.shenjian.util.GaoDeUtils
@@ -34,14 +36,19 @@ class SchoolLatLng {
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)
def address = school.get(1)
def pois = GaoDeUtils.searchPoi(city, address);
if (CollectionUtils.isNotEmpty(pois)) {
for (AmapPoi amapPoi : pois) {
if (address.contains(amapPoi.getName())) {
String[] locations = amapPoi.getLocation().split(",")
list.add(locations[0])
list.add(locations[1])
list.add(amapPoi.getId())
result.put(address, list)
break;
}
}
}
println("已处理:" + address)
} catch(Exception ex) {
@@ -56,10 +63,12 @@ class SchoolLatLng {
List<List<String>> head = new ArrayList<>()
for (List<String> school : schools) {
def address = school.get(4)
school.addAll(result.get(address))
def address = school.get(1)
if (result.contains(address)) {
school.addAll(result.get(address))
}
}
EasyExcel.write("D:\\Users\\Domi\\Desktop\\房价网\\项目\\模型\\学校\\" + name + "结果.xlsx").head(head).sheet("小学").doWrite(schools)
EasyExcel.write("D:\\Users\\Domi\\Desktop\\房价网\\项目\\模型\\学校\\" + name + "结果.xlsx").head(head).sheet("学校").doWrite(schools)
executorService.shutdown()
}
}
@@ -1,9 +1,13 @@
package com.fangjia.script.temp.shenjian.util
import com.alibaba.excel.EasyExcel
import com.alibaba.excel.EasyExcelFactory
import com.alibaba.excel.ExcelReader
import com.alibaba.excel.context.AnalysisContext
import com.alibaba.excel.event.AnalysisEventListener
import com.alibaba.excel.read.metadata.ReadSheet
import java.util.concurrent.ConcurrentHashMap
/**
* excel工具类
@@ -13,6 +17,45 @@ import com.alibaba.excel.event.AnalysisEventListener
*/
class ExcelUtils {
static List<List<String>> read(String filePath) {
SimpleExcelListener listener = new SimpleExcelListener(filePath);
ExcelReader excelReader = EasyExcelFactory.read(filePath, null, listener).headRowNumber(0).build();
excelReader.readAll()
List<List<String>> dataList = listener.getDataList();
excelReader.finish()
return dataList;
}
static List<List<String>> readSheet(String fileName, String[] sheetNames) {
Map<String, List<List<String>>> result = new ConcurrentHashMap<>();
ExcelReader excelReader = EasyExcel.read(fileName).build();
// 每个sheet分别创建监听器
SimpleExcelListener listener0 = new SimpleExcelListener();
SimpleExcelListener listener1 = new SimpleExcelListener();
ReadSheet readSheet0 = EasyExcel.readSheet(0).registerReadListener(listener0).build();
ReadSheet readSheet1 = EasyExcel.readSheet(1).registerReadListener(listener1).build();
// 这里注意 一定要把sheet1 sheet2 一起传进去,不然有个问题就是03版的excel 会读取多次,浪费性能
// 可以传多个ReadSheet对象,读多个sheet
excelReader.read(readSheet0, readSheet1);
// 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
excelReader.finish();
ArrayList<SimpleExcelListener> listenerList = new ArrayList<>();
listenerList.add(listener0);
listenerList.add(listener1);
for (SimpleExcelListener listener : listenerList) {
result.put(listener.getSheetName(), listener.getDataList())
}
return result
}
public static List<List<String>> read(InputStream inputStream) {
SimpleExcelListener listener = new SimpleExcelListener();
ExcelReader excelReader = EasyExcelFactory.read(inputStream, null, listener)
@@ -25,6 +68,15 @@ class ExcelUtils {
public static class SimpleExcelListener extends AnalysisEventListener {
private List<List<String>> dataList = new ArrayList<>();
private String sheetName;
public SimpleExcelListener() {
}
public SimpleExcelListener(String sheetName) {
this.sheetName = sheetName
}
@Override
public void invoke(Object o, AnalysisContext analysisContext) {
@@ -44,5 +96,9 @@ class ExcelUtils {
void setDataList(List<List<String>> dataList) {
this.dataList = dataList;
}
public String getSheetName() {
return sheetName
}
}
}
@@ -165,6 +165,62 @@ class GaoDeUtils {
return result
}
/**
* 查找关键字经纬度
*/
static List searchPoi(String city, String keywords){
def params = [:]
params.key = GAO_DE_AK[keyUsed.intValue()]
params.keywords = keywords
params.city = city
params.put("extensions", "all")
params.put("citylimit","true")
params.put("offset","20")
List result = []
int page = 1
int count = 10000
while (result.size() < count) {
params.page = "${page}".toString()
page++
JSONObject json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v3/place/text", params);
if (json == null) {
json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v3/place/text", params);
}
if (json && json.info && json.info == "OK") {
count = json.getString("count").toInteger()
try {
List<AmapPoi> ret = JSONObject.parseArray(json.pois.toString(), AmapPoi.class)
result.addAll(ret)
} catch(Exception ex) {
ex.printStackTrace()
}
} else if (json && json.infocode && json.infocode == "10044") {
synchronized (this) {
for (int i = 0; i < GAO_DE_AK.size(); i++) {
if (GAO_DE_AK[i] == params.key) {
if (i + 1 > GAO_DE_AK.size()) {
System.exit(1)
}
keyUsed = new AtomicInteger(i+1)
}
}
break;
}
} else if (json && json.infocode && json.infocode == "10004") {
// 开发者的单位时间内(1分钟)访问量超限,被系统自动封停,下一分钟自动解封。
Thread.sleep(60000)
break;
} else if (json && json.infocode && json.infocode == "10017") {
// 所请求的资源不可用
System.exit(1)
} else {
break
}
}
return result
}
/**
* 获取经纬度之间距离
*/