GeoDatabase
gma.gio.GeoDatabase
[class] GeoDatabase(data = None, driver = None, out_dst = None):Initialize a GeoDatabase object.
Optional:
data = None||str. Default None.
If None, create a new empty GeoDatabase. If str, open the file path as GeoDatabase.
driver = str||None. Default None.
If data is None, the driver name to create the GeoDatabase.
out_dst = str||None. Default None.
If data is None, the output file path to create the GeoDatabase.
Example
from gma import gio
gdb = gio.open_file('NewGDB.gpkg') # the real GeoPackage from the sample data
print('driver:', gdb.driver)
print('layers:', gdb.layer_names)
print('count :', gdb.layer_count)
driver: GPKG - Vector
layers: ['New_Australia', 'Australia']
count : 2
Method
add_layer(layer, new_name = None, over_write = False, **kwargs)
Add Layer to GeoDataBase.
Parameters:
GMALayer: Layer.
Another GMA Layer!
Optional:
new_name = str||None. Default None.
The new name of the added Layer. Default is the added Layer's name.
OverWrite = bool. Default False.
Whether to force overwrite the layer with the same name.
Returns:
Type: None.
Add the Layer in place. NOTE: gma 3.0.4.1 segfaults on this call.
Example:
from gma import gio
gdb = gio.open_file('NewGDB.gpkg')
ly = gio.open_vector('Australia.shp')
gdb.add_layer(ly, new_name='australia_copy')
print(gdb.layer_names)
Segmentation fault (gma 3.0.4.1 terminates the process before printing)
create_layer(name = None, crs = None, gtype = "Polygon", options = {})
Create a new layer in GeoDatabase.
Optional:
name = str||None. Default None.
The name for the new layer. This should ideally not match any existing layer on the datasource. None for randint.
crs = int||str||CoordinateReferenceSystem. Default None.
The coordinate system to use for the new layer, or None if no coordinate system is available.
gtype = int. Default 'Polygon'.
Geometry type for the layer. Use 'Polygon' if there are no constraints on the types geometry to be written.
options = dict||list. Default [].
Driver-specific dict or list of `name=value` options.
Returns:
Type: Layer.
The newly created empty layer.
Example:
from gma import gio
gdb = gio.GeoDatabase(driver='GPKG', out_dst='new.gpkg')
print('Before:', gdb.layer_names)
lyr = gdb.create_layer('cities', crs='WGS84', gtype='Point')
print('After :', gdb.layer_names)
print('Name :', lyr.name, type(lyr).__name__, lyr.gtype)
Before: []
After : ['cities']
Name : cities Layer Point
delete_layer(name_or_id)
Delete a Layer in GeoDataBase.
Parameters:
name_or_id: int||str.
Layer name or ID in GeoDataBase!
Returns:
Type: None.
Delete the layer from the database in place.
Example:
from gma import gio
gdb = gio.GeoDatabase(driver='GPKG', out_dst='delete.gpkg')
gdb.create_layer('tmp_layer', crs='WGS84', gtype='Point')
print('Before:', gdb.layer_names)
gdb.delete_layer('tmp_layer')
print('After :', gdb.layer_names)
Before: ['tmp_layer']
After : []
get_dataset(name_or_id = 0)
Get a Dataset in GeoDataBase.
Optional:
name_or_id = int. Default 0.
Dataset ID or name in the GeoDataBase to get.
Returns:
Type: Dataset.
The raster dataset with the given name or ID.
Example:
from gma import gio
gdb = gio.open_file('NewGDB.gpkg')
print('datasets:', gdb.dataset_names)
try:
ds = gdb.get_dataset(0)
except Exception as e:
print('get_dataset(0):', type(e).__name__, e)
datasets: []
get_dataset(0): GMAError Cannot get the DataSet(0)
get_layer(name_or_id = 0)
Get a Layer in GeoDatabase.
Optional:
name_or_id = int||str. Default 0.
Layer id or name in the GeoDatabase to get.
Returns:
Type: Layer.
The vector layer with the given name or ID.
Example:
from gma import gio
gdb = gio.open_file('NewGDB.gpkg')
ly = gdb.get_layer('Australia')
print(type(ly).__name__, ly.name, ly.gtype, len(ly))
print('bounds:', tuple(round(v, 2) for v in ly.bounds))
Layer Australia Polygon 9
bounds: (112.91, -43.62, 153.62, -10.05)
iteritems(category = None, **kwargs)
Iterate vector layers and raster datasets. --> iter
Returns:
Type: iter.
Iterate over vector layers (0 / "Layer") or raster datasets ("Dataset").
Example:
from gma import gio
gdb = gio.open_file('NewGDB.gpkg')
for item in gdb.iteritems(0): # 0 / 'Layer': vector layers
print(type(item).__name__, item.name, item.gtype, len(item))
Layer New_Australia Polygon 18
Layer Australia Polygon 9
query(expr, tran_opts = {}, **kwargs)
Query the vector Layer with a SpatiaLite SQL expression.
Parameters:
expr: str.
The sql(ite) query string to evaluate.
Optional:
tran_opts = dict. Default {}.
Other parameters passed to ._aio.layer.vector_translate.
**kwargs:
Other parameters passed to ._aio.vrt.VRTGeoDatabase.add_file.
Returns:
Type: Layer.
A new layer with the result of the SpatiaLite SQL query.
Example:
from gma import gio
gdb = gio.open_file('NewGDB.gpkg')
res = gdb.query('SELECT * FROM Australia LIMIT 3')
print(type(res).__name__, res.gtype, len(res))
print('bounds:', tuple(round(v, 2) for v in res.bounds))
Layer Polygon 3
bounds: (112.91, -38.07, 141.03, -10.97)
Property
dataset_count
Number of datasets. --> int
Example:
from gma import gio
gdb = gio.open_file('NewGDB.gpkg')
print(gdb.dataset_count)
0
dataset_names
Names of datasets. --> list[str]
Example:
from gma import gio
gdb = gio.open_file('NewGDB.gpkg')
print(gdb.dataset_names)
[]
driver
Database driver. --> VectorDriver/RasterDriver
Example:
from gma import gio
gdb = gio.open_file('NewGDB.gpkg')
print(gdb.driver)
GPKG - Vector
files
GeoDatabase associated files. --> list[str]
Example:
from gma import gio
gdb = gio.open_file('NewGDB.gpkg')
print(gdb.files)
['NewGDB.gpkg']
info
Source metadata. --> dict
Example:
from gma import gio
gdb = gio.open_file('NewGDB.gpkg')
print('keys:', list(gdb.info.keys()))
for key in ('description', 'driverShortName', 'driverLongName', 'size'):
print(key, ':', gdb.info[key])
keys: ['description', 'driverShortName', 'driverLongName', 'files', 'size', 'coordinateSystem', 'metadata', 'cornerCoordinates', 'bands', 'stac']
description : NewGDB.gpkg
driverShortName : GPKG
driverLongName : GeoPackage
size : [512, 512]
layer_count
Number of layers. --> int
Example:
from gma import gio
gdb = gio.open_file('NewGDB.gpkg')
print(gdb.layer_count)
2
layer_names
Name of Layers. --> list[str]
Example:
from gma import gio
gdb = gio.open_file('NewGDB.gpkg')
print(gdb.layer_names)
['New_Australia', 'Australia']
link
GeoDatabase path link. --> str
Example:
from gma import gio
gdb = gio.open_file('NewGDB.gpkg')
print(gdb.link)
NewGDB.gpkg
name
GeoDatabase Name. --> str
Example:
from gma import gio
gdb = gio.open_file('NewGDB.gpkg')
print(gdb.name)
NewGDB