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

Formatting.

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