def check_coverage(img_src, locations, neighborhood = 1, plot_id_name = 'plot_id'): u""" Check whether the given locations have valid data values in the specified raster dataset. Optionally a specified neighborhood can be checked for valid data values. """ # extracting raster values for locations and the given neighborhood spectra = gdal_utils.extract_spectra(img_src, locations, neighborhood) coverage = dict() for spectrum, location in zip(spectra, locations): # retrieving plot id plot_id = location['attributes'][plot_id_name] # if there are no raster values at all or the mean value of the # neighborhood does not equal 1 then the location has not exclusively # valid data values if type(spectrum) is DictionaryType: if spectrum is None or spectrum['mean'] != 1: coverage[plot_id] = 0 else: coverage[plot_id] = 1 else: if spectrum == 1: coverage[plot_id] = 1 else: coverage[plot_id] = 0 return coverage
def extract_spectra(self, description_field='', verbose=True): u""" Extract spectra from source imagery and creates well-defined spectrum objects. """ # extracting spectra as simple lists spectra = gdal_utils.extract_spectra(self.img_src, self.extract_locations, neighborhood=self.neighborhood, verbose=verbose, bad_bands=self.bad_bands, scale_factor=self.slope, nb_type=self.neighborhood_type) # converting lists to spectrum objects # defining list of all extracted spectra # iterating over location/spectrum pairs for cp, sp in zip(self.extract_locations, spectra): # retrieving location id if cp.has_key('attributes'): additional_attributes = cp['attributes'].keys() additional_attributes.remove(self.loc_id_field) loc_id = cp[self.loc_id_field] # creating new spectrum object using location id and according coordinates spectrum = Spectrum(loc_id, (cp['x'], cp['y'])) for aa in additional_attributes: spectrum.set_attribute(aa, cp['attributes'][aa]) spectrum.set_neighborhood(self.neighborhood) spectrum.set_neighborhood_type(self.neighborhood_type) spectrum.set_source(self.img_id) if description_field: spectrum.set_description(cp[description_field]) # adding values to spectrum for gb, val in zip(self.good_bands, sp): spectrum.set_value(gb, val) # adding values of bad bands to spectrum for bb in self.bad_bands: spectrum.set_invalid(bb) # adding current spectrum to list of all extracted spectra self.spectra.append(spectrum)
def extract_spectra(self, description_field = '', verbose = True): u""" Extract spectra from source imagery and creates well-defined spectrum objects. """ # extracting spectra as simple lists spectra = gdal_utils.extract_spectra(self.img_src, self.extract_locations, neighborhood = self.neighborhood, verbose = verbose, bad_bands = self.bad_bands, scale_factor = self.slope, nb_type = self.neighborhood_type) # converting lists to spectrum objects # defining list of all extracted spectra # iterating over location/spectrum pairs for cp, sp in zip(self.extract_locations, spectra): # retrieving location id if cp.has_key('attributes'): additional_attributes = cp['attributes'].keys() additional_attributes.remove(self.loc_id_field) loc_id = cp[self.loc_id_field] # creating new spectrum object using location id and according coordinates spectrum = Spectrum(loc_id, (cp['x'], cp['y'])) for aa in additional_attributes: spectrum.set_attribute(aa, cp['attributes'][aa]) spectrum.set_neighborhood(self.neighborhood) spectrum.set_neighborhood_type(self.neighborhood_type) spectrum.set_source(self.img_id) if description_field: spectrum.set_description(cp[description_field]) # adding values to spectrum for gb, val in zip(self.good_bands, sp): spectrum.set_value(gb, val) # adding values of bad bands to spectrum for bb in self.bad_bands: spectrum.set_invalid(bb) # adding current spectrum to list of all extracted spectra self.spectra.append(spectrum)
# setting up a list of plots that are linked with the current strip strip_locations = list() # setting up the output for current strip id strip_output = list() # retrieving plots that are linked with the current strip for cp in sorted(cached_plots, key=itemgetter('attributes')): plot_id = cp['attributes']['plot_id'] if plot_id in image_plt_links[strip_id]: strip_locations.append(cp) # extracting spectra from the current image strip for all plots that # are linked with it # bands that have been regarded bad are ignored spectra = gdal_utils.extract_spectra(image_data[strip_id], strip_locations, verbose=True, neighborhood=neighborhood, bad_bands=bad_bands) # as there are as much plots for the current strip as there are spectra # we can simultaneously iterate over them for strip_location, spectrum in zip(strip_locations, spectra): plot_id = strip_location['attributes']['plot_id'] # checking whether were in some nxn-neighborhood, i.e. retrieved # values from a group of pixels if neighborhood > 1: single_band_output = list() # retrieving raw values and basic descriptive statistics for band_sp in spectrum: raw = "\t".join([str(f) for f in band_sp['raw']]) mean = str(band_sp['mean'])
tgt_file = "".join((re.search("(.+)_\d_", os.path.basename(image_data[strip_id])).group(1), '_spectra.txt')) # setting up a list of plots that are linked with the current strip strip_locations = list() # setting up the output for current strip id strip_output = list() # retrieving plots that are linked with the current strip for cp in sorted(cached_plots, key = itemgetter('attributes')): plot_id = cp['attributes']['plot_id'] if plot_id in image_plt_links[strip_id]: strip_locations.append(cp) # extracting spectra from the current image strip for all plots that # are linked with it # bands that have been regarded bad are ignored spectra = gdal_utils.extract_spectra(image_data[strip_id], strip_locations, verbose = True, neighborhood = neighborhood, bad_bands = bad_bands) # as there are as much plots for the current strip as there are spectra # we can simultaneously iterate over them for strip_location, spectrum in zip(strip_locations, spectra): plot_id = strip_location['attributes']['plot_id'] # checking whether were in some nxn-neighborhood, i.e. retrieved # values from a group of pixels if neighborhood > 1: single_band_output = list() # retrieving raw values and basic descriptive statistics for band_sp in spectrum: raw = "\t".join([str(f) for f in band_sp['raw']]) mean = str(band_sp['mean']) std_dev = str(band_sp['std_dev']) # putting it all together in output text for a single band