提交 54c3fc72 authored 作者: Thomas Mueller's avatar Thomas Mueller

Formatting.

上级 dc4f999e
......@@ -32,7 +32,7 @@ import com.vividsolutions.jts.io.WKTReader;
/**
* Spatial datatype and index tests.
*
*
* @author Thomas Mueller
* @author Noel Grandin
* @author Nicolas Fortin, Atelier SIG, IRSTV FR CNRS 24888
......@@ -100,8 +100,9 @@ public class TestSpatial extends TestBase {
}
/**
* Generate a random linestring under the given bounding box.
*
* Generate a random line string under the given bounding box.
*
* @param geometryRand the random generator
* @param minX Bounding box min x
* @param maxX Bounding box max x
* @param minY Bounding box min y
......@@ -109,13 +110,13 @@ public class TestSpatial extends TestBase {
* @param maxLength LineString maximum length
* @return A segment within this bounding box
*/
public static Geometry getRandomGeometry(Random geometryRand,
double minX, double maxX,
public static Geometry getRandomGeometry(Random geometryRand,
double minX, double maxX,
double minY, double maxY, double maxLength) {
GeometryFactory factory = new GeometryFactory();
// Create the start point
Coordinate start = new Coordinate(
geometryRand.nextDouble() * (maxX - minX) + minX,
geometryRand.nextDouble() * (maxX - minX) + minX,
geometryRand.nextDouble() * (maxY - minY) + minY);
// Compute an angle
double angle = geometryRand.nextDouble() * Math.PI * 2;
......@@ -135,41 +136,41 @@ public class TestSpatial extends TestBase {
testRandom(conn, 44, 3500);
conn.close();
}
private void testRandom(Connection conn, long seed, long size) throws SQLException {
Statement stat = conn.createStatement();
stat.execute("drop table if exists test");
Random geometryRand = new Random(seed);
// Generate a set of geometry
// It is marked as random, but it generate always the same geometry set, given the same seed
stat.execute("create memory table test(" +
"id long primary key auto_increment, poly geometry)");
// Create segment generation bounding box
Envelope bbox = ValueGeometry.get("POLYGON ((" +
"301804.1049793153 2251719.1222191923, " +
"301804.1049793153 2254747.2888244865, " +
"304646.87362918374 2254747.2888244865, " +
"304646.87362918374 2251719.1222191923, " +
"301804.1049793153 2251719.1222191923))")
.getGeometry().getEnvelopeInternal();
// Create overlap test bounding box
String testBBoxString = "POLYGON ((" +
"302215.44416332216 2252748, " +
"302215.44416332216 2253851.781225762, " +
"303582.85796541866 2253851.781225762, " +
"303582.85796541866 2252748.526908161, " +
"302215.44416332216 2252748))";
Envelope testBBox = ValueGeometry.get(testBBoxString).getGeometry().getEnvelopeInternal();
PreparedStatement ps = conn.prepareStatement(
"insert into test(poly) values (?)");
long overlapCount = 0;
private void testRandom(Connection conn, long seed, long size) throws SQLException {
Statement stat = conn.createStatement();
stat.execute("drop table if exists test");
Random geometryRand = new Random(seed);
// generate a set of geometry (always the same set, given the same seed)
stat.execute("create memory table test(" +
"id long primary key auto_increment, poly geometry)");
// Create segment generation bounding box
Envelope boundingBox = ValueGeometry.get("POLYGON ((" +
"301804.1049793153 2251719.1222191923, " +
"301804.1049793153 2254747.2888244865, " +
"304646.87362918374 2254747.2888244865, " +
"304646.87362918374 2251719.1222191923, " +
"301804.1049793153 2251719.1222191923))")
.getGeometry().getEnvelopeInternal();
// Create overlap test bounding box
String testBoundingBoxString = "POLYGON ((" +
"302215.44416332216 2252748, " +
"302215.44416332216 2253851.781225762, " +
"303582.85796541866 2253851.781225762, " +
"303582.85796541866 2252748.526908161, " +
"302215.44416332216 2252748))";
Envelope testBBox = ValueGeometry.get(testBoundingBoxString).
getGeometry().getEnvelopeInternal();
PreparedStatement ps = conn.prepareStatement(
"insert into test(poly) values (?)");
long overlapCount = 0;
Set<Integer> overlaps = new HashSet<Integer>(680);
for (int i = 1; i <= size; i++) {
Geometry geometry = getRandomGeometry(
geometryRand,
bbox.getMinX(), bbox.getMaxX(),
bbox.getMinY(), bbox.getMaxY(), 200);
geometryRand,
boundingBox.getMinX(), boundingBox.getMaxX(),
boundingBox.getMinY(), boundingBox.getMaxY(), 200);
ps.setObject(1, geometry);
ps.execute();
ResultSet keys = ps.getGeneratedKeys();
......@@ -178,14 +179,13 @@ public class TestSpatial extends TestBase {
overlapCount++;
overlaps.add(keys.getInt(1));
}
}
ps.close();
// Create index
stat.execute("create spatial index idx_test_poly on test(poly)");
// Must find the same overlap count with index
ps = conn.prepareStatement(
"select id from test where poly && ?::Geometry");
ps.setString(1, testBBoxString);
}
ps.close();
// Create index
stat.execute("create spatial index idx_test_poly on test(poly)");
// Must find the same overlap count with index
ps = conn.prepareStatement("select id from test where poly && ?::Geometry");
ps.setString(1, testBoundingBoxString);
ResultSet rs = ps.executeQuery();
long found = 0;
while (rs.next()) {
......@@ -194,10 +194,11 @@ public class TestSpatial extends TestBase {
}
// Index count must be the same as sequential count
assertEquals(overlapCount, found);
// Missing id still in overlaps map
assertTrue(overlaps.isEmpty());
stat.execute("drop table if exists test");
}
// Missing id still in overlaps map
assertTrue(overlaps.isEmpty());
stat.execute("drop table if exists test");
}
private void testOverlap() throws SQLException {
deleteDb("spatial");
Connection conn = getConnection("spatial");
......@@ -209,7 +210,7 @@ public class TestSpatial extends TestBase {
stat.execute("insert into test values(3, 'POLYGON ((1 3, 1 4, 2 4, 1 3))')");
ResultSet rs = stat.executeQuery(
"select * from test " +
"select * from test " +
"where poly && 'POINT (1.5 1.5)'::Geometry");
assertTrue(rs.next());
assertEquals(1, rs.getInt("id"));
......@@ -220,8 +221,8 @@ public class TestSpatial extends TestBase {
}
}
private void testPersistentSpatialIndex() throws SQLException {
deleteDb("spatial_pers");
Connection conn = getConnection("spatial_pers");
deleteDb("spatialPersistent");
Connection conn = getConnection("spatialPersistent");
try {
Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key, poly geometry)");
......@@ -231,7 +232,7 @@ public class TestSpatial extends TestBase {
stat.execute("create spatial index on test(poly)");
ResultSet rs = stat.executeQuery(
"select * from test " +
"select * from test " +
"where poly && 'POINT (1.5 1.5)'::Geometry");
assertTrue(rs.next());
assertEquals(1, rs.getInt("id"));
......@@ -240,8 +241,8 @@ public class TestSpatial extends TestBase {
// Test with multiple operator
rs = stat.executeQuery(
"select * from test " +
"where poly && 'POINT (1.5 1.5)'::Geometry " +
"select * from test " +
"where poly && 'POINT (1.5 1.5)'::Geometry " +
"AND poly && 'POINT (1.7 1.75)'::Geometry");
assertTrue(rs.next());
assertEquals(1, rs.getInt("id"));
......@@ -251,16 +252,16 @@ public class TestSpatial extends TestBase {
// Close the database
conn.close();
}
if (config.memory) {
return;
}
conn = getConnection("spatial_pers");
conn = getConnection("spatialPersistent");
try {
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(
"select * from test " +
"select * from test " +
"where poly && 'POINT (1.5 1.5)'::Geometry");
assertTrue(rs.next());
assertEquals(1, rs.getInt("id"));
......@@ -282,7 +283,7 @@ public class TestSpatial extends TestBase {
stat.execute("insert into test values(3, 'POLYGON ((1 3, 1 4, 2 4, 1 3))')");
ResultSet rs = stat.executeQuery(
"select * from test " +
"select * from test " +
"where NOT poly && 'POINT (1.5 1.5)'::Geometry");
assertTrue(rs.next());
assertEquals(2, rs.getInt("id"));
......@@ -296,23 +297,36 @@ public class TestSpatial extends TestBase {
}
private static void createTestTable(Statement stat) throws SQLException {
stat.execute("create table area(idarea int primary key, the_geom geometry)");
stat.execute("create table area(idArea int primary key, the_geom geometry)");
stat.execute("create spatial index on area(the_geom)");
stat.execute("insert into area values(1, 'POLYGON ((-10 109, 90 109, 90 9, -10 9, -10 109))')");
stat.execute("insert into area values(2, 'POLYGON ((90 109, 190 109, 190 9, 90 9, 90 109))')");
stat.execute("insert into area values(3, 'POLYGON ((190 109, 290 109, 290 9, 190 9, 190 109))')");
stat.execute("insert into area values(4, 'POLYGON ((-10 9, 90 9, 90 -91, -10 -91, -10 9))')");
stat.execute("insert into area values(5, 'POLYGON ((90 9, 190 9, 190 -91, 90 -91, 90 9))')");
stat.execute("insert into area values(6, 'POLYGON ((190 9, 290 9, 290 -91, 190 -91, 190 9))')");
stat.execute("create table roads(idroad int primary key, the_geom geometry)");
stat.execute("insert into area values(1, " +
"'POLYGON ((-10 109, 90 109, 90 9, -10 9, -10 109))')");
stat.execute("insert into area values(2, " +
"'POLYGON ((90 109, 190 109, 190 9, 90 9, 90 109))')");
stat.execute("insert into area values(3, " +
"'POLYGON ((190 109, 290 109, 290 9, 190 9, 190 109))')");
stat.execute("insert into area values(4, " +
"'POLYGON ((-10 9, 90 9, 90 -91, -10 -91, -10 9))')");
stat.execute("insert into area values(5, " +
"'POLYGON ((90 9, 190 9, 190 -91, 90 -91, 90 9))')");
stat.execute("insert into area values(6, " +
"'POLYGON ((190 9, 290 9, 290 -91, 190 -91, 190 9))')");
stat.execute("create table roads(idRoad int primary key, the_geom geometry)");
stat.execute("create spatial index on roads(the_geom)");
stat.execute("insert into roads values(1, 'LINESTRING (27.65595463138 -16.728733459357244, 47.61814744801515 40.435727788279806)')");
stat.execute("insert into roads values(2, 'LINESTRING (17.674858223062415 55.861058601134246, 55.78449905482046 76.73062381852554)')");
stat.execute("insert into roads values(3, 'LINESTRING (68.48771266540646 67.65689981096412, 108.4120982986768 88.52646502835542)')");
stat.execute("insert into roads values(4, 'LINESTRING (177.3724007561437 18.65879017013235, 196.4272211720227 -16.728733459357244)')");
stat.execute("insert into roads values(5, 'LINESTRING (106.5973534971645 -12.191871455576518, 143.79962192816637 30.454631379962223)')");
stat.execute("insert into roads values(6, 'LINESTRING (144.70699432892252 55.861058601134246, 150.1512287334594 83.9896030245747)')");
stat.execute("insert into roads values(7, 'LINESTRING (60.321361058601155 -13.099243856332663, 149.24385633270325 5.955576559546344)')");
stat.execute("insert into roads values(1, " +
"'LINESTRING (27.65595463138 -16.728733459357244, 47.61814744801515 40.435727788279806)')");
stat.execute("insert into roads values(2, " +
"'LINESTRING (17.674858223062415 55.861058601134246, 55.78449905482046 76.73062381852554)')");
stat.execute("insert into roads values(3, " +
"'LINESTRING (68.48771266540646 67.65689981096412, 108.4120982986768 88.52646502835542)')");
stat.execute("insert into roads values(4, " +
"'LINESTRING (177.3724007561437 18.65879017013235, 196.4272211720227 -16.728733459357244)')");
stat.execute("insert into roads values(5, " +
"'LINESTRING (106.5973534971645 -12.191871455576518, 143.79962192816637 30.454631379962223)')");
stat.execute("insert into roads values(6, " +
"'LINESTRING (144.70699432892252 55.861058601134246, 150.1512287334594 83.9896030245747)')");
stat.execute("insert into roads values(7, " +
"'LINESTRING (60.321361058601155 -13.099243856332663, 149.24385633270325 5.955576559546344)')");
}
private void testSpatialIndexQueryMultipleTable() throws SQLException {
......@@ -330,28 +344,28 @@ public class TestSpatial extends TestBase {
}
private void testRoadAndArea(Statement stat) throws SQLException {
ResultSet rs = stat.executeQuery(
"select idarea, COUNT(idroad) roadscount " +
"from area, roads " +
"where area.the_geom && roads.the_geom " +
"GROUP BY idarea ORDER BY idarea");
"select idArea, COUNT(idRoad) roadCount " +
"from area, roads " +
"where area.the_geom && roads.the_geom " +
"GROUP BY idArea ORDER BY idArea");
assertTrue(rs.next());
assertEquals(1, rs.getInt("idarea"));
assertEquals(3, rs.getInt("roadscount"));
assertEquals(1, rs.getInt("idArea"));
assertEquals(3, rs.getInt("roadCount"));
assertTrue(rs.next());
assertEquals(2, rs.getInt("idarea"));
assertEquals(4, rs.getInt("roadscount"));
assertEquals(2, rs.getInt("idArea"));
assertEquals(4, rs.getInt("roadCount"));
assertTrue(rs.next());
assertEquals(3, rs.getInt("idarea"));
assertEquals(1, rs.getInt("roadscount"));
assertEquals(3, rs.getInt("idArea"));
assertEquals(1, rs.getInt("roadCount"));
assertTrue(rs.next());
assertEquals(4, rs.getInt("idarea"));
assertEquals(2, rs.getInt("roadscount"));
assertEquals(4, rs.getInt("idArea"));
assertEquals(2, rs.getInt("roadCount"));
assertTrue(rs.next());
assertEquals(5, rs.getInt("idarea"));
assertEquals(3, rs.getInt("roadscount"));
assertEquals(5, rs.getInt("idArea"));
assertEquals(3, rs.getInt("roadCount"));
assertTrue(rs.next());
assertEquals(6, rs.getInt("idarea"));
assertEquals(1, rs.getInt("roadscount"));
assertEquals(6, rs.getInt("idArea"));
assertEquals(1, rs.getInt("roadCount"));
assertFalse(rs.next());
rs.close();
}
......@@ -365,31 +379,31 @@ public class TestSpatial extends TestBase {
createTestTable(stat);
Savepoint sp = conn.setSavepoint();
// Remove a row but do not commit
stat.execute("delete from roads where idroad=7");
stat.execute("delete from roads where idRoad=7");
// Check if index is updated
ResultSet rs = stat.executeQuery(
"select idarea, COUNT(idroad) roadscount " +
"from area, roads " +
"where area.the_geom && roads.the_geom " +
"GROUP BY idarea ORDER BY idarea");
"select idArea, COUNT(idRoad) roadCount " +
"from area, roads " +
"where area.the_geom && roads.the_geom " +
"GROUP BY idArea ORDER BY idArea");
assertTrue(rs.next());
assertEquals(1, rs.getInt("idarea"));
assertEquals(3, rs.getInt("roadscount"));
assertEquals(1, rs.getInt("idArea"));
assertEquals(3, rs.getInt("roadCount"));
assertTrue(rs.next());
assertEquals(2, rs.getInt("idarea"));
assertEquals(4, rs.getInt("roadscount"));
assertEquals(2, rs.getInt("idArea"));
assertEquals(4, rs.getInt("roadCount"));
assertTrue(rs.next());
assertEquals(3, rs.getInt("idarea"));
assertEquals(1, rs.getInt("roadscount"));
assertEquals(3, rs.getInt("idArea"));
assertEquals(1, rs.getInt("roadCount"));
assertTrue(rs.next());
assertEquals(4, rs.getInt("idarea"));
assertEquals(1, rs.getInt("roadscount"));
assertEquals(4, rs.getInt("idArea"));
assertEquals(1, rs.getInt("roadCount"));
assertTrue(rs.next());
assertEquals(5, rs.getInt("idarea"));
assertEquals(2, rs.getInt("roadscount"));
assertEquals(5, rs.getInt("idArea"));
assertEquals(2, rs.getInt("roadCount"));
assertTrue(rs.next());
assertEquals(6, rs.getInt("idarea"));
assertEquals(1, rs.getInt("roadscount"));
assertEquals(6, rs.getInt("idArea"));
assertEquals(1, rs.getInt("roadCount"));
assertFalse(rs.next());
rs.close();
conn.rollback(sp);
......@@ -400,10 +414,10 @@ public class TestSpatial extends TestBase {
}
}
/**
* Test the in the in-memory spatial index
*/
/**
* Test the in the in-memory spatial index
*/
private void testMemorySpatialIndex() throws SQLException {
deleteDb("spatialIndex");
Connection conn = getConnection("spatialIndex");
......@@ -413,42 +427,43 @@ public class TestSpatial extends TestBase {
stat.execute("create spatial index idx_test_polygon on test(polygon)");
stat.execute("insert into test values(1, 'POLYGON ((1 1, 1 2, 2 2, 1 1))')");
ResultSet rs;
// an query that can not possibly return a result
rs = stat.executeQuery("select * from test " +
rs = stat.executeQuery("select * from test " +
"where polygon && 'POLYGON ((1 1, 1 2, 2 2, 1 1))'::Geometry " +
"and polygon && 'POLYGON ((10 10, 10 20, 20 20, 10 10))'::Geometry");
assertFalse(rs.next());
rs = stat.executeQuery(
"explain select * from test " +
"explain select * from test " +
"where polygon && 'POLYGON ((1 1, 1 2, 2 2, 1 1))'::Geometry");
rs.next();
assertContains(rs.getString(1), "/* PUBLIC.IDX_TEST_POLYGON: POLYGON &&");
int todo;
// TODO equality should probably also use the spatial index
// rs = stat.executeQuery("explain select * from test where polygon = 'POLYGON ((1 1, 1 2, 2 2, 1 1))'");
// rs = stat.executeQuery("explain select * from test " +
// "where polygon = 'POLYGON ((1 1, 1 2, 2 2, 1 1))'");
// rs.next();
// assertContains(rs.getString(1), "/* PUBLIC.IDX_TEST_POLYGON: POLYGON =");
// these queries actually have no meaning in the context of a spatial index, but
// these queries actually have no meaning in the context of a spatial index, but
// check them anyhow
stat.executeQuery("select * from test where polygon > 'POLYGON ((1 1, 1 2, 2 2, 1 1))'::Geometry");
stat.executeQuery("select * from test where polygon < 'POLYGON ((1 1, 1 2, 2 2, 1 1))'::Geometry");
rs = stat.executeQuery(
"select * from test " +
"select * from test " +
"where intersects(polygon, 'POLYGON ((1 1, 1 2, 2 2, 1 1))')");
assertTrue(rs.next());
rs = stat.executeQuery(
"select * from test " +
"select * from test " +
"where intersects(polygon, 'POINT (1 1)')");
assertTrue(rs.next());
rs = stat.executeQuery(
"select * from test " +
"select * from test " +
"where intersects(polygon, 'POINT (0 0)')");
assertFalse(rs.next());
......@@ -465,10 +480,11 @@ public class TestSpatial extends TestBase {
Connection conn = getConnection("spatialIndex");
try {
Statement stat = conn.createStatement();
stat.execute("CREATE ALIAS T_GEOMFROMTEXT FOR \"" + TestSpatial.class.getName() + ".geomFromText\"");
stat.execute("CREATE ALIAS T_GEOM_FROM_TEXT FOR \"" + TestSpatial.class.getName() + ".geomFromText\"");
stat.execute("create table test(id int primary key auto_increment, the_geom geometry)");
stat.execute("insert into test(the_geom) values(T_GEOMFROMTEXT('POLYGON ((62 48, 84 48, 84 42, 56 34, 62 48))',1488))");
stat.execute("DROP ALIAS T_GEOMFROMTEXT");
stat.execute("insert into test(the_geom) values(" +
"T_GEOM_FROM_TEXT('POLYGON ((62 48, 84 48, 84 42, 56 34, 62 48))',1488))");
stat.execute("DROP ALIAS T_GEOM_FROM_TEXT");
ResultSet rs = stat.executeQuery("select the_geom from test");
assertTrue(rs.next());
assertEquals("POLYGON ((62 48, 84 48, 84 42, 56 34, 62 48))", rs.getObject(1).toString());
......@@ -486,13 +502,13 @@ public class TestSpatial extends TestBase {
Connection conn = getConnection("spatialIndex");
try {
Statement stat = conn.createStatement();
stat.execute("CREATE ALIAS T_RANDOM_GEOM_TABLE FOR \"" +
stat.execute("CREATE ALIAS T_RANDOM_GEOM_TABLE FOR \"" +
TestSpatial.class.getName() + ".getRandomGeometryTable\"");
stat.execute(
"create table test as " +
"create table test as " +
"select * from T_RANDOM_GEOM_TABLE(42,20,-100,100,-100,100,4)");
stat.execute("DROP ALIAS T_RANDOM_GEOM_TABLE");
ResultSet rs = stat.executeQuery("select count(*) cpt from test");
ResultSet rs = stat.executeQuery("select count(*) from test");
assertTrue(rs.next());
assertEquals(20, rs.getInt(1));
} finally {
......@@ -503,7 +519,7 @@ public class TestSpatial extends TestBase {
/**
* Generate a result set with random geometry data.
*
*
* @param seed the random seed
* @param rowCount the number of rows
* @param minX the smallest x
......@@ -514,21 +530,21 @@ public class TestSpatial extends TestBase {
* @return a result set
*/
public static ResultSet getRandomGeometryTable(
final long seed, final long rowCount,
final long seed, final long rowCount,
final double minX, final double maxX,
final double minY, final double maxY, final double maxLength) {
SimpleResultSet rs = new SimpleResultSet(new SimpleRowSource() {
private final Random random = new Random(seed);
private int currentRow;
@Override
public Object[] readRow() throws SQLException {
if (currentRow++ < rowCount) {
return new Object[] {
getRandomGeometry(random,
minX, maxX, minY, maxY, maxLength) };
return new Object[] {
getRandomGeometry(random,
minX, maxX, minY, maxY, maxLength) };
}
return null;
}
......@@ -549,9 +565,9 @@ public class TestSpatial extends TestBase {
/**
* Convert the text to a geometry object.
*
* @param text Geometry in Well Known Text
* @param srid Projection ID
*
* @param text the geometry as a Well Known Text
* @param srid the projection id
* @return Geometry object
*/
public static Geometry geomFromText(String text, int srid) throws SQLException {
......@@ -564,5 +580,5 @@ public class TestSpatial extends TestBase {
throw new SQLException(ex);
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论