本文整理汇总了Python中rasterio.features.shapes函数的典型用法代码示例。如果您正苦于以下问题:Python shapes函数的具体用法?Python shapes怎么用?Python shapes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了shapes函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_shapes_blank_mask
def test_shapes_blank_mask(basic_image):
"""Mask is blank so results should mask shapes without mask."""
assert np.array_equal(
list(shapes(
basic_image,
mask=np.ones(basic_image.shape, dtype=rasterio.bool_))
),
list(shapes(basic_image))
)
开发者ID:brendan-ward,项目名称:rasterio,代码行数:9,代码来源:test_features.py
示例2: test_shapes_band
def test_shapes_band(pixelated_image, pixelated_image_file):
"""Shapes from a band should match shapes from an array."""
truth = list(shapes(pixelated_image))
with rasterio.open(pixelated_image_file) as src:
band = rasterio.band(src, 1)
assert truth == list(shapes(band))
# Mask band should function, but will mask out some results
assert truth[0] == list(shapes(band, mask=band))[0]
开发者ID:brendan-ward,项目名称:rasterio,代码行数:10,代码来源:test_features.py
示例3: test_shapes_blank_mask
def test_shapes_blank_mask(basic_image):
""" Mask is blank so results should mask shapes without mask """
with rasterio.drivers():
assert numpy.array_equal(
list(shapes(
basic_image,
mask=numpy.ones(basic_image.shape, dtype=rasterio.bool_))
),
list(shapes(basic_image))
)
开发者ID:aashish24,项目名称:rasterio,代码行数:11,代码来源:test_features.py
示例4: union
def union(inputtiles, parsenames):
tiles = sutils.tile_parser(inputtiles, parsenames)
xmin, xmax, ymin, ymax = sutils.get_range(tiles)
zoom = sutils.get_zoom(tiles)
# make an array of shape (xrange + 3, yrange + 3)
burn = sutils.burnXYZs(tiles, xmin, xmax, ymin, ymax, 0)
nw = mercantile.xy(*mercantile.ul(xmin, ymin, zoom))
se = mercantile.xy(*mercantile.ul(xmax + 1, ymax + 1, zoom))
aff = Affine(((se[0] - nw[0]) / float(xmax - xmin + 1)), 0.0, nw[0],
0.0, -((nw[1] - se[1]) / float(ymax - ymin + 1)), nw[1])
unprojecter = sutils.Unprojecter()
unionedTiles = [
{
'geometry': unprojecter.unproject(feature),
'properties': {},
'type': 'Feature'
} for feature, shapes in features.shapes(np.asarray(np.flipud(np.rot90(burn)).astype(np.uint8), order='C'), transform=aff) if shapes == 1
]
return unionedTiles
开发者ID:mapbox,项目名称:supermercado,代码行数:29,代码来源:uniontiles.py
示例5: test_shapes_internal_driver_manager
def test_shapes_internal_driver_manager():
"""Access to shapes of labeled features"""
image = numpy.zeros((20, 20), dtype=rasterio.ubyte)
image[5:15,5:15] = 127
shapes = ftrz.shapes(image)
shape, val = next(shapes)
assert shape['type'] == 'Polygon'
开发者ID:robintw,项目名称:rasterio,代码行数:7,代码来源:test_features_shapes.py
示例6: polygonize
def polygonize(input_tif_dir, output_shp_dir, countries):
rm_and_mkdir(output_shp_dir)
for country in countries:
shp_filename = country + '.shp'
output_shp_path = os.path.join(output_shp_dir, shp_filename)
tif_filename = country + '.tif'
input_tif_path = os.path.join(input_tif_dir, tif_filename)
with rasterio.open(input_tif_path) as src:
band = src.read(1)
mask = band != 255
shapes = features.shapes(band, mask=mask, transform=src.transform)
geomvals = list(shapes)
geom_val_trios = []
for idx, geom_val in enumerate(geomvals):
shapely_geom = shape(geomvals[idx][0])
shapely_val = geomvals[idx][1]
geom_val_trio = [shapely_geom, shapely_val, country]
geom_val_trios.append(geom_val_trio)
gdf = gpd.GeoDataFrame(geom_val_trios, columns={'geometry', 'val', 'country'})
gdf.crs = {'init': 'epsg:4326', 'no_defs': True}
gdf.to_file(output_shp_path)
开发者ID:ChihChengLiang,项目名称:metis_projects,代码行数:25,代码来源:geoprocess.py
示例7: main
def main(raster_file, vector_file, driver, mask_value):
with rasterio.drivers():
with rasterio.open(raster_file) as src:
image = src.read_band(1)
if mask_value is not None:
mask = image == mask_value
else:
mask = None
results = (
{'properties': {'raster_val': v}, 'geometry': s}
for i, (s, v)
in enumerate(
shapes(image, mask=mask, transform=src.affine)))
with fiona.open(
vector_file, 'w',
driver=driver,
crs=src.crs,
schema={'properties': [('raster_val', 'int')],
'geometry': 'Polygon'}) as dst:
dst.writerecords(results)
return dst.name
开发者ID:AsgerPetersen,项目名称:rasterio,代码行数:27,代码来源:rasterio_polygonize.py
示例8: test_shapes
def test_shapes(basic_image):
""" Test creation of shapes from pixel values """
with Env():
results = list(shapes(basic_image))
assert len(results) == 2
shape, value = results[0]
assert shape == {
'coordinates': [
[(2, 2), (2, 5), (5, 5), (5, 2), (2, 2)]
],
'type': 'Polygon'
}
assert value == 1
shape, value = results[1]
assert shape == {
'coordinates': [
[(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)],
[(2, 2), (5, 2), (5, 5), (2, 5), (2, 2)]
],
'type': 'Polygon'
}
assert value == 0
开发者ID:EricAlex,项目名称:rasterio,代码行数:26,代码来源:test_features.py
示例9: polygonize
def polygonize(self, band_number=1):
"""
Extract shapes from raster features. This is the inverse operation of rasterizing shapes.
Uses the `Rasterio <https://mapbox.github.io/rasterio/_modules/rasterio/features.html>'_ library
for this purpose. The data is loaded into a `geopandas <http://geopandas.org/user.html>`_ GeoDataFrame.
GeoDataFrame data structures are pandas DataFrames with added functionality, containing a ``geometry``
column for the `Shapely <http://toblerity.org/shapely/shapely.geometry.html>`_ geometries.
The raster data should be loaded in the layer before calling this method.
:param int band_number: The index of the raster band which is to be used as input for extracting \
gemetrical shapes.
:returns: geopandas.GeoDataFrame
"""
raster_data = self.read(band_number)
mask = raster_data != self.raster_reader.nodata
T0 = self.raster_reader.affine
shapes = features.shapes(raster_data, mask=mask, transform=T0)
df = GeoDataFrame.from_records(shapes, columns=['geometry', 'value'])
# convert the geometry dictionary from a dictionary format like {'coordinates': [[(-73.5, 83.5),
# (-73.5, 83.0),
# (-68.0, 83.0),
# (-68.0, 83.5),
# (-73.5, 83.5)]],
# 'type': 'Polygon'}
# to a proper shapely polygon format
df.geometry = df.geometry.apply(lambda row: Polygon(row['coordinates'][0]))
df.crs = self.raster_reader.crs
return df
开发者ID:remenska,项目名称:iSDM,代码行数:30,代码来源:environment.py
示例10: test_shapes_connectivity_rook
def test_shapes_connectivity_rook(diagonal_image):
"""
Diagonals are not connected, so there will be 1 feature per pixel plus
background.
"""
with rasterio.Env():
assert len(list(shapes(diagonal_image, connectivity=4))) == 12
开发者ID:alexatodd,项目名称:rasterio,代码行数:7,代码来源:test_features.py
示例11: test_shapes_connectivity_queen
def test_shapes_connectivity_queen(diagonal_image):
"""
Diagonals are connected, so there will be 1 feature for all pixels plus
background.
"""
with rasterio.Env():
assert len(list(shapes(diagonal_image, connectivity=8))) == 2
开发者ID:alexatodd,项目名称:rasterio,代码行数:7,代码来源:test_features.py
示例12: raster_shape
def raster_shape(raster_path):
with rasterio.open(raster_path) as src:
# read the first band and create a binary mask
arr = src.read(1)
ndv = src.nodata
binarray = (arr == ndv).astype('uint8')
# extract shapes from raster
shapes = features.shapes(binarray, transform=src.transform)
# create geojson feature collection
fc = {
'type': 'FeatureCollection',
'features': []}
for geom, val in shapes:
if val == 0: # not nodata, i.e. valid data
feature = {
'type': 'Feature',
'properties': {'name': raster_path},
'geometry': geom}
fc['features'].append(feature)
# Write to file
with NamedTemporaryFile(suffix=".geojson", delete=False) as temp:
temp.file.write(json.dumps(fc))
return temp.name
开发者ID:ranchodeluxe,项目名称:sheepdawg,代码行数:28,代码来源:worker.py
示例13: test_rasterize_geometries_symmetric
def test_rasterize_geometries_symmetric():
"""Make sure that rasterize is symmetric with shapes."""
transform = (1.0, 0.0, 0.0, 0.0, -1.0, 0.0)
truth = np.zeros(DEFAULT_SHAPE, dtype=rasterio.ubyte)
truth[2:5, 2:5] = 1
s = shapes(truth, transform=transform)
result = rasterize(s, out_shape=DEFAULT_SHAPE, transform=transform)
assert np.array_equal(result, truth)
开发者ID:brendan-ward,项目名称:rasterio,代码行数:8,代码来源:test_features.py
示例14: test_shapes_internal_driver_manager
def test_shapes_internal_driver_manager():
"""Make sure this works if driver is managed outside this test"""
image = numpy.zeros((20, 20), dtype=rasterio.ubyte)
image[5:15, 5:15] = 127
shapes = ftrz.shapes(image)
shape, val = next(shapes)
assert shape['type'] == 'Polygon'
开发者ID:HydroLogic,项目名称:rasterio,代码行数:8,代码来源:test_features_shapes.py
示例15: test_shapes_invalid_mask_dtype
def test_shapes_invalid_mask_dtype(basic_image):
"""A mask that is the wrong dtype should fail."""
for dtype in ('int8', 'int16', 'int32'):
with pytest.raises(ValueError):
next(shapes(
basic_image,
mask=np.ones(basic_image.shape, dtype=dtype)
))
开发者ID:brendan-ward,项目名称:rasterio,代码行数:8,代码来源:test_features.py
示例16: test_shapes_band_shortcut
def test_shapes_band_shortcut():
"""Access to shapes of labeled features"""
with rasterio.drivers():
with rasterio.open('rasterio/tests/data/shade.tif') as src:
shapes = ftrz.shapes(rasterio.band(src, 1))
shape, val = next(shapes)
assert shape['type'] == 'Polygon'
assert len(shape['coordinates']) == 1
assert val == 255
开发者ID:robintw,项目名称:rasterio,代码行数:9,代码来源:test_features_shapes.py
示例17: test_shapes_connectivity
def test_shapes_connectivity():
"""Test connectivity options"""
image = numpy.zeros((20, 20), dtype=rasterio.ubyte)
image[5:11,5:11] = 1
image[11,11] = 1
shapes = ftrz.shapes(image, connectivity=8)
shape, val = next(shapes)
assert len(shape['coordinates'][0]) == 9
开发者ID:KDOTGIS,项目名称:rasterio,代码行数:9,代码来源:test_features_shapes.py
示例18: test_rasterize_geometries_symmetric
def test_rasterize_geometries_symmetric():
"""Make sure that rasterize is symmetric with shapes"""
rows = cols = 10
transform = (1.0, 0.0, 0.0, 0.0, -1.0, 0.0)
truth = numpy.zeros((rows, cols), dtype=rasterio.ubyte)
truth[2:5, 2:5] = 1
with rasterio.drivers():
s = shapes(truth, transform=transform)
result = rasterize(s, out_shape=(rows, cols), transform=transform)
assert numpy.array_equal(result, truth)
开发者ID:SIGISLV,项目名称:rasterio,代码行数:10,代码来源:test_features_rasterize.py
示例19: test_rasterize_geometries_symmetric
def test_rasterize_geometries_symmetric():
"""Make sure that rasterize is symmetric with shapes"""
rows = cols = 10
transform = [0, 1, 0, 0, 0, 1]
truth = numpy.zeros((rows, cols), dtype=rasterio.ubyte)
truth[2:5, 2:5] = 1
with rasterio.drivers():
s = shapes(truth, transform=transform)
result = rasterize(s, out_shape=(rows, cols), transform=transform)
assert (result == truth).min() == True
开发者ID:KDOTGIS,项目名称:rasterio,代码行数:10,代码来源:test_features_rasterize.py
示例20: test_shapes_invalid_mask_shape
def test_shapes_invalid_mask_shape(basic_image):
"""A mask that is the wrong shape should fail."""
with pytest.raises(ValueError):
next(shapes(
basic_image,
mask=np.ones(
(basic_image.shape[0] + 10, basic_image.shape[1] + 10),
dtype=rasterio.bool_
)
))
开发者ID:brendan-ward,项目名称:rasterio,代码行数:10,代码来源:test_features.py
注:本文中的rasterio.features.shapes函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论