com.orientechnologies.spatial.shape.OLineStringShapeBuilder Maven / Gradle / Ivy
Show all versions of orientdb-lucene Show documentation
/**
* Copyright 2010-2016 OrientDB LTD (http://orientdb.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.
*
*
For more information: http://www.orientdb.com
*/
package com.orientechnologies.spatial.shape;
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.db.ODatabaseInternal;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import java.util.List;
import java.util.stream.Collectors;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.locationtech.spatial4j.shape.jts.JtsGeometry;
public class OLineStringShapeBuilder extends OComplexShapeBuilder {
@Override
public String getName() {
return "OLineString";
}
@Override
public OShapeType getType() {
return OShapeType.LINESTRING;
}
@Override
public void initClazz(ODatabaseInternal db) {
OSchema schema = db.getMetadata().getSchema();
OClass lineString = schema.createAbstractClass(getName(), superClass(db));
lineString.createProperty(COORDINATES, OType.EMBEDDEDLIST, OType.EMBEDDEDLIST);
if (OGlobalConfiguration.SPATIAL_ENABLE_DIRECT_WKT_READER.getValueAsBoolean()) {
OClass lineStringZ = schema.createAbstractClass(getName() + "Z", superClass(db));
lineStringZ.createProperty(COORDINATES, OType.EMBEDDEDLIST, OType.EMBEDDEDLIST);
}
}
@Override
public JtsGeometry fromDoc(ODocument document) {
validate(document);
List> coordinates = document.field(COORDINATES);
Coordinate[] coords = new Coordinate[coordinates.size()];
int i = 0;
for (List coordinate : coordinates) {
coords[i] = new Coordinate(coordinate.get(0).doubleValue(), coordinate.get(1).doubleValue());
i++;
}
return toShape(GEOMETRY_FACTORY.createLineString(coords));
}
@Override
public ODocument toDoc(JtsGeometry shape) {
ODocument doc = new ODocument(getName());
LineString lineString = (LineString) shape.getGeom();
doc.field(COORDINATES, coordinatesFromLineString(lineString));
return doc;
}
@Override
protected ODocument toDoc(JtsGeometry shape, Geometry geometry) {
if (geometry == null || Double.isNaN(geometry.getCoordinate().getZ())) {
return toDoc(shape);
}
ODocument doc = new ODocument(getName() + "Z");
doc.field(COORDINATES, coordinatesFromLineStringZ(geometry));
return doc;
}
@Override
public String asText(ODocument document) {
if (document.getClassName().equals("OLineStringZ")) {
List> coordinates = document.getProperty("coordinates");
String result =
coordinates.stream()
.map(
point ->
(point.stream().map(coord -> format(coord)).collect(Collectors.joining(" "))))
.collect(Collectors.joining(", "));
return "LINESTRING Z (" + result + ")";
} else {
return super.asText(document);
}
}
}