<filename>geoprocessing/handlers/ConnectionDescriptor.py
from collections import defaultdict
from itertools import product
from typing import Sequence

from geoprocessing import pipe
from geoprocessing.coord.geolib import prepare_for_multipolygon
from geoprocessing.entities import GeojsonFeature, GeneralDescriptor, GeogineObj

from shapely.geometry import Polygon, MultiPolygon


class ConnectionDescriptor(pipe.BatchProcesser):
    requires = GeojsonFeature
    provides = Sequence[GeogineObj]

    def process_all(self, geojsons: Sequence[GeojsonFeature], dtype):
        dd = {}
        arr = []
        for g in geojsons:
            if g.geom_type == 'Polygon':
                dd[g.id] = (g.geo, Polygon(g.coords[0]))
            elif g.geom_type == 'MultiPolygon':
                dd[g.id] = (g.geo, MultiPolygon(prepare_for_multipolygon(g.coords)))
            arr.append(g.geo)

        # todo: decide how to group Geojson into parameter arrays?
        # todo      filter attribute (epsg?)

        # create a cache of neighboring countries
        c_cache = self.conf.get('connectivity.country_cache')
        cc = {}
        if c_cache:
            cc = defaultdict(set)
            cc.update(self.conf['country_cache'])
        #conn = defaultdict(set)

        # based on country cache
        for (id1, (geo1, geom1)), (id2, (geo2, geom2)) in product(dd.items(), dd.items()):
            if id1 == id2:
                continue
            elif id2 in [id1]:
                continue
            elif c_cache and geo1.iso != geo2.iso and not geo2.iso.lower() in cc[geo1.iso.lower()]:
                continue
            elif geom1.intersects(geom2):
                geo1.conn.add(id2)
                geo2.conn.add(id1)

        yield arr

<filename>geoprocessing/handlers/ConnectionDescriptor.py
from collections import defaultdict
from itertools import product
from typing import Sequence

from geoprocessing import pipe
from geoprocessing.coord.geolib import prepare_for_multipolygon
from geoprocessing.entities import GeojsonFeature, GeneralDescriptor, GeogineObj

from shapely.geometry import Polygon, MultiPolygon


class ConnectionDescriptor(pipe.BatchProcesser):
    requires = GeojsonFeature
    provides = Sequence[GeogineObj]

    def process_all(self, geojsons: Sequence[GeojsonFeature], dtype):
        dd = {}
        arr = []
        for g in geojsons:
            if g.geom_type == 'Polygon':
                dd[g.id] = (g.geo, Polygon(g.coords[0]))
            elif g.geom_type == 'MultiPolygon':
                dd[g.id] = (g.geo, MultiPolygon(prepare_for_multipolygon(g.coords)))
            arr.append(g.geo)

        # todo: decide how to group Geojson into parameter arrays?
        # todo      filter attribute (epsg?)

        # create a cache of neighboring countries
        c_cache = self.conf.get('connectivity.country_cache')
        cc = {}
        if c_cache:
            cc = defaultdict(set)
            cc.update(self.conf['country_cache'])
        #conn = defaultdict(set)

        # based on country cache
        for (id1, (geo1, geom1)), (id2, (geo2, geom2)) in product(dd.items(), dd.items()):
            if id1 == id2:
                continue
            elif id2 in [id1]:
                continue
            elif c_cache and geo1.iso != geo2.iso and not geo2.iso.lower() in cc[geo1.iso.lower()]:
                continue
            elif geom1.intersects(geom2):
                geo1.conn.add(id2)
                geo2.conn.add(id1)

        yield arr

# todo: decide how to group Geojson into parameter arrays? # todo      filter attribute (epsg?) # create a cache of neighboring countries #conn = defaultdict(set) # based on country cache