use of org.locationtech.spatial4j.shape.Shape in project ddf by codice.
the class CqlResult method normalizeDistance.
private Double normalizeDistance(Result result, Query query, FilterAdapter filterAdapter) {
Double distance = result.getDistanceInMeters();
try {
String queryWkt = filterAdapter.adapt(query, WKT_QUERY_DELEGATE);
if (StringUtils.isNotBlank(queryWkt)) {
Shape queryShape = WKT_READER.read(queryWkt);
if (result.getMetacard() != null && StringUtils.isNotBlank(result.getMetacard().getLocation())) {
Shape locationShape = WKT_READER.read(result.getMetacard().getLocation());
distance = DistanceUtils.degrees2Dist(SPATIAL_CONTEXT.calcDistance(locationShape.getCenter(), queryShape.getCenter()), DistanceUtils.EARTH_MEAN_RADIUS_KM) * 1000;
}
}
} catch (IOException | ParseException | UnsupportedQueryException e) {
LOGGER.debug("Unable to parse query wkt", e);
}
if (distance != null && (distance < 0 || distance > Double.MAX_VALUE)) {
distance = null;
}
return distance;
}
use of org.locationtech.spatial4j.shape.Shape in project janusgraph by JanusGraph.
the class GeoshapeHelperTest method testGetPoint.
@Test
public void testGetPoint() {
Geoshape shape = new Geoshape(getShapeFactory().pointXY(1.0, 2.0));
Point point = getHelper().getPoint(shape, 0);
Assert.assertEquals(1.0, point.getLongitude(), 0.0);
Assert.assertEquals(2.0, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().circle(1.0, 2.0, 200));
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(1.0, point.getLongitude(), 0.0);
Assert.assertEquals(2.0, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().lineString().pointXY(1.0, 2.0).pointXY(3.0, 4.0).build());
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(1.0, point.getLongitude(), 0.0);
Assert.assertEquals(2.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 1);
Assert.assertEquals(3.0, point.getLongitude(), 0.0);
Assert.assertEquals(4.0, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().rect(-1.0, 2.0, -3.0, 4.0));
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(-1.0, point.getLongitude(), 0.0);
Assert.assertEquals(-3.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 1);
Assert.assertEquals(2.0, point.getLongitude(), 0.0);
Assert.assertEquals(4.0, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().multiPoint().pointXY(60.0, 90.0).pointXY(120.0, 60.0).build());
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(60, point.getLongitude(), 0.0);
Assert.assertEquals(90, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 1);
Assert.assertEquals(120, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().multiLineString().add(getShapeFactory().lineString().pointXY(59.0, 60.0).pointXY(61.0, 60.0)).add(getShapeFactory().lineString().pointXY(119.0, 60.0).pointXY(121.0, 60.0)).build());
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(59, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 1);
Assert.assertEquals(61, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 2);
Assert.assertEquals(119, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 3);
Assert.assertEquals(121, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().multiShape(Shape.class).add(getShapeFactory().pointXY(59.0, 60.0)).add(getShapeFactory().lineString().pointXY(119.0, 60.0).pointXY(121.0, 60.0).build()).build());
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(59, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 1);
Assert.assertEquals(119, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 2);
Assert.assertEquals(121, point.getLongitude(), 0.0);
Assert.assertEquals(60, point.getLatitude(), 0.0);
if (supportJts()) {
shape = new Geoshape(getShapeFactory().polygon().pointXY(1.0, 2.0).pointXY(3.0, 4.0).pointXY(3.0, -4.0).pointXY(1.0, -4.0).pointXY(1.0, 2.0).build());
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(1.0, point.getLongitude(), 0.0);
Assert.assertEquals(2.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 1);
Assert.assertEquals(3.0, point.getLongitude(), 0.0);
Assert.assertEquals(4.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 2);
Assert.assertEquals(3.0, point.getLongitude(), 0.0);
Assert.assertEquals(-4.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 3);
Assert.assertEquals(1.0, point.getLongitude(), 0.0);
Assert.assertEquals(-4.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 4);
Assert.assertEquals(1.0, point.getLongitude(), 0.0);
Assert.assertEquals(2.0, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().multiPolygon().add(Geoshape.getShapeFactory().polygon().pointXY(59.0, 59.0).pointXY(61.0, 59.0).pointXY(61.0, 61.0).pointXY(59.0, 61.0).pointXY(59.0, 59.0)).add(Geoshape.getShapeFactory().polygon().pointXY(119.0, 59.0).pointXY(121.0, 59.0).pointXY(121.0, 61.0).pointXY(119.0, 61.0).pointXY(119.0, 59.0)).build());
point = getHelper().getPoint(shape, 2);
Assert.assertEquals(61.0, point.getLongitude(), 0.0);
Assert.assertEquals(61.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 7);
Assert.assertEquals(121.0, point.getLongitude(), 0.0);
Assert.assertEquals(61.0, point.getLatitude(), 0.0);
shape = new Geoshape(getShapeFactory().multiShape(Shape.class).add(getShapeFactory().pointXY(60.0, 60.0)).add(getShapeFactory().lineString().pointXY(119.0, 60.0).pointXY(121.0, 60.0).build()).add(getShapeFactory().polygon().pointXY(119.0, 59.0).pointXY(121.0, 59.0).pointXY(121.0, 61.0).pointXY(119.0, 61.0).pointXY(119.0, 59.0).build()).build());
point = getHelper().getPoint(shape, 0);
Assert.assertEquals(60.0, point.getLongitude(), 0.0);
Assert.assertEquals(60.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 2);
Assert.assertEquals(121.0, point.getLongitude(), 0.0);
Assert.assertEquals(60.0, point.getLatitude(), 0.0);
point = getHelper().getPoint(shape, 6);
Assert.assertEquals(119.0, point.getLongitude(), 0.0);
Assert.assertEquals(61.0, point.getLatitude(), 0.0);
}
}
use of org.locationtech.spatial4j.shape.Shape in project janusgraph by JanusGraph.
the class LuceneIndex method convertQuery.
private SearchParams convertQuery(Condition<?> condition, final KeyInformation.StoreRetriever information, final LuceneCustomAnalyzer delegatingAnalyzer) {
final SearchParams params = new SearchParams();
if (condition instanceof PredicateCondition) {
final PredicateCondition<String, ?> atom = (PredicateCondition) condition;
Object value = atom.getValue();
final String key = atom.getKey();
final JanusGraphPredicate janusgraphPredicate = atom.getPredicate();
if (value instanceof Number) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on numeric types: " + janusgraphPredicate);
params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, (Number) value));
} else if (value instanceof String) {
if (janusgraphPredicate == Cmp.LESS_THAN) {
params.addQuery(TermRangeQuery.newStringRange(key, null, value.toString(), false, false));
} else if (janusgraphPredicate == Cmp.LESS_THAN_EQUAL) {
params.addQuery(TermRangeQuery.newStringRange(key, null, value.toString(), false, true));
} else if (janusgraphPredicate == Cmp.GREATER_THAN) {
params.addQuery(TermRangeQuery.newStringRange(key, value.toString(), null, false, false));
} else if (janusgraphPredicate == Cmp.GREATER_THAN_EQUAL) {
params.addQuery(TermRangeQuery.newStringRange(key, value.toString(), null, true, false));
} else {
final Mapping map = Mapping.getMapping(information.get(key));
if ((map == Mapping.DEFAULT || map == Mapping.TEXT) && !Text.HAS_CONTAINS.contains(janusgraphPredicate))
throw new IllegalArgumentException("Text mapped string values only support CONTAINS queries and not: " + janusgraphPredicate);
if (map == Mapping.STRING && Text.HAS_CONTAINS.contains(janusgraphPredicate))
throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + janusgraphPredicate);
if (janusgraphPredicate == Text.CONTAINS) {
tokenize(params, map, delegatingAnalyzer, ((String) value).toLowerCase(), key, janusgraphPredicate);
} else if (janusgraphPredicate == Text.CONTAINS_PREFIX) {
tokenize(params, map, delegatingAnalyzer, (String) value, key, janusgraphPredicate);
} else if (janusgraphPredicate == Text.PREFIX) {
params.addQuery(new PrefixQuery(new Term(key, (String) value)));
} else if (janusgraphPredicate == Text.REGEX) {
final RegexpQuery rq = new RegexpQuery(new Term(key, (String) value));
params.addQuery(rq);
} else if (janusgraphPredicate == Text.CONTAINS_REGEX) {
// This is terrible -- there is probably a better way
// putting this to lowercase because Text search is supposed to be case insensitive
final RegexpQuery rq = new RegexpQuery(new Term(key, ".*" + (((String) value).toLowerCase()) + ".*"));
params.addQuery(rq);
} else if (janusgraphPredicate == Cmp.EQUAL) {
tokenize(params, map, delegatingAnalyzer, (String) value, key, janusgraphPredicate);
} else if (janusgraphPredicate == Cmp.NOT_EQUAL) {
final BooleanQuery.Builder q = new BooleanQuery.Builder();
q.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
q.add(new TermQuery(new Term(key, (String) value)), BooleanClause.Occur.MUST_NOT);
params.addQuery(q.build());
} else if (janusgraphPredicate == Text.FUZZY) {
params.addQuery(new FuzzyQuery(new Term(key, (String) value)));
} else if (janusgraphPredicate == Text.CONTAINS_FUZZY) {
value = ((String) value).toLowerCase();
final Builder b = new BooleanQuery.Builder();
for (final String term : Text.tokenize((String) value)) {
b.add(new FuzzyQuery(new Term(key, term)), BooleanClause.Occur.MUST);
}
params.addQuery(b.build());
} else
throw new IllegalArgumentException("Relation is not supported for string value: " + janusgraphPredicate);
}
} else if (value instanceof Geoshape) {
Preconditions.checkArgument(janusgraphPredicate instanceof Geo, "Relation not supported on geo types: " + janusgraphPredicate);
final Shape shape = ((Geoshape) value).getShape();
final SpatialOperation spatialOp = SPATIAL_PREDICATES.get(janusgraphPredicate);
final SpatialArgs args = new SpatialArgs(spatialOp, shape);
params.addQuery(getSpatialStrategy(key, information.get(key)).makeQuery(args));
} else if (value instanceof Date) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on date types: " + janusgraphPredicate);
params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, ((Date) value).getTime()));
} else if (value instanceof Instant) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on instant types: " + janusgraphPredicate);
params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, ((Instant) value).toEpochMilli()));
} else if (value instanceof Boolean) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on boolean types: " + janusgraphPredicate);
final int intValue;
switch((Cmp) janusgraphPredicate) {
case EQUAL:
intValue = ((Boolean) value) ? 1 : 0;
params.addQuery(IntPoint.newRangeQuery(key, intValue, intValue));
break;
case NOT_EQUAL:
intValue = ((Boolean) value) ? 0 : 1;
params.addQuery(IntPoint.newRangeQuery(key, intValue, intValue));
break;
default:
throw new IllegalArgumentException("Boolean types only support EQUAL or NOT_EQUAL");
}
} else if (value instanceof UUID) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on UUID types: " + janusgraphPredicate);
if (janusgraphPredicate == Cmp.EQUAL) {
params.addQuery(new TermQuery(new Term(key, value.toString())));
} else if (janusgraphPredicate == Cmp.NOT_EQUAL) {
final BooleanQuery.Builder q = new BooleanQuery.Builder();
q.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
q.add(new TermQuery(new Term(key, value.toString())), BooleanClause.Occur.MUST_NOT);
params.addQuery(q.build());
} else {
throw new IllegalArgumentException("Relation is not supported for UUID type: " + janusgraphPredicate);
}
} else {
throw new IllegalArgumentException("Unsupported type: " + value);
}
} else if (condition instanceof Not) {
final SearchParams childParams = convertQuery(((Not) condition).getChild(), information, delegatingAnalyzer);
params.addQuery(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
params.addParams(childParams, BooleanClause.Occur.MUST_NOT);
} else if (condition instanceof And) {
for (final Condition c : condition.getChildren()) {
final SearchParams childParams = convertQuery(c, information, delegatingAnalyzer);
params.addParams(childParams, BooleanClause.Occur.MUST);
}
} else if (condition instanceof Or) {
for (final Condition c : condition.getChildren()) {
final SearchParams childParams = convertQuery(c, information, delegatingAnalyzer);
params.addParams(childParams, BooleanClause.Occur.SHOULD);
}
} else
throw new IllegalArgumentException("Invalid condition: " + condition);
return params;
}
use of org.locationtech.spatial4j.shape.Shape in project janusgraph by JanusGraph.
the class LuceneExample method indexDocs.
void indexDocs(IndexWriter writer, String documentId, Map<String, Object> docMap) throws IOException {
Document doc = new Document();
Field documentIdField = new StringField("docid", documentId, Field.Store.YES);
doc.add(documentIdField);
for (Map.Entry<String, Object> kv : docMap.entrySet()) {
String key = kv.getKey();
Object value = kv.getValue();
if (value instanceof Number) {
final Field field;
if (value instanceof Integer || value instanceof Long) {
field = new LongPoint(key, ((Number) value).longValue());
} else {
// double or float
field = new DoublePoint(key, ((Number) value).doubleValue());
}
doc.add(field);
} else if (value instanceof String) {
String str = (String) value;
Field field = new TextField(key + TXT_SUFFIX, str, Field.Store.NO);
doc.add(field);
if (str.length() < 256)
field = new StringField(key + STR_SUFFIX, str, Field.Store.NO);
doc.add(field);
} else if (value instanceof Geoshape) {
Shape shape = ((Geoshape) value).getShape();
for (IndexableField f : getSpatialStrategy(key).createIndexableFields(shape)) {
doc.add(f);
}
} else
throw new IllegalArgumentException("Unsupported type: " + value);
}
writer.updateDocument(new Term("docid", documentId), doc);
}
use of org.locationtech.spatial4j.shape.Shape in project elasticsearch by elastic.
the class ShapeBuilderTests method testShapeWithTangentialHole.
public void testShapeWithTangentialHole() {
// test a shape with one tangential (shared) vertex (should pass)
PolygonBuilder builder = ShapeBuilders.newPolygon(new CoordinatesBuilder().coordinate(179, 10).coordinate(168, 15).coordinate(164, 0).coordinate(166, -15).coordinate(179, -10).coordinate(179, 10));
builder.hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-177, 10).coordinate(-178, -10).coordinate(-180, -5).coordinate(-180, 5).coordinate(-177, 10)));
Shape shape = builder.close().build();
assertMultiPolygon(shape);
}
Aggregations