本文整理汇总了Python中fiona.open方法的典型用法代码示例。如果您正苦于以下问题:Python fiona.open方法的具体用法?Python fiona.open怎么用?Python fiona.open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fiona
的用法示例。
在下文中一共展示了fiona.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read_terrace_shapefile
# 需要导入模块: import fiona [as 别名]
# 或者: from fiona import open [as 别名]
def read_terrace_shapefile(DataDirectory, shapefile_name):
"""
This function reads in a shapefile of digitised terraces
using shapely and fiona
Args:
DataDirectory (str): the data directory
shapefile_name (str): the name of the shapefile
Returns: shapely polygons with terraces
Author: FJC
"""
Polygons = {}
with fiona.open(DataDirectory+shapefile_name, 'r') as input:
for f in input:
this_shape = Polygon(shape(f['geometry']))
this_id = f['properties']['id']
Polygons[this_id] = this_shape
return Polygons
示例2: read_terrace_centrelines
# 需要导入模块: import fiona [as 别名]
# 或者: from fiona import open [as 别名]
def read_terrace_centrelines(DataDirectory, shapefile_name):
"""
This function reads in a shapefile of terrace centrelines
using shapely and fiona
Args:
DataDirectory (str): the data directory
shapefile_name (str): the name of the shapefile
Returns: shapely polygons with terraces
Author: FJC
"""
Lines = {}
with fiona.open(DataDirectory+shapefile_name, 'r') as input:
for f in input:
this_line = LineString(shape(f['geometry']))
this_id = f['properties']['id']
Lines[this_id] = this_line
return Lines
示例3: wkt_to_shp
# 需要导入模块: import fiona [as 别名]
# 或者: from fiona import open [as 别名]
def wkt_to_shp(wkt_list, shp_file):
'''Take output of build_graph_wkt() and render the list of linestrings
into a shapefile
# https://gis.stackexchange.com/questions/52705/how-to-write-shapely-geometries-to-shapefiles
'''
# Define a linestring feature geometry with one attribute
schema = {
'geometry': 'LineString',
'properties': {'id': 'int'},
}
# Write a new shapefile
with fiona.open(shp_file, 'w', 'ESRI Shapefile', schema) as c:
for i, line in enumerate(wkt_list):
shape = shapely.wkt.loads(line)
c.write({
'geometry': mapping(shape),
'properties': {'id': i},
})
return
###############################################################################
示例4: shp2meta
# 需要导入模块: import fiona [as 别名]
# 或者: from fiona import open [as 别名]
def shp2meta(s3_obj):
tmp_file = path.join(tmp, s3_obj.object_key)
file_name = s3_obj.object_key.split('.')[0]
# 파일 다운로드
with open(tmp_file, 'wb') as data:
s3_obj.object.download_fileobj(data)
# 압축 해제
extract_dir = path.join(tmp, file_name)
make_dir(extract_dir)
with tarfile.open(tmp_file) as tar:
tar.extractall(extract_dir)
# json 파일로 변경
shp_file = [path.join(extract_dir, name) for name in os.listdir(extract_dir) if name.split('.')[-1] == "shp"][0]
metadata = get_meta(shp_file)
s3_meta = s3_obj.object.get()['Metadata']
filter_keys = ['upload_by', 'uploader', 'description']
for k in filter_keys:
for key in s3_meta:
if k in key:
metadata[k] = s3_meta[key]
continue
record = MetaData(hash_key=s3_obj.object_key, **metadata)
record.save()
示例5: upload_s3
# 需要导入模块: import fiona [as 别名]
# 或者: from fiona import open [as 别名]
def upload_s3(bucket, json_file, metadata):
"""
파일을 gz하여 s3로 업로드
:param json_file: 업로드할 json 파일명
:return:
"""
gz_name = f"{json_file}.gz"
obj_key = f"json/{path.basename(gz_name)}"
print("업로드", gz_name, obj_key)
with open(json_file, 'rb') as f:
gz = gzip.compress(f.read())
s3.put_object(
Body=gz,
Bucket=bucket,
ContentEncoding='gzip',
ContentLanguage='string',
ContentType='application/json',
Key=obj_key,
# todo : 메타데이터 추가 - 2018-07-28
Metadata=metadata,
)
示例6: overlay_bitmap
# 需要导入模块: import fiona [as 别名]
# 或者: from fiona import open [as 别名]
def overlay_bitmap(bitmap, raster_dataset, out_path, color='blue'):
"""Overlay the given satellite image with a bitmap."""
# RGB values for possible color options.
colors = {
"red": (255, 0, 0),
"green": (0, 255, 0),
"blue": (0, 0, 255)
}
red, green, blue = raster_dataset.read()
red[bitmap == 1] = colors[color][0]
green[bitmap == 1] = colors[color][1]
blue[bitmap == 1] = colors[color][2]
profile = raster_dataset.profile
with rasterio.open(out_path, 'w', **profile) as dst:
dst.write(red, 1)
dst.write(green, 2)
dst.write(blue, 3)
return rasterio.open(out_path)
示例7: test_shp_to_shp_records_geom_type_is_multilinestring
# 需要导入模块: import fiona [as 别名]
# 或者: from fiona import open [as 别名]
def test_shp_to_shp_records_geom_type_is_multilinestring(
create_input_file, create_output_centerline_file
):
EXPECTED_TYPE = "MultiLineString"
input_polygon_shp = create_input_file("polygons", "shp")
output_centerline_shp = create_output_centerline_file("shp")
runner = CliRunner()
runner.invoke(
create_centerlines, [input_polygon_shp, output_centerline_shp]
)
with fiona.open(output_centerline_shp) as dst:
for record in dst:
assert record.get("geometry").get("type") == EXPECTED_TYPE
示例8: test_shp_to_geojson_records_geom_type_is_multilinestring
# 需要导入模块: import fiona [as 别名]
# 或者: from fiona import open [as 别名]
def test_shp_to_geojson_records_geom_type_is_multilinestring(
create_input_file, create_output_centerline_file
):
EXPECTED_TYPE = "MultiLineString"
input_polygon_shp = create_input_file("polygons", "shp")
output_centerline_geojson = create_output_centerline_file("geojson")
runner = CliRunner()
runner.invoke(
create_centerlines, [input_polygon_shp, output_centerline_geojson]
)
with fiona.open(output_centerline_geojson) as dst:
for record in dst:
assert record.get("geometry").get("type") == EXPECTED_TYPE
示例9: polygonize_raster
# 需要导入模块: import fiona [as 别名]
# 或者: from fiona import open [as 别名]
def polygonize_raster(infile, outfile, mask_value=1, driver='ESRI Shapefile'):
with rasterio.open(infile) as src:
image = src.read(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.transform)))
with fiona.open(
outfile, 'w',
driver=driver,
crs=src.crs,
schema={'properties': [('raster_val', 'int')],
'geometry': 'Polygon'}) as dst:
dst.writerecords(results)
示例10: read
# 需要导入模块: import fiona [as 别名]
# 或者: from fiona import open [as 别名]
def read(self, output_tile, **kwargs):
"""
Read existing process output.
Parameters
----------
output_tile : ``BufferedTile``
must be member of output ``TilePyramid``
Returns
-------
process output : list
"""
try:
with fiona.open(self.get_path(output_tile), "r") as src:
return list(src)
except DriverError as e:
for i in ("does not exist in the file system", "No such file or directory"):
if i in str(e):
return self.empty(output_tile)
else:
raise
示例11: bbox
# 需要导入模块: import fiona [as 别名]
# 或者: from fiona import open [as 别名]
def bbox(self, out_crs=None):
"""
Return data bounding box.
Parameters
----------
out_crs : ``rasterio.crs.CRS``
rasterio CRS object (default: CRS of process pyramid)
Returns
-------
bounding box : geometry
Shapely geometry object
"""
out_crs = self.pyramid.crs if out_crs is None else out_crs
with fiona.open(self.path) as inp:
inp_crs = CRS(inp.crs)
bbox = box(*inp.bounds)
# TODO find a way to get a good segmentize value in bbox source CRS
return reproject_geometry(bbox, src_crs=inp_crs, dst_crs=out_crs)
示例12: test_read_raster_window_resampling
# 需要导入模块: import fiona [as 别名]
# 或者: from fiona import open [as 别名]
def test_read_raster_window_resampling(cleantopo_br_tif):
"""Assert various resampling options work."""
tp = BufferedTilePyramid("geodetic")
with rasterio.open(cleantopo_br_tif, "r") as src:
tiles = tp.tiles_from_bounds(src.bounds, 4)
for tile in tiles:
outputs = [
read_raster_window(
cleantopo_br_tif, tile, resampling=resampling)
for resampling in [
"nearest", "bilinear", "cubic", "cubic_spline", "lanczos",
"average", "mode"
]
]
# resampling test:
assert any([
not np.array_equal(w, v) for v, w in zip(outputs[:-1], outputs[1:])
])
示例13: test_convert_png
# 需要导入模块: import fiona [as 别名]
# 或者: from fiona import open [as 别名]
def test_convert_png(cleantopo_br_tif, mp_tmpdir):
"""Automatic PNG tile pyramid creation of raster files."""
run_cli([
"convert",
cleantopo_br_tif,
mp_tmpdir,
"--output-pyramid", "mercator",
"--output-format", "PNG"
])
for zoom, row, col in [(4, 15, 15), (3, 7, 7)]:
out_file = os.path.join(
*[mp_tmpdir, str(zoom), str(row), str(col) + ".png"])
with rasterio.open(out_file, "r") as src:
assert src.meta["driver"] == "PNG"
assert src.meta["dtype"] == "uint8"
data = src.read(masked=True)
assert data.mask.any()
示例14: test_convert_bidx
# 需要导入模块: import fiona [as 别名]
# 或者: from fiona import open [as 别名]
def test_convert_bidx(cleantopo_br_tif, mp_tmpdir):
"""Automatic geodetic tile pyramid creation of raster files."""
single_gtiff = os.path.join(mp_tmpdir, "single_out_bidx.tif")
run_cli([
"convert",
cleantopo_br_tif,
single_gtiff,
"--output-pyramid", "geodetic",
"-z", "3",
"--bidx", "1"
])
with rasterio.open(single_gtiff, "r") as src:
assert src.meta["driver"] == "GTiff"
assert src.meta["dtype"] == "uint16"
data = src.read(masked=True)
assert data.mask.any()
assert not src.overviews(1)
示例15: test_convert_single_gtiff
# 需要导入模块: import fiona [as 别名]
# 或者: from fiona import open [as 别名]
def test_convert_single_gtiff(cleantopo_br_tif, mp_tmpdir):
"""Automatic geodetic tile pyramid creation of raster files."""
single_gtiff = os.path.join(mp_tmpdir, "single_out.tif")
run_cli([
"convert",
cleantopo_br_tif,
single_gtiff,
"--output-pyramid", "geodetic",
"-z", "3"
])
with rasterio.open(single_gtiff, "r") as src:
assert src.meta["driver"] == "GTiff"
assert src.meta["dtype"] == "uint16"
data = src.read(masked=True)
assert data.mask.any()
assert not src.overviews(1)