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

Formatting.

上级 dc4f999e
......@@ -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
......@@ -140,12 +141,11 @@ public class TestSpatial extends TestBase {
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
// 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 bbox = ValueGeometry.get("POLYGON ((" +
Envelope boundingBox = ValueGeometry.get("POLYGON ((" +
"301804.1049793153 2251719.1222191923, " +
"301804.1049793153 2254747.2888244865, " +
"304646.87362918374 2254747.2888244865, " +
......@@ -153,13 +153,14 @@ public class TestSpatial extends TestBase {
"301804.1049793153 2251719.1222191923))")
.getGeometry().getEnvelopeInternal();
// Create overlap test bounding box
String testBBoxString = "POLYGON ((" +
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(testBBoxString).getGeometry().getEnvelopeInternal();
Envelope testBBox = ValueGeometry.get(testBoundingBoxString).
getGeometry().getEnvelopeInternal();
PreparedStatement ps = conn.prepareStatement(
"insert into test(poly) values (?)");
......@@ -168,8 +169,8 @@ public class TestSpatial extends TestBase {
for (int i = 1; i <= size; i++) {
Geometry geometry = getRandomGeometry(
geometryRand,
bbox.getMinX(), bbox.getMaxX(),
bbox.getMinY(), bbox.getMaxY(), 200);
boundingBox.getMinX(), boundingBox.getMaxX(),
boundingBox.getMinY(), boundingBox.getMaxY(), 200);
ps.setObject(1, geometry);
ps.execute();
ResultSet keys = ps.getGeneratedKeys();
......@@ -183,9 +184,8 @@ public class TestSpatial extends TestBase {
// 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 = conn.prepareStatement("select id from test where poly && ?::Geometry");
ps.setString(1, testBoundingBoxString);
ResultSet rs = ps.executeQuery();
long found = 0;
while (rs.next()) {
......@@ -198,6 +198,7 @@ public class TestSpatial extends TestBase {
assertTrue(overlaps.isEmpty());
stat.execute("drop table if exists test");
}
private void testOverlap() throws SQLException {
deleteDb("spatial");
Connection conn = getConnection("spatial");
......@@ -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)");
......@@ -256,7 +257,7 @@ public class TestSpatial extends TestBase {
return;
}
conn = getConnection("spatial_pers");
conn = getConnection("spatialPersistent");
try {
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(
......@@ -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 " +
"select idArea, COUNT(idRoad) roadCount " +
"from area, roads " +
"where area.the_geom && roads.the_geom " +
"GROUP BY idarea ORDER BY idarea");
"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 " +
"select idArea, COUNT(idRoad) roadCount " +
"from area, roads " +
"where area.the_geom && roads.the_geom " +
"GROUP BY idarea ORDER BY idarea");
"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);
......@@ -428,7 +442,8 @@ public class TestSpatial extends TestBase {
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 =");
......@@ -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());
......@@ -492,7 +508,7 @@ public class TestSpatial extends TestBase {
"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 {
......@@ -550,8 +566,8 @@ 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 {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论