Package com.bericotech.clavin.gazetteer

Examples of com.bericotech.clavin.gazetteer.GeoName


    @Test
    public void testResolveAncestry() throws ClavinException {
        List<ResolvedLocation> locs = instance.getClosestLocations(queryBuilder.location("Reston").build());
        assertNotNull("Null results list received from Gazetteer", locs);
        assertEquals("Expected single result from Gazetteer", 1, locs.size());
        GeoName geo = locs.get(0).getGeoname();
        List<Integer> ancestryPath = new ArrayList<Integer>();
        while (geo != null) {
            ancestryPath.add(geo.getGeonameID());
            geo = geo.getParent();
        }
        List<Integer> expectedAncestryPath = Arrays.asList(RESTON_VA, FAIRFAX_COUNTY_VA, VIRGINIA, UNITED_STATES);
        assertEquals("Expected ancestry path of Reston, Fairfax County, Virginia, United States", expectedAncestryPath, ancestryPath);
    }
View Full Code Here


    public void testResolveLocations_RestrictedParents() throws ClavinException {
        queryBuilder.location("Reston").maxResults(10).addParentIds(UNITED_STATES);
        List<ResolvedLocation> locs = instance.getClosestLocations(queryBuilder.build());
        assertFalse("Expected at least one result", locs.isEmpty());
        for (ResolvedLocation loc : locs) {
            GeoName parent = loc.getGeoname();
            while (parent != null && parent.getGeonameID() != UNITED_STATES) {
                parent = parent.getParent();
            }
            assertNotNull(String.format("Expected to find United States [%d] as an ancestor. Key: %s; Loc: %s",
                    UNITED_STATES, loc.getGeoname().getParentAncestryKey(), loc), parent);
        }
    }
View Full Code Here

                addFeatureCodes(FeatureCode.ADMD);
        List<ResolvedLocation> locs = instance.getClosestLocations(queryBuilder.build());
        assertFalse("Expected at least one result.", locs.isEmpty());

        for (ResolvedLocation loc : locs) {
            GeoName geo = loc.getGeoname();
            // verify that all returned GeoNames are ADMD records
            assertEquals(String.format("Incorrect feature code for location: %s", loc), FeatureCode.ADMD, geo.getFeatureCode());
            // verify that all GeoNames are found in the United States
            while (geo != null && geo.getGeonameID() != UNITED_STATES) {
                geo = geo.getParent();
            }
            assertNotNull(String.format("Expected to find United States [%d] as an ancestor. Key: %s; Loc: %s",
                    UNITED_STATES, loc.getGeoname().getParentAncestryKey(), loc), geo);
        }
View Full Code Here

            new Object[]{BOSTON_MA, "Boston, MA"},
            new Object[]{STRAßENHAUS_DE, "Straßenhaus, DE"},
            new Object[]{GUN_BARREL_CITY_TX, "Gun Barrell City, TX"}
        };
        for (Object[] test : testCases) {
            GeoName geoname = instance.getGeoName((Integer) test[0]);
            assertNotNull(String.format("Unexpected null returned by Gazetteer for '%s'", test[1]), geoname);
            assertEquals(String.format("Expected GeoName ID [%d] for '%s'", test[0], test[1]), test[0], geoname.getGeonameID());
        }
    }
View Full Code Here

        // two identical sample gazetteer records from GeoNames.org
        String geonamesEntry = "4781530\tReston\tReston\tReston,Рестон\t38.96872\t-77.3411\tP\tPPL\tUS\tVA\t059\t58404\t100\t102\tAmerica/New_York\t2011-05-14";
        String geonamesEntry2 = "478153\tReston\tReston\tReston,Рестон\t38.96872\t-77.3411\tP\tPPL\tUS\tVA\t059\t58404\t100\t102\tAmerica/New_York\t2011-05-14";

        // create corresponding Lucene Documents for gazetteer records
        GeoName geoname = GeoName.parseFromGeoNamesRecord(geonamesEntry);
        GeoName geoname2 = GeoName.parseFromGeoNamesRecord(geonamesEntry2);

        // a bogus LocationOccurrence object for testing
        LocationOccurrence locationA = new LocationOccurrence("A", 0);

        // two ResolvedLocation objects created from same Lucene Doc, etc.
