Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added first implementation of intersect function
- Loading branch information
Showing
11 changed files
with
272 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/com/orientechnologies/orient/spatial/functions/STIntersectsFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* | ||
* * 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.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.SpatialQueryBuilderIntersects; | ||
import com.spatial4j.core.shape.Shape; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Created by Enrico Risa on 12/08/15. | ||
*/ | ||
public class STIntersectsFunction extends OSpatialFunctionAbstract { | ||
|
||
public static final String NAME = "st_intersects"; | ||
|
||
public STIntersectsFunction() { | ||
super(NAME, 2, 2); | ||
} | ||
|
||
@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]); | ||
|
||
return factory.operation().intersect(shape, shape1); | ||
} | ||
|
||
@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 SpatialQueryBuilderIntersects.NAME; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...ain/java/com/orientechnologies/orient/spatial/strategy/SpatialQueryBuilderIntersects.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* | ||
* * 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.strategy; | ||
|
||
import com.orientechnologies.lucene.engine.OLuceneSpatialIndexContainer; | ||
import com.orientechnologies.lucene.query.SpatialQueryContext; | ||
import com.orientechnologies.orient.spatial.shape.OShapeBuilder; | ||
import com.spatial4j.core.shape.Shape; | ||
import org.apache.lucene.search.Filter; | ||
import org.apache.lucene.search.MatchAllDocsQuery; | ||
import org.apache.lucene.spatial.SpatialStrategy; | ||
import org.apache.lucene.spatial.query.SpatialArgs; | ||
import org.apache.lucene.spatial.query.SpatialOperation; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Created by Enrico Risa on 11/08/15. | ||
*/ | ||
public class SpatialQueryBuilderIntersects extends SpatialQueryBuilderAbstract { | ||
|
||
public static final String NAME = "intersects"; | ||
|
||
public SpatialQueryBuilderIntersects(OLuceneSpatialIndexContainer manager, OShapeBuilder factory) { | ||
super(manager, factory); | ||
} | ||
|
||
@Override | ||
public SpatialQueryContext build(Map<String, Object> query) throws Exception { | ||
Shape shape = parseShape(query); | ||
SpatialStrategy strategy = manager.strategy(); | ||
|
||
if (isOnlyBB(strategy)) { | ||
shape = shape.getBoundingBox(); | ||
} | ||
SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, shape); | ||
Filter filter = strategy.makeFilter(args); | ||
return new SpatialQueryContext(null, manager.searcher(), new MatchAllDocsQuery(), filter); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...est/java/com/orientechnologies/lucene/test/geo/functions/LuceneSpatialIntersectsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* | ||
* * 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. | ||
*/ | ||
public class LuceneSpatialIntersectsTest { | ||
|
||
@Test | ||
public void testIntersectsNoIndex() { | ||
|
||
OrientGraphNoTx graph = new OrientGraphNoTx("memory:functionsTest"); | ||
try { | ||
ODatabaseDocumentTx db = graph.getRawGraph(); | ||
|
||
List<ODocument> execute = db.command(new OCommandSQL("SELECT ST_Intersects('POINT(0 0)', 'LINESTRING ( 2 0, 0 2 )')")) | ||
.execute(); | ||
ODocument next = execute.iterator().next(); | ||
|
||
Assert.assertEquals(next.field("ST_Intersects"), false); | ||
execute = db.command(new OCommandSQL("SELECT ST_Intersects('POINT(0 0)', 'LINESTRING ( 0 0, 0 2 )')")).execute(); | ||
next = execute.iterator().next(); | ||
|
||
Assert.assertEquals(next.field("ST_Intersects"), true); | ||
|
||
} finally { | ||
graph.drop(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testIntersectsIndex() { | ||
|
||
OrientGraphNoTx graph = new OrientGraphNoTx("memory:functionsTest"); | ||
try { | ||
ODatabaseDocumentTx db = graph.getRawGraph(); | ||
|
||
db.command(new OCommandSQL("create class Lines extends v")).execute(); | ||
db.command(new OCommandSQL("create property Lines.geometry EMBEDDED OLINESTRING")).execute(); | ||
|
||
db.command(new OCommandSQL("insert into Lines set geometry = ST_GeomFromText('LINESTRING ( 2 0, 0 2 )')")).execute(); | ||
db.command(new OCommandSQL("insert into Lines set geometry = ST_GeomFromText('LINESTRING ( 0 0, 0 2 )')")).execute(); | ||
|
||
db.command(new OCommandSQL("create index L.g on Lines (geometry) SPATIAL engine lucene")).execute(); | ||
List<ODocument> execute = db.command(new OCommandSQL("SELECT from lines where ST_Intersects(geometry, 'POINT(0 0)') = true")) | ||
.execute(); | ||
|
||
Assert.assertEquals(execute.size(), 1); | ||
|
||
} finally { | ||
graph.drop(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters