def test_open_closed_zip(): """Get an exception when opening a dataset on a closed ZipMemoryFile""" memfile = ZipMemoryFile() memfile.close() assert memfile.closed with pytest.raises(OSError): memfile.open()
def get_bigquery_schema(filepath, layer_name=None, gdb_name=None): """ Generate a Bigquery table schema from a geospatial file python -m geobeam.util get_bigquery_schema ...args Args: filepath (str): full path to the input file layer_name (str, optional): name of the layer, if file contains multiple layers Returns: dict: the schema, convertable to json by json.dumps(schema, indent=2) """ import fiona from fiona.io import ZipMemoryFile from fiona import prop_type bq_schema = [] if layer_name is None: profile = fiona.open(filepath).profile elif gdb_name is None: profile = fiona.open(filepath, layer=layer_name).profile else: f = open(filepath, 'rb') mem = ZipMemoryFile(f.read()) profile = mem.open(gdb_name, layer=layer_name).profile for field_name, field_type in profile['schema']['properties'].items(): fiona_type = prop_type(field_type) bq_type = BQ_FIELD_TYPES[fiona.schema.FIELD_TYPES_MAP_REV[fiona_type]] bq_schema.append({'name': field_name, 'type': bq_type}) bq_schema.append({ 'name': 'geom', 'type': 'GEOGRAPHY', 'description': '{} reprojected from {}. source: {}'.format( profile['schema']['geometry'], profile['crs']['init'], profile['driver']) }) return bq_schema