use of org.locationtech.spatial4j.shape.impl.PointImpl 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;
}
use of org.locationtech.spatial4j.shape.impl.PointImpl 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.locationtech.spatial4j.shape.impl.PointImpl in project crate by crate.
the class JavascriptUserDefinedFunctionTest method testGeoTypeReturnTypeWithWKT.
@Test
public void testGeoTypeReturnTypeWithWKT() throws Exception {
registerUserDefinedFunction("f", DataTypes.GEO_POINT, List.of(), "function f() { return \"POINT (1.0 2.0)\"; }");
assertEvaluate("f()", new PointImpl(1.0, 2.0, JtsSpatialContext.GEO));
}
use of org.locationtech.spatial4j.shape.impl.PointImpl in project crate by crate.
the class JavascriptUserDefinedFunctionTest method testGeoTypeReturnTypeWithDoubleArray.
@Test
public void testGeoTypeReturnTypeWithDoubleArray() throws Exception {
registerUserDefinedFunction("f", DataTypes.GEO_POINT, List.of(), "function f() { return [1, 1]; }");
assertEvaluate("f()", new PointImpl(1.0, 1.0, JtsSpatialContext.GEO));
}
use of org.locationtech.spatial4j.shape.impl.PointImpl in project crate by crate.
the class GeoPointType method implicitCast.
@Override
public Point implicitCast(Object value) throws IllegalArgumentException, ClassCastException {
if (value == null) {
return null;
} else if (value instanceof Point) {
return ((Point) value);
} else if (value instanceof Double[]) {
Double[] doubles = (Double[]) value;
checkLengthIs2(doubles.length);
ensurePointsInRange(doubles[0], doubles[1]);
return new PointImpl(doubles[0], doubles[1], JtsSpatialContext.GEO);
} else if (value instanceof Object[]) {
Object[] values = (Object[]) value;
checkLengthIs2(values.length);
PointImpl point = new PointImpl(((Number) values[0]).doubleValue(), ((Number) values[1]).doubleValue(), JtsSpatialContext.GEO);
ensurePointsInRange(point.getX(), point.getY());
return point;
} else if (value instanceof String) {
return pointFromString((String) value);
} else if (value instanceof List) {
List<?> values = (List<?>) value;
checkLengthIs2(values.size());
PointImpl point = new PointImpl(((Number) values.get(0)).doubleValue(), ((Number) values.get(1)).doubleValue(), JtsSpatialContext.GEO);
ensurePointsInRange(point.getX(), point.getY());
return point;
} else {
throw new ClassCastException("Can't cast '" + value + "' to " + getName());
}
}
Aggregations