示例#1
0
def test_read_vector_window(geojson, landpoly_3857):
    """Read vector data from read_vector_window."""
    zoom = 4
    config = MapcheteConfig(geojson.path)
    vectorfile = config.params_at_zoom(zoom)["input"]["file1"]
    pixelbuffer = 5
    tile_pyramid = BufferedTilePyramid("geodetic", pixelbuffer=pixelbuffer)
    tiles = tile_pyramid.tiles_from_geom(vectorfile.bbox(), zoom)
    feature_count = 0
    for tile in tiles:
        for feature in read_vector_window(vectorfile.path, tile):
            assert "properties" in feature
            assert shape(feature["geometry"]).is_valid
            feature_count += 1
    assert feature_count
    # into different CRS
    raw_config = geojson.dict
    raw_config["input"].update(file1=landpoly_3857)
    config = MapcheteConfig(raw_config)
    vectorfile = config.params_at_zoom(zoom)["input"]["file1"]
    pixelbuffer = 5
    tile_pyramid = BufferedTilePyramid("geodetic", pixelbuffer=pixelbuffer)
    tiles = tile_pyramid.tiles_from_geom(vectorfile.bbox(), zoom)
    feature_count = 0
    for tile in tiles:
        for feature in read_vector_window(vectorfile.path, tile):
            assert "properties" in feature
            assert shape(feature["geometry"]).is_valid
            feature_count += 1
    assert feature_count
示例#2
0
def test_read_vector_window_errors(invalid_geojson):
    with pytest.raises(FileNotFoundError):
        read_vector_window("invalid_path",
                           BufferedTilePyramid("geodetic").tile(0, 0, 0))
    with pytest.raises(MapcheteIOError):
        read_vector_window(invalid_geojson,
                           BufferedTilePyramid("geodetic").tile(0, 0, 0))
示例#3
0
def _read_as_tiledir(data_type=None,
                     out_tile=None,
                     td_crs=None,
                     tiles_paths=None,
                     profile=None,
                     validity_check=False,
                     indexes=None,
                     resampling=None,
                     dst_nodata=None,
                     gdal_opts=None,
                     **kwargs):
    logger.debug("reading data from CRS %s to CRS %s", td_crs, out_tile.tp.crs)
    if data_type == "vector":
        if tiles_paths:
            return read_vector_window([path for _, path in tiles_paths],
                                      out_tile,
                                      validity_check=validity_check)
        else:
            return []
    elif data_type == "raster":
        if tiles_paths:
            return read_raster_window([path for _, path in tiles_paths],
                                      out_tile,
                                      indexes=indexes,
                                      resampling=resampling,
                                      src_nodata=profile["nodata"],
                                      dst_nodata=dst_nodata,
                                      gdal_opts=gdal_opts)
        else:
            bands = len(indexes) if indexes else profile["count"]
            return ma.masked_array(data=np.full(
                (bands, out_tile.height, out_tile.width),
                profile["nodata"],
                dtype=profile["dtype"]),
                                   mask=True)
示例#4
0
 def _read_from_cache(self, validity_check):
     checked = "checked" if validity_check else "not_checked"
     if checked not in self._cache:
         self._cache[checked] = list(
             read_vector_window(self.vector_file.path,
                                self.tile,
                                validity_check=validity_check))
     return self._cache[checked]
示例#5
0
    def read(
        self, validity_check=False, indexes=None, resampling="nearest",
        dst_nodata=None, gdal_opts=None
    ):
        """
        Read reprojected & resampled input data.

        Parameters
        ----------
        validity_check : bool
            vector file: also run checks if reprojected geometry is valid,
            otherwise throw RuntimeError (default: True)

        indexes : list or int
            raster file: a list of band numbers; None will read all.
        resampling : string
            raster file: one of "nearest", "average", "bilinear" or "lanczos"
        dst_nodata : int or float, optional
            raster file: if not set, the nodata value from the source dataset
            will be used
        gdal_opts : dict
            raster file: GDAL options passed on to rasterio.Env()

        Returns
        -------
        data : list for vector files or numpy array for raster files
        """
        if self._file_type == "vector":
            if self.is_empty():
                return []
            return list(chain.from_iterable([
                read_vector_window(
                    _path, self.tile, validity_check=validity_check)
                for _, _path in self._tiles_paths
            ]))
        else:
            if self.is_empty():
                count = (len(indexes) if indexes else self._profile["count"], )
                return ma.masked_array(
                    data=np.full(
                        count + self.tile.shape, self._profile["nodata"],
                        dtype=self._profile["dtype"]),
                    mask=True
                )
            tiles = [
                (_tile, read_raster_window(
                    _path, _tile, indexes=indexes, resampling=resampling,
                    src_nodata=self._profile["nodata"], dst_nodata=dst_nodata,
                    gdal_opts=gdal_opts))
                for _tile, _path in self._tiles_paths
            ]
            return resample_from_array(
                in_raster=create_mosaic(
                    tiles=tiles, nodata=self._profile["nodata"]),
                out_tile=self.tile,
                resampling=resampling,
                nodataval=self._profile["nodata"])
示例#6
0
文件: test_io.py 项目: gijs/mapchete
def test_read_vector_window():
    """Read vector data from read_vector_window."""
    zoom = 4
    config = MapcheteConfig(
        os.path.join(SCRIPTDIR, "testdata/geojson.mapchete"))
    vectorfile = config.at_zoom(zoom)["input"]["file1"]
    pixelbuffer = 5
    tile_pyramid = BufferedTilePyramid("geodetic", pixelbuffer=pixelbuffer)
    tiles = tile_pyramid.tiles_from_geom(vectorfile.bbox(), zoom)
    feature_count = 0
    for tile in tiles:
        for feature in vector.read_vector_window(vectorfile.path, tile):
            assert "properties" in feature
            assert shape(feature["geometry"]).is_valid
            feature_count += 1
    assert feature_count