def test_Grid(self): from utils.grid import latlon_to_grid, grid_to_latlon lats = [ 33.995833, -5.6625, -3.1375, 6.12083] lons = [-112.004167, 106.104167, -12.412541, 85.22916] for lat, lon in map(None, lats, lons): grid = latlon_to_grid(lat, lon) lat2, lon2 = grid_to_latlon(grid) self.assertAlmostEqual(lat, lat2, places=4) self.assertAlmostEqual(lon, lon2, places=4)
def _outputGeometryFromGridId(self, grid_id): [lat, lon] = grid_to_latlon(int(grid_id)) return QgsGeometry.fromRect( QgsRectangle( lon - DEFAULT_HALF_GRID_SIZE, lat - DEFAULT_HALF_GRID_SIZE, lon + DEFAULT_HALF_GRID_SIZE, lat + DEFAULT_HALF_GRID_SIZE, ) )
def _outputGeometryFromGridId(self, grid_id): [lat, lon] = grid_to_latlon(int(grid_id)) return QgsGeometry.fromRect(QgsRectangle(lon-DEFAULT_HALF_GRID_SIZE, lat-DEFAULT_HALF_GRID_SIZE, lon+DEFAULT_HALF_GRID_SIZE, lat+DEFAULT_HALF_GRID_SIZE))
@logAPICall def do_operation(self): """ perform create mapping scheme operation """ # input/output verification already performed during set input/ouput svy_layer = self.inputs[0].value # make sure input is correct # NOTE: these checks cannot be performed at set input time # because the data layer maybe is not loaded yet self._test_layer_loaded(svy_layer) total_features = svy_layer.dataProvider().featureCount() if total_features > MAX_FEATURES_IN_MEMORY: # use bsddb to store temporary lat/lon tmp_db_file = '%sdb_%s.db' % (self._tmp_dir, get_unique_filename()) db = bsddb.btopen(tmp_db_file, 'c') else: db = {} # tally statistics for each grid_id/building type combination tax_idx = layer_field_index(svy_layer, TAX_FIELD_NAME) for f in layer_features(svy_layer): geom = f.geometry() centroid = geom.centroid().asPoint() grid_id = latlon_to_grid(centroid.y(), centroid.x()) tax_str = str(f.attributeMap()[tax_idx].toString()) key = '%s %s' % (tax_str, grid_id) if db.has_key(key): db[key] = str(int(db[key]) + 1) # value as string required by bsddb else: db[key] = '1' # value as string required by bsddb # loop through all zones and assign mapping scheme # outputs exposure_layername = 'exp_%s' % get_unique_filename() exposure_file = '%s%s.shp' % (self._tmp_dir, exposure_layername) try: writer = QgsVectorFileWriter(exposure_file, "utf-8", self._fields, self._outputGeometryType(), self._crs, "ESRI Shapefile") f = QgsFeature() gid = 0 for key, val in db.iteritems(): (tax_str, grid_id) = key.split(' ') lon, lat = grid_to_latlon(int(grid_id)) f.setGeometry(self._outputGeometryFromGridId(grid_id)) f.addAttribute(0, QVariant(grid_id)) f.addAttribute(1, QVariant(lon)) f.addAttribute(2, QVariant(lat)) f.addAttribute(3, QVariant(tax_str)) f.addAttribute(4, QVariant('')) f.addAttribute(5, QVariant(val)) writer.addFeature(f) gid += 1 del writer, f except Exception as err: remove_shapefile(exposure_file) raise OperatorError("error creating exposure file: %s" % err, self.__class__) # load shapefile as layer exposure_layer = load_shapefile(exposure_file, exposure_layername) if not exposure_layer: raise OperatorError('Error loading exposure file %s' % (exposure_file), self.__class__) # store data in output self.outputs[0].value = exposure_layer
def do_operation(self): """ perform create mapping scheme operation """ # input/output verification already performed during set input/ouput svy_layer = self.inputs[0].value # make sure input is correct # NOTE: these checks cannot be performed at set input time # because the data layer maybe is not loaded yet self._test_layer_loaded(svy_layer) total_features = svy_layer.dataProvider().featureCount() if total_features > MAX_FEATURES_IN_MEMORY: # use bsddb to store temporary lat/lon tmp_db_file = '%sdb_%s.db' % (self._tmp_dir, get_unique_filename()) db = bsddb.btopen(tmp_db_file, 'c') else: db = {} # tally statistics for each grid_id/building type combination tax_idx = layer_field_index(svy_layer, TAX_FIELD_NAME) for f in layer_features(svy_layer): geom = f.geometry() centroid = geom.centroid().asPoint() grid_id = latlon_to_grid(centroid.y(), centroid.x()) tax_str = str(f.attributeMap()[tax_idx].toString()) key = '%s %s' % (tax_str, grid_id) if db.has_key(key): db[key] = str(int(db[key]) + 1) # value as string required by bsddb else: db[key] = '1' # value as string required by bsddb # loop through all zones and assign mapping scheme # outputs exposure_layername = 'exp_%s' % get_unique_filename() exposure_file = '%s%s.shp' % (self._tmp_dir, exposure_layername) try: writer = QgsVectorFileWriter(exposure_file, "utf-8", self._fields, self._outputGeometryType(), self._crs, "ESRI Shapefile") f = QgsFeature() gid = 0 for key, val in db.iteritems(): (tax_str, grid_id) = key.split(' ') lon, lat = grid_to_latlon(int(grid_id)) f.setGeometry(self._outputGeometryFromGridId(grid_id)) f.addAttribute(0, QVariant(grid_id)) f.addAttribute(1, QVariant(lon)) f.addAttribute(2, QVariant(lat)) f.addAttribute(3, QVariant(tax_str)) f.addAttribute(4, QVariant('')) f.addAttribute(5, QVariant(val)) writer.addFeature(f) gid += 1 del writer, f except Exception as err: remove_shapefile(exposure_file) raise OperatorError("error creating exposure file: %s" % err, self.__class__) # load shapefile as layer exposure_layer = load_shapefile(exposure_file, exposure_layername) if not exposure_layer: raise OperatorError( 'Error loading exposure file %s' % (exposure_file), self.__class__) # store data in output self.outputs[0].value = exposure_layer self.outputs[1].value = exposure_file