def test_subtract_partition(self):
     from qc_tool.vector.helper import GapTable
     from qc_tool.vector.helper import PartitionedLayer
     cursor = self.params["connection_manager"].get_connection().cursor()
     cursor.execute(
         "CREATE TABLE gap_mylayer (fid SERIAL PRIMARY KEY, geom geometry(Polygon, 4326));"
     )
     cursor.execute(
         "INSERT INTO gap_mylayer (geom) VALUES (ST_MakeEnvelope(0, 0, 1, 1, 4326)),"
         " (ST_MakeEnvelope(0, 2, 2, 3, 4326)),"
         " (ST_MakeEnvelope(0, 3, 2, 4, 4326));")
     cursor.execute(
         "CREATE TABLE interior_mylayer (partition_id integer, geom geometry(MultiPolygon, 4326));"
     )
     cursor.execute(
         "INSERT INTO interior_mylayer VALUES (1, ST_Multi(ST_MakeEnvelope(-1, -1, 10, 10, 4326))),"
         " (3, ST_Multi(ST_Union(ST_MakeEnvelope(0, 2, 1, 4, 4326),"
         " ST_MakeEnvelope(1, 3, 2, 4, 4326))));")
     partitioned_layer = PartitionedLayer(cursor.connection,
                                          "mylayer",
                                          "xfid",
                                          srid=4326)
     partitioned_layer._create_polygon_dump()
     gap_table = GapTable(partitioned_layer, "myboundary", None)
     gap_table._subtract_partition(3)
     cursor.execute(
         "SELECT fid, ST_AsText(geom) FROM gap_mylayer ORDER BY fid;")
     self.assertListEqual([(1, 'POLYGON((0 0,0 1,1 1,1 0,0 0))'),
                           (4, 'POLYGON((1 3,2 3,2 2,1 2,1 3))')],
                          cursor.fetchall())
 def test_split_features(self):
     from qc_tool.vector.helper import GapTable
     from qc_tool.vector.helper import PartitionedLayer
     cursor = self.params["connection_manager"].get_connection().cursor()
     cursor.execute(
         "CREATE TABLE gap_mylayer (fid SERIAL PRIMARY KEY, geom geometry(Polygon, 4326));"
     )
     cursor.execute(
         "INSERT INTO gap_mylayer (geom) VALUES (ST_MakeEnvelope(-6.8, -1.6, -1.2, -1.1, 4326)),"
         " (ST_Union(ST_Union(ST_MakeEnvelope(0.2, 0.2, 8, 1.8, 4326),"
         " ST_MakeEnvelope(0.2, 3.2, 9.2, 4.2, 4326)),"
         " ST_MakeEnvelope(0.1, 0.2, 5, 4.2, 4326)));")
     partitioned_layer = PartitionedLayer(cursor.connection,
                                          "mylayer",
                                          "xfid",
                                          srid=4326,
                                          max_vertices=5)
     partitioned_layer._create_polygon_dump()
     gap_table = GapTable(partitioned_layer, "myboundary", None)
     gap_table._create_split_geom()
     count = gap_table._split_features()
     self.assertEqual(1, count)
     cursor.execute(
         "SELECT fid, ST_AsText(geom) FROM gap_mylayer ORDER BY fid;")
     self.assertListEqual([
         (1,
          'POLYGON((-6.8 -1.6,-6.8 -1.1,-1.2 -1.1,-1.2 -1.6,-6.8 -1.6))'),
         (3,
          'POLYGON((5 4.2,5 3.2,5 1.8,5 0.2,0.2 0.2,0.1 0.2,0.1 4.2,0.2 4.2,5 4.2))'
          ), (4, 'POLYGON((5 0.2,5 1.8,8 1.8,8 0.2,5 0.2))'),
         (5, 'POLYGON((5 3.2,5 4.2,9.2 4.2,9.2 3.2,5 3.2))')
     ], cursor.fetchall())
 def test_fill_subpartitions(self):
     from qc_tool.vector.helper import PartitionedLayer
     cursor = self.connection.cursor()
     cursor.execute("CREATE TABLE partition_mylayer (partition_id integer,"
                    " superpartition_id integer,"
                    " num_vertices integer,"
                    " geom geometry(Polygon, 4326));")
     cursor.execute(
         "INSERT INTO partition_mylayer VALUES (2, 1, NULL, ST_MakeEnvelope(-10, 0, 0, 3, 4326)),"
         " (3, 1, NULL, ST_MakeEnvelope(0, 0, 10, 3, 4326)),"
         " (4, 3, NULL, ST_MakeEnvelope(0, 0, 5, 3, 4326)),"
         " (5, 3, NULL, ST_MakeEnvelope(5, 0, 10, 3, 4326));")
     cursor.execute(
         "CREATE TABLE feature_mylayer (fid integer, partition_id integer, geom geometry(Polygon, 4326));"
     )
     cursor.execute(
         "INSERT INTO feature_mylayer VALUES (1, 2, ST_MakeEnvelope(-5, 1, -3, 2, 4326)),"
         " (2, 3, ST_MakeEnvelope(4, 1, 7, 2, 4326)),"
         " (3, 3, ST_MakeEnvelope(5, 1, 6, 2, 4326));")
     partitioned_layer = PartitionedLayer(self.connection, "mylayer",
                                          "xfid")
     partitioned_layer._create_polygon_dump()
     partitioned_layer._fill_subpartitions()
     cursor.execute(
         "SELECT fid, partition_id, ST_AsText(geom) FROM feature_mylayer ORDER BY fid, partition_id;"
     )
     self.assertListEqual([(1, 2, "POLYGON((-5 1,-5 2,-3 2,-3 1,-5 1))"),
                           (2, 3, "POLYGON((4 1,4 2,7 2,7 1,4 1))"),
                           (2, 4, "POLYGON((4 1,4 2,5 2,5 1,4 1))"),
                           (2, 5, "POLYGON((5 2,7 2,7 1,5 1,5 2))"),
                           (3, 5, "POLYGON((5 1,5 2,6 2,6 1,5 1))")],
                          cursor.fetchall())
 def test_fill_initial_features(self):
     from qc_tool.vector.helper import PartitionedLayer
     partitioned_layer = PartitionedLayer(self.connection,
                                          "mylayer",
                                          "xfid",
                                          srid=4326)
     partitioned_layer._create_polygon_dump()
     partitioned_layer._create_feature_table()
     partitioned_layer._fill_initial_features(1)
     cursor = self.connection.cursor()
     cursor.execute(
         "SELECT fid, partition_id, ST_AsText(geom) FROM feature_mylayer ORDER BY fid;"
     )
     self.assertListEqual(
         [(1, 1, "POLYGON((-1.1 -2.2,-1.1 1,1 1,1 -2.2,-1.1 -2.2))"),
          (2, 1, "POLYGON((10 10,10 11.4,11.3 11.4,11.3 10,10 10))")],
         cursor.fetchall())
 def test_fill_initial_features(self):
     from qc_tool.vector.helper import GapTable
     from qc_tool.vector.helper import PartitionedLayer
     cursor = self.params["connection_manager"].get_connection().cursor()
     cursor.execute(
         "CREATE TABLE myboundary (geom geometry(Polygon, 4326));")
     cursor.execute(
         "INSERT INTO myboundary VALUES (ST_MakeEnvelope(0, 0, 1, 1, 4326)),"
         " (ST_MakeEnvelope(2, 2, 3, 3, 4326));")
     partitioned_layer = PartitionedLayer(cursor.connection,
                                          "mylayer",
                                          "xfid",
                                          srid=4326)
     partitioned_layer._create_polygon_dump()
     gap_table = GapTable(partitioned_layer, "myboundary", None)
     gap_table._create_gap_table()
     gap_table._fill_initial_features()
     cursor.execute(
         "SELECT fid, ST_AsText(geom) FROM gap_mylayer ORDER BY fid;")
     self.assertListEqual([(1, 'POLYGON((0 0,0 1,1 1,1 0,0 0))'),
                           (2, 'POLYGON((2 2,2 3,3 3,3 2,2 2))')],
                          cursor.fetchall())
 def test_split_geom(self):
     from qc_tool.vector.helper import GapTable
     from qc_tool.vector.helper import PartitionedLayer
     cursor = self.params["connection_manager"].get_connection().cursor()
     partitioned_layer = PartitionedLayer(cursor.connection,
                                          "mylayer",
                                          "xfid",
                                          srid=4326)
     partitioned_layer._create_polygon_dump()
     gap_table = GapTable(partitioned_layer, "myboundary", None)
     gap_table._create_split_geom()
     cursor.execute(
         "SELECT ST_AsText(split_geom(ST_MakeEnvelope(0.6, -1.1, 9.2, 6, 4326), 1.0));"
     )
     self.assertListEqual(
         [('POLYGON((0.6 6,5 6,5 -1.1,0.6 -1.1,0.6 6))', ),
          ('POLYGON((5 -1.1,5 6,9.2 6,9.2 -1.1,5 -1.1))', )],
         cursor.fetchall())
     cursor.execute(
         "SELECT ST_AsText(split_geom(ST_MakeEnvelope(0.2, 0.1, 0.6, 0.9, 4326), 1.0));"
     )
     self.assertListEqual([], cursor.fetchall())