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

[FINNA-689][FINNA-690] QDC: Index temporal and spatial coverage and add support for geocoding #62

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
89 changes: 69 additions & 20 deletions src/RecordManager/Finna/Record/QdcRecordTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,10 @@ public function toSolrArray(Database $db = null)
}
}

foreach ($this->doc->coverage as $coverage) {
$attrs = $coverage->attributes();
if ($attrs->type == 'geocoding') {
$match = preg_match(
'/([\d\.]+)\s*,\s*([\d\.]+)/',
trim((string)$coverage),
$matches
);
if ($match) {
if ($attrs->format == 'lon,lat') {
$lon = $matches[1];
$lat = $matches[2];
} else {
$lat = $matches[1];
$lon = $matches[2];
}
$data['location_geo'][] = "POINT($lon $lat)";
}
}
}
$data['era'] = $data['era_facet'] = $this->getCoverageByType('temporal');
$data['geographic'] = $data['geographic_facet'] = $this->getCoverageByType('spatial');
$data['location_geo'] = $this->getCoverageByType('geocoding');

if (!empty($data['location_geo'])) {
$data['center_coords']
= $this->metadataUtils->getCenterCoordinates($data['location_geo']);
Expand Down Expand Up @@ -187,6 +171,71 @@ public function toSolrArray(Database $db = null)
return $data;
}

/**
* Get locations for geocoding
*
* Returns an associative array of primary and secondary locations
*
* @return array
*/
public function getLocations()
EreMaijala marked this conversation as resolved.
Show resolved Hide resolved
{
$locations = [];
// If there is already coordinates in the record, don't return anything for geocoding
if (!$this->getCoverageByType('geocoding')) {
$locations = $this->getCoverageByType('spatial');
}
return [
'primary' => $locations,
'secondary' => [],
];
}

/**
* Get coverage by type
*
* @param string $type Type attribute
*
* @return array
*/
protected function getCoverageByType(string $type)
EreMaijala marked this conversation as resolved.
Show resolved Hide resolved
{
$result = [];
foreach ($this->doc->coverage as $coverage) {
if ($type !== (string)$coverage->attributes()->type) {
continue;
}
$cov = trim((string)$coverage);
// Check if field contains coordinates
$match = preg_match(
'/([\d\.]+)\s*,\s*([\d\.]+)/',
$cov,
$matches
);
// If type is geocoding, return only coordinates.
// Other types might contain ill-formatted coordinates which should be discarded.
switch ($type) {
EreMaijala marked this conversation as resolved.
Show resolved Hide resolved
case 'geocoding':
if ($match) {
if ($coverage->attributes()->format == 'lon,lat') {
$lon = $matches[1];
$lat = $matches[2];
} else {
$lat = $matches[1];
$lon = $matches[2];
}
$result[] = "POINT($lon $lat)";
}
break;
default:
if (!$match && $stripped = $this->metadataUtils->stripTrailingPunctuation($cov, '.')) {
$result[] = $stripped;
}
}
}
return $result;
}

/**
* Check if the needle is found in the haystack using fnmatch for comparison
*
Expand Down
33 changes: 33 additions & 0 deletions tests/RecordManagerTest/Finna/Record/QdcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,39 @@ public function testDateRanges()
$this->assertEquals($expected, $fields);
}

/**
* Test coverage.
*
* @return void
*/
public function testCoverage()
{
$spatial = [
'Helsinki',
'Vantaa',
];
$temporal = [
'2010',
'2010-luku',
];
$geocoding = ['POINT(27.1826451 63.5694237)'];
$fields = $this->createRecord(
Qdc::class,
'qdc_dateranges.xml',
[],
'Finna',
[
$this->createMock(\RecordManager\Base\Http\ClientManager::class),
]
);
$fields = $fields->toSolrArray();
$this->assertEquals($spatial, $fields['geographic']);
$this->assertEquals($spatial, $fields['geographic_facet']);
$this->assertEquals($geocoding, $fields['location_geo']);
$this->assertEquals($temporal, $fields['era']);
$this->assertEquals($temporal, $fields['era_facet']);
}

/**
* Test media types
*
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/Finna/record/qdc_dateranges.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,14 @@
<date>20221212-20221214</date>
<date>20221210</date>
<date>2 028----00 05</date>

<!-- Dates and places in coverage -->
<coverage>Undefined coverage type</coverage>
<coverage type="temporal">2010</coverage>
<coverage type="temporal"> 2010-luku.</coverage>
<coverage type="spatial">Helsinki.</coverage>
<coverage type="spatial">Vantaa</coverage>
<coverage type="spatial">63.0,27.0</coverage>
<coverage type="geocoding" format="lat,lon">63.5694237,27.1826451</coverage>
EreMaijala marked this conversation as resolved.
Show resolved Hide resolved
<coverage type="geocoding" format="lat,lon">Iisalmi</coverage>
</dc>
Loading