View Full Code Here

        ResolvedLocation loc = resolver.resolveLocation(false, parts);
        if (expectedId == null) {
            assertNull("expected null location", loc);
        } else {
            assertNotNull("expected non-null location", loc);
            GeoName geo = loc.getGeoname();
            assertEquals(String.format("Incorrect Location [%s]", geo), expectedId.intValue(), geo.getGeonameID());
        }
    }
View Full Code Here

    private void verifyCity(final ResolvedLocation loc) {
        if (cityIds.isEmpty()) {
            assertNull("expected null city", loc);
        } else {
            assertNotNull("expected non-null city", loc);
            GeoName geo = loc.getGeoname();
            assertTrue(String.format("Incorrect city [%s]; expected one of %s", geo, cityIds), cityIds.contains(geo.getGeonameID()));
        }
    }
View Full Code Here

    private void verifyLocation(final String label, final Integer expected, final ResolvedLocation loc) {
        if (expected == null) {
            assertNull(String.format("expected null %s", label), loc);
        } else {
            assertNotNull(String.format("expected non-null %s", label), loc);
            GeoName geo = loc.getGeoname();
            assertEquals(String.format("Incorrect %s [%s]", label, geo), expected.intValue(), geo.getGeonameID());
        }
    }
View Full Code Here

                lastDoc = scoreDoc;
                Document doc = indexSearcher.doc(scoreDoc.doc);
                // reuse GeoName instances so all ancestry is correctly resolved if multiple names for
                // the same GeoName match the query
                int geonameID = GEONAME_ID.getValue(doc);
                GeoName geoname = geonameMap.get(geonameID);
                if (geoname == null) {
                    geoname = GeoName.parseFromGeoNamesRecord((String) GEONAME.getValue(doc), (String) PREFERRED_NAME.getValue(doc));
                    geonameMap.put(geonameID, geoname);
                } else if (dedupe) {
                    // if we have already seen this GeoName and we are removing duplicates, skip to the next doc
                    continue;
                }
                String matchedName = INDEX_NAME.getValue(doc);
                if (!geoname.isAncestryResolved()) {
                    IndexableField parentIdField = doc.getField(IndexField.PARENT_ID.key());
                    Integer parentId = parentIdField != null && parentIdField.numericValue() != null ?
                            parentIdField.numericValue().intValue() : null;
                    if (parentId != null) {
                        Set<GeoName> geos = parentMap.get(parentId);
View Full Code Here

            // Lucene query used to look for exact match on the "geonameID" field
            Query q = NumericRangeQuery.newIntRange(GEONAME_ID.key(), parentId, parentId, true, true);
            TopDocs results = indexSearcher.search(q, null, 1, POPULATION_SORT);
            if (results.scoreDocs.length > 0) {
                Document doc = indexSearcher.doc(results.scoreDocs[0].doc);
                GeoName parent = GeoName.parseFromGeoNamesRecord(doc.get(GEONAME.key()), doc.get(PREFERRED_NAME.key()));
                parentMap.put(parent.getGeonameID(), parent);
                if (!parent.isAncestryResolved()) {
                    Integer grandParentId = PARENT_ID.getValue(doc);
                    if (grandParentId != null) {
                        Set<GeoName> geos = grandParentMap.get(grandParentId);
                        if (geos == null) {
                            geos = new HashSet<GeoName>();
                            grandParentMap.put(grandParentId, geos);
                        }
                        geos.add(parent);
                    }
                }
            } else {
                LOG.error("Unable to find parent GeoName [{}]", parentId);
            }
        }

        // find all parents of the parents
        if (!grandParentMap.isEmpty()) {
            resolveParents(grandParentMap);
        }

        // set parents of children
        for (Integer parentId : childMap.keySet()) {
            GeoName parent = parentMap.get(parentId);
            if (parent == null) {
                LOG.info("Unable to find parent with ID [{}]", parentId);
                continue;
            }
            for (GeoName child : childMap.get(parentId)) {
View Full Code Here

TOP

Related Classes of com.bericotech.clavin.gazetteer.GeoName

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.