Search in sources :

Example 16 with Location

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;
}
Also used : JsonObject(com.google.gson.JsonObject) GeoDef(com.quickutil.platform.entity.GeoDef) UnknownHostException(java.net.UnknownHostException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException)

Example 17 with Location

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;
}
Also used : JsonArray(com.google.gson.JsonArray) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) GeoPoint(com.quickutil.platform.entity.GeoPoint) GeoDef(com.quickutil.platform.entity.GeoDef) UnknownHostException(java.net.UnknownHostException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException)

Example 18 with Location

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;
}
Also used : lombok.val(lombok.val) GeoLocationResponse(org.apereo.cas.authentication.adaptive.geo.GeoLocationResponse) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException)

Example 19 with Location

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);
}
Also used : lombok.val(lombok.val) CityResponse(com.maxmind.geoip2.model.CityResponse) Continent(com.maxmind.geoip2.record.Continent) MaxMind(com.maxmind.geoip2.record.MaxMind) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) Country(com.maxmind.geoip2.record.Country) City(com.maxmind.geoip2.record.City) Postal(com.maxmind.geoip2.record.Postal) Traits(com.maxmind.geoip2.record.Traits) Location(com.maxmind.geoip2.record.Location) Test(org.junit.jupiter.api.Test)

Example 20 with Location

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;
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) Continent(com.maxmind.geoip2.record.Continent) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) RepresentedCountry(com.maxmind.geoip2.record.RepresentedCountry) Country(com.maxmind.geoip2.record.Country) City(com.maxmind.geoip2.record.City) Subdivision(com.maxmind.geoip2.record.Subdivision) Postal(com.maxmind.geoip2.record.Postal) Location(com.maxmind.geoip2.record.Location)

Aggregations

CityResponse (com.maxmind.geoip2.model.CityResponse)12 AddressNotFoundException (com.maxmind.geoip2.exception.AddressNotFoundException)11 Location (com.maxmind.geoip2.record.Location)10 Country (com.maxmind.geoip2.record.Country)9 UnknownHostException (java.net.UnknownHostException)8 City (com.maxmind.geoip2.record.City)7 JsonObject (com.google.gson.JsonObject)6 Continent (com.maxmind.geoip2.record.Continent)6 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 Postal (com.maxmind.geoip2.record.Postal)5 Subdivision (com.maxmind.geoip2.record.Subdivision)5 GeoIp2Exception (com.maxmind.geoip2.exception.GeoIp2Exception)4 RepresentedCountry (com.maxmind.geoip2.record.RepresentedCountry)4 InetAddress (java.net.InetAddress)4 CountryResponse (com.maxmind.geoip2.model.CountryResponse)3 Traits (com.maxmind.geoip2.record.Traits)3 GeoDef (com.quickutil.platform.def.GeoDef)3 GeoDef (com.quickutil.platform.entity.GeoDef)3 JsonArray (com.google.gson.JsonArray)2