Skip to content

Commit 217b5ff

Browse files
committed
fix export to schem-org
1 parent 9b941bf commit 217b5ff

File tree

1 file changed

+61
-73
lines changed

1 file changed

+61
-73
lines changed

pygeometa/schemas/schema_org/__init__.py

+61-73
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,16 @@ def write(self, mcf: dict, stringify: str = True) -> Union[dict, str]:
216216
LOGGER.debug('Generating baseline record')
217217
record = {
218218
'identifier': mcf['metadata']['identifier'],
219-
'@type': dict(zip(TYPES.values(), TYPES.keys()))[mcf['metadata']['hierarchylevel']], # noqa
219+
"@context": "http://schema.org/",
220+
'@type': 'schema:' + dict(zip(TYPES.values(), TYPES.keys()))[mcf['metadata']['hierarchylevel']], # noqa
220221
'spatialCoverage': [{
221-
'@type': 'Place',
222+
'@type': 'schema:Place',
222223
'geo': {
223-
'@type': 'GeoShape',
224+
'@type': 'schema:GeoShape',
224225
'box': f'{miny},{minx} {maxy},{maxx}'
225226
}
226227
}],
227-
'title': title[0],
228+
'name': title[0],
228229
'description': description[0],
229230
'distribution': []
230231
}
@@ -234,7 +235,7 @@ def write(self, mcf: dict, stringify: str = True) -> Union[dict, str]:
234235

235236
LOGGER.debug('Checking for temporal')
236237
try:
237-
begin = mcf['identification']['extents']['temporal'][0]['begin']
238+
begin = mcf['identification']['extents']['temporal'][0].get('begin')
238239
end = mcf['identification']['extents']['temporal'][0].get('end')
239240

240241
if begin in ['now', 'None', None]:
@@ -267,8 +268,9 @@ def write(self, mcf: dict, stringify: str = True) -> Union[dict, str]:
267268
record['datePublished'] = generate_datetime(value)
268269

269270
LOGGER.debug('Checking for contacts')
270-
record['contacts'] = self.generate_contacts(
271-
mcf['contact'])
271+
for ct in ['author', 'publisher', 'creator', 'provider', 'funder']:
272+
record[ct] = self.generate_contacts(
273+
mcf['contact'], ct)
272274

273275
all_keywords = []
274276

@@ -306,13 +308,7 @@ def write(self, mcf: dict, stringify: str = True) -> Union[dict, str]:
306308

307309
if 'url' in license:
308310
LOGGER.debug('Encoding license as link')
309-
license_link = {
310-
'rel': 'license',
311-
'type': 'text/html',
312-
'title': license.get('name', 'license for this resource'),
313-
'url': license['url']
314-
}
315-
record['distribution'].append(self.generate_link(license_link))
311+
record['license'] = license['url']
316312
else:
317313
LOGGER.debug('Encoding license as property')
318314
record['license'] = license['name']
@@ -327,14 +323,14 @@ def write(self, mcf: dict, stringify: str = True) -> Union[dict, str]:
327323
return record
328324

329325
def generate_party(self, contact: dict,
330-
lang1: str, lang2: str, roles: list) -> dict:
326+
lang1: str, lang2: str) -> dict:
331327
"""
332328
generate party construct from MCF contact
333329
334330
:param contact: dict of MCF contact
335331
:param self.lang1: primary language
336332
:param self.lang2: alternate language
337-
:param roles: roles of contact
333+
338334
339335
:returns: MCF contact as a party representation
340336
"""
@@ -373,60 +369,58 @@ def generate_party(self, contact: dict,
373369
'roles': []
374370
}
375371

376-
if organization_name[0] is not None:
377-
rp['organization'] = organization_name[0]
378372
if individual_name[0] is not None:
373+
rp['@type'] = "schema:Person"
379374
rp['name'] = individual_name[0]
380-
if position_name[0] is not None:
381-
rp['position'] = position_name[0]
382-
if hours_of_service[0] is not None:
383-
rp['hoursOfService'] = hours_of_service[0]
384-
if contact_instructions[0] is not None:
385-
rp['contactInstructions'] = contact_instructions[0]
375+
if position_name[0] is not None:
376+
rp['jobTitle'] = position_name[0]
377+
rp['affiliation'] = {
378+
'@type': "schema:Organization",
379+
'name': organization_name[0]
380+
}
381+
else:
382+
rp['@type'] = "schema:Organization"
383+
rp['name'] = organization_name[0]
384+
385+
#if hours_of_service[0] is not None:
386+
# rp['hoursOfService'] = hours_of_service[0]
387+
#if contact_instructions[0] is not None:
388+
# rp['contactInstructions'] = contact_instructions[0]
386389

