Ejemplo n.º 1
0
 def test_get_intersection(self):
     pol1 = self.generate_rect_geojson([0,0,2,1])
     pol2 = self.generate_rect_geojson([1,0,3,1])
     shp1 = gis_util.geojson_to_shape(pol1)
     shp2 = gis_util.geojson_to_shape(pol2)
     intersection = gis_util.get_intersection(shp1, shp2)
     gis_util.shape_to_wkt(intersection)
Ejemplo n.º 2
0
    def get_cracked_cells_for_stat_area(self, stat_area):
        cracked_cells = []
        candidates = self.cell_spatial_hash.items_for_rect(stat_area.mbr)
        for icell in candidates:
            intersection = gis_util.get_intersection(stat_area.shape, icell.shape)
            if not intersection:
                continue

            intersection_area = gis_util.get_shape_area(intersection)
            pct_area = intersection_area/icell.area

            # Set cracked cell values in proportion to percentage
            # of parent cell's area.
            ccell_keyed_values = {}
            icell_keyed_values = self.c_values[icell.id]
            for effort_key, icell_values in icell_keyed_values.items():
                ccell_values = ccell_keyed_values.setdefault(effort_key, {})
                for attr, value in icell_values.items():
                    ccell_values[attr] = pct_area * value

            cracked_cells.append(models.CrackedCell(
                parent_cell=icell,
                area=intersection_area,
                keyed_values=ccell_keyed_values,
            ))
        return cracked_cells
Ejemplo n.º 3
0
 def get_stat_area_for_pos(self, lat, lon):
     pos_wkt = 'POINT(%s %s)' % (lon, lat)
     pnt_shp = gis_util.wkt_to_shape(pos_wkt)
     candidates = self.sa_spatial_hash.items_for_point((lon,lat))
     for c in candidates:
         if gis_util.get_intersection(c.shape, pnt_shp):
             return c
     return None
Ejemplo n.º 4
0
 def get_cell_for_pos(self, lat, lon):
     """
     Get cell which contains given point, via
     spatial hash.
     """
     pos_wkt = 'POINT(%s %s)' % (lon, lat)
     pnt_shp = gis_util.wkt_to_shape(pos_wkt)
     candidates = self.cell_spatial_hash.items_for_point((lon,lat))
     for c in candidates:
         if gis_util.get_intersection(c.shape, pnt_shp):
             return c
     return None
Ejemplo n.º 5
0
    def post_process_cells(self, log_interval=1000):
        base_msg = 'Calculating cell compositions...'
        self.logger.info(base_msg)
        logger = self.get_section_logger('habitat_areas', base_msg)

        num_cells = len(self.cells)
        counter = 0
        for cell in self.cells.values():
            counter += 1
            if (counter % log_interval) == 0:
                logger.info(
                    " %d of %d (%.1f%%)" %
                    (counter, num_cells, 1.0 * counter / num_cells * 100))

            composition = {}
            cell.depth = 0

            # Get candidate intersecting habitats.
            candidate_habs = self.habs_spatial_hash.items_for_rect(cell.mbr)
            for hab in candidate_habs:
                intersection = gis_util.get_intersection(cell.shape, hab.shape)
                if not intersection:
                    continue
                intersection_area = gis_util.get_shape_area(
                    intersection,
                    target_crs=self.geographic_crs,
                )
                hab_key = (
                    hab.substrate_id,
                    hab.energy_id,
                )
                pct_area = intersection_area / cell.area
                composition[hab_key] = composition.get(hab_key, 0) + pct_area
                cell.depth += pct_area * hab.depth

            cell.habitat_composition = composition

            # Convert cell area to km^2.
            cell.area = cell.area / (1000.0**2)

            self.dao.save(cell, commit=False)
        self.dao.commit()
Ejemplo n.º 6
0
    def post_process_cells(self, log_interval=1000):
        base_msg = 'Calculating cell compositions...'
        self.logger.info(base_msg)
        logger = self.get_section_logger('habitat_areas', base_msg)

        num_cells = len(self.cells)
        counter = 0
        for cell in self.cells.values():
            counter += 1
            if (counter % log_interval) == 0:
                logger.info(" %d of %d (%.1f%%)" % (
                    counter, num_cells, 1.0 * counter/num_cells* 100))

            composition = {}
            cell.depth = 0

            # Get candidate intersecting habitats.
            candidate_habs = self.habs_spatial_hash.items_for_rect(cell.mbr)
            for hab in candidate_habs:
                intersection = gis_util.get_intersection(cell.shape, hab.shape)
                if not intersection:
                    continue
                intersection_area = gis_util.get_shape_area(
                    intersection,
                    target_crs=self.geographic_crs,
                )
                hab_key = (hab.substrate_id, hab.energy_id,)
                pct_area = intersection_area/cell.area
                composition[hab_key] = composition.get(hab_key, 0) + pct_area
                cell.depth += pct_area * hab.depth

            cell.habitat_composition = composition

            # Convert cell area to km^2.
            cell.area = cell.area/(1000.0**2)

            self.dao.save(cell, commit=False)
        self.dao.commit()