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
def ingest_cells(self, parent_logger=None, limit=None): self.cells = {} self.cell_spatial_hash = SpatialHash(cell_size=.1) self.c_values = {} logger = self.get_logger_logger( name='cell_ingest', base_msg='Ingesting cells...', parent_logger=parent_logger ) Ingestor( reader=ShapefileReader(shp_file=self.grid_path, reproject_to='EPSG:4326'), processors=[ ClassMapper( clazz=models.Cell, mappings=[{'source': 'ID', 'target': 'id'}, {'source': '__shape', 'target': 'shape'},], ), DictWriter(dict_=self.cells, key_func=lambda c: c.id), ], logger=logger, limit=limit ).ingest() # Calculate cell areas and add cells to spatial hash, # and initialize c_values. for cell in self.cells.values(): cell.area = gis_util.get_shape_area(cell.shape) cell.mbr = gis_util.get_shape_mbr(cell.shape) self.cell_spatial_hash.add_rect(cell.mbr, cell) self.c_values[cell.id] = {}
def generate_efforts(cells=[], gears=[], t0=0, tf=1, dt=1): efforts = [] for cell in cells: cell_area = gis_util.get_shape_area( gis_util.wkb_to_shape(cell.geom.geom_wkb)) for t in range(t0, tf, dt): for g in gears: efforts.append(models.Effort( cell_id=cell.id, gear_id=g.id, time=t, a=cell_area/len(gears), )) return efforts
def generate_efforts(cells=[], gears=[], t0=0, tf=1, dt=1): efforts = [] for cell in cells: cell_area = gis_util.get_shape_area( gis_util.wkb_to_shape(cell.geom.geom_wkb)) for t in range(t0, tf, dt): for g in gears: efforts.append( models.Effort( cell_id=cell.id, gear_id=g.id, time=t, a=cell_area / len(gears), )) return efforts
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()
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()
def test_get_shape_area(self): geojson = self.generate_rect_geojson() shape = gis_util.geojson_to_shape(geojson) area = gis_util.get_shape_area(shape)
def add_area_mbr(self, data=None, **kwargs): data.area = gis_util.get_shape_area( data.shape, target_crs=self.geographic_crs) data.mbr = gis_util.get_shape_mbr(data.shape) return data
def add_area_mbr(self, data=None, **kwargs): data.area = gis_util.get_shape_area(data.shape, target_crs=self.geographic_crs) data.mbr = gis_util.get_shape_mbr(data.shape) return data