387390
if address[0] is not None:
388-
rp['addresses'][0]['deliveryPoint'] = [address[0]]
389-
if city[0] is not None:
390-
rp['addresses'][0]['city'] = city[0]
391-
if administrative_area[0] is not None:
392-
rp['addresses'][0]['administrativeArea'] = administrative_area[0]
393-
if postalcode[0] is not None:
394-
rp['addresses'][0]['postalCode'] = postalcode[0]
395-
if country[0] is not None:
396-
rp['addresses'][0]['country'] = country[0]
391+
rp['address'] = {"@type": "schema:PostalAddress"}
392+
rp['address']['streetAddress'] = address[0]
393+
if city[0] is not None:
394+
rp['address']['addressLocality'] = city[0]
395+
if administrative_area[0] is not None:
396+
rp['address']['addressRegion'] = administrative_area[0]
397+
if postalcode[0] is not None:
398+
rp['address']['postalCode'] = postalcode[0]
399+
if country[0] is not None:
400+
rp['address']['addressCountry'] = country[0]
397401

398402
if contact.get('phone') is not None:
399403
LOGGER.debug('Formatting phone number')
400404
phone = contact['phone']
401405
phone = phone.replace('-', '').replace('(', '').replace(')', '')
402406
phone = phone.replace('+0', '+').replace(' ', '')
403-
404-
rp['phones'] = [{'value': phone}]
407+
rp['telephone'] = phone
405408

406409
if contact.get('email') is not None:
407-
rp['emails'] = [{'value': contact.get('email')}]
408-
409-
if rp['addresses'][0] == {}:
410-
rp.pop('addresses')
411-
412-
for r in set(roles):
413-
rp['roles'].append(r)
410+
rp['email'] = contact.get('email')
414411

415412
if 'url' in contact:
416-
rp['distribution'] = [{
417-
'rel': 'canonical',
418-
'type': 'text/html',
419-
'href': contact['url']
420-
}]
413+
rp['url'] = contact['url']
421414

422415
return rp
423416

424-
def generate_contacts(self, contact: dict) -> list:
417+
def generate_contacts(self, contact: dict, tp: str) -> list:
425418
"""
426419
Generates 1..n contacts, streamlining identical
427420
contacts with multiple roles
428421
429422
:param contact: `dict` of contacts
423+
:param tp: `str` of role
430424
431425
:returns: `list` of contacts
432426
"""
@@ -435,29 +429,16 @@ def generate_contacts(self, contact: dict) -> list:
435429
contacts2 = []
436430

437431
for key, value in contact.items():
438-
if contacts:
439-
for c in contacts:
440-
if value == c['contact']:
441-
LOGGER.debug('Found matching contact; adding role')
442-
c['roles'].append(key)
443-
else:
444-
LOGGER.debug('Adding contact')
445-
contacts.append({
446-
'contact': value,
447-
'roles': [key]
448-
})
449-
else:
450-
contacts.append({
451-
'contact': value,
452-
'roles': [key]
453-
})
432+
if key == tp or value.get('role','') == tp:
433+
contacts.append(
434+
self.generate_party(value, self.lang1,
435+
self.lang2))
436+
437+
454438

455-
LOGGER.debug(f'Contacts: {contacts}')
456-
for c in contacts:
457-
contacts2.append(self.generate_party(c['contact'], self.lang1,
458-
self.lang2, c['roles']))
459439

460-
return contacts2
440+
441+
return contacts
461442

462443
def generate_link(self, distribution: dict) -> dict:
463444
"""
@@ -471,20 +452,27 @@ def generate_link(self, distribution: dict) -> dict:
471452
name = get_charstring(distribution.get('name'),
472453
self.lang1, self.lang2)
473454

455+
desc = get_charstring(distribution.get('description'),
456+
self.lang1, self.lang2)
457+
474458
link = {
459+
'@type': 'schema:DataDownload',
475460
'contentUrl': distribution['url']
476461
}
477462

478463
if distribution.get('type') is not None:
479-
link['type'] = distribution['type']
464+
link['encodingFormat'] = distribution['type']
480465

481-
reltype = distribution.get('rel') or distribution.get('function')
482-
if reltype is not None:
483-
link['rel'] = reltype
466+
#reltype = distribution.get('rel') or distribution.get('function')
467+
#if reltype is not None:
468+
# link['rel'] = reltype
484469

485470
if name != [None, None]:
486471
link['name'] = name[0]
487472
elif name != [None, None]:
488473
link['name'] = name[0]
474+
475+
if desc != [None, None]:
476+
link['description'] = desc[0]
489477

490478
return link

0 commit comments

Comments
 (0)