Skip to content

Commit

Permalink
started to implement polygon/ multipolygon and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Aug 12, 2015
1 parent ac25897 commit 072c498
Show file tree
Hide file tree
Showing 40 changed files with 2,929 additions and 269 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Expand Up @@ -30,7 +30,7 @@
<jdkVersion>1.6</jdkVersion>
<javac.src.version>1.6</javac.src.version>
<javac.target.version>1.6</javac.target.version>
<orientdb.version>2.1.0</orientdb.version>
<orientdb.version>2.2-SNAPSHOT</orientdb.version>
<lucene.version>4.7.0</lucene.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Expand Down Expand Up @@ -299,6 +299,7 @@
<scope>test</scope>
</dependency>


<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
Expand Down
Expand Up @@ -20,8 +20,8 @@
import com.orientechnologies.lucene.index.OLuceneFullTextIndex;
import com.orientechnologies.lucene.index.OLuceneSpatialIndex;
import com.orientechnologies.lucene.manager.OLuceneFullTextIndexManager;
import com.orientechnologies.lucene.manager.OLuceneSpatialIndexManager;
import com.orientechnologies.lucene.shape.OShapeFactoryImpl;
import com.orientechnologies.lucene.manager.OLuceneGeoSpatialIndexManager;
import com.orientechnologies.lucene.shape.OShapeFactory;
import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.db.ODatabaseInternal;
Expand Down Expand Up @@ -60,7 +60,7 @@ public class OLuceneIndexFactory implements OIndexFactory, ODatabaseLifecycleLis

public OLuceneIndexFactory() {

spatialManager = new OLuceneSpatialManager(new OShapeFactoryImpl());
spatialManager = new OLuceneSpatialManager(OShapeFactory.INSTANCE);
Orient.instance().addDbLifecycleListener(this);
}

Expand Down Expand Up @@ -142,7 +142,7 @@ private OIndexInternal<?> createLuceneIndex(String name, ODatabaseDocumentIntern
new OLuceneFullTextIndexManager(), indexType), valueContainerAlgorithm, metadata);
} else if (OClass.INDEX_TYPE.SPATIAL.toString().equals(indexType)) {
return new OLuceneSpatialIndex(name, indexType, LUCENE_ALGORITHM, new OLuceneIndexEngine<Set<OIdentifiable>>(
new OLuceneSpatialIndexManager(new OShapeFactoryImpl()), indexType), valueContainerAlgorithm, metadata);
new OLuceneGeoSpatialIndexManager(OShapeFactory.INSTANCE), indexType), valueContainerAlgorithm, metadata);
}
throw new OConfigurationException("Unsupported type : " + indexType);
}
Expand Down
Expand Up @@ -18,10 +18,9 @@

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.lucene.functions.spatial.OToWktFunction;
import com.orientechnologies.lucene.functions.spatial.STNearFunction;
import com.orientechnologies.lucene.manager.OLuceneIndexManagerAbstract;
import com.orientechnologies.lucene.operator.OLuceneNearOperator;
import com.orientechnologies.lucene.operator.OLuceneTextOperator;
import com.orientechnologies.lucene.operator.OLuceneWithinOperator;
import com.orientechnologies.lucene.operator.*;
import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.db.ODatabaseInternal;
import com.orientechnologies.orient.core.db.ODatabaseLifecycleListener;
Expand Down Expand Up @@ -52,7 +51,13 @@ public void startup() {
OSQLEngine.registerOperator(new OLuceneTextOperator());
OSQLEngine.registerOperator(new OLuceneWithinOperator());
OSQLEngine.registerOperator(new OLuceneNearOperator());
OSQLEngine.registerOperator(new OLuceneGeoFilterOperator());
OSQLEngine.registerOperator(new OLuceneSTContainsOperator());
OSQLEngine.registerOperator(new OLuceneSTNearOperator());
OSQLEngine.registerOperator(new OLuceneSTWithinOperator());
OSQLEngine.getInstance().registerFunction(OToWktFunction.NAME, new OToWktFunction());
OSQLEngine.getInstance().registerFunction(STNearFunction.NAME, new STNearFunction());

OLogManager.instance().info(this, "Lucene index plugin installed and active. Lucene version: %s",
OLuceneIndexManagerAbstract.LUCENE_VERSION);
}
Expand Down
Expand Up @@ -16,6 +16,8 @@
package com.orientechnologies.lucene.functions;

