Search in sources :

Example 1 with GeonamesUtility

use of org.opensextant.util.GeonamesUtility in project Xponents by OpenSextant.

the class TestGeonamesMeta method testMetadata.

@Test
public void testMetadata() throws IOException {
    GeonamesUtility util = new GeonamesUtility();
    assertTrue(!util.getAdmin1Metadata().isEmpty());
    for (Place adm1 : util.getAdmin1Metadata()) {
        print(String.format("%s (%s)", adm1.getPlaceName(), adm1.getAdmin1PostalCode()));
    }
}
Also used : GeonamesUtility(org.opensextant.util.GeonamesUtility) Place(org.opensextant.data.Place) Test(org.junit.Test)

Example 2 with GeonamesUtility

use of org.opensextant.util.GeonamesUtility in project Xponents by OpenSextant.

the class TestGazetteer method main.

/**
     * Do a basic test -- This main prog makes use of the default JVM arg for solr:  -Dopensextant.solr=/path/to/solr
     *
     *
     * @param args the arguments
     * @throws Exception the exception
     */
public static void main(String[] args) {
    try {
        GeonamesUtility geodataUtil = new GeonamesUtility();
        Country aus = geodataUtil.getCountry("AUS");
        System.out.println("Got Australia..." + aus);
        gaz = new SolrGazetteer();
        // Try to get countries
        Map<String, Country> countries = gaz.getCountries();
        for (Country c : countries.values()) {
            System.out.println(c.getKey() + " = " + c.name + "\t  Aliases: " + c.getAliases().toString());
        }
        /*
             * This test organizes country names to see if there are any country names
             * that are unique.
             */
        List<String> cnames = new ArrayList<>();
        Map<String, Boolean> done = new TreeMap<>();
        for (Country C : geodataUtil.getCountries()) {
            String q = String.format("name:%s AND -feat_code:PCL* AND -feat_code:TERR", C.getName());
            List<Place> country_name_matches = gaz.search(q, true);
            //System.out.println("Matched names for " + C.getName() + ":\t");
            String cname = TextUtils.removeDiacritics(C.getName()).toLowerCase();
            done.put(cname, false);
            for (Place p : country_name_matches) {
                String pname = TextUtils.removeDiacritics(p.getName());
                if (pname.equalsIgnoreCase(cname)) {
                    done.put(p.getName().toLowerCase(), true);
                }
            }
            cnames.add(cname);
        }
        //Collections.sort(cnames);
        for (String cname : done.keySet()) {
            System.out.println(String.format("\"%s\", Has Duplicates:", cname) + done.get(cname));
        }
        // US
        testPlacesAt(44, -118, 25, /*km*/
        "P");
        // East Asia
        testPlacesAt(44, 118, 100, /*km*/
        "A");
        // Europe
        testPlacesAt(44, 0, 250, /*km*/
        "A");
        // Europe
        testPlacesAt(44, 0, 10, /*km*/
        "P");
    } catch (Exception err) {
        err.printStackTrace();
    } finally {
        gaz.shutdown();
        System.exit(0);
    }
}
Also used : SolrGazetteer(org.opensextant.extractors.geo.SolrGazetteer) GeonamesUtility(org.opensextant.util.GeonamesUtility) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) SolrServerException(org.apache.solr.client.solrj.SolrServerException) Country(org.opensextant.data.Country) Place(org.opensextant.data.Place)

Example 3 with GeonamesUtility

use of org.opensextant.util.GeonamesUtility 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 4 with GeonamesUtility

use of org.opensextant.util.GeonamesUtility in project Xponents by OpenSextant.

the class TestGeonamesLanguages method test.

@Test
public void test() {
    try {
        GeonamesUtility util = new GeonamesUtility();
        // NOTE: caller has to invoke this language metadata load separately.
        // This metadata should be carried close to gazetteer, e.g., solr/gazetteer/conf/geonames.org/
        // with  "....solr/gazetteer/conf/"  path in CLASSPATH.
        // 
        util.loadCountryLanguages();
        Collection<String> langs = util.languagesInCountry("RW");
        assertNotNull(langs);
        assertTrue(!langs.isEmpty());
        print(langs.toString());
        print("is French spoken in Rwanda? " + util.countrySpeaks("FR", "RW"));
        assertTrue(util.countrySpeaks("FR", "RW"));
        print("is French spoken in US? " + util.countrySpeaks("fr", "US"));
        assertTrue(util.countrySpeaks("fr", "US"));
        print("is RW French spoken in Rwanda? " + util.countrySpeaks("FR-RW", "RW"));
        print("Primary language of Rwanda? " + util.primaryLanguage("RW"));
        Collection<String> countries = util.countriesSpeaking("fr");
        print("Countries speaking French " + countries.toString());
        // Test island nations -- these entries in geonames.org data have fewer columns: no neighbors column.
        // 
        print("Spoken in Philippines - " + util.languagesInCountry("PH"));
        print("Unkonwn Language IDs " + util.unknownLanguages.toString());
        String[] primaryLangCountries = { "US", "AT", "RU", "ZA", "CN", "TW", "KR", "KP", "JP", "TH", "ID", "PH", "MY", "FR", "ES", "DE", "CA", "MX", "SV", "SD", "SS", "EG" };
        for (String cc : primaryLangCountries) {
            print("Primary language of " + cc + "? " + util.primaryLanguage(cc));
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : GeonamesUtility(org.opensextant.util.GeonamesUtility) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with GeonamesUtility

use of org.opensextant.util.GeonamesUtility 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

GeonamesUtility (org.opensextant.util.GeonamesUtility)6 Test (org.junit.Test)3 Country (org.opensextant.data.Country)3 IOException (java.io.IOException)2 Place (org.opensextant.data.Place)2 ArrayList (java.util.ArrayList)1 List (java.util.List)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 NamedList (org.apache.solr.common.util.NamedList)1 SolrGazetteer (org.opensextant.extractors.geo.SolrGazetteer)1 Logger (org.slf4j.Logger)1