新增对口经纬度获取

This commit is contained in:
沈健
2021-09-03 14:22:59 +08:00
parent d78e6a664a
commit 776b32e1aa
3 changed files with 136 additions and 3 deletions
@@ -0,0 +1,86 @@
package com.fangjia.script.temp.shenjian.poi
import com.fangjia.script.temp.shenjian.util.GaoDeUtils
import com.fangjia.script.util.MongoUtils
import com.mongodb.BasicDBList
import com.mongodb.BasicDBObject
import com.mongodb.DBCollection
import com.mongodb.DBObject
import java.util.concurrent.CountDownLatch
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicLong
/**
* 对口小学初中经纬度
*/
class SeaweedSchoolLatLng {
static DBCollection collection = MongoUtils.getCollectionByName("fangjia_craw", "seaweed_school", "192.168.0.235", 27018)
static ExecutorService executorService = Executors.newFixedThreadPool(20)
static void main(String[] args) {
AtomicLong atomicLong = new AtomicLong(0);
collection.find(new BasicDBObject()).batchSize(1000).each {objT ->
final DBObject obj = objT
executorService.submit(new Runnable() {
@Override
void run() {
try {
BasicDBList primarySchools = obj.primarySchool
BasicDBList middleSchools = obj.middleSchool
if (primarySchools != null) {
for (BasicDBObject dbObject : primarySchools) {
def name = dbObject.name
if (dbObject.lng == null) {
def location = GaoDeUtils.parserByGaode("上海", "上海市浦东新区" + name)
if (location && location.lng) {
def lng = Double.valueOf(location.lng)
def lat = Double.valueOf(location.lat)
dbObject.lng = lng
dbObject.lat = lat
}
}
if (dbObject.lng != null) {
String address = GaoDeUtils.getGaoDeAddress(dbObject.lng + "," + dbObject.lat)
dbObject.address = address
}
}
}
if (middleSchools != null) {
for (BasicDBObject dbObject : middleSchools) {
def name = dbObject.name
if (dbObject.lng == null) {
def location = GaoDeUtils.parserByGaode("上海", "上海市浦东新区" + name)
if (location && location.lng) {
def lng = Double.valueOf(location.lng)
def lat = Double.valueOf(location.lat)
dbObject.lng = lng
dbObject.lat = lat
}
}
if (dbObject.lng != null) {
String address = GaoDeUtils.getGaoDeAddress(dbObject.lng + "," + dbObject.lat)
dbObject.address = address
}
}
}
collection.update(new BasicDBObject('url', obj.url), new BasicDBObject('$set', obj), false, true)
} catch(Exception ex) {
ex.printStackTrace()
} finally {
atomicLong.incrementAndGet()
synchronized (this) {
if (atomicLong.longValue() % 100 == 0) {
println(atomicLong.longValue())
}
}
}
}
})
};
executorService.shutdown()
}
}
@@ -19,7 +19,9 @@ import org.bson.Document
import org.jdom.Content;
/**
* @author shenjian* @since 2021/9/2
* 对口小学、初中
* @author shenjian
* @since 2021/9/2
*/
class SeaweedSchool {
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.fangjia.core.util.DataUtil
import com.fangjia.core.util.HttpRequest
import com.fangjia.script.common.DataHandleUtils
import com.fangjia.script.statistics.poi.AmapPoi
import com.fangjia.script.util.GeoHelper
import com.fangjia.script.util.HttpRequestUtil
@@ -214,8 +215,6 @@ class GaoDeUtils {
} else if (json && json.infocode && json.infocode == "10017") {
// 所请求的资源不可用
System.exit(1)
} else {
break
}
}
return result
@@ -228,6 +227,52 @@ class GaoDeUtils {
return GeoHelper.GetDistance(Lat, Lng, Lat1, Lng1)
}
/**
* 经纬度转地址<br>
* 规则: 最多支持20个坐标点。多个点之间用"|"分割。经度在前,纬度在后,经纬度间以“,”分割,经纬度小数点后不得超过6位
* @author yinjihuan
* @date 2016年9月9日
* @param location
* @return
*/
static def getGaoDeAddress(location) {
Map<String, String> params = new HashMap<String, String>()
params.put("key", GAO_DE_AK[keyUsed.intValue()])
params.put("location", location)
String content
try {
content = new HttpRequest().get("http://restapi.amap.com/v3/geocode/regeo", params, "utf-8")
} catch (Exception e) {
content = new HttpRequest().get("http://restapi.amap.com/v3/geocode/regeo", params, "utf-8")
}
net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(content)
Map<String, Object> infoMap = DataHandleUtils.JsonToMap(json);
if (infoMap && infoMap.info == "OK") {
def regeocode = infoMap.regeocode
if (regeocode) {
return regeocode.formatted_address;
}
} 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)
}
}
}
} else if (json && json.infocode && json.infocode == "10004") {
// 开发者的单位时间内(1分钟)访问量超限,被系统自动封停,下一分钟自动解封。
Thread.sleep(60000)
} else if (json && json.infocode && json.infocode == "10017") {
// 所请求的资源不可用
System.exit(1)
}
return null
}
/**
* 获取地址经纬度
*/