com.maxmind.geoip2.record.Location Java Examples

The following examples show how to use com.maxmind.geoip2.record.Location. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocRecord.java    From openvisualtraceroute with GNU Lesser General Public License v3.0 6 votes vote down vote up
public LocRecord(final String owner, final double lat, final double lon) {
	this.owner = owner;
	final StringBuilder sb = new StringBuilder();
	//owner TTL class LOC ( d1 [m1 [s1]] {"N"|"S"} d2 [m2 [s2]] {"E"|"W"} alt["m"] [siz["m"] [hp["m"] [vp["m"]]]] )
	final double[] latDms = Angle.fromDegreesLatitude(lat).toDMS();
	final double[] lonDms = Angle.fromDegreesLatitude(lon).toDMS();
	sb.append(owner).append(".").append(SPACE).append(0).append(SPACE).append("IN").append(SPACE).append("LOC").append(SPACE);
	sb.append(Math.abs((int) latDms[0])).append(SPACE).append((int) latDms[1]).append(SPACE).append((float) latDms[2]).append(SPACE).append(lat >= 0 ? "N" : "S")
			.append(SPACE);
	sb.append(Math.abs((int) lonDms[0])).append(SPACE).append((int) lonDms[1]).append(SPACE).append((float) lonDms[2]).append(SPACE).append(lon >= 0 ? "E" : "W")
			.append(SPACE);
	sb.append("0m").append(SPACE).append("0m").append(SPACE).append("0m").append(SPACE).append("0m");
	raw = sb.toString();
	final Map<String, String> names = new HashMap<>();
	names.put("name", "Loc record");
	final City city = new City(null, null, null, names);
	final Country country = new Country(null, 0, 0, LOC, names);
	final Location loc = new Location(0, 0, lat, lon, null, null, null);
	location = new CityResponse(city, null, country, loc, null, null, null, null, null, null);
	valid = true;
}
 
Example #2
Source File: MaxMindGeoLocationServiceTest.java    From prebid-server-java with Apache License 2.0 5 votes vote down vote up
@Test
public void lookupShouldReturnCountryIsoWhenDatabaseReaderWasSet() throws NoSuchFieldException, IOException,
        GeoIp2Exception {
    // given
    final Country country = new Country(null, null, null, "fr", null);
    final Continent continent = new Continent(null, "eu", null, null);
    final City city = new City(singletonList("test"), null, null, singletonMap("test", "Paris"));
    final Location location = new Location(null, null, 48.8566, 2.3522,
            null, null, null);
    final ArrayList<Subdivision> subdivisions = new ArrayList<>();
    subdivisions.add(new Subdivision(null, null, null, "paris", null));
    final CityResponse cityResponse = new CityResponse(city, continent, country, location, null,
            null, null, null, subdivisions, null);

    final DatabaseReader databaseReader = Mockito.mock(DatabaseReader.class);
    given(databaseReader.city(any())).willReturn(cityResponse);

    FieldSetter.setField(maxMindGeoLocationService,
            maxMindGeoLocationService.getClass().getDeclaredField("databaseReader"), databaseReader);

    // when
    final Future<GeoInfo> future = maxMindGeoLocationService.lookup(TEST_IP, null);

    // then
    assertThat(future.succeeded()).isTrue();
    assertThat(future.result())
            .isEqualTo(GeoInfo.builder()
                    .vendor("maxmind")
                    .continent("eu")
                    .country("fr")
                    .region("paris")
                    .city("Paris")
                    .lat(48.8566f)
                    .lon(2.3522f)
                    .build());
}
 
Example #3
Source File: StatisticsLocationUtil.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
private Map<String, Object> ip2SpatialData(InetAddress ip) {
    if (!enabled) {
        return null;
    }
    if (reader == null) {
        LOG.warn("Location database is not initialized. Exiting.");
        return null;
    }
    try {
        Map<String, Object> holder = new HashMap<>(3);
        if (dbType == LocationDatabaseType.COUNTRY) {
            Country country = reader.country(ip).getCountry();
            holder.put(ObjectEsParameterFactory.GEOLOC_COUNTRY_CODE.getName(), country.getIsoCode());
        } else {
            CityResponse city = reader.city(ip);
            Location loc = city.getLocation();
            holder.put(ObjectEsParameterFactory.GEOLOC_COUNTRY_CODE.getName(), city.getCountry().getIsoCode());
            holder.put(ObjectEsParameterFactory.GEOLOC_CITY_NAME.getName(), city.getCity().getName());
            holder.put(ObjectEsParameterFactory.GEOLOC_GEO_POINT.getName(),
                       new GeoPoint(loc.getLatitude(), loc.getLongitude()));
        }
        return holder;
    } catch (Throwable e) {
        LOG.warn("Can't convert IP to GeoIp", e);
    }
    return null;
}
 
Example #4
Source File: IPLookupService.java    From nifi with Apache License 2.0 5 votes vote down vote up
private Record createRecord(final CityResponse city) {
    if (city == null) {
        return null;
    }

    final Map<String, Object> values = new HashMap<>();
    values.put(CitySchema.CITY.getFieldName(), city.getCity().getName());

    final Location location = city.getLocation();
    values.put(CitySchema.ACCURACY.getFieldName(), location.getAccuracyRadius());
    values.put(CitySchema.METRO_CODE.getFieldName(), location.getMetroCode());
    values.put(CitySchema.TIMEZONE.getFieldName(), location.getTimeZone());
    values.put(CitySchema.LATITUDE.getFieldName(), location.getLatitude());
    values.put(CitySchema.LONGITUDE.getFieldName(), location.getLongitude());
    values.put(CitySchema.CONTINENT.getFieldName(), city.getContinent().getName());
    values.put(CitySchema.POSTALCODE.getFieldName(), city.getPostal().getCode());
    values.put(CitySchema.COUNTRY.getFieldName(), createRecord(city.getCountry()));

    final Object[] subdivisions = new Object[city.getSubdivisions().size()];
    int i = 0;
    for (final Subdivision subdivision : city.getSubdivisions()) {
        subdivisions[i++] = createRecord(subdivision);
    }
    values.put(CitySchema.SUBDIVISIONS.getFieldName(), subdivisions);

    return new MapRecord(CitySchema.GEO_SCHEMA, values);
}
 
Example #5
Source File: MaxMindGeoLocationService.java    From prebid-server-java with Apache License 2.0 4 votes vote down vote up
private Location getLocation(InetAddress inetAddress) throws IOException, GeoIp2Exception {
    return getCity(inetAddress).getLocation();
}
 
Example #6
Source File: GEOInfo.java    From proxylive with MIT License 4 votes vote down vote up
public Location getLocation() {
    return location;
}
 
Example #7
Source File: GEOInfo.java    From proxylive with MIT License 4 votes vote down vote up
public void setLocation(Location location) {
    this.location = location;
}