Question

I have to deal with three tables having geometries.

DECLARE
  CURSOR c1 IS
    SELECT
      store_number,
      0 AS total_area,
      0 AS bg_id,
      0 AS store_geom
    FROM table1 WHERE client_id = 1 AND org_id = 1;

  TYPE c1_tab_type IS TABLE OF c1%ROWTYPE;
  c1_list c1_tab_type;

BEGIN

  FOR r1 IN c1
  LOOP

    SELECT bg_id, store_number, total_area
    BULK COLLECT INTO c1_list
    FROM (
        SELECT
          bg_id,
          store_number,
          b.geometry                                                       store_geom,
          ((sdo_geom.SDO_AREA(sdo_geom.SDO_INTERSECTION(a.geometry, b.geometry, 0.005), 0.005, 'unit=sq_mile') /
            sdo_geom.SDO_AREA((a.geometry), 0.005, 'unit=sq_mile')) * 100) total_area
        FROM table2 a, table1 b
        WHERE store_number != r1.store_number
              AND sdo_relate(a.geometry, b.geometry, 'mask=anyinteract') = 'TRUE');

    IF total_area = 100 THEN
      FOR i IN 1..c1_list.count LOOP
        INSERT INTO temp_prop_area_100 VALUES c1_list(i);
      END LOOP;
    ELSE IF
      FOR i IN 1..c1_list.count LOOP
        INSERT INTO temp_Prop_area_block VALUES c1_list(i);
      END LOOP;
    END IF;

  END LOOP;
END;

So I select a record(the biggest geometry of all three tables) from table 1 and overlay over smaller polygons from table 2. I get the list of the overlapping geometries from table 2. This may have a few of them intersecting and a few of them completely under the polygon from table 1.

Then I want to put them in to two different tables . The ones that are completely under in temp_prop_area and the ones which intersect in temp_prop_area_block.

Now the issue I am having is, when I get the id's for all intersecting polygons(temp_prop_area_block) I want to overlay each of this polygon over the polygons from table 3 which area smaller polygons.And similarly find out which polygons do they intesect and what is the area of intersection.

The polygon size are in this order polygons from table 1 as the biggest, the from table 2 or temp_prop_area_block and then the polygons from table 3.

Was it helpful?

Solution

You have a couple of options. total_area doesn't exist outside the record type you've defined, so you can't use it in an if where you've shown, but you can just adjust that slightly:

...
                  For i in 1..c1_list.count loop
                       if c1_list(i).total_area=100
                          then                   
                            insert into temp_Prop_area_100
                            values c1_list(i);
                       else
                            insert into temp_Prop_area_block
                            values c1_list(i);
                       end if;
                  End Loop;
            End Loop;
End;

Or you can split your list into two:

   Type C1_TAB_TYPE is table of c1%ROWTYPE;      
   c1_list c1_TAB_TYPE;
   c1_list_100 c1_TAB_TYPE;
   c1_list_block c1_TAB_TYPE;
...
                  For i in 1..c1_list.count loop
                       if c1_list(i).total_area=100
                          then                   
                            c1_list_100.extend();
                            c1_list_100(c1_list_100.last) := c1_list(i);
                       else
                            c1_list_block.extend();
                            c1_list_block(c1_list_block.last) := c1_list(i);
                       end if;
                  End Loop;

                  For i in 1..c1_list_100.count loop
                       insert into temp_Prop_area_100
                       values c1_list_100(i);
                  End Loop;
                  For i in 1..c1_list_block.count loop
                       insert into temp_Prop_area_block
                       values c1_list_block(i);
                  End Loop;
            End Loop;
End;

Which looks like its adding extra complexity for not much gain, which is maybe true if you continue to use individual inserts. But that also allows you to use the forall syntax mentioned in a previous answer:

    Type C1_TAB_TYPE is table of temp_prop_area%ROWTYPE;      
...
                  Forall i in 1..c1_list_100.count
                       insert into temp_Prop_area_100
                       values c1_list_100(i);
                  Forall i in 1..c1_list_block.count
                       insert into temp_Prop_area_block
                       values c1_list_block(i);
            End Loop;
End;

You could also do separate select ... bulk collect statements into the two lists, with different filters, but that involves hitting the tables twice and is likely to be less efficient.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top