use of org.exist.indexing.spatial.AbstractGMLJDBCIndexWorker in project exist by eXist-db.
the class FunGMLProducers method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
Sequence result = null;
try {
AbstractGMLJDBCIndexWorker indexWorker = (AbstractGMLJDBCIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(AbstractGMLJDBCIndex.ID);
if (indexWorker == null) {
logger.error("Unable to find a spatial index worker");
throw new XPathException(this, "Unable to find a spatial index worker");
}
Geometry geometry = null;
String targetSRS = null;
if (isCalledAs("transform")) {
if (args[0].isEmpty())
result = Sequence.EMPTY_SEQUENCE;
else {
NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
// Try to get the geometry from the index
String sourceSRS = null;
if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
sourceSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
hasUsedIndex = true;
}
// Otherwise, build it
if (geometry == null) {
sourceSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
}
if (geometry == null) {
logger.error("Unable to get a geometry from the node");
throw new XPathException("Unable to get a geometry from the node");
}
targetSRS = args[1].itemAt(0).getStringValue().trim();
geometry = indexWorker.transformGeometry(geometry, sourceSRS, targetSRS);
}
} else if (isCalledAs("WKTtoGML")) {
if (args[0].isEmpty())
result = Sequence.EMPTY_SEQUENCE;
else {
String wkt = args[0].itemAt(0).getStringValue();
WKTReader wktReader = new WKTReader();
try {
geometry = wktReader.read(wkt);
} catch (ParseException e) {
logger.error(e.getMessage());
throw new XPathException(e);
}
if (geometry == null) {
logger.error("Unable to get a geometry from the node");
throw new XPathException("Unable to get a geometry from the node");
}
targetSRS = args[1].itemAt(0).getStringValue().trim();
}
} else if (isCalledAs("buffer")) {
if (args[0].isEmpty())
result = Sequence.EMPTY_SEQUENCE;
else {
NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
// Try to get the geometry from the index
if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
hasUsedIndex = true;
}
// Otherwise, build it
if (geometry == null) {
targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
}
if (geometry == null) {
logger.error("Unable to get a geometry from the node");
throw new XPathException("Unable to get a geometry from the node");
}
double distance = ((DoubleValue) args[1].itemAt(0)).getDouble();
int quadrantSegments = 8;
int endCapStyle = BufferOp.CAP_ROUND;
if (getArgumentCount() > 2 && Type.subTypeOf(args[2].itemAt(0).getType(), Type.INTEGER))
quadrantSegments = ((IntegerValue) args[2].itemAt(0)).getInt();
if (getArgumentCount() > 3 && Type.subTypeOf(args[3].itemAt(0).getType(), Type.INTEGER))
endCapStyle = ((IntegerValue) args[3].itemAt(0)).getInt();
switch(endCapStyle) {
case BufferOp.CAP_ROUND:
case BufferOp.CAP_BUTT:
case BufferOp.CAP_SQUARE:
// OK
break;
default:
{
logger.error("Invalid line end style");
throw new XPathException("Invalid line end style");
}
}
geometry = geometry.buffer(distance, quadrantSegments, endCapStyle);
}
} else if (isCalledAs("getBbox")) {
if (args[0].isEmpty())
result = Sequence.EMPTY_SEQUENCE;
else {
NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
// Try to get the geometry from the index
if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
hasUsedIndex = true;
}
// Otherwise, build it
if (geometry == null) {
targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
}
if (geometry == null) {
logger.error("Unable to get a geometry from the node");
throw new XPathException("Unable to get a geometry from the node");
}
geometry = geometry.getEnvelope();
}
} else if (isCalledAs("convexHull")) {
if (args[0].isEmpty())
result = Sequence.EMPTY_SEQUENCE;
else {
NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
// Try to get the geometry from the index
if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
hasUsedIndex = true;
}
// Otherwise, build it
if (geometry == null) {
targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
}
if (geometry == null) {
logger.error("Unable to get a geometry from the node");
throw new XPathException("Unable to get a geometry from the node");
}
geometry = geometry.convexHull();
}
} else if (isCalledAs("boundary")) {
if (args[0].isEmpty())
result = Sequence.EMPTY_SEQUENCE;
else {
NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
// Try to get the geometry from the index
if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
hasUsedIndex = true;
}
// Otherwise, build it
if (geometry == null) {
targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
}
if (geometry == null) {
logger.error("Unable to get a geometry from the node");
throw new XPathException("Unable to get a geometry from the node");
}
geometry = geometry.getBoundary();
}
} else {
Geometry geometry1 = null;
Geometry geometry2 = null;
if (args[0].isEmpty() && args[1].isEmpty())
result = Sequence.EMPTY_SEQUENCE;
else if (!args[0].isEmpty() && args[1].isEmpty())
result = args[0].itemAt(0).toSequence();
else if (args[0].isEmpty() && !args[1].isEmpty())
result = args[1].itemAt(0).toSequence();
else {
NodeValue geometryNode1 = (NodeValue) args[0].itemAt(0);
NodeValue geometryNode2 = (NodeValue) args[1].itemAt(0);
String srsName1 = null;
String srsName2 = null;
// Try to get the geometries from the index
if (geometryNode1.getImplementationType() == NodeValue.PERSISTENT_NODE) {
srsName1 = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode1, "SRS_NAME").getStringValue();
geometry1 = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode1, false);
hasUsedIndex = true;
}
if (geometryNode2.getImplementationType() == NodeValue.PERSISTENT_NODE) {
srsName2 = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode2, "SRS_NAME").getStringValue();
geometry2 = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode2, false);
hasUsedIndex = true;
}
// Otherwise build them
if (geometry1 == null) {
srsName1 = ((Element) geometryNode1.getNode()).getAttribute("srsName").trim();
geometry1 = indexWorker.streamNodeToGeometry(context, geometryNode1);
}
if (geometry2 == null) {
srsName2 = ((Element) geometryNode2.getNode()).getAttribute("srsName").trim();
geometry2 = indexWorker.streamNodeToGeometry(context, geometryNode2);
}
if (geometry1 == null) {
logger.error("Unable to get a geometry from the first node");
throw new XPathException("Unable to get a geometry from the first node");
}
if (geometry2 == null) {
logger.error("Unable to get a geometry from the second node");
throw new XPathException("Unable to get a geometry from the second node");
}
if (srsName1 == null)
throw new XPathException("Unable to get a SRS for the first geometry");
if (srsName2 == null)
throw new XPathException("Unable to get a SRS for the second geometry");
// Transform the second geometry in the SRS of the first one if necessary
if (!srsName1.equalsIgnoreCase(srsName2)) {
geometry2 = indexWorker.transformGeometry(geometry2, srsName1, srsName2);
}
if (isCalledAs("intersection")) {
geometry = geometry1.intersection(geometry2);
} else if (isCalledAs("union")) {
geometry = geometry1.union(geometry2);
} else if (isCalledAs("difference")) {
geometry = geometry1.difference(geometry2);
} else if (isCalledAs("symetricDifference")) {
geometry = geometry1.symDifference(geometry2);
}
targetSRS = srsName1;
}
}
if (result == null) {
String gmlPrefix = context.getPrefixForURI(AbstractGMLJDBCIndexWorker.GML_NS);
if (gmlPrefix == null) {
logger.error("namespace is not defined:" + SpatialModule.PREFIX);
throw new XPathException("'" + AbstractGMLJDBCIndexWorker.GML_NS + "' namespace is not defined");
}
context.pushDocumentContext();
try {
MemTreeBuilder builder = context.getDocumentBuilder();
DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
result = (NodeValue) indexWorker.streamGeometryToElement(geometry, targetSRS, receiver);
} finally {
context.popDocumentContext();
}
}
} catch (SpatialIndexException e) {
logger.error(e.getMessage());
throw new XPathException(e);
}
return result;
}
use of org.exist.indexing.spatial.AbstractGMLJDBCIndexWorker in project exist by eXist-db.
the class FunSpatialSearch method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
Sequence result = null;
Sequence nodes = args[0];
if (nodes.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else if (args[1].isEmpty()) {
// TODO : to be discussed. We could also return an empty sequence here
result = nodes;
} else {
try {
AbstractGMLJDBCIndexWorker indexWorker = (AbstractGMLJDBCIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(AbstractGMLJDBCIndex.ID);
if (indexWorker == null) {
logger.error("Unable to find a spatial index worker");
throw new XPathException(this, "Unable to find a spatial index worker");
}
Geometry EPSG4326_geometry = null;
NodeValue geometryNode = (NodeValue) args[1].itemAt(0);
if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE)
// Get the geometry from the index if available
EPSG4326_geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, true);
if (EPSG4326_geometry == null) {
String sourceCRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
Geometry geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
EPSG4326_geometry = indexWorker.transformGeometry(geometry, sourceCRS, "EPSG:4326");
}
if (EPSG4326_geometry == null) {
logger.error("Unable to get a geometry from the node");
throw new XPathException("Unable to get a geometry from the node");
}
int spatialOp = SpatialOperator.UNKNOWN;
if (isCalledAs("equals"))
spatialOp = SpatialOperator.EQUALS;
else if (isCalledAs("disjoint"))
spatialOp = SpatialOperator.DISJOINT;
else if (isCalledAs("intersects"))
spatialOp = SpatialOperator.INTERSECTS;
else if (isCalledAs("touches"))
spatialOp = SpatialOperator.TOUCHES;
else if (isCalledAs("crosses"))
spatialOp = SpatialOperator.CROSSES;
else if (isCalledAs("within"))
spatialOp = SpatialOperator.WITHIN;
else if (isCalledAs("contains"))
spatialOp = SpatialOperator.CONTAINS;
else if (isCalledAs("overlaps"))
spatialOp = SpatialOperator.OVERLAPS;
// Search the EPSG:4326 in the index
result = indexWorker.search(context.getBroker(), nodes.toNodeSet(), EPSG4326_geometry, spatialOp);
hasUsedIndex = true;
} catch (SpatialIndexException e) {
logger.error(e.getMessage(), e);
throw new XPathException(e);
}
}
return result;
}
use of org.exist.indexing.spatial.AbstractGMLJDBCIndexWorker in project exist by eXist-db.
the class FunGeometricProperties method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
Sequence result = null;
Sequence nodes = args[0];
if (nodes.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
try {
Geometry geometry = null;
String sourceCRS = null;
AbstractGMLJDBCIndexWorker indexWorker = (AbstractGMLJDBCIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(AbstractGMLJDBCIndex.ID);
if (indexWorker == null) {
logger.error("Unable to find a spatial index worker");
throw new XPathException(this, "Unable to find a spatial index worker");
}
String propertyName = null;
if (isCalledAs("getWKT")) {
propertyName = "WKT";
} else if (isCalledAs("getWKB")) {
propertyName = "WKB";
} else if (isCalledAs("getMinX")) {
propertyName = "MINX";
} else if (isCalledAs("getMaxX")) {
propertyName = "MAXX";
} else if (isCalledAs("getMinY")) {
propertyName = "MINY";
} else if (isCalledAs("getMaxY")) {
propertyName = "MAXY";
} else if (isCalledAs("getCentroidX")) {
propertyName = "CENTROID_X";
} else if (isCalledAs("getCentroidY")) {
propertyName = "CENTROID_Y";
} else if (isCalledAs("getArea")) {
propertyName = "AREA";
} else if (isCalledAs("getEPSG4326WKT")) {
propertyName = "EPSG4326_WKT";
} else if (isCalledAs("getEPSG4326WKB")) {
propertyName = "EPSG4326_WKB";
} else if (isCalledAs("getEPSG4326MinX")) {
propertyName = "EPSG4326_MINX";
} else if (isCalledAs("getEPSG4326MaxX")) {
propertyName = "EPSG4326_MAXX";
} else if (isCalledAs("getEPSG4326MinY")) {
propertyName = "EPSG4326_MINY";
} else if (isCalledAs("getEPSG4326MaxY")) {
propertyName = "EPSG4326_MAXY";
} else if (isCalledAs("getEPSG4326CentroidX")) {
propertyName = "EPSG4326_CENTROID_X";
} else if (isCalledAs("getEPSG4326CentroidY")) {
propertyName = "EPSG4326_CENTROID_Y";
} else if (isCalledAs("getEPSG4326Area")) {
propertyName = "EPSG4326_AREA";
} else if (isCalledAs("getSRS")) {
propertyName = "SRS_NAME";
} else if (isCalledAs("getGeometryType")) {
propertyName = "GEOMETRY_TYPE";
} else if (isCalledAs("isClosed")) {
propertyName = "IS_CLOSED";
} else if (isCalledAs("isSimple")) {
propertyName = "IS_SIMPLE";
} else if (isCalledAs("isValid")) {
propertyName = "IS_VALID";
} else {
logger.error("Unknown spatial property: {}", getName().getLocalPart());
throw new XPathException("Unknown spatial property: " + getName().getLocalPart());
}
NodeValue geometryNode = (NodeValue) nodes.itemAt(0);
if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
// The node should be indexed : get its property
result = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, propertyName);
hasUsedIndex = true;
} else {
// builds the geometry
sourceCRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
if (geometry == null) {
logger.error("Unable to get a geometry from the node");
throw new XPathException("Unable to get a geometry from the node");
}
// Transform the geometry to EPSG:4326 if relevant
if (propertyName.contains("EPSG4326")) {
geometry = indexWorker.transformGeometry(geometry, sourceCRS, "EPSG:4326");
if (isCalledAs("getEPSG4326WKT")) {
result = new StringValue(wktWriter.write(geometry));
} else if (isCalledAs("getEPSG4326WKB")) {
byte[] data = wkbWriter.write(geometry);
return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream(data));
} else if (isCalledAs("getEPSG4326MinX")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMinX());
} else if (isCalledAs("getEPSG4326MaxX")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMaxX());
} else if (isCalledAs("getEPSG4326MinY")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMinY());
} else if (isCalledAs("getEPSG4326MaxY")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMaxY());
} else if (isCalledAs("getEPSG4326CentroidX")) {
result = new DoubleValue(geometry.getCentroid().getX());
} else if (isCalledAs("getEPSG4326CentroidY")) {
result = new DoubleValue(geometry.getCentroid().getY());
} else if (isCalledAs("getEPSG4326Area")) {
result = new DoubleValue(geometry.getArea());
}
} else if (isCalledAs("getWKT")) {
result = new StringValue(wktWriter.write(geometry));
} else if (isCalledAs("getWKB")) {
byte[] data = wkbWriter.write(geometry);
return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream(data));
} else if (isCalledAs("getMinX")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMinX());
} else if (isCalledAs("getMaxX")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMaxX());
} else if (isCalledAs("getMinY")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMinY());
} else if (isCalledAs("getMaxY")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMaxY());
} else if (isCalledAs("getCentroidX")) {
result = new DoubleValue(geometry.getCentroid().getX());
} else if (isCalledAs("getCentroidY")) {
result = new DoubleValue(geometry.getCentroid().getY());
} else if (isCalledAs("getArea")) {
result = new DoubleValue(geometry.getArea());
} else if (isCalledAs("getSRS")) {
result = new StringValue(((Element) geometryNode).getAttribute("srsName"));
} else if (isCalledAs("getGeometryType")) {
result = new StringValue(geometry.getGeometryType());
} else if (isCalledAs("isClosed")) {
result = new BooleanValue(!geometry.isEmpty());
} else if (isCalledAs("isSimple")) {
result = new BooleanValue(geometry.isSimple());
} else if (isCalledAs("isValid")) {
result = new BooleanValue(geometry.isValid());
} else {
logger.error("Unknown spatial property: {}", getName().getLocalPart());
throw new XPathException("Unknown spatial property: " + getName().getLocalPart());
}
}
} catch (SpatialIndexException e) {
logger.error(e.getMessage());
throw new XPathException(e);
}
}
return result;
}
Aggregations