use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.
the class GeoNamesQueryLuceneIndex method doGetNearestCities.
protected List<NearbyLocation> doGetNearestCities(final Shape shape, final int radiusInKm, final int maxResults, final Directory directory) throws GeoEntryQueryException {
notNull(shape, "GeoNamesQueryLuceneIndex.doGetNearestCities(): argument 'shape' may not be null.");
if (radiusInKm <= 0) {
throw new IllegalArgumentException("GeoNamesQueryLuceneIndex.doGetNearestCities(): radiusInKm must be positive.");
}
if (maxResults <= 0) {
throw new IllegalArgumentException("GeoNamesQueryLuceneIndex.doGetNearestCities(): maxResults must be positive.");
}
if (directory == null) {
return Collections.emptyList();
}
try (final IndexReader indexReader = createIndexReader(directory)) {
final IndexSearcher indexSearcher = createIndexSearcher(indexReader);
final List<NearbyLocation> closestCities = new ArrayList<>();
final Point center = shape.getCenter();
final Query filter = createSpatialQuery(center, radiusInKm);
// Query for all the documents in the index that are cities, then filter those
// results for the ones that are in the search area.
final BooleanQuery booleanQuery = new BooleanQuery.Builder().add(PPL_QUERY, BooleanClause.Occur.MUST).add(filter, BooleanClause.Occur.FILTER).build();
final TopDocs topDocs = indexSearcher.search(booleanQuery, maxResults, SORT);
if (topDocs.totalHits > 0) {
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
final double lat = Double.parseDouble(indexSearcher.doc(scoreDoc.doc).get(GeoNamesLuceneConstants.LATITUDE_FIELD));
final double lon = Double.parseDouble(indexSearcher.doc(scoreDoc.doc).get(GeoNamesLuceneConstants.LONGITUDE_FIELD));
final String name = indexSearcher.doc(scoreDoc.doc).get(GeoNamesLuceneConstants.NAME_FIELD);
final NearbyLocation city = new NearbyLocationImpl(center, new PointImpl(lon, lat, SPATIAL_CONTEXT), name);
closestCities.add(city);
}
}
return closestCities;
} catch (IOException e) {
throw new GeoEntryQueryException("Error reading the index", e);
}
}
use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.
the class GeoNamesQueryLuceneIndex method doGetCountryCode.
protected String doGetCountryCode(Shape shape, int radiusInKm, Directory directory) throws GeoEntryQueryException {
notNull(shape, "GeoNamesQueryLuceneIndex.doGetCountryCode(): argument 'shape' may not be null.");
notNull(directory, "GeoNamesQueryLuceneIndex.doGetCountryCode(): argument 'directory' may not be null.");
if (radiusInKm <= 0) {
throw new IllegalArgumentException("GeoNamesQueryLuceneIndex.doGetCountryCode(): radiusInKm must be positive.");
}
try (final IndexReader indexReader = createIndexReader(directory)) {
final IndexSearcher indexSearcher = createIndexSearcher(indexReader);
final Point center = shape.getCenter();
final Query filter = createSpatialQuery(center, radiusInKm);
final BooleanQuery booleanQuery = new BooleanQuery.Builder().add(filter, BooleanClause.Occur.FILTER).build();
final TopDocs topDocs = indexSearcher.search(booleanQuery, 1, SORT);
String countryCode = null;
if (topDocs.totalHits > 0) {
countryCode = indexSearcher.doc(topDocs.scoreDocs[0].doc).get(GeoNamesLuceneConstants.COUNTRY_CODE_FIELD);
}
return countryCode;
} catch (IOException e) {
throw new GeoEntryQueryException("Error reading the index", e);
}
}
use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.
the class GeoNamesQueryLuceneIndex method doQuery.
protected List<GeoEntry> doQuery(final String queryString, final int maxResults, final Directory directory) throws GeoEntryQueryException {
if (StringUtils.isBlank(queryString)) {
throw new IllegalArgumentException("The query string cannot be null or empty.");
}
if (maxResults < 1) {
throw new IllegalArgumentException("maxResults must be positive.");
}
if (directory == null) {
return Collections.emptyList();
}
try (final IndexReader indexReader = createIndexReader(directory)) {
final IndexSearcher indexSearcher = createIndexSearcher(indexReader);
final Query query = createQuery(queryString);
final TopDocs topDocs = indexSearcher.search(query, maxResults);
if (topDocs.totalHits > 0) {
final List<GeoEntry> results = new ArrayList<>();
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
final Document document = indexSearcher.doc(scoreDoc.doc);
// The alternate names aren't being stored (they are only used for queries),
// so we don't retrieve them here.
results.add(new GeoEntry.Builder().name(document.get(GeoNamesLuceneConstants.NAME_FIELD)).latitude(Double.parseDouble(document.get(GeoNamesLuceneConstants.LATITUDE_FIELD))).longitude(Double.parseDouble(document.get(GeoNamesLuceneConstants.LONGITUDE_FIELD))).featureCode(document.get(GeoNamesLuceneConstants.FEATURE_CODE_FIELD)).population(Long.parseLong(document.get(GeoNamesLuceneConstants.POPULATION_FIELD))).countryCode(document.get(GeoNamesLuceneConstants.COUNTRY_CODE_FIELD)).build());
}
return results;
} else {
return Collections.emptyList();
}
} catch (IOException e) {
throw new GeoEntryQueryException("Error reading the index", e);
} catch (ParseException e) {
throw new GeoEntryQueryException("Error parsing query", e);
}
}
use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.
the class GeoNamesQueryLuceneDirectoryIndex method openDirectoryAndCheckForIndex.
private Directory openDirectoryAndCheckForIndex() throws GeoEntryQueryException {
Directory directory;
try {
directory = openDirectory();
if (!indexExists(directory)) {
directory.close();
LOGGER.debug("There is no index at {}. Load a Geonames file into the offline gazetteer", indexLocation);
return null;
}
return directory;
} catch (IOException e) {
throw new GeoEntryQueryException("Error opening the index directory at " + indexLocation, e);
}
}
use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.
the class GeoCoderEndpoint method getNearbyCities.
@GET
@Path("nearby/cities/{wkt}")
public Response getNearbyCities(@PathParam("wkt") String wkt) {
GeoCoder geoCoder = geoCoderFactory.getService();
try {
NearbyLocation nearbyLocation = geoCoder.getNearbyCity(wkt);
if (nearbyLocation != null) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("direction", nearbyLocation.getCardinalDirection());
jsonObject.put("distance", nearbyLocation.getDistance());
jsonObject.put("name", nearbyLocation.getName());
return Response.ok(jsonObject.toJSONString()).build();
} else {
return Response.status(Response.Status.NO_CONTENT).build();
}
} catch (GeoEntryQueryException e) {
LOGGER.debug("Error querying GeoNames resource with wkt:{}", wkt, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
Aggregations