Exemple #1
0
    def rasterize_line_rois(self, img_src, output=False):
        u"""
        Convert lines of interests from vector coordinates to pixel coordinates
        in the specified source image.
        """
        # retrieving necessary information from template image
        tpl_ds = gdal.Open(img_src)
        tpl_geo_transform = tpl_ds.GetGeoTransform()
        tpl_x_size, tpl_y_size = tpl_ds.RasterXSize, tpl_ds.RasterYSize

        # opening vector layer to be rasterized
        roi_ds = ogr.Open(self.roi_src)
        roi_ly = roi_ds.GetLayer(0)

        if output:
            mem_ds = gdal.GetDriverByName("MEM").Create("", tpl_x_size, tpl_y_size, 1, gdal.GDT_Byte)
            mem_ds.SetGeoTransform(tpl_geo_transform)
            mem_ds.SetProjection(roi_ly.GetSpatialRef().ExportToWkt())
            mem_data = mem_ds.GetRasterBand(1).ReadAsArray()

        img_coordinates = dict()

        for ft in roi_ly:
            fid = ft.GetFID()
            print "Working on FID %d..." % fid
            gm = ft.GetGeometryRef()
            vertices = gm.GetPoints()

            pixels = list()

            for i in range(0, len(vertices) - 2):
                # retrieving real world coordinates of sub-line endpoints
                x0, y0 = vertices[i]
                x1, y1 = vertices[i + 1]

                # converting real-world coordinates to image coordinates
                c0 = int((x0 - tpl_geo_transform[0]) / tpl_geo_transform[1])
                r0 = int((y0 - tpl_geo_transform[3]) / tpl_geo_transform[5])
                c1 = int((x1 - tpl_geo_transform[0]) / tpl_geo_transform[1])
                r1 = int((y1 - tpl_geo_transform[3]) / tpl_geo_transform[5])

                # finding sequential pixels between current vertices
                pixels += geom_utils.retrieve_line_pixels(c0, r0, c1, r1)
            # finally eliminating duplicate pixels
            else:
                pixels = general_utils.ordered_set(pixels)

            if output:
                for col, row in pixels:
                    mem_data[row, col] = 255

            img_coordinates[fid] = pixels

        if output:
            gdal_utils.export_data(mem_data, r"z:\mem_out_bresenham.img", tpl_ds)

        return img_coordinates
Exemple #2
0
    def rasterize_line_rois(self, img_src, output = False):
        u"""
        Convert lines of interests from vector coordinates to pixel coordinates
        in the specified source image.
        """
        # retrieving necessary information from template image
        tpl_ds = gdal.Open(img_src)
        tpl_geo_transform = tpl_ds.GetGeoTransform()
        tpl_x_size, tpl_y_size = tpl_ds.RasterXSize, tpl_ds.RasterYSize

        # opening vector layer to be rasterized
        roi_ds = ogr.Open(self.roi_src)
        roi_ly = roi_ds.GetLayer(0)

        if output:
            mem_ds = gdal.GetDriverByName('MEM').Create("", tpl_x_size, tpl_y_size, 1, gdal.GDT_Byte)
            mem_ds.SetGeoTransform(tpl_geo_transform)
            mem_ds.SetProjection(roi_ly.GetSpatialRef().ExportToWkt())
            mem_data = mem_ds.GetRasterBand(1).ReadAsArray()
        
        img_coordinates = dict()
        
        for ft in roi_ly:
            fid = ft.GetFID()
            print "Working on FID %d..." % fid
            gm = ft.GetGeometryRef()
            vertices = gm.GetPoints()
            
            pixels = list()
            
            for i in range(0, len(vertices) - 2):
                # retrieving real world coordinates of sub-line endpoints
                x0, y0 = vertices[i]
                x1, y1 = vertices[i + 1]
                
                # converting real-world coordinates to image coordinates
                c0 = int((x0 - tpl_geo_transform[0]) / tpl_geo_transform[1])
                r0 = int((y0 - tpl_geo_transform[3]) / tpl_geo_transform[5])
                c1 = int((x1 - tpl_geo_transform[0]) / tpl_geo_transform[1])
                r1 = int((y1 - tpl_geo_transform[3]) / tpl_geo_transform[5])
            
                # finding sequential pixels between current vertices
                pixels += geom_utils.retrieve_line_pixels(c0, r0, c1, r1)
            # finally eliminating duplicate pixels
            else:
                pixels = general_utils.ordered_set(pixels)
            
            if output:
                for col, row in pixels:
                    mem_data[row, col] = 255

            img_coordinates[fid] = pixels

        if output:
            gdal_utils.export_data(mem_data, r"z:\mem_out_bresenham.img", tpl_ds)
        
        return img_coordinates
