Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A couple quick fixes #4

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/pygeometadata/mcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def set_abstract(self, abstract):
self.mcf['identification']['abstract'] = abstract

def set_keywords(self, keywords, section='default', keywords_type='theme',
vocabulary=None):
vocabulary=None):
"""Describe a dataset with a list of keywords.

Keywords are grouped into sections for the purpose of complying with
Expand Down Expand Up @@ -299,6 +299,7 @@ def describe_band(self, band_number, name=None, title=None, abstract=None,
def describe_field(self, name, title=None, abstract=None,
units=None):
"""Define metadata for a tabular field.

Args:
name (str): name and unique identifier of the field
title (str): title for the field
Expand All @@ -324,7 +325,16 @@ def get_attr(attribute_list):
self.mcf['content_info']['attributes'][idx] = attribute

def write(self):
"""Write MCF to disk."""
"""Write MCF and ISO-19139 XML to disk.

This creates sidecar files with '.yml' and '.xml' extensions
appended to the full filename of the data source. For example,

- 'myraster.tif'
- 'myraster.tif.yml'
- 'myraster.tif.xml'

"""
with open(self.mcf_path, 'w') as file:
file.write(yaml.dump(self.mcf, Dumper=_NoAliasDumper))
# TODO: allow user to override the iso schema choice
Expand All @@ -347,8 +357,9 @@ def to_string(self):

def _set_spatial_info(self):
"""Populate the MCF using properties of the dataset."""
gis_type = pygeoprocessing.get_gis_type(self.datasource)
if gis_type == pygeoprocessing.UNKNOWN_TYPE:
try:
gis_type = pygeoprocessing.get_gis_type(self.datasource)
except ValueError:
self.mcf['metadata']['hierarchylevel'] = 'nonGeographicDataset'
return

Expand Down Expand Up @@ -388,7 +399,8 @@ def _set_spatial_info(self):
attribute['title'] = ''
attribute['abstract'] = ''
attributes.append(attribute)
self.mcf['content_info']['attributes'] = attributes
if len(attributes):
self.mcf['content_info']['attributes'] = attributes
vector = None
layer = None

Expand All @@ -412,7 +424,8 @@ def _set_spatial_info(self):
attribute['title'] = ''
attribute['abstract'] = band.GetDescription()
attributes.append(attribute)
self.mcf['content_info']['attributes'] = attributes
if len(attributes):
self.mcf['content_info']['attributes'] = attributes
raster = None

gis_info = pygeoprocessing.get_raster_info(self.datasource)
Expand Down
28 changes: 23 additions & 5 deletions tests/test_mcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
}


def create_vector(target_filepath, field_map):
attribute_list = [{
k: _OGR_TYPES_VALUES_MAP[v]
for k, v in field_map.items()
}]
def create_vector(target_filepath, field_map=None):
attribute_list = None
if field_map:
attribute_list = [{
k: _OGR_TYPES_VALUES_MAP[v]
for k, v in field_map.items()
}]
projection = osr.SpatialReference()
projection.ImportFromEPSG(3116)
pygeoprocessing.shapely_geometry_to_vector(
Expand Down Expand Up @@ -112,6 +114,22 @@ def test_vector_MCF(self):
f'{e}')
mcf.write()

def test_vector_no_fields(self):
"""MCF: validate MCF for basic vector with no fields."""
from pygeometadata.mcf import MCF

datasource_path = os.path.join(self.workspace_dir, 'vector.geojson')
create_vector(datasource_path, None)

mcf = MCF(datasource_path)
try:
mcf.validate()
except (MCFValidationError, SchemaError) as e:
self.fail(
'unexpected validation error occurred\n'
f'{e}')
mcf.write()

def test_raster_MCF(self):
"""MCF: validate basic raster MCF."""
from pygeometadata.mcf import MCF
Expand Down