Search in sources :

Example 1 with Country

use of org.opensextant.data.Country in project Xponents by OpenSextant.

the class PlaceGeocoder method countryInScope.

/**
     * Record how often country references are made.
     * 
     * @param cc
     */
@Override
public void countryInScope(String cc) {
    Country C = countryCatalog.get(cc);
    if (C == null) {
        log.debug("Unknown country code {}", cc);
        return;
    }
    CountryCount counter = relevantCountries.get(C.getCountryCode());
    if (counter == null) {
        counter = new CountryCount();
        // Well, we must deal with a potential unknown country.  
        // Historical differences, XK = Kosovo, YU = Yugoslavia; 
        // FIPS vs. ISO differences, etc.  Some country codes may not resolve cleanly.
        counter.country = C;
        relevantCountries.put(C.getCountryCode(), counter);
    } else {
        ++counter.count;
    }
}
Also used : Country(org.opensextant.data.Country)

Example 2 with Country

use of org.opensextant.data.Country in project Xponents by OpenSextant.

the class GeonamesUtility method loadCountryNameMap.

private void loadCountryNameMap() throws IOException {
    java.io.InputStream io = getClass().getResourceAsStream("/country-names-2015.csv");
    java.io.Reader countryIO = new InputStreamReader(io);
    CsvMapReader countryMap = new CsvMapReader(countryIO, CsvPreference.EXCEL_PREFERENCE);
    String[] columns = countryMap.getHeader(true);
    Map<String, String> country_names = null;
    while ((country_names = countryMap.read(columns)) != null) {
        String n = country_names.get("country_name");
        String cc = country_names.get("ISO2_cc");
        String iso3 = country_names.get("ISO3_cc");
        String fips = country_names.get("FIPS_cc");
        //ISO2
        iso2fips.put(cc, fips);
        iso2fips.put(iso3, fips);
        fips2iso.put(fips, cc);
        if (n == null || cc == null) {
            continue;
        }
        cc = cc.toUpperCase(Locale.ENGLISH);
        fips = fips.toUpperCase(Locale.ENGLISH);
        // Unique Name?  E.g., "Georgia" country name is not unique.
        // This flag helps inform Disambiguation choose countries and places.
        boolean isUniq = Boolean.parseBoolean(country_names.get("is_unique_name"));
        boolean isTerr = Boolean.parseBoolean(country_names.get("territory"));
        // FIPS could be *, but as long as we use ISO2, we're fine. if
        // ("*".equals(cc)){ cc = fips.toUpperCase(); }
        // Normalize: "US" =&gt; "united states of america"
        defaultCountryNames.put(cc, n.toLowerCase(Locale.ENGLISH));
        Country C = new Country(cc, n);
        C.CC_FIPS = fips;
        C.CC_ISO2 = cc;
        C.CC_ISO3 = iso3;
        C.setUniqueName(isUniq);
        C.isTerritory = isTerr;
        // ISO
        if (!C.isTerritory) {
            isoCountries.put(cc, C);
            isoCountries.put(iso3, C);
        }
        // FIPS  -- mostly unique.
        if (!fips.equals("*")) {
            fipsCountries.put(fips, C);
        }
        countries.add(C);
    }
    countryMap.close();
    if (defaultCountryNames.isEmpty()) {
        throw new IOException("No data found in country name map");
    }
    // Less standard country/territory codes:
    //
    // Gaza Strip, GAZ is occassionally used.
    isoCountries.put("GAZ", isoCountries.get("PS"));
    // East Timor-Leste; 
    isoCountries.put("TMP", isoCountries.get("TLS"));
    // Missing Country. 
    // Valid value for country code, just means no geo-political affiliation.
    // Note, Undersea Features and International waters are mapped to ZZ. 
    // This overrides those settings.
    Country noCountryAffiliation = new Country("ZZ", "(No Country)");
    noCountryAffiliation.CC_FIPS = "ZZ";
    noCountryAffiliation.CC_ISO3 = "ZZZ";
    isoCountries.put(noCountryAffiliation.CC_ISO2, noCountryAffiliation);
    isoCountries.put(noCountryAffiliation.CC_ISO3, noCountryAffiliation);
    /* Once all country data is known, associate territories appropriately.
         * 
         */
    for (Country C : countries) {
        if (C.isTerritory) {
            Country owningCountry = isoCountries.get(C.getCountryCode());
            if (owningCountry != null) {
                owningCountry.addTerritory(C);
            } else {
                // This territory is unique, and country code does not conflict with anything else.
                // 
                isoCountries.put(C.getCountryCode(), C);
                isoCountries.put(C.CC_ISO3, C);
            }
        }
    }
    /**
         * Important data for many tools where time-of-day or other metadata is meaningful.
         */
    loadCountryTimezones();
}
Also used : InputStreamReader(java.io.InputStreamReader) CsvMapReader(org.supercsv.io.CsvMapReader) Country(org.opensextant.data.Country) Reader(java.io.Reader) IOException(java.io.IOException) InputStream(java.io.InputStream)

