com.stratio.cassandra.lucene.schema.mapping.GeoShapeMapper Maven / Gradle / Ivy
/**
* Copyright (C) 2014 Stratio (http://stratio.com)
*
* 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.stratio.cassandra.lucene.schema.mapping;
import com.google.common.base.MoreObjects;
import com.spatial4j.core.context.jts.JtsSpatialContext;
import com.spatial4j.core.shape.jts.JtsGeometry;
import com.stratio.cassandra.lucene.IndexException;
import com.stratio.cassandra.lucene.common.GeoTransformation;
import com.stratio.cassandra.lucene.util.GeospatialUtils;
import com.stratio.cassandra.lucene.util.GeospatialUtilsJTS;
import org.apache.cassandra.db.marshal.AsciiType;
import org.apache.cassandra.db.marshal.UTF8Type;
import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.search.SortField;
import org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy;
import org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree;
import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;
/**
* A {@link Mapper} to map geographical shapes represented according to the
* Well Known Text (WKT) format.
*
* This class depends on Java Topology Suite (JTS). This library can't
* be distributed together with this project due to license compatibility problems, but you can add it by putting jts-core-1.14.0.jar
* into Cassandra lib directory.
*
* Pole wrapping is not supported.
*
* @author Andres de la Pena {@literal }
*/
public class GeoShapeMapper extends SingleColumnMapper {
public static final JtsSpatialContext SPATIAL_CONTEXT = JtsSpatialContext.GEO;
/** The name of the mapped column. */
public final String column;
/** The max number of levels in the tree. */
public final int maxLevels;
/** The spatial strategy for radial distance searches. */
public final RecursivePrefixTreeStrategy strategy;
/** The sequence of transformations to be applied to the shape before indexing. */
public final List transformations;
/**
* Builds a new {@link GeoShapeMapper}.
*
* @param field the name of the field
* @param column the name of the column
* @param validated if the field must be validated
* @param maxLevels the maximum number of levels in the tree
* @param transformations the sequence of operations to be applied to the specified shape
*/
public GeoShapeMapper(String field,
String column,
Boolean validated,
Integer maxLevels,
List transformations) {
super(field, column, true, false, validated, null, String.class, AsciiType.instance, UTF8Type.instance);
this.column = column == null ? field : column;
if (StringUtils.isWhitespace(column)) {
throw new IndexException("Column must not be whitespace, but found '%s'", column);
}
this.maxLevels = GeospatialUtils.validateGeohashMaxLevels(maxLevels);
SpatialPrefixTree grid = new GeohashPrefixTree(SPATIAL_CONTEXT, this.maxLevels);
strategy = new RecursivePrefixTreeStrategy(grid, field);
this.transformations = (transformations == null) ? Collections.emptyList() : transformations;
}
/** {@inheritDoc} */
@Override
public void addIndexedFields(Document document, String name, String value) {
// Parse shape
JtsGeometry shape = GeospatialUtilsJTS.geometryFromWKT(SPATIAL_CONTEXT, value);
// Apply transformations
if (transformations != null) {
for (GeoTransformation transformation : transformations) {
shape = transformation.apply(shape, SPATIAL_CONTEXT);
}
}
// Add fields
for (IndexableField indexableField : strategy.createIndexableFields(shape)) {
document.add(indexableField);
}
}
/** {@inheritDoc} */
@Override
public void addSortedFields(Document document, String name, String value) {
// Nothing to do here
}
/** {@inheritDoc} */
@Override
public SortField sortField(String name, boolean reverse) {
throw new IndexException("GeoShape mapper '%s' does not support simple sorting", name);
}
protected String doBase(String field, @NotNull Object value) {
return value.toString();
}
/** {@inheritDoc} */
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("field", field)
.add("column", column)
.add("validated", validated)
.add("maxLevels", maxLevels)
.add("transformations", transformations)
.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy