Package com.bericotech.clavin.gazetteer

Examples of com.bericotech.clavin.gazetteer.GeoName


     * @throws ClavinException   if an error occurs
     */
    @Override
    public GeoName getGeoName(final int geonameId) throws ClavinException {
        try {
            GeoName geoName = null;
            // Lucene query used to look for exact match on the "geonameID" field
            Query q = NumericRangeQuery.newIntRange(GEONAME_ID.key(), geonameId, geonameId, true, true);
            // retrieve only one matching document
            TopDocs results = indexSearcher.search(q, 1);
            if (results.scoreDocs.length > 0) {
                Document doc = indexSearcher.doc(results.scoreDocs[0].doc);
                geoName = GeoName.parseFromGeoNamesRecord(doc.get(GEONAME.key()), doc.get(PREFERRED_NAME.key()));
                if (!geoName.isAncestryResolved()) {
                    Integer parentId = PARENT_ID.getValue(doc);
                    if (parentId != null) {
                        Map<Integer, Set<GeoName>> childMap = new HashMap<Integer, Set<GeoName>>();
                        childMap.put(parentId, Collections.singleton(geoName));
                        resolveParents(childMap);
View Full Code Here


        // matched ancestors in the search results and populate the map;
        // otherwise, we cannot populate the map with anything other than
        // the best result because we cannot verify which search result
        // is the parent of the selected location
        if (bestMatch.getGeoname().isAncestryResolved()) {
            GeoName parent = bestMatch.getGeoname().getParent();
            while (parent != null) {
                SearchLevel level = SearchLevel.forGeoName(parent);
                if (resultsMap.containsKey(level)) {
                    // find parent GeoName in the results; this should exist because
                    // searches are filtered by ancestry from prior results
                    ResolvedLocation parentLoc = null;
                    List<ResolvedLocation> searchResults = resultsMap.get(level);
                    int depth;
                    for (depth = 0; depth < searchResults.size(); depth++) {
                        ResolvedLocation loc = searchResults.get(depth);
                        if (parent.getGeonameID() == loc.getGeoname().getGeonameID()) {
                            parentLoc = loc;
                            break;
                        }
                    }
                    if (parentLoc == null) {
                        // log this as an error condition; it indicates a problem with either
                        // gazetteer construction or ancestry indexing; this has been noticed
                        // with certain historical locations, specifically Netherlands Antilles (PCLH),
                        // which lists Curacao (PCLIX), another country, as a parent. We shouldn't
                        // fail the entire matching algorithm at this point; just log and ignore
                        LOG.error(String.format("Missing parent [%s] in search results for match: %s.", parent, bestMatch));
                    } else {
                        // found a match, add it to the list
                        matches.put(level, new Match(level, parentLoc, depth));
                    }
                }
                parent = parent.getParent();
            }
        }
    }
View Full Code Here

            Set<Integer> parentIds = new HashSet<Integer>();
            Set<String> parentCodes = new HashSet<String>();
            Set<String> foundParents = new HashSet<String>();
            // only include the first (best) result for each distinct parent in the filter set
            for (ResolvedLocation loc : results) {
                GeoName geo = loc.getGeoname();
                String pCode = lastMatch != null ? lastMatch.level.getCode(geo) : null;
                // if there were no parent filters or we have not found a child for this parent
                // code, add this location to the filter set
                if (lastMatch == null || !foundParents.contains(pCode)) {
                    parentIds.add(geo.getGeonameID());
                    parentCodes.add(level.getCode(geo));
                    foundParents.add(pCode);
                }
                // if there was a previous filter set, short-circuit once we have
                // a child from each parent
View Full Code Here

                    count += 1;
                    // print progress update to console
                    if (count % 100000 == 0 ) {
                        LOG.info("rowcount: " + count);
                    }
                    GeoName geoName = GeoName.parseFromGeoNamesRecord(line);
                    resolveAncestry(geoName);
                } catch (IOException e) {
                    LOG.info("Skipping... Error on line: {}", line);
                } catch (RuntimeException re) {
                    LOG.info("Skipping... Error on line: {}", line);
View Full Code Here

        // if this is an administrative division, configure the parent of any waiting
        // GeoNames and notify all 2nd level and further descendants their tree has been
        // updated
        String myKey = geoname.getAncestryKey();
        if (myKey != null) {
            GeoName conflict = adminMap.get(myKey);
            if (conflict != null) {
                LOG.error(String.format("Resolved duplicate admin key [%s] for GeoNames (%d %s:%s %s) and (%d %s:%s %s)",
                        myKey, conflict.getGeonameID(), conflict.getFeatureClass(), conflict.getFeatureCode(), conflict.getName(),
                        geoname.getGeonameID(), geoname.getFeatureClass(), geoname.getFeatureCode(), geoname.getName()));
            }
            adminMap.put(myKey, geoname);
            checkDescendantsResolved(geoname, true);
        }
View Full Code Here

            Set<GeoName> descendants = unresolvedMap.get(key);
            if (descendants != null) {
                // use an iterator so we can remove elements
                Iterator<GeoName> iter = descendants.iterator();
                while (iter.hasNext()) {
                    GeoName desc = iter.next();
                    if (setParent) {
                        if (!desc.setParent(geoname)) {
                            LOG.error("Error setting parent [{}] of GeoName [{}].", geoname, desc);
                        }
                    }
                    if (desc.isAncestryResolved()) {
                        checkDescendantsResolved(desc, false);
                        indexGeoName(desc);
                        iter.remove();
                    }
                }
View Full Code Here

        // iterate over keys, attempting to resolve less specific keys first; if
        // they are resolved, this may result in more specific keys being resolved
        // as well
        for (String key : keys) {
            String subKey = key;
            GeoName parent = null;
            int lastDot;
            while (parent == null && (lastDot = subKey.lastIndexOf(".")) > 0) {
                subKey = key.substring(0, lastDot);
                parent = adminMap.get(subKey);
            }
            if (parent != null) {
                Set<GeoName> unresolved = unresolvedMap.get(key);
                if (unresolved == null) {
                    // resolving a higher-level key also resolved this key; do nothing
                    break;
                }
                Iterator<GeoName> iter = unresolved.iterator();
                // use iterator so we can remove
                while (iter.hasNext()) {
                    GeoName geoName = iter.next();
                    // first check to see if a previous loop resolved all parents
                    if (geoName.isAncestryResolved()) {
                        indexGeoName(geoName);
                        iter.remove();
                    } else if (geoName.setParent(parent)) {
                        if (geoName.isAncestryResolved()) {
                            // ancestry has been resolved, remove from the unresolved collection
                            indexGeoName(geoName);
                            iter.remove();
                        } else {
                            LOG.error("GeoName [{}] should be fully resolved. (parent: {})", geoName, parent);
View Full Code Here

        // if the alternate names file was loaded and we found a preferred name for this GeoName, store it
        if (preferredName != null) {
            doc.add(new StoredField(PREFERRED_NAME.key(), preferredName.name));
        }
        // index the direct parent ID in the PARENT_ID field
        GeoName parent = geoName.getParent();
        if (parent != null) {
            doc.add(new IntField(PARENT_ID.key(), parent.getGeonameID(), Field.Store.YES));
        }
        // index all ancestor IDs in the ANCESTOR_IDS field; this is a secondary field
        // so it can be used to restrict searches and PARENT_ID can be used for ancestor
        // resolution
        while (parent != null) {
            doc.add(new IntField(ANCESTOR_IDS.key(), parent.getGeonameID(), Field.Store.YES));
            parent = parent.getParent();
        }
        doc.add(new LongField(POPULATION.key(), geoName.getPopulation(), Field.Store.YES));
        // set up sort field based on population and geographic feature type
        if (geoName.getFeatureClass().equals(FeatureClass.P) || geoName.getFeatureCode().name().startsWith("PCL")) {
            if (geoName.getGeonameID() != 2643741) // todo: temporary hack until GeoNames.org fixes the population for City of London
View Full Code Here

        }
        auditRepository.auditAnalyzedBy(AuditAction.ANALYZED_BY, sourceVertex, getClass().getSimpleName(), user, sourceVertex.getVisibility());
    }

    private String toSign(final ResolvedLocation location) {
        GeoName geoname = location.getGeoname();
        return String.format("%s (%s, %s)", geoname.getName(), geoname.getPrimaryCountryCode(), geoname.getAdmin1Code());
    }
View Full Code Here

    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static HashMap writeAboutnessLocationToHash(FocusLocation location){
        HashMap loc = new HashMap();
        GeoName place = location.getGeoName();
        loc.put("score", location.getScore());
        loc.put("id",place.getGeonameID());
        loc.put("name",place.getName());
        String primaryCountryCodeAlpha2 = "";
        if(place.getPrimaryCountryCode()!=CountryCode.NULL){
            primaryCountryCodeAlpha2 = place.getPrimaryCountryCode().toString();
        }
        String admin1Code = "";
       
        if(place.getAdmin1Code() !=null){
            admin1Code = place.getAdmin1Code();
        }
        String featureCode = place.getFeatureCode().toString();
        loc.put("featureClass", place.getFeatureClass().toString());
        loc.put("featureCode", featureCode);
        loc.put("population", place.getPopulation());
        loc.put("stateCode", admin1Code);
        loc.put("countryCode",primaryCountryCodeAlpha2);
        loc.put("lat",place.getLatitude());
        loc.put("lon",place.getLongitude());
       
        return loc;
    }
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.