提交 3f58272d authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Use own utilities instead of JTS

上级 9ca18311
...@@ -14,18 +14,17 @@ import org.h2.index.Index; ...@@ -14,18 +14,17 @@ import org.h2.index.Index;
import org.h2.mvstore.db.MVSpatialIndex; import org.h2.mvstore.db.MVSpatialIndex;
import org.h2.table.Column; import org.h2.table.Column;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.util.geometry.GeometryUtils;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueGeometry; import org.h2.value.ValueGeometry;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.GeometryFactory;
/** /**
* Data stored while calculating an aggregate. * Data stored while calculating an aggregate.
*/ */
class AggregateDataEnvelope extends AggregateData { class AggregateDataEnvelope extends AggregateData {
private Envelope envelope; private double[] envelope;
/** /**
* Get the index (if any) for the column specified in the geometry * Get the index (if any) for the column specified in the geometry
...@@ -62,18 +61,15 @@ class AggregateDataEnvelope extends AggregateData { ...@@ -62,18 +61,15 @@ class AggregateDataEnvelope extends AggregateData {
if (v == ValueNull.INSTANCE) { if (v == ValueNull.INSTANCE) {
return; return;
} }
if (envelope == null) { envelope = GeometryUtils.union(envelope, ((ValueGeometry) v.convertTo(Value.GEOMETRY)).getEnvelopeNoCopy());
envelope = new Envelope();
}
envelope.expandToInclude(((ValueGeometry) v.convertTo(Value.GEOMETRY)).getEnvelopeNoCopy());
} }
@Override @Override
Value getValue(Database database, int dataType, boolean distinct) { Value getValue(Database database, int dataType, boolean distinct) {
if (envelope == null || envelope.isNull()) { if (envelope == null) {
return ValueNull.INSTANCE; return ValueNull.INSTANCE;
} }
return ValueGeometry.getFromGeometry(new GeometryFactory().toGeometry(envelope)); return ValueGeometry.get(GeometryUtils.envelope2wkb(envelope));
} }
} }
...@@ -5,6 +5,11 @@ ...@@ -5,6 +5,11 @@
*/ */
package org.h2.index; package org.h2.index;
import static org.h2.util.geometry.GeometryUtils.MAX_X;
import static org.h2.util.geometry.GeometryUtils.MAX_Y;
import static org.h2.util.geometry.GeometryUtils.MIN_X;
import static org.h2.util.geometry.GeometryUtils.MIN_Y;
import java.util.Iterator; import java.util.Iterator;
import org.h2.command.dml.AllColumnsForPlan; import org.h2.command.dml.AllColumnsForPlan;
import org.h2.engine.Session; import org.h2.engine.Session;
...@@ -23,7 +28,6 @@ import org.h2.table.TableFilter; ...@@ -23,7 +28,6 @@ import org.h2.table.TableFilter;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueGeometry; import org.h2.value.ValueGeometry;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
import org.locationtech.jts.geom.Envelope;
/** /**
* This is an index based on a MVR-TreeMap. * This is an index based on a MVR-TreeMap.
...@@ -133,10 +137,9 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex { ...@@ -133,10 +137,9 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex {
if (v == ValueNull.INSTANCE) { if (v == ValueNull.INSTANCE) {
return new SpatialKey(row.getKey()); return new SpatialKey(row.getKey());
} }
Envelope env = ((ValueGeometry) v.convertTo(Value.GEOMETRY)).getEnvelopeNoCopy(); double[] env = ((ValueGeometry) v.convertTo(Value.GEOMETRY)).getEnvelopeNoCopy();
return new SpatialKey(row.getKey(), return new SpatialKey(row.getKey(),
(float) env.getMinX(), (float) env.getMaxX(), (float) env[MIN_X], (float) env[MAX_X], (float) env[MIN_Y], (float) env[MAX_Y]);
(float) env.getMinY(), (float) env.getMaxY());
} }
@Override @Override
......
...@@ -5,6 +5,11 @@ ...@@ -5,6 +5,11 @@
*/ */
package org.h2.mvstore.db; package org.h2.mvstore.db;
import static org.h2.util.geometry.GeometryUtils.MAX_X;
import static org.h2.util.geometry.GeometryUtils.MAX_Y;
import static org.h2.util.geometry.GeometryUtils.MIN_X;
import static org.h2.util.geometry.GeometryUtils.MIN_Y;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
...@@ -29,12 +34,11 @@ import org.h2.result.SearchRow; ...@@ -29,12 +34,11 @@ import org.h2.result.SearchRow;
import org.h2.result.SortOrder; import org.h2.result.SortOrder;
import org.h2.table.IndexColumn; import org.h2.table.IndexColumn;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.util.geometry.GeometryUtils;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueGeometry; import org.h2.value.ValueGeometry;
import org.h2.value.ValueLong; import org.h2.value.ValueLong;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.GeometryFactory;
/** /**
* This is an index based on a MVRTreeMap. * This is an index based on a MVRTreeMap.
...@@ -264,8 +268,7 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex { ...@@ -264,8 +268,7 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex {
bmaxyf = maxyf; bmaxyf = maxyf;
} }
} }
return ValueGeometry.getFromGeometry(new GeometryFactory().toGeometry( return ValueGeometry.get(GeometryUtils.envelope2wkb(new double[] {bminxf, bmaxxf, bminyf, bmaxyf}));
new Envelope(bminxf, bmaxxf, bminyf, bmaxyf)));
} }
return ValueNull.INSTANCE; return ValueNull.INSTANCE;
} }
...@@ -275,10 +278,10 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex { ...@@ -275,10 +278,10 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex {
if (v == ValueNull.INSTANCE) { if (v == ValueNull.INSTANCE) {
return new SpatialKey(row.getKey()); return new SpatialKey(row.getKey());
} }
Envelope env = ((ValueGeometry) v.convertTo(Value.GEOMETRY)).getEnvelopeNoCopy(); double[] env = ((ValueGeometry) v.convertTo(Value.GEOMETRY)).getEnvelopeNoCopy();
return new SpatialKey(row.getKey(), return new SpatialKey(row.getKey(),
(float) env.getMinX(), (float) env.getMaxX(), (float) env[MIN_X], (float) env[MAX_X],
(float) env.getMinY(), (float) env.getMaxY()); (float) env[MIN_Y], (float) env[MAX_Y]);
} }
@Override @Override
...@@ -467,10 +470,9 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex { ...@@ -467,10 +470,9 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex {
if (hasBounds) { if (hasBounds) {
if ((minxf <= bminxf || maxxf >= bmaxxf || minyf <= bminyf || maxyf >= bmaxyf) if ((minxf <= bminxf || maxxf >= bmaxxf || minyf <= bminyf || maxyf >= bmaxyf)
&& map.containsKey(key)) { && map.containsKey(key)) {
Envelope env = ((ValueGeometry) mvTable.getRow(session, key.getId()).getValue(columnId)) double[] env = ((ValueGeometry) mvTable.getRow(session, key.getId()).getValue(columnId))
.getEnvelopeNoCopy(); .getEnvelopeNoCopy();
double minxd = env.getMinX(), maxxd = env.getMaxX(), minyd = env.getMinY(), double minxd = env[MIN_X], maxxd = env[MAX_X], minyd = env[MIN_Y], maxyd = env[MAX_Y];
maxyd = env.getMaxY();
if (minxd < bminxd) { if (minxd < bminxd) {
bminxf = minxf; bminxf = minxf;
bminxd = minxd; bminxd = minxd;
...@@ -490,16 +492,16 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex { ...@@ -490,16 +492,16 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex {
} }
} else if (map.containsKey(key)) { } else if (map.containsKey(key)) {
hasBounds = true; hasBounds = true;
Envelope env = ((ValueGeometry) mvTable.getRow(session, key.getId()).getValue(columnId)) double[] env = ((ValueGeometry) mvTable.getRow(session, key.getId()).getValue(columnId))
.getEnvelopeNoCopy(); .getEnvelopeNoCopy();
bminxf = minxf; bminxf = minxf;
bminxd = env.getMinX(); bminxd = env[MIN_X];
bmaxxf = maxxf; bmaxxf = maxxf;
bmaxxd = env.getMaxX(); bmaxxd = env[MAX_X];
bminyf = minyf; bminyf = minyf;
bminyd = env.getMinY(); bminyd = env[MIN_Y];
bmaxyf = maxyf; bmaxyf = maxyf;
bmaxyd = env.getMaxY(); bmaxyd = env[MAX_Y];
} }
} else if (hasBounds) { } else if (hasBounds) {
if (minxf <= bminxf || maxxf >= bmaxxf || minyf <= bminyf || maxyf >= bmaxyf) { if (minxf <= bminxf || maxxf >= bmaxxf || minyf <= bminyf || maxyf >= bmaxyf) {
...@@ -512,8 +514,9 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex { ...@@ -512,8 +514,9 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex {
} }
Value getBounds() { Value getBounds() {
return hasBounds ? ValueGeometry.getFromGeometry(new GeometryFactory().toGeometry( return hasBounds ? ValueGeometry.get(
new Envelope(bminxd, bmaxxd, bminyd, bmaxyd))) : ValueNull.INSTANCE; GeometryUtils.envelope2wkb(new double[] {bminxd, bmaxxd, bminyd, bmaxyd}))
: ValueNull.INSTANCE;
} }
} }
......
...@@ -550,22 +550,26 @@ public final class GeometryUtils { ...@@ -550,22 +550,26 @@ public final class GeometryUtils {
} else if (envelope2 == null) { } else if (envelope2 == null) {
return envelope1; return envelope1;
} }
double minX1 = envelope1[MIN_X], maxX1 = envelope1[MAX_X], minY1 = envelope1[MIN_Y], maxY1 = envelope1[MAX_Y]; double minX1 = envelope1[MIN_X], maxX1 = envelope1[MAX_X], minY1 = envelope1[MIN_Y], maxY1 = envelope1[MAX_Y];
double minX2 = envelope2[MIN_X], maxX2 = envelope2[MAX_X], minY2 = envelope2[MIN_Y], maxY2 = envelope2[MAX_Y]; double minX2 = envelope2[MIN_X], maxX2 = envelope2[MAX_X], minY2 = envelope2[MIN_Y], maxY2 = envelope2[MAX_Y];
boolean modified = false;
if (minX1 > minX2) { if (minX1 > minX2) {
minX1 = minX2; minX1 = minX2;
modified = true;
} }
if (maxX1 < maxX2) { if (maxX1 < maxX2) {
maxX1 = maxX2; maxX1 = maxX2;
modified = true;
} }
if (minY1 > minY2) { if (minY1 > minY2) {
minY1 = minY2; minY1 = minY2;
modified = true;
} }
if (maxY1 < maxY2) { if (maxY1 < maxY2) {
maxY1 = maxY2; maxY1 = maxY2;
modified = true;
} }
return new double[] { minX1, maxX1, minY1, maxY1 }; return modified ? new double[] { minX1, maxX1, minY1, maxY1 } : envelope1;
} }
/** /**
......
...@@ -898,7 +898,7 @@ public class DataType { ...@@ -898,7 +898,7 @@ public class DataType {
case Value.RESULT_SET: case Value.RESULT_SET:
return ResultSet.class.getName(); return ResultSet.class.getName();
case Value.GEOMETRY: case Value.GEOMETRY:
return GEOMETRY_CLASS_NAME; return GEOMETRY_CLASS != null ? GEOMETRY_CLASS_NAME : String.class.getName();
case Value.INTERVAL_YEAR: case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH: case Value.INTERVAL_MONTH:
case Value.INTERVAL_DAY: case Value.INTERVAL_DAY:
......
...@@ -612,12 +612,13 @@ public class TestSpatial extends TestDb { ...@@ -612,12 +612,13 @@ public class TestSpatial extends TestDb {
ValueGeometry geom3d = ValueGeometry.get(ewkt); ValueGeometry geom3d = ValueGeometry.get(ewkt);
assertEquals(ewkt, geom3d.getString()); assertEquals(ewkt, geom3d.getString());
ValueGeometry copy = ValueGeometry.get(geom3d.getBytes()); ValueGeometry copy = ValueGeometry.get(geom3d.getBytes());
assertEquals(6, copy.getGeometry().getCoordinates()[0].z); Geometry g = (Geometry) copy.getGeometry();
assertEquals(5, copy.getGeometry().getCoordinates()[1].z); assertEquals(6, g.getCoordinates()[0].z);
assertEquals(4, copy.getGeometry().getCoordinates()[2].z); assertEquals(5, g.getCoordinates()[1].z);
assertEquals(4, g.getCoordinates()[2].z);
// Test SRID // Test SRID
copy = ValueGeometry.get(geom3d.getBytes()); copy = ValueGeometry.get(geom3d.getBytes());
assertEquals(27572, copy.getGeometry().getSRID()); assertEquals(27572, g.getSRID());
Point point = new GeometryFactory().createPoint((new Coordinate(1.1d, 1.2d))); Point point = new GeometryFactory().createPoint((new Coordinate(1.1d, 1.2d)));
// SRID 0 // SRID 0
...@@ -696,7 +697,7 @@ public class TestSpatial extends TestDb { ...@@ -696,7 +697,7 @@ public class TestSpatial extends TestDb {
assertFalse(valueGeometry.equals(valueGeometry2)); assertFalse(valueGeometry.equals(valueGeometry2));
ValueGeometry valueGeometry3 = ValueGeometry.getFromGeometry(geometry); ValueGeometry valueGeometry3 = ValueGeometry.getFromGeometry(geometry);
assertEquals(valueGeometry, valueGeometry3); assertEquals(valueGeometry, valueGeometry3);
assertEquals(geometry.getSRID(), valueGeometry3.getGeometry().getSRID()); assertEquals(geometry.getSRID(), ((Geometry) valueGeometry3.getGeometry()).getSRID());
// Check illegal geometry (no WKB representation) // Check illegal geometry (no WKB representation)
try { try {
ValueGeometry.get("POINT EMPTY"); ValueGeometry.get("POINT EMPTY");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论