Example #1
0
def command(sourcepath):
    """ Do something spectacular. """
    dataset = gdal.Open(sourcepath)
    array = read_as_shared_array(dataset)
    shared = rasters.array2dataset(array, crs=None, extent=None)
    shared.SetProjection(dataset.GetProjection())
    shared.SetGeoTransform(dataset.GetGeoTransform())
    process = multiprocessing.Process(
        target=func,
        kwargs=dict(shared=shared),
    )
    process.start()
    process.join()
    print(shared.ReadAsArray()[0,0])
Example #2
0
    def get_data_for_polygon(self, wkb, crs, size):
        """
        Return a numpy array for the data.
        """
        # Quick out if polygon bounds match polygon
        geometry = vectors.Geometry(wkb)
        envelope = geometry.envelope
        extent = geometry.extent
        nodatavalue = self.info['nodatavalue']
        datatype = self.info['datatype']

        # Initialize resulting array to nodatavalue
        dtype = gdal_array.flip_code(datatype)
        array = np.ones(
            (1, size[1], size[0]),
            dtype=dtype,
        ) * dtype(nodatavalue)

        # Create dataset and use it to retrieve data from the store
        dataset = rasters.array2dataset(array=array, extent=extent, crs=crs)
        self.warpinto(dataset)
        dataset.FlushCache()

        # Cut when necessary
        if not envelope.Equals(wkb):
            source = OGR_MEM_DRIVER.CreateDataSource('')
            sr = projections.get_spatial_reference(crs)
            layer = source.CreateLayer(b'', sr)
            defn = layer.GetLayerDefn()
            feature = ogr.Feature(defn)
            feature.SetGeometry(wkb)
            layer.CreateFeature(feature)
            gdal.RasterizeLayer(dataset, (1,), layer,
                                burn_values=(nodatavalue,))
            dataset.FlushCache()

        return np.ma.masked_equal(array, nodatavalue, copy=False)