http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/7727b165/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/geo/MongoGeoTupleSet.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/geo/MongoGeoTupleSet.java b/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/geo/MongoGeoTupleSet.java deleted file mode 100644 index 3ab9037..0000000 --- a/extras/indexing/src/main/java/mvm/rya/indexing/mongodb/geo/MongoGeoTupleSet.java +++ /dev/null @@ -1,361 +0,0 @@ -package mvm.rya.indexing.mongodb.geo; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - - -import info.aduna.iteration.CloseableIteration; - -import java.util.Map; -import java.util.Set; - -import mvm.rya.indexing.GeoIndexer; -import mvm.rya.indexing.IndexingExpr; -import mvm.rya.indexing.IteratorFactory; -import mvm.rya.indexing.SearchFunction; -import mvm.rya.indexing.StatementConstraints; -import mvm.rya.indexing.accumulo.geo.GeoConstants; -import mvm.rya.indexing.accumulo.geo.GeoTupleSet; -import mvm.rya.indexing.external.tupleSet.ExternalTupleSet; - -import org.apache.hadoop.conf.Configuration; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.query.BindingSet; -import org.openrdf.query.QueryEvaluationException; - -import com.google.common.base.Joiner; -import com.google.common.collect.Maps; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.io.ParseException; -import com.vividsolutions.jts.io.WKTReader; - -public class MongoGeoTupleSet extends ExternalTupleSet { - - private Configuration conf; - private GeoIndexer geoIndexer; - private IndexingExpr filterInfo; - - - public MongoGeoTupleSet(IndexingExpr filterInfo, GeoIndexer geoIndexer) { - this.filterInfo = filterInfo; - this.geoIndexer = geoIndexer; - this.conf = geoIndexer.getConf(); - } - - @Override - public Set<String> getBindingNames() { - return filterInfo.getBindingNames(); - } - - public GeoTupleSet clone() { - return new GeoTupleSet(filterInfo, geoIndexer); - } - - @Override - public double cardinality() { - return 0.0; // No idea how the estimate cardinality here. - } - - - @Override - public String getSignature() { - return "(GeoTuple Projection) " + "variables: " + Joiner.on(", ").join(this.getBindingNames()).replaceAll("\\s+", " "); - } - - - - @Override - public boolean equals(Object other) { - if (other == this) { - return true; - } - if (!(other instanceof MongoGeoTupleSet)) { - return false; - } - MongoGeoTupleSet arg = (MongoGeoTupleSet) other; - return this.filterInfo.equals(arg.filterInfo); - } - - @Override - public int hashCode() { - int result = 17; - result = 31*result + filterInfo.hashCode(); - - return result; - } - - - - /** - * Returns an iterator over the result set of the contained IndexingExpr. - * <p> - * Should be thread-safe (concurrent invocation {@link OfflineIterable} this - * method can be expected with some query evaluators. - */ - @Override - public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(BindingSet bindings) - throws QueryEvaluationException { - - - URI funcURI = filterInfo.getFunction(); - SearchFunction searchFunction = (new MongoGeoSearchFunctionFactory(conf)).getSearchFunction(funcURI); - if(filterInfo.getArguments().length > 1) { - throw new IllegalArgumentException("Index functions do not support more than two arguments."); - } - - String queryText = filterInfo.getArguments()[0].stringValue(); - - return IteratorFactory.getIterator(filterInfo.getSpConstraint(), bindings, queryText, searchFunction); - } - - - - //returns appropriate search function for a given URI - //search functions used in GeoMesaGeoIndexer to access index - public class MongoGeoSearchFunctionFactory { - - Configuration conf; - - private final Map<URI, SearchFunction> SEARCH_FUNCTION_MAP = Maps.newHashMap(); - - public MongoGeoSearchFunctionFactory(Configuration conf) { - this.conf = conf; - } - - - /** - * Get a {@link GeoSearchFunction} for a given URI. - * - * @param searchFunction - * @return - */ - public SearchFunction getSearchFunction(final URI searchFunction) { - - SearchFunction geoFunc = null; - - try { - geoFunc = getSearchFunctionInternal(searchFunction); - } catch (QueryEvaluationException e) { - e.printStackTrace(); - } - - return geoFunc; - } - - private SearchFunction getSearchFunctionInternal(final URI searchFunction) throws QueryEvaluationException { - SearchFunction sf = SEARCH_FUNCTION_MAP.get(searchFunction); - - if (sf != null) { - return sf; - } else { - throw new QueryEvaluationException("Unknown Search Function: " + searchFunction.stringValue()); - } - } - - private final SearchFunction GEO_EQUALS = new SearchFunction() { - - @Override - public CloseableIteration<Statement, QueryEvaluationException> performSearch(String queryText, - StatementConstraints contraints) throws QueryEvaluationException { - try { - WKTReader reader = new WKTReader(); - Geometry geometry = reader.read(queryText); - CloseableIteration<Statement, QueryEvaluationException> statements = geoIndexer.queryWithin( - geometry, contraints); - return statements; - } catch (ParseException e) { - throw new QueryEvaluationException(e); - } - } - - @Override - public String toString() { - return "GEO_EQUALS"; - }; - }; - - private final SearchFunction GEO_DISJOINT = new SearchFunction() { - - @Override - public CloseableIteration<Statement, QueryEvaluationException> performSearch(String queryText, - StatementConstraints contraints) throws QueryEvaluationException { - try { - WKTReader reader = new WKTReader(); - Geometry geometry = reader.read(queryText); - CloseableIteration<Statement, QueryEvaluationException> statements = geoIndexer.queryWithin( - geometry, contraints); - return statements; - } catch (ParseException e) { - throw new QueryEvaluationException(e); - } - } - - @Override - public String toString() { - return "GEO_DISJOINT"; - }; - }; - - private final SearchFunction GEO_INTERSECTS = new SearchFunction() { - - @Override - public CloseableIteration<Statement, QueryEvaluationException> performSearch(String queryText, - StatementConstraints contraints) throws QueryEvaluationException { - try { - WKTReader reader = new WKTReader(); - Geometry geometry = reader.read(queryText); - CloseableIteration<Statement, QueryEvaluationException> statements = geoIndexer.queryWithin( - geometry, contraints); - return statements; - } catch (ParseException e) { - throw new QueryEvaluationException(e); - } - } - - @Override - public String toString() { - return "GEO_INTERSECTS"; - }; - }; - - private final SearchFunction GEO_TOUCHES = new SearchFunction() { - - @Override - public CloseableIteration<Statement, QueryEvaluationException> performSearch(String queryText, - StatementConstraints contraints) throws QueryEvaluationException { - try { - WKTReader reader = new WKTReader(); - Geometry geometry = reader.read(queryText); - CloseableIteration<Statement, QueryEvaluationException> statements = geoIndexer.queryWithin( - geometry, contraints); - return statements; - } catch (ParseException e) { - throw new QueryEvaluationException(e); - } - } - - @Override - public String toString() { - return "GEO_TOUCHES"; - }; - }; - - private final SearchFunction GEO_CONTAINS = new SearchFunction() { - - @Override - public CloseableIteration<Statement, QueryEvaluationException> performSearch(String queryText, - StatementConstraints contraints) throws QueryEvaluationException { - try { - WKTReader reader = new WKTReader(); - Geometry geometry = reader.read(queryText); - CloseableIteration<Statement, QueryEvaluationException> statements = geoIndexer.queryWithin( - geometry, contraints); - return statements; - } catch (ParseException e) { - throw new QueryEvaluationException(e); - } - } - - @Override - public String toString() { - return "GEO_CONTAINS"; - }; - }; - - private final SearchFunction GEO_OVERLAPS = new SearchFunction() { - - @Override - public CloseableIteration<Statement, QueryEvaluationException> performSearch(String queryText, - StatementConstraints contraints) throws QueryEvaluationException { - try { - WKTReader reader = new WKTReader(); - Geometry geometry = reader.read(queryText); - CloseableIteration<Statement, QueryEvaluationException> statements = geoIndexer.queryWithin( - geometry, contraints); - return statements; - } catch (ParseException e) { - throw new QueryEvaluationException(e); - } - } - - @Override - public String toString() { - return "GEO_OVERLAPS"; - }; - }; - - private final SearchFunction GEO_CROSSES = new SearchFunction() { - - @Override - public CloseableIteration<Statement, QueryEvaluationException> performSearch(String queryText, - StatementConstraints contraints) throws QueryEvaluationException { - try { - WKTReader reader = new WKTReader(); - Geometry geometry = reader.read(queryText); - CloseableIteration<Statement, QueryEvaluationException> statements = geoIndexer.queryWithin( - geometry, contraints); - return statements; - } catch (ParseException e) { - throw new QueryEvaluationException(e); - } - } - - @Override - public String toString() { - return "GEO_CROSSES"; - }; - }; - - private final SearchFunction GEO_WITHIN = new SearchFunction() { - - @Override - public CloseableIteration<Statement, QueryEvaluationException> performSearch(String queryText, - StatementConstraints contraints) throws QueryEvaluationException { - try { - WKTReader reader = new WKTReader(); - Geometry geometry = reader.read(queryText); - CloseableIteration<Statement, QueryEvaluationException> statements = geoIndexer.queryWithin( - geometry, contraints); - return statements; - } catch (ParseException e) { - throw new QueryEvaluationException(e); - } - } - - @Override - public String toString() { - return "GEO_WITHIN"; - }; - }; - - { - SEARCH_FUNCTION_MAP.put(GeoConstants.GEO_SF_EQUALS, GEO_EQUALS); - SEARCH_FUNCTION_MAP.put(GeoConstants.GEO_SF_DISJOINT, GEO_DISJOINT); - SEARCH_FUNCTION_MAP.put(GeoConstants.GEO_SF_INTERSECTS, GEO_INTERSECTS); - SEARCH_FUNCTION_MAP.put(GeoConstants.GEO_SF_TOUCHES, GEO_TOUCHES); - SEARCH_FUNCTION_MAP.put(GeoConstants.GEO_SF_CONTAINS, GEO_CONTAINS); - SEARCH_FUNCTION_MAP.put(GeoConstants.GEO_SF_OVERLAPS, GEO_OVERLAPS); - SEARCH_FUNCTION_MAP.put(GeoConstants.GEO_SF_CROSSES, GEO_CROSSES); - SEARCH_FUNCTION_MAP.put(GeoConstants.GEO_SF_WITHIN, GEO_WITHIN); - } - - } - -}
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/7727b165/extras/indexing/src/main/java/mvm/rya/sail/config/RyaSailFactory.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/mvm/rya/sail/config/RyaSailFactory.java b/extras/indexing/src/main/java/mvm/rya/sail/config/RyaSailFactory.java index b0a719b..d3c891c 100644 --- a/extras/indexing/src/main/java/mvm/rya/sail/config/RyaSailFactory.java +++ b/extras/indexing/src/main/java/mvm/rya/sail/config/RyaSailFactory.java @@ -146,7 +146,7 @@ public class RyaSailFactory { return dao; } - private static void updateAccumuloConfig(final AccumuloRdfConfiguration config, final String user, final String pswd, final String ryaInstance) throws AccumuloException, AccumuloSecurityException { + public static void updateAccumuloConfig(final AccumuloRdfConfiguration config, final String user, final String pswd, final String ryaInstance) throws AccumuloException, AccumuloSecurityException { try { final PasswordToken pswdToken = new PasswordToken(pswd); final Instance accInst = ConfigUtils.getInstance(config); http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/7727b165/extras/indexing/src/test/java/mvm/rya/indexing/accumulo/geo/GeoIndexerSfTest.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/accumulo/geo/GeoIndexerSfTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/accumulo/geo/GeoIndexerSfTest.java deleted file mode 100644 index 5395267..0000000 --- a/extras/indexing/src/test/java/mvm/rya/indexing/accumulo/geo/GeoIndexerSfTest.java +++ /dev/null @@ -1,514 +0,0 @@ -package mvm.rya.indexing.accumulo.geo; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import static org.junit.Assert.*; - -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import org.apache.accumulo.core.client.admin.TableOperations; -import org.geotools.geometry.jts.Geometries; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -import org.openrdf.model.Resource; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.model.Value; -import org.openrdf.model.ValueFactory; -import org.openrdf.model.impl.StatementImpl; -import org.openrdf.model.impl.URIImpl; -import org.openrdf.model.impl.ValueFactoryImpl; - -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.GeometryFactory; -import com.vividsolutions.jts.geom.LineString; -import com.vividsolutions.jts.geom.LinearRing; -import com.vividsolutions.jts.geom.Point; -import com.vividsolutions.jts.geom.Polygon; -import com.vividsolutions.jts.geom.PrecisionModel; -import com.vividsolutions.jts.geom.impl.PackedCoordinateSequence; -import com.vividsolutions.jts.io.ParseException; -import com.vividsolutions.jts.io.gml2.GMLWriter; - -import info.aduna.iteration.CloseableIteration; -import mvm.rya.accumulo.AccumuloRdfConfiguration; -import mvm.rya.api.domain.RyaStatement; -import mvm.rya.api.resolver.RdfToRyaConversions; -import mvm.rya.api.resolver.RyaToRdfConversions; -import mvm.rya.indexing.StatementConstraints; -import mvm.rya.indexing.accumulo.ConfigUtils; - -/** - * Tests all of the "simple functions" of the geoindexer specific to GML. - * Parameterized so that each test is run for WKT and for GML. - */ -@RunWith(value = Parameterized.class) -public class GeoIndexerSfTest { - private static AccumuloRdfConfiguration conf; - private static GeometryFactory gf = new GeometryFactory(new PrecisionModel(), 4326); - private static GeoMesaGeoIndexer g; - - private static final StatementConstraints EMPTY_CONSTRAINTS = new StatementConstraints(); - - // Here is the landscape: - /** - * <pre> - * 2---+---+---+---+---+---+ - * | F |G | - * 1 A o(-1,1) o C | - * | | | - * 0---+---+ +---+---+(3,0) - * | | E | - * -1 B + .---+---+ - * | | /| | | - * -2---+---+-/-+---+ + - * ^ / | D | - * -3 -2 -1 0---1---2 3 4 - * </pre> - **/ - private static final Polygon A = poly(bbox(-3, -2, 1, 2)); - private static final Polygon B = poly(bbox(-3, -2, -1, 0)); - private static final Polygon C = poly(bbox(1, 0, 3, 2)); - private static final Polygon D = poly(bbox(0, -3, 2, -1)); - - private static final Point F = point(-1, 1); - private static final Point G = point(1, 1); - - private static final LineString E = line(-1, -3, 0, -1); - - private static final Map<Geometry, String> names = Maps.newHashMap(); - static { - names.put(A, "A"); - names.put(B, "B"); - names.put(C, "C"); - names.put(D, "D"); - names.put(E, "E"); - names.put(F, "F"); - names.put(G, "G"); - } - - /** - * JUnit 4 parameterized iterates thru this list and calls the constructor with each. - * For each test, Call the constructor three times, for WKT and for GML encoding 1, and GML encoding 2 - * @return - */ - final static URI useJtsLibEncoding = new URIImpl("uri:useLib") ; - final static URI useRoughEncoding = new URIImpl("uri:useRough") ; - - @Parameters - public static Collection<URI[]> constructorData() { - URI[][] data = new URI[][] { { GeoConstants.XMLSCHEMA_OGC_WKT,useJtsLibEncoding }, { GeoConstants.XMLSCHEMA_OGC_GML,useJtsLibEncoding } , { GeoConstants.XMLSCHEMA_OGC_GML,useRoughEncoding } }; - return Arrays.asList(data); - } - - private URI schemaToTest; - private URI encodeMethod; - /** - * Constructor required by JUnit parameterized runner. See data() for constructor values. - */ - public GeoIndexerSfTest(URI schemaToTest, URI encodeMethod) { - this.schemaToTest=schemaToTest; - this.encodeMethod = encodeMethod; - } - /** - * Run before each test method. - * @throws Exception - */ - @Before - public void before() throws Exception { - conf = new AccumuloRdfConfiguration(); - conf.setTablePrefix("triplestore_"); - String tableName = GeoMesaGeoIndexer.getTableName(conf); - conf.setBoolean(ConfigUtils.USE_MOCK_INSTANCE, true); - conf.set(ConfigUtils.CLOUDBASE_USER, "USERNAME"); - conf.set(ConfigUtils.CLOUDBASE_PASSWORD, "PASS"); - conf.set(ConfigUtils.CLOUDBASE_AUTHS, "U"); - - TableOperations tops = ConfigUtils.getConnector(conf).tableOperations(); - // get all of the table names with the prefix - Set<String> toDel = Sets.newHashSet(); - for (String t : tops.list()) { - if (t.startsWith(tableName)) { - toDel.add(t); - } - } - for (String t : toDel) { - tops.delete(t); - } - - g = new GeoMesaGeoIndexer(); - g.setConf(conf); - // Convert the statements as schema WKT or GML, then GML has two methods to encode. - g.storeStatement(RyaStatement(A,schemaToTest, encodeMethod)); - g.storeStatement(RyaStatement(B,schemaToTest, encodeMethod)); - g.storeStatement(RyaStatement(C,schemaToTest, encodeMethod)); - g.storeStatement(RyaStatement(D,schemaToTest, encodeMethod)); - g.storeStatement(RyaStatement(F,schemaToTest, encodeMethod)); - g.storeStatement(RyaStatement(E,schemaToTest, encodeMethod)); - g.storeStatement(RyaStatement(G,schemaToTest, encodeMethod)); - } - - private static RyaStatement RyaStatement(Geometry geo, URI schema, URI encodingMethod) { - return RdfToRyaConversions.convertStatement(genericStatement(geo,schema,encodingMethod)); - } - private static Statement genericStatement(Geometry geo, URI schema, URI encodingMethod) { - if (schema.equals(GeoConstants.XMLSCHEMA_OGC_WKT)) { - return genericStatementWkt(geo); - } else if (schema.equals(GeoConstants.XMLSCHEMA_OGC_GML)) { - return genericStatementGml(geo, encodingMethod); - } - throw new Error("schema unsupported: "+schema); - } - private static Statement genericStatementWkt(Geometry geo) { - ValueFactory vf = new ValueFactoryImpl(); - Resource subject = vf.createURI("uri:" + names.get(geo)); - URI predicate = GeoConstants.GEO_AS_WKT; - Value object = vf.createLiteral(geo.toString(), GeoConstants.XMLSCHEMA_OGC_WKT); - return new StatementImpl(subject, predicate, object); - } - - private static Statement genericStatementGml(Geometry geo, URI encodingMethod) { - ValueFactory vf = new ValueFactoryImpl(); - Resource subject = vf.createURI("uri:" + names.get(geo)); - URI predicate = GeoConstants.GEO_AS_GML; - - final String gml ; - if (encodingMethod==useJtsLibEncoding) - gml = geoToGmlUseJtsLib(geo); - else if (encodingMethod==useRoughEncoding) - gml = geoToGmlRough(geo); - else - throw new Error("invalid encoding method: "+encodingMethod); - // System.out.println("===created GML===="); - // System.out.println(gml); - // System.out.println("========== GML===="); - - Value object = vf.createLiteral(gml, GeoConstants.XMLSCHEMA_OGC_GML); - return new StatementImpl(subject, predicate, object); - } - - /** - * JTS library conversion from geometry to GML. - * @param geo base Geometry gets delegated - * @return String gml encoding of the geomoetry - */ - private static String geoToGmlUseJtsLib(Geometry geo) { - int srid = geo.getSRID(); - GMLWriter gmlWriter = new GMLWriter(); - gmlWriter.setNamespace(false); - gmlWriter.setPrefix(null); - - if (srid != -1 || srid != 0) { - gmlWriter.setSrsName("EPSG:" + geo.getSRID()); - } - String gml = gmlWriter.write(geo); - // Hack to replace a gml 2.0 deprecated element in the Polygon. - // It should tolerate this as it does other depreciated elements like <gml:coordinates>. - return gml.replace("outerBoundaryIs", "exterior"); - } - - /** - * Rough conversion from geometry to GML using a template. - * @param geo base Geometry gets delegated - * @return String gml encoding of the gemoetry - */ - private static String geoToGmlRough(Geometry geo) { - final Geometries theType = org.geotools.geometry.jts.Geometries.get(geo); - switch (theType) { - case POINT: - return geoToGml((Point)geo); - case LINESTRING: - return geoToGml((LineString)geo); - case POLYGON: - return geoToGml((Polygon)geo); - case MULTIPOINT: - case MULTILINESTRING: - case MULTIPOLYGON: - default: - throw new Error("No code to convert to GML for this type: "+theType); - } - } - - private static Point point(double x, double y) { - return gf.createPoint(new Coordinate(x, y)); - } - - private static String geoToGml(Point point) { - //CRS:84 long X,lat Y - //ESPG:4326 lat Y,long X - return "<Point"// - + " srsName='CRS:84'"// TODO: point.getSRID() - + "><pos>"+point.getX()+" "+point.getY()+"</pos> "// assumes Y=lat X=long - + " </Point>"; - } - - private static LineString line(double x1, double y1, double x2, double y2) { - return new LineString(new PackedCoordinateSequence.Double(new double[] { x1, y1, x2, y2 }, 2), gf); - } - /** - * convert a lineString geometry to GML - * @param line - * @return String that is XML that is a GMLLiteral of line - */ - private static String geoToGml(LineString line) { - StringBuilder coordString = new StringBuilder() ; - for (Coordinate coor : line.getCoordinates()) { - coordString.append(" ").append(coor.x).append(" ").append(coor.y); //ESPG:4326 lat/long - } - return " <gml:LineString srsName=\"http://www.opengis.net/def/crs/EPSG/0/4326\" xmlns:gml='http://www.opengis.net/gml'>\n" - + "<gml:posList srsDimension=\"2\">"// - + coordString // - + "</gml:posList></gml:LineString >"; - } - - private static Polygon poly(double[] arr) { - LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(arr, 2)); - Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - return p1; - } - /** - * convert a Polygon geometry to GML - * @param geometry - * @return String that is XML that is a GMLLiteral of line - */ - private static String geoToGml(Polygon poly) { - StringBuilder coordString = new StringBuilder() ; - for (Coordinate coor : poly.getCoordinates()) { - coordString.append(" ").append(coor.x).append(" ").append(coor.y); //ESPG:4326 lat/long - //with commas: coordString.append(" ").append(coor.x).append(",").append(coor.y); - } - return "<gml:Polygon srsName=\"EPSG:4326\" xmlns:gml='http://www.opengis.net/gml'>\r\n"// - + "<gml:exterior><gml:LinearRing>\r\n"// - + "<gml:posList srsDimension='2'>\r\n" - + coordString - + "</gml:posList>\r\n"// - + "</gml:LinearRing></gml:exterior>\r\n</gml:Polygon>\r\n"; - } - - private static double[] bbox(double x1, double y1, double x2, double y2) { - return new double[] { x1, y1, x1, y2, x2, y2, x2, y1, x1, y1 }; - } - - public void compare(CloseableIteration<Statement, ?> actual, Geometry... expected) throws Exception { - Set<Statement> expectedSet = Sets.newHashSet(); - for (Geometry geo : expected) { - expectedSet.add(RyaToRdfConversions.convertStatement(RyaStatement(geo,this.schemaToTest, encodeMethod))); - } - - Assert.assertEquals(expectedSet, getSet(actual)); - } - - private static <X> Set<X> getSet(CloseableIteration<X, ?> iter) throws Exception { - Set<X> set = new HashSet<X>(); - while (iter.hasNext()) { - set.add(iter.next()); - } - return set; - } - - private static Geometry[] EMPTY_RESULTS = {}; - - @Test - public void testParsePoly() throws Exception { - assertParseable(D); - } - - @Test - public void testParseLine() throws Exception { - assertParseable(E); - } - - @Test - public void testParsePoint() throws Exception { - assertParseable(F); - } - - /** - * Convert Geometry to Wkt|GML (schemaToTest), parse to Geometry, and compare to original. - * @throws ParseException - */ - public void assertParseable(Geometry originalGeom) throws ParseException { - Geometry parsedGeom = GeoParseUtils.getGeometry(genericStatement(originalGeom,schemaToTest, encodeMethod)); - assertTrue("Parsed should equal original: "+originalGeom+" parsed: "+parsedGeom, originalGeom.equalsNorm(parsedGeom)); - // assertEquals( originalGeom, parsedGeom ); //also passes - // assertTrue( originalGeom.equalsExact(parsedGeom) ); //also passes - } - - @Test - public void testEquals() throws Exception { - // point - compare(g.queryEquals(F, EMPTY_CONSTRAINTS), F); - compare(g.queryEquals(point(-1, -1), EMPTY_CONSTRAINTS), EMPTY_RESULTS); - - // line - compare(g.queryEquals(E, EMPTY_CONSTRAINTS), E); - compare(g.queryEquals(line(-1, -1, 0, 0), EMPTY_CONSTRAINTS), EMPTY_RESULTS); - - // poly - compare(g.queryEquals(A, EMPTY_CONSTRAINTS), A); - compare(g.queryEquals(poly(bbox(-2, -2, 1, 2)), EMPTY_CONSTRAINTS), EMPTY_RESULTS); - - } - - @Test - public void testDisjoint() throws Exception { - // point - compare(g.queryDisjoint(F, EMPTY_CONSTRAINTS), B, C, D, E, G); - - // line - compare(g.queryDisjoint(E, EMPTY_CONSTRAINTS), B, C, F, G); - - // poly - compare(g.queryDisjoint(A, EMPTY_CONSTRAINTS), EMPTY_RESULTS); - compare(g.queryDisjoint(B, EMPTY_CONSTRAINTS), C, D, F, E, G); - } - - @Test - @Ignore - public void testIntersectsPoint() throws Exception { - // This seems like a bug - // scala.MatchError: POINT (2 4) (of class com.vividsolutions.jts.geom.Point) - // at org.locationtech.geomesa.filter.FilterHelper$.updateToIDLSafeFilter(FilterHelper.scala:53) - // compare(g.queryIntersects(F, EMPTY_CONSTRAINTS), A, F); - // compare(g.queryIntersects(F, EMPTY_CONSTRAINTS), EMPTY_RESULTS); - } - - @Ignore - @Test - public void testIntersectsLine() throws Exception { - // This seems like a bug - // fails with: - // scala.MatchError: LINESTRING (2 0, 3 3) (of class com.vividsolutions.jts.geom.LineString) - // at org.locationtech.geomesa.filter.FilterHelper$.updateToIDLSafeFilter(FilterHelper.scala:53) - //compare(g.queryIntersects(E, EMPTY_CONSTRAINTS), A, E, D); - //compare(g.queryIntersects(E, EMPTY_CONSTRAINTS), EMPTY_RESULTS); - } - - @Test - public void testIntersectsPoly() throws Exception { - compare(g.queryIntersects(A, EMPTY_CONSTRAINTS), A, B, C, D, F, E, G); - } - - @Test - public void testTouchesPoint() throws Exception { - compare(g.queryTouches(F, EMPTY_CONSTRAINTS), EMPTY_RESULTS); - compare(g.queryTouches(G, EMPTY_CONSTRAINTS), A, C); - } - - @Test - public void testTouchesLine() throws Exception { - compare(g.queryTouches(E, EMPTY_CONSTRAINTS), D); - } - - @Test - public void testTouchesPoly() throws Exception { - compare(g.queryTouches(A, EMPTY_CONSTRAINTS), C,G); - } - - @Test - public void testCrossesPoint() throws Exception { - compare(g.queryCrosses(F, EMPTY_CONSTRAINTS), EMPTY_RESULTS); - compare(g.queryCrosses(G, EMPTY_CONSTRAINTS), EMPTY_RESULTS); - // bug? java.lang.IllegalStateException: getX called on empty Point - // compare(g.queryCrosses(point(2, 0), EMPTY_CONSTRAINTS), E); - } - - @Ignore - @Test - public void testCrossesLine() throws Exception { - // fails with: - // java.lang.IllegalStateException: getX called on empty Point - // at com.vividsolutions.jts.geom.Point.getX(Point.java:124) - // at org.locationtech.geomesa.utils.geohash.GeohashUtils$.considerCandidate$1(GeohashUtils.scala:1023) - - // compare(g.queryCrosses(E, EMPTY_CONSTRAINTS), A); - } - - @Test - public void testCrossesPoly() throws Exception { - compare(g.queryCrosses(A, EMPTY_CONSTRAINTS), E); - compare(g.queryCrosses(poly(bbox(-0.9, -2.9, -0.1, -1.1)), EMPTY_CONSTRAINTS), E); - } - - @Test - public void testWithin() throws Exception { - // point - // geomesa bug? scala.MatchError: POINT (2 4) (of class com.vividsolutions.jts.geom.Point) - // compare(g.queryWithin(F, EMPTY_CONSTRAINTS), F); - - // line - // geomesa bug? scala.MatchError: LINESTRING (2 0, 3 2) (of class com.vividsolutions.jts.geom.LineString) - // compare(g.queryWithin(E, EMPTY_CONSTRAINTS), E); - - // poly - compare(g.queryWithin(A, EMPTY_CONSTRAINTS), A, B, F); - } - - @Test - public void testContainsPoint() throws Exception { - compare(g.queryContains(F, EMPTY_CONSTRAINTS), A, F); - } - - @Ignore - @Test - public void testContainsLine() throws Exception { - // compare(g.queryContains(E, EMPTY_CONSTRAINTS), E); - } - - @Test - public void testContainsPoly() throws Exception { - compare(g.queryContains(A, EMPTY_CONSTRAINTS), A); - compare(g.queryContains(B, EMPTY_CONSTRAINTS), A, B); - } - - @Ignore - @Test - public void testOverlapsPoint() throws Exception { - // compare(g.queryOverlaps(F, EMPTY_CONSTRAINTS), F); - // You cannot have overlapping points - // compare(g.queryOverlaps(F, EMPTY_CONSTRAINTS), EMPTY_RESULTS); - } - - @Ignore - @Test - public void testOverlapsLine() throws Exception { - // compare(g.queryOverlaps(E, EMPTY_CONSTRAINTS), A, E); - // You cannot have overlapping lines - // compare(g.queryOverlaps(E, EMPTY_CONSTRAINTS), EMPTY_RESULTS); - } - - @Test - public void testOverlapsPoly() throws Exception { - compare(g.queryOverlaps(A, EMPTY_CONSTRAINTS), D); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/7727b165/extras/indexing/src/test/java/mvm/rya/indexing/accumulo/geo/GeoIndexerTest.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/accumulo/geo/GeoIndexerTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/accumulo/geo/GeoIndexerTest.java deleted file mode 100644 index 1e95db7..0000000 --- a/extras/indexing/src/test/java/mvm/rya/indexing/accumulo/geo/GeoIndexerTest.java +++ /dev/null @@ -1,400 +0,0 @@ -package mvm.rya.indexing.accumulo.geo; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import static mvm.rya.api.resolver.RdfToRyaConversions.convertStatement; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; -import org.apache.accumulo.core.client.admin.TableOperations; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.openrdf.model.Resource; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.model.Value; -import org.openrdf.model.ValueFactory; -import org.openrdf.model.impl.ContextStatementImpl; -import org.openrdf.model.impl.StatementImpl; -import org.openrdf.model.impl.ValueFactoryImpl; - -import com.google.common.collect.Sets; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.GeometryFactory; -import com.vividsolutions.jts.geom.LinearRing; -import com.vividsolutions.jts.geom.Point; -import com.vividsolutions.jts.geom.Polygon; -import com.vividsolutions.jts.geom.PrecisionModel; -import com.vividsolutions.jts.geom.impl.PackedCoordinateSequence; - -import info.aduna.iteration.CloseableIteration; -import mvm.rya.accumulo.AccumuloRdfConfiguration; -import mvm.rya.api.RdfCloudTripleStoreConfiguration; -import mvm.rya.indexing.StatementConstraints; -import mvm.rya.indexing.accumulo.ConfigUtils; - -/** - * Tests higher level functioning of the geoindexer parse WKT, predicate list, - * prime and anti meridian, delete, search, context, search with Statement Constraints. - */ -public class GeoIndexerTest { - - private static final StatementConstraints EMPTY_CONSTRAINTS = new StatementConstraints(); - - private AccumuloRdfConfiguration conf; - GeometryFactory gf = new GeometryFactory(new PrecisionModel(), 4326); - - @Before - public void before() throws Exception { - conf = new AccumuloRdfConfiguration(); - conf.setTablePrefix("triplestore_"); - String tableName = GeoMesaGeoIndexer.getTableName(conf); - conf.setBoolean(ConfigUtils.USE_MOCK_INSTANCE, true); - conf.set(ConfigUtils.CLOUDBASE_USER, "USERNAME"); - conf.set(ConfigUtils.CLOUDBASE_PASSWORD, "PASS"); - conf.set(ConfigUtils.CLOUDBASE_AUTHS, "U"); - - TableOperations tops = ConfigUtils.getConnector(conf).tableOperations(); - // get all of the table names with the prefix - Set<String> toDel = Sets.newHashSet(); - for (String t : tops.list()){ - if (t.startsWith(tableName)){ - toDel.add(t); - } - } - for (String t : toDel) { - tops.delete(t); - } - } - - @Test - public void testRestrictPredicatesSearch() throws Exception { - conf.setStrings(ConfigUtils.GEO_PREDICATES_LIST, "pred:1,pred:2"); - try (GeoMesaGeoIndexer f = new GeoMesaGeoIndexer()) { - f.setConf(conf); - - ValueFactory vf = new ValueFactoryImpl(); - - Point point = gf.createPoint(new Coordinate(10, 10)); - Value pointValue = vf.createLiteral("Point(10 10)", GeoConstants.XMLSCHEMA_OGC_WKT); - URI invalidPredicate = GeoConstants.GEO_AS_WKT; - - // These should not be stored because they are not in the predicate list - f.storeStatement(convertStatement(new StatementImpl(vf.createURI("foo:subj1"), invalidPredicate, pointValue))); - f.storeStatement(convertStatement(new StatementImpl(vf.createURI("foo:subj2"), invalidPredicate, pointValue))); - - URI pred1 = vf.createURI("pred:1"); - URI pred2 = vf.createURI("pred:2"); - - // These should be stored because they are in the predicate list - Statement s3 = new StatementImpl(vf.createURI("foo:subj3"), pred1, pointValue); - Statement s4 = new StatementImpl(vf.createURI("foo:subj4"), pred2, pointValue); - f.storeStatement(convertStatement(s3)); - f.storeStatement(convertStatement(s4)); - - // This should not be stored because the object is not valid wkt - f.storeStatement(convertStatement(new StatementImpl(vf.createURI("foo:subj5"), pred1, vf.createLiteral("soint(10 10)")))); - - // This should not be stored because the object is not a literal - f.storeStatement(convertStatement(new StatementImpl(vf.createURI("foo:subj6"), pred1, vf.createURI("p:Point(10 10)")))); - - f.flush(); - - Set<Statement> actual = getSet(f.queryEquals(point, EMPTY_CONSTRAINTS)); - Assert.assertEquals(2, actual.size()); - Assert.assertTrue(actual.contains(s3)); - Assert.assertTrue(actual.contains(s4)); - } - } - - private static <X> Set<X> getSet(CloseableIteration<X, ?> iter) throws Exception { - Set<X> set = new HashSet<X>(); - while (iter.hasNext()) { - set.add(iter.next()); - } - return set; - } - - @Test - public void testPrimeMeridianSearch() throws Exception { - try (GeoMesaGeoIndexer f = new GeoMesaGeoIndexer()) { - f.setConf(conf); - - ValueFactory vf = new ValueFactoryImpl(); - Resource subject = vf.createURI("foo:subj"); - URI predicate = GeoConstants.GEO_AS_WKT; - Value object = vf.createLiteral("Point(0 0)", GeoConstants.XMLSCHEMA_OGC_WKT); - Resource context = vf.createURI("foo:context"); - - Statement statement = new ContextStatementImpl(subject, predicate, object, context); - f.storeStatement(convertStatement(statement)); - f.flush(); - - double[] ONE = { 1, 1, -1, 1, -1, -1, 1, -1, 1, 1 }; - double[] TWO = { 2, 2, -2, 2, -2, -2, 2, -2, 2, 2 }; - double[] THREE = { 3, 3, -3, 3, -3, -3, 3, -3, 3, 3 }; - - LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(ONE, 2)); - LinearRing r2 = gf.createLinearRing(new PackedCoordinateSequence.Double(TWO, 2)); - LinearRing r3 = gf.createLinearRing(new PackedCoordinateSequence.Double(THREE, 2)); - - Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - Polygon p2 = gf.createPolygon(r2, new LinearRing[] {}); - Polygon p3 = gf.createPolygon(r3, new LinearRing[] {}); - - Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p1, EMPTY_CONSTRAINTS))); - Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p2, EMPTY_CONSTRAINTS))); - Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p3, EMPTY_CONSTRAINTS))); - - // Test a ring with a hole in it - Polygon p3m2 = gf.createPolygon(r3, new LinearRing[] { r2 }); - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(p3m2, EMPTY_CONSTRAINTS))); - - // test a ring outside the point - double[] OUT = { 3, 3, 1, 3, 1, 1, 3, 1, 3, 3 }; - LinearRing rOut = gf.createLinearRing(new PackedCoordinateSequence.Double(OUT, 2)); - Polygon pOut = gf.createPolygon(rOut, new LinearRing[] {}); - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(pOut, EMPTY_CONSTRAINTS))); - } - } - - @Test - public void testDcSearch() throws Exception { - // test a ring around dc - try (GeoMesaGeoIndexer f = new GeoMesaGeoIndexer()) { - f.setConf(conf); - - ValueFactory vf = new ValueFactoryImpl(); - Resource subject = vf.createURI("foo:subj"); - URI predicate = GeoConstants.GEO_AS_WKT; - Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT); - Resource context = vf.createURI("foo:context"); - - Statement statement = new ContextStatementImpl(subject, predicate, object, context); - f.storeStatement(convertStatement(statement)); - f.flush(); - - double[] IN = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 }; - LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(IN, 2)); - Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p1, EMPTY_CONSTRAINTS))); - - // test a ring outside the point - double[] OUT = { -77, 39, -76, 39, -76, 38, -77, 38, -77, 39 }; - LinearRing rOut = gf.createLinearRing(new PackedCoordinateSequence.Double(OUT, 2)); - Polygon pOut = gf.createPolygon(rOut, new LinearRing[] {}); - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(pOut, EMPTY_CONSTRAINTS))); - } - } - - @Test - public void testDeleteSearch() throws Exception { - // test a ring around dc - try (GeoMesaGeoIndexer f = new GeoMesaGeoIndexer()) { - f.setConf(conf); - - ValueFactory vf = new ValueFactoryImpl(); - Resource subject = vf.createURI("foo:subj"); - URI predicate = GeoConstants.GEO_AS_WKT; - Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT); - Resource context = vf.createURI("foo:context"); - - Statement statement = new ContextStatementImpl(subject, predicate, object, context); - f.storeStatement(convertStatement(statement)); - f.flush(); - - f.deleteStatement(convertStatement(statement)); - - // test a ring that the point would be inside of if not deleted - double[] in = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 }; - LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(in, 2)); - Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(p1, EMPTY_CONSTRAINTS))); - - // test a ring that the point would be outside of if not deleted - double[] out = { -77, 39, -76, 39, -76, 38, -77, 38, -77, 39 }; - LinearRing rOut = gf.createLinearRing(new PackedCoordinateSequence.Double(out, 2)); - Polygon pOut = gf.createPolygon(rOut, new LinearRing[] {}); - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(pOut, EMPTY_CONSTRAINTS))); - - // test a ring for the whole world and make sure the point is gone - // Geomesa is a little sensitive around lon 180, so we only go to 179 - double[] world = { -180, 90, 179, 90, 179, -90, -180, -90, -180, 90 }; - LinearRing rWorld = gf.createLinearRing(new PackedCoordinateSequence.Double(world, 2)); - Polygon pWorld = gf.createPolygon(rWorld, new LinearRing[] {}); - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(pWorld, EMPTY_CONSTRAINTS))); - } - } - - @Test - public void testDcSearchWithContext() throws Exception { - // test a ring around dc - try (GeoMesaGeoIndexer f = new GeoMesaGeoIndexer()) { - f.setConf(conf); - - ValueFactory vf = new ValueFactoryImpl(); - Resource subject = vf.createURI("foo:subj"); - URI predicate = GeoConstants.GEO_AS_WKT; - Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT); - Resource context = vf.createURI("foo:context"); - - Statement statement = new ContextStatementImpl(subject, predicate, object, context); - f.storeStatement(convertStatement(statement)); - f.flush(); - - double[] IN = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 }; - LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(IN, 2)); - Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - - // query with correct context - Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p1, new StatementConstraints().setContext(context)))); - - // query with wrong context - Assert.assertEquals(Sets.newHashSet(), - getSet(f.queryWithin(p1, new StatementConstraints().setContext(vf.createURI("foo:context2"))))); - } - } - - @Test - public void testDcSearchWithSubject() throws Exception { - // test a ring around dc - try (GeoMesaGeoIndexer f = new GeoMesaGeoIndexer()) { - f.setConf(conf); - - ValueFactory vf = new ValueFactoryImpl(); - Resource subject = vf.createURI("foo:subj"); - URI predicate = GeoConstants.GEO_AS_WKT; - Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT); - Resource context = vf.createURI("foo:context"); - - Statement statement = new ContextStatementImpl(subject, predicate, object, context); - f.storeStatement(convertStatement(statement)); - f.flush(); - - double[] IN = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 }; - LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(IN, 2)); - Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - - // query with correct subject - Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p1, new StatementConstraints().setSubject(subject)))); - - // query with wrong subject - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(p1, new StatementConstraints().setSubject(vf.createURI("foo:subj2"))))); - } - } - - @Test - public void testDcSearchWithSubjectAndContext() throws Exception { - // test a ring around dc - try (GeoMesaGeoIndexer f = new GeoMesaGeoIndexer()) { - f.setConf(conf); - - ValueFactory vf = new ValueFactoryImpl(); - Resource subject = vf.createURI("foo:subj"); - URI predicate = GeoConstants.GEO_AS_WKT; - Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT); - Resource context = vf.createURI("foo:context"); - - Statement statement = new ContextStatementImpl(subject, predicate, object, context); - f.storeStatement(convertStatement(statement)); - f.flush(); - - double[] IN = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 }; - LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(IN, 2)); - Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - - // query with correct context subject - Assert.assertEquals(Sets.newHashSet(statement), - getSet(f.queryWithin(p1, new StatementConstraints().setContext(context).setSubject(subject)))); - - // query with wrong context - Assert.assertEquals(Sets.newHashSet(), - getSet(f.queryWithin(p1, new StatementConstraints().setContext(vf.createURI("foo:context2"))))); - - // query with wrong subject - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(p1, new StatementConstraints().setSubject(vf.createURI("foo:subj2"))))); - } - } - - @Test - public void testDcSearchWithPredicate() throws Exception { - // test a ring around dc - try (GeoMesaGeoIndexer f = new GeoMesaGeoIndexer()) { - f.setConf(conf); - - ValueFactory vf = new ValueFactoryImpl(); - Resource subject = vf.createURI("foo:subj"); - URI predicate = GeoConstants.GEO_AS_WKT; - Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT); - Resource context = vf.createURI("foo:context"); - - Statement statement = new ContextStatementImpl(subject, predicate, object, context); - f.storeStatement(convertStatement(statement)); - f.flush(); - - double[] IN = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 }; - LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(IN, 2)); - Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - - // query with correct Predicate - Assert.assertEquals(Sets.newHashSet(statement), - getSet(f.queryWithin(p1, new StatementConstraints().setPredicates(Collections.singleton(predicate))))); - - // query with wrong predicate - Assert.assertEquals(Sets.newHashSet(), - getSet(f.queryWithin(p1, new StatementConstraints().setPredicates(Collections.singleton(vf.createURI("other:pred")))))); - } - } - - // @Test - public void testAntiMeridianSearch() throws Exception { - // verify that a search works if the bounding box crosses the anti meridian - try (GeoMesaGeoIndexer f = new GeoMesaGeoIndexer()) { - f.setConf(conf); - - ValueFactory vf = new ValueFactoryImpl(); - Resource context = vf.createURI("foo:context"); - - Resource subjectEast = vf.createURI("foo:subj:east"); - URI predicateEast = GeoConstants.GEO_AS_WKT; - Value objectEast = vf.createLiteral("Point(179 0)", GeoConstants.XMLSCHEMA_OGC_WKT); - Statement statementEast = new ContextStatementImpl(subjectEast, predicateEast, objectEast, context); - f.storeStatement(convertStatement(statementEast)); - - Resource subjectWest = vf.createURI("foo:subj:west"); - URI predicateWest = GeoConstants.GEO_AS_WKT; - Value objectWest = vf.createLiteral("Point(-179 0)", GeoConstants.XMLSCHEMA_OGC_WKT); - Statement statementWest = new ContextStatementImpl(subjectWest, predicateWest, objectWest, context); - f.storeStatement(convertStatement(statementWest)); - - f.flush(); - - double[] ONE = { 178.1, 1, -178, 1, -178, -1, 178.1, -1, 178.1, 1 }; - - LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(ONE, 2)); - - Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - - Assert.assertEquals(Sets.newHashSet(statementEast, statementWest), getSet(f.queryWithin(p1, EMPTY_CONSTRAINTS))); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/7727b165/extras/indexing/src/test/java/mvm/rya/indexing/mongo/MongoGeoIndexerSfTest.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/mongo/MongoGeoIndexerSfTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/mongo/MongoGeoIndexerSfTest.java deleted file mode 100644 index fe6b51a..0000000 --- a/extras/indexing/src/test/java/mvm/rya/indexing/mongo/MongoGeoIndexerSfTest.java +++ /dev/null @@ -1,305 +0,0 @@ -package mvm.rya.indexing.mongo; -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.UUID; - -import org.apache.hadoop.conf.Configuration; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.openrdf.model.Resource; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.model.Value; -import org.openrdf.model.ValueFactory; -import org.openrdf.model.impl.StatementImpl; -import org.openrdf.model.impl.ValueFactoryImpl; - -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import com.mongodb.MongoClient; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.GeometryFactory; -import com.vividsolutions.jts.geom.LineString; -import com.vividsolutions.jts.geom.LinearRing; -import com.vividsolutions.jts.geom.Point; -import com.vividsolutions.jts.geom.Polygon; -import com.vividsolutions.jts.geom.PrecisionModel; -import com.vividsolutions.jts.geom.impl.PackedCoordinateSequence; - -import de.flapdoodle.embed.mongo.distribution.Version; -import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory; -import info.aduna.iteration.CloseableIteration; -import mvm.rya.api.RdfCloudTripleStoreConfiguration; -import mvm.rya.api.domain.RyaStatement; -import mvm.rya.api.resolver.RdfToRyaConversions; -import mvm.rya.api.resolver.RyaToRdfConversions; -import mvm.rya.indexing.StatementConstraints; -import mvm.rya.indexing.accumulo.ConfigUtils; -import mvm.rya.indexing.accumulo.geo.GeoConstants; -import mvm.rya.indexing.mongodb.geo.MongoGeoIndexer; -import mvm.rya.mongodb.MongoDBRdfConfiguration; - -/** - * Tests all of the "simple functions" of the geoindexer. - */ -public class MongoGeoIndexerSfTest { - private MongoDBRdfConfiguration conf; - private static GeometryFactory gf = new GeometryFactory(new PrecisionModel(), 4326); - private static MongoGeoIndexer g; - - private static final StatementConstraints EMPTY_CONSTRAINTS = new StatementConstraints(); - - // Here is the landscape: - /** - * <pre> - * +---+---+---+---+---+---+---+ - * | F | | - * + A + + C + - * | | | - * +---+---+ E +---+---+ - * | | / | - * + B + /+---+---+ - * | | / | | - * +---+---+/--+---+---+ - * / | D | - * / +---+---+ - * </pre> - **/ - - private static final Polygon A = poly(bbox(0, 1, 4, 5)); - private static final Polygon B = poly(bbox(0, 1, 2, 3)); - private static final Polygon C = poly(bbox(4, 3, 6, 5)); - private static final Polygon D = poly(bbox(3, 0, 5, 2)); - - private static final Point F = point(2, 4); - - private static final LineString E = line(2, 0, 3, 3); - - private static final Map<Geometry, String> names = Maps.newHashMap(); - static { - names.put(A, "A"); - names.put(B, "B"); - names.put(C, "C"); - names.put(D, "D"); - names.put(E, "E"); - names.put(F, "F"); - } - - @Before - public void before() throws Exception { - System.out.println(UUID.randomUUID().toString()); - conf = new MongoDBRdfConfiguration(); - conf.set(ConfigUtils.USE_MONGO, "true"); - conf.set(MongoDBRdfConfiguration.USE_TEST_MONGO, "true"); - conf.set(MongoDBRdfConfiguration.MONGO_DB_NAME, "test"); - conf.set(MongoDBRdfConfiguration.MONGO_COLLECTION_PREFIX, "rya_"); - conf.set(ConfigUtils.GEO_PREDICATES_LIST, "http://www.opengis.net/ont/geosparql#asWKT"); - conf.set(ConfigUtils.USE_GEO, "true"); - conf.setTablePrefix("rya_"); - - final MongodForTestsFactory testsFactory = MongodForTestsFactory.with(Version.Main.PRODUCTION); - final MongoClient mongoClient = testsFactory.newMongo(); - g = new MongoGeoIndexer(); - g.initIndexer(conf, mongoClient); - g.storeStatement(statement(A)); - g.storeStatement(statement(B)); - g.storeStatement(statement(C)); - g.storeStatement(statement(D)); - g.storeStatement(statement(F)); - g.storeStatement(statement(E)); - } - - private static RyaStatement statement(final Geometry geo) { - final ValueFactory vf = new ValueFactoryImpl(); - final Resource subject = vf.createURI("uri:" + names.get(geo)); - final URI predicate = GeoConstants.GEO_AS_WKT; - final Value object = vf.createLiteral(geo.toString(), GeoConstants.XMLSCHEMA_OGC_WKT); - return RdfToRyaConversions.convertStatement(new StatementImpl(subject, predicate, object)); - - } - - private static Point point(final double x, final double y) { - return gf.createPoint(new Coordinate(x, y)); - } - - private static LineString line(final double x1, final double y1, final double x2, final double y2) { - return new LineString(new PackedCoordinateSequence.Double(new double[] { x1, y1, x2, y2 }, 2), gf); - } - - private static Polygon poly(final double[] arr) { - final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(arr, 2)); - final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - return p1; - } - - private static double[] bbox(final double x1, final double y1, final double x2, final double y2) { - return new double[] { x1, y1, x1, y2, x2, y2, x2, y1, x1, y1 }; - } - - public void compare(final CloseableIteration<Statement, ?> actual, final Geometry... expected) throws Exception { - final Set<Statement> expectedSet = Sets.newHashSet(); - for (final Geometry geo : expected) { - expectedSet.add(RyaToRdfConversions.convertStatement(statement(geo))); - } - - Assert.assertEquals(expectedSet, getSet(actual)); - } - - private static <X> Set<X> getSet(final CloseableIteration<X, ?> iter) throws Exception { - final Set<X> set = new HashSet<X>(); - while (iter.hasNext()) { - set.add(iter.next()); - } - return set; - } - - private static Geometry[] EMPTY_RESULTS = {}; - - @Test - public void testEquals() throws Exception { - // point - compare(g.queryEquals(F, EMPTY_CONSTRAINTS), F); - compare(g.queryEquals(point(2, 2), EMPTY_CONSTRAINTS), EMPTY_RESULTS); - - // line - compare(g.queryEquals(E, EMPTY_CONSTRAINTS), E); - compare(g.queryEquals(line(2, 2, 3, 3), EMPTY_CONSTRAINTS), EMPTY_RESULTS); - - // poly - compare(g.queryEquals(A, EMPTY_CONSTRAINTS), A); - compare(g.queryEquals(poly(bbox(1, 1, 4, 5)), EMPTY_CONSTRAINTS), EMPTY_RESULTS); - - } - -// @Test -// public void testDisjoint() throws Exception { -// // point -// compare(g.queryDisjoint(F, EMPTY_CONSTRAINTS), B, C, D, E); -// -// // line -// compare(g.queryDisjoint(E, EMPTY_CONSTRAINTS), B, C, D, F); -// -// // poly -// compare(g.queryDisjoint(A, EMPTY_CONSTRAINTS), EMPTY_RESULTS); -// compare(g.queryDisjoint(B, EMPTY_CONSTRAINTS), C, D, F, E); -// } - - @Test - public void testIntersectsPoint() throws Exception { - // This seems like a bug - // compare(g.queryIntersects(F, EMPTY_CONSTRAINTS), A, F); - // compare(g.queryIntersects(F, EMPTY_CONSTRAINTS), EMPTY_RESULTS); - } - - @Test - public void testIntersectsLine() throws Exception { - // This seems like a bug - // compare(g.queryIntersects(E, EMPTY_CONSTRAINTS), A, E); - // compare(g.queryIntersects(E, EMPTY_CONSTRAINTS), EMPTY_RESULTS); - } - -// @Test -// public void testIntersectsPoly() throws Exception { -// compare(g.queryIntersects(A, EMPTY_CONSTRAINTS), A, B, C, D, F, E); -// } - -// @Test -// public void testTouchesPoint() throws Exception { -// compare(g.queryTouches(F, EMPTY_CONSTRAINTS), EMPTY_RESULTS); -// } -// -// @Test -// public void testTouchesLine() throws Exception { -// compare(g.queryTouches(E, EMPTY_CONSTRAINTS), EMPTY_RESULTS); -// } - -// @Test -// public void testTouchesPoly() throws Exception { -// compare(g.queryTouches(A, EMPTY_CONSTRAINTS), C); -// } - -// @Test -// public void testCrossesPoint() throws Exception { -// compare(g.queryCrosses(F, EMPTY_CONSTRAINTS), EMPTY_RESULTS); -// } - - @Test - public void testCrossesLine() throws Exception { - // compare(g.queryCrosses(E, EMPTY_CONSTRAINTS), A); - } - -// @Test -// public void testCrossesPoly() throws Exception { -// compare(g.queryCrosses(A, EMPTY_CONSTRAINTS), E); -// } - -// @Test -// public void testWithin() throws Exception { -// // point -// // compare(g.queryWithin(F, EMPTY_CONSTRAINTS), F); -// -// // line -//// compare(g.queryWithin(E, EMPTY_CONSTRAINTS), E); -// -// // poly -// compare(g.queryWithin(A, EMPTY_CONSTRAINTS), A, B, F); -// } - -// @Test -// public void testContainsPoint() throws Exception { -// compare(g.queryContains(F, EMPTY_CONSTRAINTS), A, F); -// } - - @Test - public void testContainsLine() throws Exception { - // compare(g.queryContains(E, EMPTY_CONSTRAINTS), E); - } - -// @Test -// public void testContainsPoly() throws Exception { -// compare(g.queryContains(A, EMPTY_CONSTRAINTS), A); -// compare(g.queryContains(B, EMPTY_CONSTRAINTS), A, B); -// } - - @Test - public void testOverlapsPoint() throws Exception { - // compare(g.queryOverlaps(F, EMPTY_CONSTRAINTS), F); - // You cannot have overlapping points - // compare(g.queryOverlaps(F, EMPTY_CONSTRAINTS), EMPTY_RESULTS); - } - - @Test - public void testOverlapsLine() throws Exception { - // compare(g.queryOverlaps(E, EMPTY_CONSTRAINTS), A, E); - // You cannot have overlapping lines - // compare(g.queryOverlaps(E, EMPTY_CONSTRAINTS), EMPTY_RESULTS); - } - -// @Test -// public void testOverlapsPoly() throws Exception { -// compare(g.queryOverlaps(A, EMPTY_CONSTRAINTS), D); -// } - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/7727b165/extras/indexing/src/test/java/mvm/rya/indexing/mongo/MongoGeoIndexerTest.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/mongo/MongoGeoIndexerTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/mongo/MongoGeoIndexerTest.java deleted file mode 100644 index 7a20deb..0000000 --- a/extras/indexing/src/test/java/mvm/rya/indexing/mongo/MongoGeoIndexerTest.java +++ /dev/null @@ -1,396 +0,0 @@ -package mvm.rya.indexing.mongo; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - - - -import static mvm.rya.api.resolver.RdfToRyaConversions.convertStatement; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - -import org.apache.hadoop.conf.Configuration; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.openrdf.model.Resource; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.model.Value; -import org.openrdf.model.ValueFactory; -import org.openrdf.model.impl.ContextStatementImpl; -import org.openrdf.model.impl.StatementImpl; -import org.openrdf.model.impl.ValueFactoryImpl; - -import com.google.common.collect.Sets; -import com.mongodb.MongoClient; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.GeometryFactory; -import com.vividsolutions.jts.geom.LinearRing; -import com.vividsolutions.jts.geom.Point; -import com.vividsolutions.jts.geom.Polygon; -import com.vividsolutions.jts.geom.PrecisionModel; -import com.vividsolutions.jts.geom.impl.PackedCoordinateSequence; - -import de.flapdoodle.embed.mongo.distribution.Version; -import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory; -import info.aduna.iteration.CloseableIteration; -import mvm.rya.indexing.StatementConstraints; -import mvm.rya.indexing.accumulo.ConfigUtils; -import mvm.rya.indexing.accumulo.geo.GeoConstants; -import mvm.rya.indexing.mongodb.geo.MongoGeoIndexer; -import mvm.rya.mongodb.MongoDBRdfConfiguration; - -public class MongoGeoIndexerTest { - - private static final StatementConstraints EMPTY_CONSTRAINTS = new StatementConstraints(); - - MongoDBRdfConfiguration conf; - MongoClient mongoClient; - GeometryFactory gf = new GeometryFactory(new PrecisionModel(), 4326); - - @Before - public void before() throws Exception { - conf = new MongoDBRdfConfiguration(); - conf.set(ConfigUtils.USE_MONGO, "true"); - conf.set(MongoDBRdfConfiguration.USE_TEST_MONGO, "true"); - conf.set(MongoDBRdfConfiguration.MONGO_DB_NAME, "test"); - conf.set(MongoDBRdfConfiguration.MONGO_COLLECTION_PREFIX, "rya_"); - conf.set(ConfigUtils.GEO_PREDICATES_LIST, "http://www.opengis.net/ont/geosparql#asWKT"); - conf.set(ConfigUtils.USE_GEO, "true"); - conf.setTablePrefix("rya_"); - - final MongodForTestsFactory testsFactory = MongodForTestsFactory.with(Version.Main.PRODUCTION); - mongoClient = testsFactory.newMongo(); - } - - @Test - public void testRestrictPredicatesSearch() throws Exception { - conf.setStrings(ConfigUtils.GEO_PREDICATES_LIST, "pred:1,pred:2"); - try (MongoGeoIndexer f = new MongoGeoIndexer()) { - f.initIndexer(conf, mongoClient); - - final ValueFactory vf = new ValueFactoryImpl(); - - final Point point = gf.createPoint(new Coordinate(10, 10)); - final Value pointValue = vf.createLiteral("Point(10 10)", GeoConstants.XMLSCHEMA_OGC_WKT); - final URI invalidPredicate = GeoConstants.GEO_AS_WKT; - - // These should not be stored because they are not in the predicate list - f.storeStatement(convertStatement(new StatementImpl(vf.createURI("foo:subj1"), invalidPredicate, pointValue))); - f.storeStatement(convertStatement(new StatementImpl(vf.createURI("foo:subj2"), invalidPredicate, pointValue))); - - final URI pred1 = vf.createURI("pred:1"); - final URI pred2 = vf.createURI("pred:2"); - - // These should be stored because they are in the predicate list - final Statement s3 = new StatementImpl(vf.createURI("foo:subj3"), pred1, pointValue); - final Statement s4 = new StatementImpl(vf.createURI("foo:subj4"), pred2, pointValue); - f.storeStatement(convertStatement(s3)); - f.storeStatement(convertStatement(s4)); - - // This should not be stored because the object is not valid wkt - f.storeStatement(convertStatement(new StatementImpl(vf.createURI("foo:subj5"), pred1, vf.createLiteral("soint(10 10)")))); - - // This should not be stored because the object is not a literal - f.storeStatement(convertStatement(new StatementImpl(vf.createURI("foo:subj6"), pred1, vf.createURI("p:Point(10 10)")))); - - f.flush(); - - final Set<Statement> actual = getSet(f.queryEquals(point, EMPTY_CONSTRAINTS)); - Assert.assertEquals(2, actual.size()); - Assert.assertTrue(actual.contains(s3)); - Assert.assertTrue(actual.contains(s4)); - } - } - - private static <X> Set<X> getSet(final CloseableIteration<X, ?> iter) throws Exception { - final Set<X> set = new HashSet<X>(); - while (iter.hasNext()) { - set.add(iter.next()); - } - return set; - } - - @Test - public void testPrimeMeridianSearch() throws Exception { - try (MongoGeoIndexer f = new MongoGeoIndexer()) { - f.initIndexer(conf, mongoClient); - - final ValueFactory vf = new ValueFactoryImpl(); - final Resource subject = vf.createURI("foo:subj"); - final URI predicate = GeoConstants.GEO_AS_WKT; - final Value object = vf.createLiteral("Point(0 0)", GeoConstants.XMLSCHEMA_OGC_WKT); - final Resource context = vf.createURI("foo:context"); - - final Statement statement = new ContextStatementImpl(subject, predicate, object, context); - f.storeStatement(convertStatement(statement)); - f.flush(); - - final double[] ONE = { 1, 1, -1, 1, -1, -1, 1, -1, 1, 1 }; - final double[] TWO = { 2, 2, -2, 2, -2, -2, 2, -2, 2, 2 }; - final double[] THREE = { 3, 3, -3, 3, -3, -3, 3, -3, 3, 3 }; - - final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(ONE, 2)); - final LinearRing r2 = gf.createLinearRing(new PackedCoordinateSequence.Double(TWO, 2)); - final LinearRing r3 = gf.createLinearRing(new PackedCoordinateSequence.Double(THREE, 2)); - - final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - final Polygon p2 = gf.createPolygon(r2, new LinearRing[] {}); - final Polygon p3 = gf.createPolygon(r3, new LinearRing[] {}); - - Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p1, EMPTY_CONSTRAINTS))); - Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p2, EMPTY_CONSTRAINTS))); - Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p3, EMPTY_CONSTRAINTS))); - - // Test a ring with a hole in it - final Polygon p3m2 = gf.createPolygon(r3, new LinearRing[] { r2 }); - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(p3m2, EMPTY_CONSTRAINTS))); - - // test a ring outside the point - final double[] OUT = { 3, 3, 1, 3, 1, 1, 3, 1, 3, 3 }; - final LinearRing rOut = gf.createLinearRing(new PackedCoordinateSequence.Double(OUT, 2)); - final Polygon pOut = gf.createPolygon(rOut, new LinearRing[] {}); - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(pOut, EMPTY_CONSTRAINTS))); - } - } - - @Test - public void testDcSearch() throws Exception { - // test a ring around dc - try (MongoGeoIndexer f = new MongoGeoIndexer()) { - f.initIndexer(conf, mongoClient); - - final ValueFactory vf = new ValueFactoryImpl(); - final Resource subject = vf.createURI("foo:subj"); - final URI predicate = GeoConstants.GEO_AS_WKT; - final Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT); - final Resource context = vf.createURI("foo:context"); - - final Statement statement = new ContextStatementImpl(subject, predicate, object, context); - f.storeStatement(convertStatement(statement)); - f.flush(); - - final double[] IN = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 }; - final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(IN, 2)); - final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p1, EMPTY_CONSTRAINTS))); - - // test a ring outside the point - final double[] OUT = { -77, 39, -76, 39, -76, 38, -77, 38, -77, 39 }; - final LinearRing rOut = gf.createLinearRing(new PackedCoordinateSequence.Double(OUT, 2)); - final Polygon pOut = gf.createPolygon(rOut, new LinearRing[] {}); - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(pOut, EMPTY_CONSTRAINTS))); - } - } - - @Test - public void testDeleteSearch() throws Exception { - // test a ring around dc - try (MongoGeoIndexer f = new MongoGeoIndexer()) { - f.initIndexer(conf, mongoClient); - - final ValueFactory vf = new ValueFactoryImpl(); - final Resource subject = vf.createURI("foo:subj"); - final URI predicate = GeoConstants.GEO_AS_WKT; - final Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT); - final Resource context = vf.createURI("foo:context"); - - final Statement statement = new ContextStatementImpl(subject, predicate, object, context); - f.storeStatement(convertStatement(statement)); - f.flush(); - - f.deleteStatement(convertStatement(statement)); - - // test a ring that the point would be inside of if not deleted - final double[] in = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 }; - final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(in, 2)); - final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(p1, EMPTY_CONSTRAINTS))); - - // test a ring that the point would be outside of if not deleted - final double[] out = { -77, 39, -76, 39, -76, 38, -77, 38, -77, 39 }; - final LinearRing rOut = gf.createLinearRing(new PackedCoordinateSequence.Double(out, 2)); - final Polygon pOut = gf.createPolygon(rOut, new LinearRing[] {}); - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(pOut, EMPTY_CONSTRAINTS))); - - // test a ring for the whole world and make sure the point is gone - // Geomesa is a little sensitive around lon 180, so we only go to 179 - final double[] world = { -180, 90, 179, 90, 179, -90, -180, -90, -180, 90 }; - final LinearRing rWorld = gf.createLinearRing(new PackedCoordinateSequence.Double(world, 2)); - final Polygon pWorld = gf.createPolygon(rWorld, new LinearRing[] {}); - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(pWorld, EMPTY_CONSTRAINTS))); - } - } - - @Test - public void testDcSearchWithContext() throws Exception { - // test a ring around dc - try (MongoGeoIndexer f = new MongoGeoIndexer()) { - f.initIndexer(conf, mongoClient); - - final ValueFactory vf = new ValueFactoryImpl(); - final Resource subject = vf.createURI("foo:subj"); - final URI predicate = GeoConstants.GEO_AS_WKT; - final Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT); - final Resource context = vf.createURI("foo:context"); - - final Statement statement = new ContextStatementImpl(subject, predicate, object, context); - f.storeStatement(convertStatement(statement)); - f.flush(); - - final double[] IN = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 }; - final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(IN, 2)); - final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - - // query with correct context - Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p1, new StatementConstraints().setContext(context)))); - - // query with wrong context - Assert.assertEquals(Sets.newHashSet(), - getSet(f.queryWithin(p1, new StatementConstraints().setContext(vf.createURI("foo:context2"))))); - } - } - - @Test - public void testDcSearchWithSubject() throws Exception { - // test a ring around dc - try (MongoGeoIndexer f = new MongoGeoIndexer()) { - f.initIndexer(conf, mongoClient); - - final ValueFactory vf = new ValueFactoryImpl(); - final Resource subject = vf.createURI("foo:subj"); - final URI predicate = GeoConstants.GEO_AS_WKT; - final Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT); - final Resource context = vf.createURI("foo:context"); - - final Statement statement = new ContextStatementImpl(subject, predicate, object, context); - f.storeStatement(convertStatement(statement)); - f.flush(); - - final double[] IN = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 }; - final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(IN, 2)); - final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - - // query with correct subject - Assert.assertEquals(Sets.newHashSet(statement), getSet(f.queryWithin(p1, new StatementConstraints().setSubject(subject)))); - - // query with wrong subject - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(p1, new StatementConstraints().setSubject(vf.createURI("foo:subj2"))))); - } - } - - @Test - public void testDcSearchWithSubjectAndContext() throws Exception { - // test a ring around dc - try (MongoGeoIndexer f = new MongoGeoIndexer()) { - f.initIndexer(conf, mongoClient); - - final ValueFactory vf = new ValueFactoryImpl(); - final Resource subject = vf.createURI("foo:subj"); - final URI predicate = GeoConstants.GEO_AS_WKT; - final Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT); - final Resource context = vf.createURI("foo:context"); - - final Statement statement = new ContextStatementImpl(subject, predicate, object, context); - f.storeStatement(convertStatement(statement)); - f.flush(); - - final double[] IN = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 }; - final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(IN, 2)); - final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - - // query with correct context subject - Assert.assertEquals(Sets.newHashSet(statement), - getSet(f.queryWithin(p1, new StatementConstraints().setContext(context).setSubject(subject)))); - - // query with wrong context - Assert.assertEquals(Sets.newHashSet(), - getSet(f.queryWithin(p1, new StatementConstraints().setContext(vf.createURI("foo:context2"))))); - - // query with wrong subject - Assert.assertEquals(Sets.newHashSet(), getSet(f.queryWithin(p1, new StatementConstraints().setSubject(vf.createURI("foo:subj2"))))); - } - } - - @Test - public void testDcSearchWithPredicate() throws Exception { - // test a ring around dc - try (MongoGeoIndexer f = new MongoGeoIndexer()) { - f.initIndexer(conf, mongoClient); - - final ValueFactory vf = new ValueFactoryImpl(); - final Resource subject = vf.createURI("foo:subj"); - final URI predicate = GeoConstants.GEO_AS_WKT; - final Value object = vf.createLiteral("Point(-77.03524 38.889468)", GeoConstants.XMLSCHEMA_OGC_WKT); - final Resource context = vf.createURI("foo:context"); - - final Statement statement = new ContextStatementImpl(subject, predicate, object, context); - f.storeStatement(convertStatement(statement)); - f.flush(); - - final double[] IN = { -78, 39, -77, 39, -77, 38, -78, 38, -78, 39 }; - final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(IN, 2)); - final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - - // query with correct Predicate - Assert.assertEquals(Sets.newHashSet(statement), - getSet(f.queryWithin(p1, new StatementConstraints().setPredicates(Collections.singleton(predicate))))); - - // query with wrong predicate - Assert.assertEquals(Sets.newHashSet(), - getSet(f.queryWithin(p1, new StatementConstraints().setPredicates(Collections.singleton(vf.createURI("other:pred")))))); - } - } - - // @Test - public void testAntiMeridianSearch() throws Exception { - // verify that a search works if the bounding box crosses the anti meridian - try (MongoGeoIndexer f = new MongoGeoIndexer()) { - f.initIndexer(conf, mongoClient); - - final ValueFactory vf = new ValueFactoryImpl(); - final Resource context = vf.createURI("foo:context"); - - final Resource subjectEast = vf.createURI("foo:subj:east"); - final URI predicateEast = GeoConstants.GEO_AS_WKT; - final Value objectEast = vf.createLiteral("Point(179 0)", GeoConstants.XMLSCHEMA_OGC_WKT); - final Statement statementEast = new ContextStatementImpl(subjectEast, predicateEast, objectEast, context); - f.storeStatement(convertStatement(statementEast)); - - final Resource subjectWest = vf.createURI("foo:subj:west"); - final URI predicateWest = GeoConstants.GEO_AS_WKT; - final Value objectWest = vf.createLiteral("Point(-179 0)", GeoConstants.XMLSCHEMA_OGC_WKT); - final Statement statementWest = new ContextStatementImpl(subjectWest, predicateWest, objectWest, context); - f.storeStatement(convertStatement(statementWest)); - - f.flush(); - - final double[] ONE = { 178.1, 1, -178, 1, -178, -1, 178.1, -1, 178.1, 1 }; - - final LinearRing r1 = gf.createLinearRing(new PackedCoordinateSequence.Double(ONE, 2)); - - final Polygon p1 = gf.createPolygon(r1, new LinearRing[] {}); - - Assert.assertEquals(Sets.newHashSet(statementEast, statementWest), getSet(f.queryWithin(p1, EMPTY_CONSTRAINTS))); - } - } -}