use of com.maxmind.geoip2.record.Location in project quickutil by quickutil.
the class GeoUtil method geoGpsByBaidu.
/**
* 根据经纬度查询地理信息-百度世界
*/
public static GeoDef geoGpsByBaidu(double latitude, double longitude) {
try {
double[] delta = WGSToGCJPointer(latitude, longitude);
delta = GCJToBDPointer(delta[0], delta[1]);
String queryUrl = String.format("http://api.map.baidu.com/geocoder/v2/?ak=%s&location=%s,%s&output=json", baiduKeyIn, delta[0], delta[1]);
JsonObject object = JsonUtil.toJsonObject(FileUtil.stream2string(HttpUtil.httpGet(queryUrl).getEntity().getContent()));
String country = object.getAsJsonObject("result").getAsJsonObject("addressComponent").get("country").getAsString();
if (country.equals("中国")) {
country = "China";
}
if (country.equals("England")) {
country = "United Kingdom";
}
String countryCode = countryCodeByCountryName(country);
String countryChinese = countryChineseByCountryCode(countryCode);
String state = object.getAsJsonObject("result").getAsJsonObject("addressComponent").get("province").getAsString();
String stateCode = "";
if (country.equals("China")) {
stateCode = stateCodeByStateChinese(countryCode, state);
} else {
stateCode = stateCodeByStateName(countryCode, state);
}
state = stateNameByStateCode(countryCode, stateCode);
String stateChinese = stateChineseByStateCode(countryCode, stateCode);
String city = object.getAsJsonObject("result").getAsJsonObject("addressComponent").get("city").getAsString();
String description = object.getAsJsonObject("result").getAsJsonObject("addressComponent").get("district").getAsString();
countryCode = (countryCode == null) ? UNKNOWN : countryCode;
country = (country == null) ? UNKNOWN : country;
countryChinese = (countryChinese == null) ? UNKNOWN : countryChinese;
stateCode = (stateCode == null) ? UNKNOWN : stateCode;
state = (state == null) ? UNKNOWN : state;
stateChinese = (stateChinese == null) ? UNKNOWN : stateChinese;
city = (city == null) ? UNKNOWN : city;
return new GeoDef(latitude, longitude, countryCode, country, countryChinese, stateCode, state, stateChinese, city, description);
} catch (Exception e) {
LOGGER.error(Symbol.BLANK, e);
}
return null;
}
use of com.maxmind.geoip2.record.Location in project quickutil by quickutil.
the class GeoUtil method geoGpsByBaidu.
/**
* 根据经纬度查询地理信息-百度批量
*/
public static List<GeoDef> geoGpsByBaidu(List<GeoPoint> points) {
try {
if (points.size() > 20) {
return null;
}
StringBuilder builder = new StringBuilder();
for (int i = 0; i < points.size(); i++) {
double[] delta = WGSToGCJPointer(points.get(i).latitude, points.get(i).longitude);
delta = GCJToBDPointer(delta[0], delta[1]);
builder.append(delta[0] + "," + delta[1] + "|");
}
String queryUrl = String.format("http://api.map.baidu.com/geocoder/v2/?ak=%s&location=%s&output=json&batch=true", baiduKeyIn, URLEncoder.encode(builder.substring(0, builder.length() - 1), "UTF-8"));
JsonObject object = JsonUtil.toJsonObject(FileUtil.stream2string(HttpUtil.httpGet(queryUrl).getEntity().getContent()));
JsonArray array = object.getAsJsonArray("areas");
List<GeoDef> geodefList = new ArrayList<GeoDef>();
for (int i = 0; i < points.size(); i++) {
object = array.get(i).getAsJsonObject();
String country = object.get("country").getAsString();
if (country.equals("中国")) {
country = "China";
}
if (country.equals("England")) {
country = "United Kingdom";
}
String countryCode = countryCodeByCountryName(country);
String countryChinese = countryChineseByCountryCode(countryCode);
String state = object.get("province").getAsString();
String stateCode = "";
if (country.equals("China")) {
stateCode = stateCodeByStateChinese(countryCode, state);
} else {
stateCode = stateCodeByStateName(countryCode, state);
}
state = stateNameByStateCode(countryCode, stateCode);
String stateChinese = stateChineseByStateCode(countryCode, stateCode);
String city = object.get("city").getAsString();
String description = object.get("district").getAsString();
countryCode = (countryCode == null) ? UNKNOWN : countryCode;
country = (country == null) ? UNKNOWN : country;
countryChinese = (countryChinese == null) ? UNKNOWN : countryChinese;
stateCode = (stateCode == null) ? UNKNOWN : stateCode;
state = (state == null) ? UNKNOWN : state;
stateChinese = (stateChinese == null) ? UNKNOWN : stateChinese;
city = (city == null) ? UNKNOWN : city;
geodefList.add(new GeoDef(points.get(i).latitude, points.get(i).longitude, countryCode, country, countryChinese, stateCode, state, stateChinese, city, description));
}
return geodefList;
} catch (Exception e) {
LOGGER.error(Symbol.BLANK, e);
}
return null;
}
use of com.maxmind.geoip2.record.Location in project cas by apereo.
the class MaxmindDatabaseGeoLocationService method locate.
@Override
public GeoLocationResponse locate(final InetAddress address) {
try {
if (cityDatabaseReader == null && countryDatabaseReader == null) {
throw new IllegalArgumentException("No geolocation services have been defined for Maxmind");
}
val location = new GeoLocationResponse();
if (this.cityDatabaseReader != null) {
val response = this.cityDatabaseReader.city(address);
location.addAddress(response.getCity().getName());
val loc = response.getLocation();
if (loc != null) {
if (loc.getLatitude() != null) {
location.setLatitude(loc.getLatitude());
}
if (loc.getLongitude() != null) {
location.setLongitude(loc.getLongitude());
}
}
}
if (this.countryDatabaseReader != null) {
val response = this.countryDatabaseReader.country(address);
location.addAddress(response.getCountry().getName());
}
LOGGER.debug("Geo location for [{}] is calculated as [{}]", address, location);
return location;
} catch (final AddressNotFoundException e) {
LOGGER.info(e.getMessage(), e);
} catch (final Exception e) {
LoggingUtils.error(LOGGER, e);
}
return null;
}
use of com.maxmind.geoip2.record.Location in project cas by apereo.
the class MaxmindDatabaseGeoLocationServiceTests method verifyCity.
@Test
public void verifyCity() throws Exception {
val cityReader = mock(DatabaseReader.class);
val cityResponse = new CityResponse(new City(), new Continent(), new Country(), new Location(10, 100, 40D, 70D, 1, 1, "UTC"), new MaxMind(), new Postal(), new Country(), new RepresentedCountry(), new ArrayList<>(), new Traits());
when(cityReader.city(any())).thenReturn(cityResponse);
val service = new MaxmindDatabaseGeoLocationService(cityReader, null);
val response = service.locate("127.0.0.1");
assertNotNull(response);
}
use of com.maxmind.geoip2.record.Location in project nutch by apache.
the class GeoIPDocumentCreator method createDocFromCityDb.
public static NutchDocument createDocFromCityDb(String serverIp, NutchDocument doc, DatabaseReader reader) throws UnknownHostException, IOException, GeoIp2Exception {
addIfNotNull(doc, "ip", serverIp);
CityResponse response = reader.city(InetAddress.getByName(serverIp));
City city = response.getCity();
// 'Minneapolis'
addIfNotNull(doc, "cityName", city.getName());
// 50
addIfNotNull(doc, "cityConfidence", city.getConfidence());
addIfNotNull(doc, "cityGeoNameId", city.getGeoNameId());
Continent continent = response.getContinent();
addIfNotNull(doc, "continentCode", continent.getCode());
addIfNotNull(doc, "continentGeoNameId", continent.getGeoNameId());
addIfNotNull(doc, "continentName", continent.getName());
Country country = response.getCountry();
// 'US'
addIfNotNull(doc, "countryIsoCode", country.getIsoCode());
// 'United States'
addIfNotNull(doc, "countryName", country.getName());
// 99
addIfNotNull(doc, "countryConfidence", country.getConfidence());
addIfNotNull(doc, "countryGeoName", country.getGeoNameId());
Location location = response.getLocation();
// 44.9733,
addIfNotNull(doc, "latLon", location.getLatitude() + "," + location.getLongitude());
// -93.2323
// 3
addIfNotNull(doc, "accRadius", location.getAccuracyRadius());
// 'America/Chicago'
addIfNotNull(doc, "timeZone", location.getTimeZone());
addIfNotNull(doc, "metroCode", location.getMetroCode());
Postal postal = response.getPostal();
// '55455'
addIfNotNull(doc, "postalCode", postal.getCode());
// 40
addIfNotNull(doc, "postalConfidence", postal.getConfidence());
RepresentedCountry rCountry = response.getRepresentedCountry();
addIfNotNull(doc, "countryType", rCountry.getType());
Subdivision subdivision = response.getMostSpecificSubdivision();
// 'Minnesota'
addIfNotNull(doc, "subDivName", subdivision.getName());
// 'MN'
addIfNotNull(doc, "subDivIdoCode", subdivision.getIsoCode());
// 90
addIfNotNull(doc, "subDivConfidence", subdivision.getConfidence());
addIfNotNull(doc, "subDivGeoNameId", subdivision.getGeoNameId());
return doc;
}
Aggregations