Skip to content

Commit

Permalink
new function ST_DWithIn
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto Franchin authored and Roberto Franchin committed Oct 23, 2015
1 parent 83eda3d commit d224f34
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 1 deletion.
@@ -0,0 +1,80 @@
/*
*
* * Copyright 2014 Orient Technologies.
* *
* * Licensed 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.
*
*/

package com.orientechnologies.orient.spatial.functions;

import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ridbag.DoubleReferenceItem;
import com.orientechnologies.orient.core.sql.parser.OBinaryCompareOperator;
import com.orientechnologies.orient.core.sql.parser.OExpression;
import com.orientechnologies.orient.core.sql.parser.OFromClause;
import com.orientechnologies.orient.spatial.strategy.SpatialQueryBuilderWithin;
import com.spatial4j.core.shape.Shape;
import com.spatial4j.core.shape.SpatialRelation;

import java.util.Collection;

/**
* Created by Enrico Risa on 12/08/15.
*/
public class OSTDWithinFunction extends OSpatialFunctionAbstract {

public static final String NAME = "st_dwithin";

public OSTDWithinFunction() {
super(NAME, 3, 3);
}

@Override
public Object execute(Object iThis, OIdentifiable iCurrentRecord, Object iCurrentResult, Object[] iParams,
OCommandContext iContext) {

Shape shape = factory.fromObject(iParams[0]);

Shape shape1 = factory.fromObject(iParams[1]);

Double distance = (Double) iParams[2];

return factory.operation().isWithInDistance(shape, shape1,distance);
}

@Override
public String getSyntax() {
return null;
}

@Override
public Iterable<OIdentifiable> searchFromTarget(OFromClause target, OBinaryCompareOperator operator, Object rightValue,
OCommandContext ctx, OExpression... args) {
return results(target, args, ctx);
}

@Override
public long estimate(OFromClause target, OBinaryCompareOperator operator, Object rightValue, OCommandContext ctx,
OExpression... args) {

Collection resultSet = results(target, args, ctx);
return resultSet == null ? -1 : resultSet.size();
}

@Override
protected String operator() {
return SpatialQueryBuilderWithin.NAME;
}
}
Expand Up @@ -34,6 +34,7 @@ public class OSpatialFunctionsFactory implements OSQLFunctionFactory {
register(OSTGeomFromTextFunction.NAME, new OSTGeomFromTextFunction());
register(OSTAsTextFunction.NAME, new OSTAsTextFunction());
register(OSTWithinFunction.NAME, new OSTWithinFunction());
register(OSTDWithinFunction.NAME, new OSTDWithinFunction());
register(OSTEqualsFunction.NAME, new OSTEqualsFunction());
register(OSTAsBinaryFunction.NAME, new OSTAsBinaryFunction());
register(OSTEnvelopFunction.NAME, new OSTEnvelopFunction());
Expand Down
Expand Up @@ -182,7 +182,7 @@ public ODocument toDoc(Shape shape) {
doc = factories.get("OMultiPolygon").toDoc(createMultiPolygon(collection));
} else if (isMultiPoint(collection)) {
doc = factories.get("OMultiPoint").toDoc(createMultiPoint(collection));
} else if (isMultiLine(collection)) {
} else if (isMultiLine(collection)) {
doc = factories.get("OMultiLineString").toDoc(createMultiLine(collection));
} else {
doc = factories.get("OGeometryCollection").toDoc(shape);
Expand Down
Expand Up @@ -27,5 +27,7 @@ public interface OShapeOperation {

public double distance(Shape s1, Shape s2);

public boolean isWithInDistance(Shape s1, Shape s2, Double dist);

public boolean intersect(Shape s1,Shape s2);
}
Expand Up @@ -39,6 +39,13 @@ public double distance(Shape s1, Shape s2) {
return geometry.distance(geometry1);
}

@Override
public boolean isWithInDistance(Shape s1, Shape s2, Double dist) {
Geometry geometry = factory.toGeometry(s1);
Geometry geometry1 = factory.toGeometry(s2);
return geometry.isWithinDistance(geometry1, dist) ;
}

@Override
public boolean intersect(Shape s1, Shape s2) {
Geometry geometry = factory.toGeometry(s1);
Expand Down
@@ -0,0 +1,87 @@
/*
*
* * Copyright 2014 Orient Technologies.
* *
* * Licensed 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.
*
*/

package com.orientechnologies.lucene.test.geo.functions;

import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.List;

/**
* Created by Enrico Risa on 28/09/15.
*/
@Test(groups = "embedded")
public class LuceneSpatialDWithinTest {

@Test
public void testDWithinNoIndex() {

OrientGraphNoTx graph = new OrientGraphNoTx("memory:functionsTest");
try {
ODatabaseDocumentTx db = graph.getRawGraph();

List<ODocument> execute = db
.command(
new OCommandSQL(
"SELECT ST_DWithin(ST_GeomFromText('POLYGON((0 0, 10 0, 10 5, 0 5, 0 0))'), " +
"ST_GeomFromText('POLYGON((12 0, 14 0, 14 6, 12 6, 12 0))'), 2.0d) as distance"))
.execute();
ODocument next = execute.iterator().next();

Assert.assertEquals(next.field("distance"), true);

} finally {
graph.drop();
}
}

@Test(enabled = false)
public void testWithinIndex() {

OrientGraphNoTx graph = new OrientGraphNoTx("memory:functionsTest");
try {
ODatabaseDocumentTx db = graph.getRawGraph();

db.command(new OCommandSQL("create class Polygon extends v")).execute();
db.command(new OCommandSQL("create property Polygon.geometry EMBEDDED OPolygon")).execute();

db.command(new OCommandSQL("insert into Polygon set geometry = ST_GeomFromText('POLYGON((0 0, 10 0, 10 5, 0 5, 0 0))')")).execute();

db.command(new OCommandSQL("create index Polygon.g on Polygon (geometry) SPATIAL engine lucene")).execute();
List<ODocument> execute = db.command(new OCommandSQL("SELECT from Polygon where ST_DWithin(geometry, ST_GeomFromText('POLYGON((12 0, 14 0, 14 6, 12 6, 12 0))'), 2.0d)) = true"))
.execute();

Assert.assertEquals(execute.size(), 1);

// execute = db.command(
// new OCommandSQL("SELECT from Polygon where ST_Within(geometry, ST_Buffer(ST_GeomFromText('POINT(50 50)'), 30)) = true"))
// .execute();

// Assert.assertEquals(execute.size(), 1);

} finally {
graph.drop();
}
}

}

0 comments on commit d224f34

Please sign in to comment.