Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
    def extract_spectra(self):
        u"""
        Extract spectra from source imagery and creates well-defined spectrum
        objects.
        """
        for img_id in self.image_data:
            self.img_src = self.image_data[img_id]
            self.convert_vector_to_raster(self.img_src)

            curr_coordinates = dict()

            if self.coverage:
                for fid in self.img_coordinates:
                    loc_id = self.thematic_info[fid][self.roi_id]
                    if loc_id in self.coverage[img_id]:
                        curr_coordinates[fid] = self.img_coordinates[fid]
            else:
                curr_coordinates = self.img_coordinates

            if self.roi_type == "point":
                # extracting spectra as simple lists
                spectra = gdal_utils.extract_point_spectra(
                    self.img_src,
                    curr_coordinates,
                    bad_bands=self.bad_bands,
                    calibration=(self.slope, self.intercept),
                    context_type=self.context_type,
                    context_range=self.context_range,
                    verbose=self.verbosity,
                )
            elif self.roi_type == "polygon":
                # curr_coordinates = self.img_coordinates
                spectra = gdal_utils.extract_polygon_spectra(
                    self.img_src,
                    self.img_coordinates,
                    bad_bands=self.bad_bands,
                    calibration=(self.slope, self.intercept),
                    aggregate=self.aggregate,
                    verbose=self.verbosity,
                )
            elif self.roi_type == "line string":
                spectra = gdal_utils.extract_line_spectra(
                    self.img_src,
                    self.img_coordinates,
                    bad_bands=self.bad_bands,
                    calibration=(self.slope, self.intercept),
                    verbose=self.verbosity,
                )

            # converting dict of raw spectra to spectrum objects
            for fid in spectra:
                # retrieving basic thematic information for current fid
                roi_id = self.thematic_info[fid][self.roi_id]
                x = self.thematic_info[fid]["x"]
                y = self.thematic_info[fid]["y"]
                # retrieving additional attributes for current fid
                if self.thematic_info[fid].has_key("attributes"):
                    additional_attributes = self.thematic_info[fid]["attributes"].keys()
                # workaround to allow for subsequent iteration over a
                # single (aggregated polygon or simple point) spectrum
                if self.roi_type == "point" or self.aggregate:
                    spectra[fid] = [spectra[fid]]
                if self.roi_type == "line string":
                    cnt = 0
                # i = 0
                for sp in spectra[fid]:
                    # i += 1
                    # if not i % 20 == 0:
                    #    continue
                    # else:
                    #    i = 0
                    # creating new spectrum object using region of interest
                    # id and according coordinates
                    spectrum = Spectrum(roi_id, (x, y))
                    # copyying additional attributes
                    for aa in additional_attributes:
                        spectrum.set_attribute(aa, self.thematic_info[fid]["attributes"][aa])
                    if self.roi_type == "line string":
                        cnt += 1
                        spectrum.set_attribute("_count", cnt)
                    # setting context range and type to default values
                    spectrum.set_context_range(self.context_range)
                    spectrum.set_context_type(self.context_type)
                    spectrum.set_source(img_id + 1)
                    # copying values to spectrum object
                    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)
Ejemplo n.º 4
0
    def extract_spectra(self):
        u"""
        Extract spectra from source imagery and creates well-defined spectrum
        objects.
        """
        for img_id in self.image_data:
            self.img_src = self.image_data[img_id]
            self.convert_vector_to_raster(self.img_src)
            
            curr_coordinates = dict()
            
            if self.coverage:
                for fid in self.img_coordinates:
                    loc_id = self.thematic_info[fid][self.roi_id]
                    if loc_id in self.coverage[img_id]:
                        curr_coordinates[fid] = self.img_coordinates[fid]
            else:
                curr_coordinates = self.img_coordinates
                
            if self.roi_type == 'point':
                # extracting spectra as simple lists
                spectra = gdal_utils.extract_point_spectra(self.img_src, curr_coordinates,
                    bad_bands = self.bad_bands, calibration = (self.slope, self.intercept),
                    context_type = self.context_type, context_range = self.context_range,
                    verbose = self.verbosity)
            elif self.roi_type == 'polygon':
                #curr_coordinates = self.img_coordinates
                spectra = gdal_utils.extract_polygon_spectra(self.img_src, self.img_coordinates,
                    bad_bands = self.bad_bands, calibration = (self.slope, self.intercept),
                    aggregate = self.aggregate, verbose = self.verbosity)
            elif self.roi_type == 'line string':
                spectra = gdal_utils.extract_line_spectra(self.img_src, self.img_coordinates,
                    bad_bands = self.bad_bands, calibration = (self.slope, self.intercept),
                    verbose = self.verbosity)

            # converting dict of raw spectra to spectrum objects
            for fid in spectra:
                # retrieving basic thematic information for current fid
                roi_id = self.thematic_info[fid][self.roi_id]
                x = self.thematic_info[fid]['x']
                y = self.thematic_info[fid]['y']
                # retrieving additional attributes for current fid
                if self.thematic_info[fid].has_key('attributes'):
                    additional_attributes = self.thematic_info[fid]['attributes'].keys()
                # workaround to allow for subsequent iteration over a
                # single (aggregated polygon or simple point) spectrum
                if self.roi_type == 'point' or self.aggregate:
                    spectra[fid] = [spectra[fid]]
                if self.roi_type == 'line string':
                    cnt = 0
                #i = 0
                for sp in spectra[fid]:
                    #i += 1
                    #if not i % 20 == 0:
                    #    continue
                    #else:
                    #    i = 0
                    # creating new spectrum object using region of interest
                    # id and according coordinates
                    spectrum = Spectrum(roi_id, (x, y))
                    # copyying additional attributes
                    for aa in additional_attributes:
                        spectrum.set_attribute(aa, self.thematic_info[fid]['attributes'][aa])
                    if self.roi_type == 'line string':
                        cnt += 1
                        spectrum.set_attribute('_count', cnt)
                    # setting context range and type to default values
                    spectrum.set_context_range(self.context_range)
                    spectrum.set_context_type(self.context_type)
                    spectrum.set_source(img_id + 1)
                    # copying values to spectrum object
                    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)