import com.orientechnologies.lucene.functions.spatial.OToWktFunction;
import com.orientechnologies.lucene.functions.spatial.STNearFunction;
import com.orientechnologies.lucene.functions.spatial.STWithinFunction;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.sql.functions.OSQLFunction;
import com.orientechnologies.orient.core.sql.functions.OSQLFunctionFactory;
Expand All @@ -30,6 +32,8 @@ public class OLuceneFunctionsFactory implements OSQLFunctionFactory {

static {
register(OToWktFunction.NAME, OToWktFunction.class);
register(STWithinFunction.NAME, STWithinFunction.class);
register(STNearFunction.NAME, STNearFunction.class);
}

@Override
Expand Down
@@ -0,0 +1,50 @@
/*
*
* * 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.functions.spatial;

import com.orientechnologies.lucene.shape.OShapeFactory;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.functions.OSQLFunctionAbstract;

/**
* Created by Enrico Risa on 06/08/15.
*/
public class OToGeoJsonFunction extends OSQLFunctionAbstract {

public static final String NAME = "asgeojson";

OShapeFactory factory = OShapeFactory.INSTANCE;

public OToGeoJsonFunction() {
super(NAME, 1, 1);
}

@Override
public Object execute(Object iThis, OIdentifiable iCurrentRecord, Object iCurrentResult, Object[] iParams,
OCommandContext iContext) {
return factory.asText((ODocument) iParams[0]);
}

@Override
public String getSyntax() {
return "asGeoJson(<doc>)";
}
}
Expand Up @@ -18,7 +18,7 @@

package com.orientechnologies.lucene.functions.spatial;

import com.orientechnologies.lucene.shape.OShapeFactoryImpl;
import com.orientechnologies.lucene.shape.OShapeFactory;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.record.impl.ODocument;
Expand All @@ -31,7 +31,7 @@ public class OToWktFunction extends OSQLFunctionAbstract {

public static final String NAME = "towkt";

OShapeFactoryImpl factory = new OShapeFactoryImpl();
OShapeFactory factory = OShapeFactory.INSTANCE;

public OToWktFunction() {
super(NAME, 1, 1);
Expand Down
@@ -0,0 +1,46 @@
/*
*
* * 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.functions.spatial;

import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.sql.functions.OSQLFunctionAbstract;

/**
* Created by Enrico Risa on 12/08/15.
*/
public class STContainsFunction extends OSQLFunctionAbstract {

public static final String NAME = "stcontains";

public STContainsFunction() {
super(NAME, 2, 2);
}

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

@Override
public String getSyntax() {
return null;
}
}
@@ -0,0 +1,73 @@
/*
*
* * 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.functions.spatial;

import com.orientechnologies.lucene.shape.OShapeFactory;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.functions.OSQLFunctionAbstract;
import com.spatial4j.core.distance.DistanceUtils;
import com.spatial4j.core.shape.Circle;
import com.spatial4j.core.shape.Point;
import com.spatial4j.core.shape.Shape;
import com.spatial4j.core.shape.SpatialRelation;

import java.util.Map;

/**
* Created by Enrico Risa on 12/08/15.
*/
public class STNearFunction extends OSQLFunctionAbstract {

public static final String NAME = "st_near";

OShapeFactory factory = OShapeFactory.INSTANCE;

public STNearFunction() {
super(NAME, 2, 2);
}

@Override
public Object execute(Object iThis, OIdentifiable iCurrentRecord, Object iCurrentResult, Object[] iParams,
OCommandContext iContext) {
Shape shape = factory.fromDoc((ODocument) iParams[0]);
Map map = (Map) iParams[1];
Shape shape1 = factory.fromMapGeoJson((Map) map.get("shape"));

double distance = 0;

Number n = (Number) map.get("maxDistance");
if (n != null) {
distance = n.doubleValue();
}
Point p = (Point) shape1;
Circle circle = factory.SPATIAL_CONTEXT.makeCircle(p.getX(), p.getY(),
DistanceUtils.dist2Degrees(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM));
double docDistDEG = factory.SPATIAL_CONTEXT.getDistCalc().distance((Point) shape, p);
final double docDistInKM = DistanceUtils.degrees2Dist(docDistDEG, DistanceUtils.EARTH_EQUATORIAL_RADIUS_KM);
iContext.setVariable("distance", docDistInKM);
return shape.relate(circle) == SpatialRelation.WITHIN;
}

@Override
public String getSyntax() {
return null;
}
}
@@ -0,0 +1,58 @@
/*
*
* * 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.functions.spatial;

import com.orientechnologies.lucene.shape.OShapeFactory;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.functions.OSQLFunctionAbstract;
import com.spatial4j.core.shape.Shape;
import com.spatial4j.core.shape.SpatialRelation;

import java.util.Map;

/**
* Created by Enrico Risa on 12/08/15.
*/
public class STWithinFunction extends OSQLFunctionAbstract {

public static final String NAME = "st_within";

OShapeFactory factory = OShapeFactory.INSTANCE;

public STWithinFunction() {
super(NAME, 2, 2);
}

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

Shape shape = factory.fromDoc((ODocument) iParams[0]);
Map map = (Map) iParams[1];
Shape shape1 = factory.fromMapGeoJson((Map) map.get("shape"));
return shape.relate(shape1) == SpatialRelation.WITHIN;
}

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

0 comments on commit 072c498

Please sign in to comment.