Exemple #3
0
    def rasterize_polygon_rois(self, img_src, output=False):
        u"""
        Convert polygons of interests from vector coordinates to pixel
        coordinates in the specified source image.
        """
        # declaring source imagery as template raster data set
        tpl_ds = gdal.Open(img_src)
        tpl_geo_transform = tpl_ds.GetGeoTransform()
        tpl_x_size, tpl_y_size = tpl_ds.RasterXSize, tpl_ds.RasterYSize

        # opening vector roi data source
        roi_ds = ogr.Open(self.roi_src)
        roi_ly = roi_ds.GetLayer(0)

        img_coordinates = dict()

        if output:
            out_ds = gdal.GetDriverByName("MEM").Create("", tpl_x_size, tpl_y_size, 1, gdal.GDT_Int16)
            out_ds.SetGeoTransform(tpl_geo_transform)
            out_ds.SetProjection(roi_ly.GetSpatialRef().ExportToWkt())
            out_data = out_ds.GetRasterBand(1).ReadAsArray()
            out_data[:] = -9999

        for ft in roi_ly:
            fid = ft.GetFID()
            print "Working on FID %d..." % fid
            mem_ds = gdal.GetDriverByName("MEM").Create("", tpl_x_size, tpl_y_size, 1, gdal.GDT_Byte)
            mem_ds.SetGeoTransform(tpl_geo_transform)
            mem_ds.SetProjection(roi_ly.GetSpatialRef().ExportToWkt())

            where_clause = "WHERE '%s' = %i" % ("FID", fid)
            query = "SELECT * FROM '%s' %s" % (roi_ly.GetName(), where_clause)
            tmp_ds = ogr.Open(self.roi_src)
            sel_ly = tmp_ds.ExecuteSQL(query)

            gdal.RasterizeLayer(mem_ds, (1,), sel_ly, burn_values=(255,))
            mem_data = mem_ds.GetRasterBand(1).ReadAsArray()

            mask = ma.masked_where(mem_data != 255, mem_data)
            if output:
                out_data[~mask.mask] = fid

            pixels = list()

            # reversing image shape to make it column-major
            shape = list(mask.shape)
            shape.reverse()

            non_mask_indices = ma.flatnotmasked_contiguous(mask)
            # iterating over each slice
            if non_mask_indices is None:
                continue
            for sl in non_mask_indices:
                start_pixel = np.unravel_index(sl.start, tuple(shape), order="F")
                stop_pixel = np.unravel_index(sl.stop, tuple(shape), order="F")
                # print sl, np.unravel_index(sl.start, mask.shape), np.unravel_index(sl.stop, mask.shape)
                pixels.append((start_pixel, stop_pixel))
            else:
                img_coordinates[fid] = pixels

        if output:
            gdal_utils.export_data(out_data, r"z:\mem_out_raster.img", tpl_ds)

        return img_coordinates
Exemple #4
0
    def rasterize_polygon_rois(self, img_src, output = False):
        u"""
        Convert polygons of interests from vector coordinates to pixel
        coordinates in the specified source image.
        """
        # declaring source imagery as template raster data set
        tpl_ds = gdal.Open(img_src)
        tpl_geo_transform = tpl_ds.GetGeoTransform()
        tpl_x_size, tpl_y_size = tpl_ds.RasterXSize, tpl_ds.RasterYSize
    
        # opening vector roi data source
        roi_ds = ogr.Open(self.roi_src)
        roi_ly = roi_ds.GetLayer(0)
        
        img_coordinates = dict()

        if output:
            out_ds = gdal.GetDriverByName('MEM').Create("", tpl_x_size, tpl_y_size, 1, gdal.GDT_Int16)
            out_ds.SetGeoTransform(tpl_geo_transform)
            out_ds.SetProjection(roi_ly.GetSpatialRef().ExportToWkt())
            out_data = out_ds.GetRasterBand(1).ReadAsArray()
            out_data[:] = -9999
        
        for ft in roi_ly:
            fid = ft.GetFID()
            print "Working on FID %d..." % fid
            mem_ds = gdal.GetDriverByName('MEM').Create("", tpl_x_size, tpl_y_size, 1, gdal.GDT_Byte)
            mem_ds.SetGeoTransform(tpl_geo_transform)
            mem_ds.SetProjection(roi_ly.GetSpatialRef().ExportToWkt())
    
            where_clause = "WHERE '%s' = %i" % ('FID', fid)   
            query = "SELECT * FROM '%s' %s" % (roi_ly.GetName(), where_clause)
            tmp_ds = ogr.Open(self.roi_src)
            sel_ly = tmp_ds.ExecuteSQL(query)
    
            gdal.RasterizeLayer(mem_ds, (1,), sel_ly, burn_values = (255,))
            mem_data = mem_ds.GetRasterBand(1).ReadAsArray()
    
            mask = ma.masked_where(mem_data != 255, mem_data)
            if output:
                out_data[~mask.mask] = fid

            pixels = list()
    
            # reversing image shape to make it column-major
            shape = list(mask.shape)
            shape.reverse()
    
            non_mask_indices = ma.flatnotmasked_contiguous(mask)
            # iterating over each slice
            if non_mask_indices is None:
                continue
            for sl in non_mask_indices:
                start_pixel = np.unravel_index(sl.start, tuple(shape), order = 'F')
                stop_pixel = np.unravel_index(sl.stop, tuple(shape), order = 'F')
                #print sl, np.unravel_index(sl.start, mask.shape), np.unravel_index(sl.stop, mask.shape)
                pixels.append((start_pixel, stop_pixel))
            else:
                img_coordinates[fid] = pixels

        if output:
            gdal_utils.export_data(out_data, r"z:\mem_out_raster.img", tpl_ds)
    
        return img_coordinates