Search in sources :

Example 6 with Country

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;
}
Also used : Country(org.opensextant.data.Country)

Example 7 with Country

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);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) CsvMapReader(org.supercsv.io.CsvMapReader) Country(org.opensextant.data.Country) Reader(java.io.Reader) InputStream(java.io.InputStream)

Example 8 with Country

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);
    }
}
Also used : Language(org.opensextant.data.Language) Country(org.opensextant.data.Country)

Example 9 with Country

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));
}
Also used : GeonamesUtility(org.opensextant.util.GeonamesUtility) Country(org.opensextant.data.Country) Test(org.junit.Test)

Example 10 with Country

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;
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) GeonamesUtility(org.opensextant.util.GeonamesUtility) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Country(org.opensextant.data.Country) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Logger(org.slf4j.Logger) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Aggregations

Country (org.opensextant.data.Country)11 InputStreamReader (java.io.InputStreamReader)3 GeonamesUtility (org.opensextant.util.GeonamesUtility)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Reader (java.io.Reader)2 Language (org.opensextant.data.Language)2 Place (org.opensextant.data.Place)2 CsvMapReader (org.supercsv.io.CsvMapReader)2 BufferedReader (java.io.BufferedReader)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)1 Test (org.junit.Test)1 CountryCount (org.opensextant.extractors.geo.CountryCount)1