Example 3 with Country

use of org.opensextant.data.Country in project Xponents by OpenSextant.

the class GeonamesUtility method primaryLanguage.

/**
     * Primary language for a given country. By our convention, this will be the major language family, not the locale.
     * E.g.,
     * primary language of Australia? 'en', not 'en_AU'; The hashmap records the first entry only which is language.
     * 
     * @param cc
     *            Country code
     * @return Language object
     */
public Language primaryLanguage(String cc) {
    Country C = isoCountries.get(cc);
    if (C == null) {
        return null;
    }
    String lid = C.getPrimaryLanguage();
    if (lid == null) {
        return null;
    }
    Language L = TextUtils.getLanguage(lid);
    if (L != null) {
        return L;
    }
    // What language?
    return new Language(lid, lid, lid);
}
Also used : Language(org.opensextant.data.Language) Country(org.opensextant.data.Country)

Example 4 with Country

use of org.opensextant.data.Country in project Xponents by OpenSextant.

the class GeonamesUtility method loadCountryLanguages.

/**
     * Parse metadata from geonames.org (file in CLASSPATH @ /geonames.org/countryInfo.txt)
     * and populate existing Country objects with language metadata.
     * By the time you call this method Countries have names, codes, regions, aliases, timezones.
     * 
     * @throws IOException
     *             if geonames.org resource file is not found
     */
public void loadCountryLanguages() throws IOException {
    final String uri = "/geonames.org/countryInfo.txt";
    // CurrencyCode    CurrencyName    Phone   Postal Code Format  Postal Code Regex   Languages   geonameid   neighbours  EquivalentFipsCode
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(GeonamesUtility.class.getResourceAsStream(uri), "UTF-8"))) {
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.trim().startsWith("#")) {
                continue;
            }
            String[] cells = line.split("\t");
            if (cells.length < 16) {
                continue;
            }
            String langs = cells[15];
            if (isBlank(langs) || isBlank(cells[0])) {
                continue;
            }
            String cc = cells[0].toUpperCase().trim();
            String[] langIDs = langs.toLowerCase().trim().split(",");
            for (String l : langIDs) {
                String lid = getLang(l);
                // TODO: If we want also the primary Locale, then we would track primary locales separately.
                if (!l.equals(lid)) {
                    // Adding lang-ID as first and primary for a given country.
                    addLang(lid, cc);
                }
                addLang(l, cc);
            }
        }
        for (String cc : isoCountries.keySet()) {
            if (cc.length() > 2) {
                continue;
            }
            Country C = isoCountries.get(cc);
            for (String lang : C.getLanguages()) {
                Set<String> ccset = languageInCountries.get(lang);
                if (ccset == null) {
                    ccset = new HashSet<>();
                    languageInCountries.put(lang, ccset);
                }
                ccset.add(cc);
            }
        }
    } catch (Exception err) {
        throw new IOException("Did not find Country Metadata at " + uri, err);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Country(org.opensextant.data.Country) IOException(java.io.IOException) IOException(java.io.IOException) ParseException(java.text.ParseException)

Example 5 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)

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