Search in sources :

Example 1 with NearbyLocationImpl

use of org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl in project ddf by codice.

the class GeoNamesWebService method getNearbyCity.

@Override
public NearbyLocation getNearbyCity(String locationWkt) {
    notNull(locationWkt, "argument locationWkt may not be null");
    Point wktCenterPoint = createPointFromWkt(locationWkt);
    String urlStr = String.format("%s://%s/findNearbyPlaceNameJSON?lat=%f&lng=%f&maxRows=1&username=%s&cities=cities5000", GEONAMES_PROTOCOL, GEONAMES_API_ADDRESS, wktCenterPoint.getY(), wktCenterPoint.getX(), USERNAME);
    Object result = query(urlStr);
    if (result instanceof JSONObject) {
        JSONObject jsonResult = (JSONObject) result;
        JSONArray geonames = (JSONArray) jsonResult.get(GEONAMES_KEY);
        if (geonames != null && geonames.size() > 0) {
            JSONObject firstResult = (JSONObject) geonames.get(0);
            if (firstResult != null) {
                double lat = Double.valueOf((String) firstResult.get(LAT_KEY));
                double lon = Double.valueOf((String) firstResult.get(LON_KEY));
                String cityName = (String) firstResult.get(PLACENAME_KEY);
                Point cityPoint = new PointImpl(lon, lat, SpatialContext.GEO);
                return new NearbyLocationImpl(wktCenterPoint, cityPoint, cityName);
            }
        }
    }
    return null;
}
Also used : NearbyLocationImpl(org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl) JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) Point(org.locationtech.spatial4j.shape.Point) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl)

Example 2 with NearbyLocationImpl

use of org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl 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);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) NearbyLocationImpl(org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) ArrayList(java.util.ArrayList) Point(org.locationtech.spatial4j.shape.Point) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) IndexReader(org.apache.lucene.index.IndexReader) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl)

Example 3 with NearbyLocationImpl

use of org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl in project ddf by codice.

the class GazetteerQueryCatalog method transformMetacardToNearbyLocation.

private NearbyLocation transformMetacardToNearbyLocation(String location, Metacard metacard) {
    String metacardLocation = getStringAttributeFromMetacard(metacard, Core.LOCATION);
    String name = getStringAttributeFromMetacard(metacard, Core.TITLE);
    if (StringUtils.isEmpty(metacardLocation) || StringUtils.isEmpty(name)) {
        LOGGER.debug("GeoEntry metacard does not contain required attribute.");
        return null;
    }
    Double lat;
    Double lon;
    PointImpl centerPoint;
    try {
        Geometry geometry = WKT_READER_THREAD_LOCAL.get().read(metacardLocation);
        Coordinate coordinate = geometry.getCoordinate();
        lat = coordinate.x;
        lon = coordinate.y;
        Point center = WKT_READER_THREAD_LOCAL.get().read(location).getCentroid();
        centerPoint = new PointImpl(center.getY(), center.getX(), SPATIAL_CONTEXT);
    } catch (org.locationtech.jts.io.ParseException e) {
        LOGGER.debug("GeoEntry metacard does not contain location attribute.");
        return null;
    }
    return new NearbyLocationImpl(centerPoint, new PointImpl(lon, lat, SPATIAL_CONTEXT), name);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) NearbyLocationImpl(org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl) Coordinate(org.locationtech.jts.geom.Coordinate) Point(org.locationtech.jts.geom.Point) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl)

Example 4 with NearbyLocationImpl

use of org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl in project ddf by codice.

the class GeoNamesWebService method getNearestCities.

@Override
public List<NearbyLocation> getNearestCities(String locationWkt, int radiusInKm, int maxResults) throws java.text.ParseException, GeoEntryQueryException {
    notNull(locationWkt, "argument locationWkt may not be null");
    Point wktCenterPoint = createPointFromWkt(locationWkt);
    String urlStr = String.format("%s://%s/findNearbyPlaceNameJSON?lat=%f&lng=%f&maxRows=%d&radius=%d&username=%s&cities=cities5000", GEONAMES_PROTOCOL, GEONAMES_API_ADDRESS, wktCenterPoint.getY(), wktCenterPoint.getX(), limitMaxRows(maxResults), radiusInKm, USERNAME);
    Object result = webQuery(urlStr);
    if (result instanceof JSONObject) {
        JSONObject jsonResult = (JSONObject) result;
        JSONArray geoNames = (JSONArray) jsonResult.get(GEONAMES_KEY);
        if (geoNames != null) {
            return geoNames.stream().map(JSONObject.class::cast).map(obj -> {
                double lat = Double.parseDouble((String) obj.get(LAT_KEY));
                double lon = Double.parseDouble((String) obj.get(LON_KEY));
                String cityName = (String) obj.get(PLACENAME_KEY);
                Point cityPoint = new PointImpl(lon, lat, SpatialContext.GEO);
                return new NearbyLocationImpl(wktCenterPoint, cityPoint, cityName);
            }).collect(toList());
        }
    }
    return Collections.emptyList();
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) LoggerFactory(org.slf4j.LoggerFactory) Validate.notNull(org.apache.commons.lang.Validate.notNull) JSONParser(net.minidev.json.parser.JSONParser) MediaType(javax.ws.rs.core.MediaType) Locale(java.util.Locale) ParseException(net.minidev.json.parser.ParseException) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryQueryable(org.codice.ddf.spatial.geocoding.GeoEntryQueryable) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) JtsSpatialContextFactory(org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory) Logger(org.slf4j.Logger) WebClient(org.apache.cxf.jaxrs.client.WebClient) MissingResourceException(java.util.MissingResourceException) Shape(org.locationtech.spatial4j.shape.Shape) StandardCharsets(java.nio.charset.StandardCharsets) Suggestion(org.codice.ddf.spatial.geocoding.Suggestion) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl) NearbyLocationImpl(org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl) Collectors.toList(java.util.stream.Collectors.toList) URLEncoder(java.net.URLEncoder) List(java.util.List) Point(org.locationtech.spatial4j.shape.Point) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) Optional(java.util.Optional) ProcessingException(javax.ws.rs.ProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) NearbyLocationImpl(org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl) JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) Point(org.locationtech.spatial4j.shape.Point) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl)

Aggregations

NearbyLocationImpl (org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl)4 PointImpl (org.locationtech.spatial4j.shape.impl.PointImpl)4 Point (org.locationtech.spatial4j.shape.Point)3 JSONArray (net.minidev.json.JSONArray)2 JSONObject (net.minidev.json.JSONObject)2 GeoEntryQueryException (org.codice.ddf.spatial.geocoding.GeoEntryQueryException)2 NearbyLocation (org.codice.ddf.spatial.geocoding.context.NearbyLocation)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URLEncoder (java.net.URLEncoder)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Locale (java.util.Locale)1 MissingResourceException (java.util.MissingResourceException)1 Optional (java.util.Optional)1 Collectors.toList (java.util.stream.Collectors.toList)1 ProcessingException (javax.ws.rs.ProcessingException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1