use of org.locationtech.jts.geom.Coordinate in project ddf by codice.
the class OpenSearchParserImplTest method populateMultipleSearchesSpatial.
@Test
public void populateMultipleSearchesSpatial() throws ParseException {
double lat = 43.25;
double lon = -123.45;
double radius = 10000;
final PointRadius pointRadius = new PointRadius(lon, lat, radius);
final Polygon polygon = GEOMETRY_FACTORY.createPolygon(GEOMETRY_FACTORY.createLinearRing(new Coordinate[] { new Coordinate(1, 1), new Coordinate(5, 1), new Coordinate(5, 5), new Coordinate(1, 5), new Coordinate(1, 1) }), null);
final Geometry geometry = new WKTReader().read(WKT_GEOMETRY);
final BoundingBox boundingBox = new BoundingBox(170, 50, -150, 60);
openSearchParser.populateSpatial(webClient, geometry, boundingBox, polygon, pointRadius, Arrays.asList("q,src,mr,start,count,mt,dn,lat,lon,radius,bbox,geometry,polygon,dtstart,dtend,dateName,filter,sort".split(",")));
assertQueryParameterPopulated(OpenSearchConstants.GEOMETRY, WKT_GEOMETRY);
assertQueryParameterPopulated(OpenSearchConstants.POLYGON, "1.0,1.0,1.0,5.0,5.0,5.0,5.0,1.0,1.0,1.0");
assertQueryParameterPopulated(OpenSearchConstants.BBOX, "170.0,50.0,-150.0,60.0");
assertQueryParameterPopulated(OpenSearchConstants.LAT, String.valueOf(lat));
assertQueryParameterPopulated(OpenSearchConstants.LON, String.valueOf(lon));
assertQueryParameterPopulated(OpenSearchConstants.RADIUS, String.valueOf(radius));
}
use of org.locationtech.jts.geom.Coordinate in project ddf by codice.
the class OpenSearchFilterVisitor method buildPointRadiusSearch.
protected static void buildPointRadiusSearch(DWithin filter, Object data) {
OpenSearchFilterVisitorObject openSearchFilterVisitorObject = getOpenSearchFilterVisitorObjectFromData(data);
if (openSearchFilterVisitorObject == null) {
return;
}
if (NestedTypes.NOT.equals(openSearchFilterVisitorObject.getCurrentNest())) {
LOGGER.debug(NOT_OPERATOR_UNSUPPORTED_MSG);
return;
}
final org.opengis.filter.expression.Expression expression1 = filter.getExpression1();
final String expectedSpatialSearchTerm = OpenSearchConstants.SUPPORTED_SPATIAL_SEARCH_TERM;
if (!expectedSpatialSearchTerm.equals(expression1.toString())) {
LOGGER.debug("The OpenSearch Source only supports spatial criteria on the term \"{}\", but expression1 is \"{}\". Ignoring filter.", expectedSpatialSearchTerm, expression1);
return;
}
// The geometry is wrapped in a <Literal> element, so have to get the geometry expression as a
// literal and then evaluate it to get the geometry.
// Example:
// <ogc:Literal>org.geotools.geometry.jts.spatialschema.geometry.primitive.PointImpl@dc33f184</ogc:Literal>
Literal literalWrapper = (Literal) filter.getExpression2();
Object geometryExpression = literalWrapper.getValue();
double distance = filter.getDistance();
final double radiusRangeLowerBound = 0;
if (distance <= radiusRangeLowerBound) {
LOGGER.debug("Radius must be greater than {}. Ignoring DWithin filter.", radiusRangeLowerBound);
} else if (geometryExpression instanceof PointImpl) {
PointImpl point = (PointImpl) literalWrapper.evaluate(null);
double[] coords = point.getCentroid().getCoordinate();
LOGGER.trace("point: coords[0] = {}, coords[1] = {}", coords[0], coords[1]);
LOGGER.trace("radius = {}", distance);
openSearchFilterVisitorObject.addPointRadiusSearch(new PointRadius(coords[0], coords[1], distance));
} else if (geometryExpression instanceof Point) {
Point point = (Point) literalWrapper.evaluate(null);
Coordinate coords = point.getCoordinate();
LOGGER.trace("point: coords.x = {}, coords.y = {}", coords.x, coords.y);
LOGGER.trace("radius = {}", distance);
openSearchFilterVisitorObject.addPointRadiusSearch(new PointRadius(coords.x, coords.y, distance));
} else {
LOGGER.debug("The OpenSearch Source only supports POINT geometry WKT for DWithin filter, but the geometry is {}.", geometryExpression);
}
}
use of org.locationtech.jts.geom.Coordinate in project ddf by codice.
the class LineString method getLineStringCoordinates.
protected Coordinates getLineStringCoordinates(Geometry geometry) {
Coordinates coordinates = new Coordinates();
for (int i = 0; i < geometry.getCoordinates().length; i++) {
Coordinate coordinate = geometry.getCoordinates()[i];
coordinates.add(convert(coordinate));
}
return coordinates;
}
use of org.locationtech.jts.geom.Coordinate in project ddf by codice.
the class GeospatialEvaluator method buildGeometry.
public static Geometry buildGeometry(String gmlText) throws IOException, SAXException, ParserConfigurationException {
String methodName = "buildGeometry";
LOGGER.trace(ENTERING_STR, methodName);
Geometry geometry = null;
gmlText = supportSRSName(gmlText);
try {
LOGGER.debug("Creating geoTools Configuration ...");
Configuration config = new org.geotools.gml3.GMLConfiguration();
LOGGER.debug("Parsing geoTools configuration");
Parser parser = new Parser(config);
LOGGER.debug("Parsing gmlText");
geometry = (Geometry) (parser.parse(new StringReader(gmlText)));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("geometry (before conversion): {}", geometry.toText());
}
// The metadata schema states that <gml:pos> elements specify points in
// LAT,LON order. But WKT specifies points in LON,LAT order. When the geoTools
// libraries return the geometry data, it's WKT is in LAT,LON order (which is
// incorrect).
// As a workaround here, for Polygons and Points (which are currently the only spatial
// criteria supported) we must swap the x,y of each coordinate so that they are
// specified in LON,LAT order and then use the swapped coordinates to create a new
// Polygon or Point to be returned to the caller.
GeometryFactory geometryFactory = new GeometryFactory();
if (geometry instanceof Polygon) {
// Build new array of coordinates using the swapped coordinates
ArrayList<Coordinate> newCoords = new ArrayList<Coordinate>();
// Swap each coordinate's x,y so that they specify LON,LAT order
for (Coordinate coord : geometry.getCoordinates()) {
newCoords.add(new Coordinate(coord.y, coord.x));
}
// Create a new polygon using the swapped coordinates
Polygon polygon = new Polygon(geometryFactory.createLinearRing(newCoords.toArray(new Coordinate[newCoords.size()])), null, geometryFactory);
if (LOGGER.isDebugEnabled()) {
// this logs the transformed WKT
LOGGER.debug("Translates to {}", polygon.toText());
// with LON,LAT ordered points
LOGGER.trace(EXITING_STR, methodName);
}
return polygon;
}
if (geometry instanceof Point) {
// Create a new point using the swapped coordinates that specify LON,LAT order
Point point = geometryFactory.createPoint(new Coordinate(geometry.getCoordinate().y, geometry.getCoordinate().x));
if (LOGGER.isDebugEnabled()) {
// this logs the transformed WKT
LOGGER.debug("Translates to {}", point.toText());
// with a LON,LAT ordered point
LOGGER.trace(EXITING_STR, methodName);
}
return point;
}
} catch (Exception e) {
LOGGER.debug("Exception using geotools", e);
}
LOGGER.debug("No translation done for geometry - probably not good ...");
LOGGER.trace(EXITING_STR, methodName);
return geometry;
}
use of org.locationtech.jts.geom.Coordinate in project ddf by codice.
the class GeoNamesCatalogIndexer method transformGeoEntryToMetacard.
private Metacard transformGeoEntryToMetacard(GeoEntry geoEntry) {
if (!GeoCodingConstants.CITY_FEATURE_CODES.contains(geoEntry.getFeatureCode())) {
return null;
}
Metacard metacard = new MetacardImpl(geoNamesMetacardType);
String id = uuidGenerator.generateUuid();
metacard.setAttribute(new AttributeImpl(Core.TITLE, String.format(TITLE_FORMAT, geoEntry.getName(), geoEntry.getCountryCode())));
metacard.setAttribute(new AttributeImpl(Core.DESCRIPTION, geoEntry.getAlternateNames()));
metacard.setAttribute(new AttributeImpl(Location.COUNTRY_CODE, geoEntry.getCountryCode()));
metacard.setAttribute(new AttributeImpl(Core.ID, id));
metacard.setAttribute(new AttributeImpl(GeoEntryAttributes.FEATURE_CODE_ATTRIBUTE_NAME, geoEntry.getFeatureCode()));
Integer gazetteerSortValue = getGeoNameGazetterSortByFeatureClass(geoEntry);
if (gazetteerSortValue != null) {
metacard.setAttribute(new AttributeImpl(GeoEntryAttributes.GAZETTEER_SORT_VALUE, gazetteerSortValue.intValue()));
} else {
metacard.setAttribute(new AttributeImpl(GeoEntryAttributes.GAZETTEER_SORT_VALUE, geoEntry.getPopulation()));
}
metacard.setAttribute(new AttributeImpl(GeoEntryAttributes.POPULATION_ATTRIBUTE_NAME, geoEntry.getPopulation()));
if (StringUtils.isNotBlank(geoEntry.getImportLocation())) {
metacard.setAttribute(new AttributeImpl(GeoEntryAttributes.IMPORT_LOCATION, geoEntry.getImportLocation()));
}
Double latitude = geoEntry.getLatitude();
Double longitude = geoEntry.getLongitude();
if (latitude != null && longitude != null) {
Coordinate coordinate = new Coordinate(longitude, latitude);
Geometry geometry = new GeometryFactory().createPoint(coordinate);
String wkt = WKT_WRITER_THREAD_LOCAL.get().write(geometry);
metacard.setAttribute(new AttributeImpl(Core.LOCATION, wkt));
}
metacard.setAttribute(new AttributeImpl(Core.METACARD_TAGS, Arrays.asList(GAZETTEER_METACARD_TAG, GeoCodingConstants.GEONAMES_TAG)));
return metacard;
}
Aggregations