use of org.opensextant.data.Country in project Xponents by OpenSextant.
the class SolrGazetteer method createCountry.
private static final Country createCountry(SolrDocument gazEntry) {
String code = SolrProxy.getString(gazEntry, "cc");
String name = SolrProxy.getString(gazEntry, "name");
String featCode = SolrProxy.getString(gazEntry, "feat_code");
Country C = new Country(code, name);
if ("TERR".equals(featCode)) {
C.isTerritory = true;
// Other conditions?
}
// Set this once. Yes, indeed we would see this metadata repeated for every country entry.
// Geo field is specifically Spatial4J lat,lon format.
double[] xy = SolrProxy.getCoordinate(gazEntry, "geo");
C.setLatitude(xy[0]);
C.setLongitude(xy[1]);
String fips = SolrProxy.getString(gazEntry, "FIPS_cc");
String iso3 = SolrProxy.getString(gazEntry, "ISO3_cc");
C.CC_FIPS = fips;
C.CC_ISO3 = iso3;
C.setName_type(SolrProxy.getChar(gazEntry, "name_type"));
return C;
}
use of org.opensextant.data.Country in project Xponents by OpenSextant.
the class GeonamesUtility method loadCountryTimezones.
/**
* Pase geonames.org TZ table.
*
* @throws IOException
* if timeZones.txt is not found or has an issue.
*/
private void loadCountryTimezones() throws IOException {
java.io.InputStream io = getClass().getResourceAsStream("/geonames.org/timeZones.txt");
java.io.Reader tzReader = new InputStreamReader(io);
CsvMapReader tzMap = new CsvMapReader(tzReader, CsvPreference.TAB_PREFERENCE);
String[] columns = tzMap.getHeader(true);
Map<String, String> tzdata = null;
while ((tzdata = tzMap.read(columns)) != null) {
String cc = tzdata.get("CountryCode");
if (cc.trim().startsWith("#")) {
continue;
}
Country C = getCountry(cc);
if (C == null) {
continue;
}
Country.TZ tz = new Country.TZ(tzdata.get("TimeZoneId"), tzdata.get("GMT offset 1. Jan 2016"), tzdata.get("DST offset 1. Jul 2016"), tzdata.get("rawOffset (independant of DST)"));
C.addTimezone(tz);
}
tzMap.close();
// Add all TZ to countries;
for (String cc : isoCountries.keySet()) {
if (cc.length() > 2) {
continue;
}
Country C = isoCountries.get(cc);
for (String tmzn : C.getAllTimezones().keySet()) {
addTimezone(tmzn, cc);
}
for (Country.TZ tz : C.getTZDatabase().values()) {
addTZOffset(utc2cc, tz.utcOffset, cc);
addTZOffset(dst2cc, tz.dstOffset, cc);
}
}
}
use of org.opensextant.data.Country in project Xponents by OpenSextant.
the class GeonamesUtility method addLang.
/**
*
* @param langOrLocale
* lang code
* @param cc
* country code
*/
private void addLang(String langOrLocale, String cc) {
String lid = langOrLocale;
if (lid.length() == 3) {
Language L = TextUtils.getLanguage(langOrLocale);
if (L == null) {
unknownLanguages.add(lid);
}
}
/**
* Special case: First language / country pairs seen, denote those as PRIMARY language.
* As geonames.org lists languages spoken by order of population of speakers.
*
*/
Country C = this.isoCountries.get(cc);
if (C != null) {
C.addLanguage(lid);
}
}
use of org.opensextant.data.Country in project Xponents by OpenSextant.
the class TestGeoUtils method testResources.
@Test
public void testResources() throws IOException {
GeonamesUtility util = new GeonamesUtility();
// About 280 entries in CSV flat file.
assert (util.getCountries().size() > 280);
for (Country c : util.getCountries()) {
print(String.format("%s, %s", c.getName(), c.getCountryCode()));
}
if (util.getCountryByAnyCode("IV") == null) {
fail("IV - Cote D'Ivoire not found");
}
print("Gaza also known as 'GAZ'");
Country C;
C = util.getCountry("GAZ");
assert (C != null);
print("Spratly's is designated territory of China -- Ensure CHN != Spratly's though.");
C = util.getCountry("CHN");
assert (C != null && C.getName().equalsIgnoreCase("china"));
C = util.getCountry("USA");
if (!C.containsUTCOffset(-5.0)) {
fail("USA contains GMT-0500");
}
if (!C.containsDSTOffset(-4.0)) {
fail("USA contains GMT-0400, e.g., EDT for east coast.");
}
C = util.getCountry("JP");
if (!C.containsUTCOffset(9.0)) {
fail("Japan contains GMT+0900");
}
/*
* Test beyond 12h.
* Test negative offsets.
*/
C = util.getCountry("KI");
if (!C.containsUTCOffset(14.0)) {
fail("Kiritimati contains GMT+1400");
}
if (C.containsUTCOffset(-9.0)) {
fail("Kiritimati does not contain GMT-0900");
}
// TODO: discover full list of TZ e.g., EDT, BST, CST, MT "Mountain Time", etc.
print("" + util.countriesInTimezone("Mountain"));
// Same or similar to Mountain TZ, GMT-0500 but -0200 more.
print("" + util.countriesInUTCOffset(-7.0));
//
print("" + util.countriesInUTCOffset(9.0));
// SAME as above.
print("" + util.countriesInUTCOffset(9.0 * 3600));
}
use of org.opensextant.data.Country in project Xponents by OpenSextant.
the class SolrGazetteer method loadCountries.
/**
* This only returns Country objects that are names; It does not produce any
* abbreviation variants.
*
* TODO: allow caller to get all entries, including abbreviations.
*
* @param index
* solr instance to query
* @return country data hash
* @throws SolrServerException
* the solr server exception
* @throws IOException
* on err, if country metadata file is not found in classpath
*/
public static Map<String, Country> loadCountries(SolrServer index) throws SolrServerException, IOException {
GeonamesUtility geodataUtil = new GeonamesUtility();
Map<String, Country> countryCodeMap = geodataUtil.getISOCountries();
Logger log = LoggerFactory.getLogger(SolrGazetteer.class);
ModifiableSolrParams ctryparams = new ModifiableSolrParams();
ctryparams.set(CommonParams.FL, "id,name,cc,FIPS_cc,ISO3_cc,adm1,adm2,feat_class,feat_code,geo,name_type");
/* TODO: Consider different behaviors for PCLI vs. PCL[DFS] */
ctryparams.set("q", "+feat_class:A +feat_code:(PCLI OR PCLIX OR TERR) +name_type:N");
/* As of 2015 we have 2300+ name variants for countries and territories */
ctryparams.set("rows", 5000);
QueryResponse response = index.query(ctryparams);
// Process Solr Response
//
SolrDocumentList docList = response.getResults();
for (SolrDocument gazEntry : docList) {
Country C = createCountry(gazEntry);
Country existingCountry = countryCodeMap.get(C.getCountryCode());
if (existingCountry != null) {
if (existingCountry.ownsTerritory(C.getName())) {
// do nothing.
} else if (C.isTerritory) {
log.debug("{} territory of {}", C, existingCountry);
existingCountry.addTerritory(C);
} else {
log.debug("{} alias of {}", C, existingCountry);
// all other metadata is same.
existingCountry.addAlias(C.getName());
}
continue;
}
log.info("Unknown country in gazetteer, that is not in flat files. C={}", C);
countryCodeMap.put(C.getCountryCode(), C);
countryCodeMap.put(C.CC_ISO3, C);
}
return countryCodeMap;
}
Aggregations