本文整理汇总了Python中osgeo.gdal.OpenEx方法的典型用法代码示例。如果您正苦于以下问题:Python gdal.OpenEx方法的具体用法?Python gdal.OpenEx怎么用?Python gdal.OpenEx使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类osgeo.gdal
的用法示例。
在下文中一共展示了gdal.OpenEx方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wkt_of_any
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import OpenEx [as 别名]
def wkt_of_any(string):
"""Wkt of user input"""
out = osr.GetUserInputAsWKT(string)
if isinstance(out, str):
return out
else:
prj = None
gdal_ds = gdal.OpenEx(string, conv.of_of_str('raster'))
if gdal_ds is not None:
prj = gdal_ds.GetProjection()
gdal_ds = gdal.OpenEx(string, conv.of_of_str('vector'))
if gdal_ds is not None:
lyr = gdal_ds.GetLayerByIndex(0)
if lyr is not None:
prj = lyr.GetSpatialRef()
if prj is not None:
return prj.ExportToWkt()
raise ValueError('Could not convert to wkt ({})'.format(str(gdal.GetLastErrorMsg()).strip('\n')))
示例2: _details_of_file
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import OpenEx [as 别名]
def _details_of_file(path):
gdal_ds = gdal.OpenEx(path, conv.of_of_str('raster'))
if gdal_ds is not None:
aff = affine.Affine.from_gdal(*gdal_ds.GetGeoTransform())
w, h = gdal_ds.RasterXSize, gdal_ds.RasterYSize
cx, cy = aff * [w / 2, h / 2]
return gdal_ds.GetProjection(), (cx, cy)
gdal_ds = gdal.OpenEx(path, conv.of_of_str('vector'))
if gdal_ds is not None:
lyr = gdal_ds.GetLayerByIndex(0)
if lyr is None:
raise ValueError('Could not open file layer')
extent = lyr.GetExtent()
if extent is None:
raise ValueError('Could not compute extent')
minx, maxx, miny, maxy = extent
cx, cy = (maxx + minx) / 2, (maxy + miny) / 2
return lyr.GetSpatialRef().ExportToWkt(), (cx, cy)
raise ValueError('Could not open file')
示例3: open_file
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import OpenEx [as 别名]
def open_file(path, driver, options, mode):
"""Open a raster dataset"""
success, payload = GDALErrorCatcher(gdal.OpenEx, none_is_error=True)(
path,
conv.of_of_mode(mode) | conv.of_of_str('raster'),
[driver],
options,
)
if not success:
raise RuntimeError('Could not open `{}` using driver `{}` (gdal error: `{}`)'.format(
path, driver, payload[1]
))
gdal_ds = payload
return gdal_ds
示例4: open_raster
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import OpenEx [as 别名]
def open_raster(filename, driver=None):
"""Open raster file, return gdal.Dataset
Parameters
----------
filename : string
raster file name
driver : string
gdal driver string
Returns
-------
dataset : gdal.Dataset
dataset
"""
dataset = gdal.OpenEx(filename)
if driver:
gdal.GetDriverByName(driver)
return dataset
示例5: open_file
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import OpenEx [as 别名]
def open_file(path, layer, driver, options, mode):
"""Open a vector dataset"""
options = [str(arg) for arg in options] if len(options) else []
success, payload = GDALErrorCatcher(gdal.OpenEx, none_is_error=True)(
path,
conv.of_of_mode(mode) | conv.of_of_str('vector'),
[driver],
options,
)
if not success:
raise RuntimeError('Could not open `{}` using driver `{}` (gdal error: `{}`)'.format(
path, driver, payload[1]
))
gdal_ds = payload
if layer is None:
layer = 0
if np.all(np.isreal(layer)):
success, payload = GDALErrorCatcher(gdal_ds.GetLayer)(int(layer))
else:
success, payload = GDALErrorCatcher(gdal_ds.GetLayerByName)(layer)
if not success: # pragma: no cover
count = gdal_ds.GetLayerCount()
raise Exception('Could not open layer `{}` of `{}` ({} layers available: {}) (gdal error: `{}`)'.format(
layer,
path,
count,
{i: gdal_ds.GetLayerByIndex(i).GetName() for i in range(count)},
payload[1],
))
lyr = payload
return gdal_ds, lyr
示例6: make_class_dict
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import OpenEx [as 别名]
def make_class_dict(path):
# Set up dict to save Xs and Ys
driver = ogr.GetDriverByName('ESRI Shapefile')
# data_source = gdal.OpenEx(vector_data_path, gdal.OF_VECTOR)
data_source = driver.Open(path, 0)
if data_source is None:
report_and_exit("File read failed: %s", path)
layer = data_source.GetLayer(0)
class_labels = []
data = []
for feature in layer:
try:
var1 = float(feature.GetField('NFDI_mag'))
# var2 = float(feature.GetField('NFDI_rmse'))
var3 = float(feature.GetField('NFDI_sin'))
var4 = float(feature.GetField('NFDI_cos'))
var5 = float(feature.GetField('Gv_mag'))
var6 = float(feature.GetField('Shade_mag'))
var7 = float(feature.GetField('NPV_mag'))
var8 = float(feature.GetField('Soil_mag'))
label = feature.GetField('class')
except:
continue
class_labels.append(label)
#data.append([var1, var2, var3, var4, var5, var6, var7, var8])
data.append([var1, var3, var4, var5, var6, var7, var8])
return class_labels, data
示例7: GeoreferenceFrame
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import OpenEx [as 别名]
def GeoreferenceFrame(task, image, output, p):
''' Save Current Image '''
global groupName
ext = ".tiff"
t = "out_" + p + ext
name = "g_" + p
src_file = os.path.join(output, t)
image.save(src_file)
# Opens source dataset
src_ds = gdal.OpenEx(src_file, gdal.OF_RASTER |
gdal.OF_READONLY, open_options=['NUM_THREADS=ALL_CPUS'])
# Open destination dataset
dst_filename = os.path.join(output, name + ext)
dst_ds = gdal.GetDriverByName("GTiff").CreateCopy(dst_filename, src_ds, 0,
options=['TILED=NO', 'BIGTIFF=NO', 'COMPRESS_OVERVIEW=DEFLATE', 'COMPRESS=LZW', 'NUM_THREADS=ALL_CPUS', 'predictor=2'])
src_ds = None
# Get raster projection
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
# Set projection
dst_ds.SetProjection(srs.ExportToWkt())
# Set location
dst_ds.SetGeoTransform(geotransform_affine)
dst_ds.GetRasterBand(1).SetNoDataValue(0)
dst_ds.FlushCache()
# Close files
dst_ds = None
# Add Layer to canvas
layer = QgsRasterLayer(dst_filename, name)
addLayerNoCrsDialog(layer, False, frames_g, isSubGroup=True)
ExpandLayer(layer, False)
if task.isCanceled():
return None
return {'task': task.description()}
示例8: SaveGeoCapture
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import OpenEx [as 别名]
def SaveGeoCapture(self, task, image, output, p, geotransform):
''' Save Current GeoReferenced Frame '''
ext = ".tiff"
t = "out_" + p + ext
name = "g_" + p
src_file = os.path.join(output, t)
image.save(src_file)
# Opens source dataset
src_ds = gdal.OpenEx(src_file, gdal.OF_RASTER |
gdal.OF_READONLY, open_options=['NUM_THREADS=ALL_CPUS'])
# Open destination dataset
dst_filename = os.path.join(output, name + ext)
dst_ds = gdal.GetDriverByName("GTiff").CreateCopy(dst_filename, src_ds, 0,
options=['TILED=NO', 'BIGTIFF=NO', 'COMPRESS_OVERVIEW=DEFLATE', 'COMPRESS=LZW', 'NUM_THREADS=ALL_CPUS', 'predictor=2'])
src_ds = None
# Get raster projection
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
# Set projection
dst_ds.SetProjection(srs.ExportToWkt())
# Set location
dst_ds.SetGeoTransform(geotransform)
dst_ds.GetRasterBand(1).SetNoDataValue(0)
dst_ds.FlushCache()
# Close files
dst_ds = None
os.remove(src_file)
if task.isCanceled():
return None
return {'task': task.description(),
'file': dst_filename}
示例9: open_vector
# 需要导入模块: from osgeo import gdal [as 别名]
# 或者: from osgeo.gdal import OpenEx [as 别名]
def open_vector(filename, driver=None, layer=0):
"""Open vector file, return gdal.Dataset and OGR.Layer
.. warning:: dataset and layer have to live in the same context,
if dataset is deleted all layer references will get lost
Parameters
----------
filename : string
vector file name
driver : string
gdal driver string
layer : int or string
Returns
-------
dataset : gdal.Dataset
dataset
layer : ogr.Layer
layer
"""
dataset = gdal.OpenEx(filename)
if driver:
gdal.GetDriverByName(driver)
layer = dataset.GetLayer(layer)
return dataset, layer