/
process-csv.php
180 lines (151 loc) · 5.78 KB
/
process-csv.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/*****
* Author: Ethan Gruber
* Date August 2019
* Function: Process Indianapolis Museum of Art spreadsheet into simple Kerameikos.org-compliant RDF
*****/
//generate RDF
$writer = new XMLWriter();
$writer->openURI('ima.rdf');
//$writer->openURI('php://output');
$writer->startDocument('1.0','UTF-8');
$writer->setIndent(true);
//now we need to define our Indent string,which is basically how many blank spaces we want to have for the indent
$writer->setIndentString(" ");
$writer->startElement('rdf:RDF');
$writer->writeAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema#');
$writer->writeAttribute('xmlns:kid', "http://kerameikos.org/id/");
$writer->writeAttribute('xmlns:kon', "http://kerameikos.org/ontology#");
$writer->writeAttribute('xmlns:dcterms', "http://purl.org/dc/terms/");
$writer->writeAttribute('xmlns:foaf', "http://xmlns.com/foaf/0.1/");
$writer->writeAttribute('xmlns:geo', "http://www.w3.org/2003/01/geo/wgs84_pos#");
$writer->writeAttribute('xmlns:rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
$writer->writeAttribute('xmlns:void', "http://rdfs.org/ns/void#");
$writer->writeAttribute('xmlns:crm', "http://www.cidoc-crm.org/cidoc-crm/");
$data = generate_json('ima.csv');
foreach ($data as $row){
$writer->startElement('crm:E22_Man-Made_Object');
$writer->writeAttribute('rdf:about', $row['URI']);
$writer->startElement('dcterms:title');
$writer->writeAttribute('xml:lang', 'en');
$writer->text($row['TitMainTitle']);
$writer->endElement();
$writer->writeElement('dcterms:identifier', $row['TitAccessionNo']);
if (strlen($row['Material URI']) > 0){
$writer->startElement('crm:P45_consists_of');
$writer->writeAttribute('rdf:resource', $row['Material URI']);
$writer->endElement();
}
if (strlen($row['Technique URI']) > 0){
$writer->startElement('crm:P32_used_general_technique');
$writer->writeAttribute('rdf:resource', $row['Technique URI']);
$writer->endElement();
}
if (strlen($row['Shape URI']) > 0){
$writer->startElement('kon:hasShape');
$writer->writeAttribute('rdf:resource', $row['Shape URI']);
$writer->endElement();
}
//production
if (strlen($row['Artist URI']) > 0 || strlen($row['Production Place URI']) > 0 || (strlen($row['CreEarliestDate']) > 0 && strlen($row['CreLatestDate']) > 0)){
$writer->startElement('crm:P108i_was_produced_by');
$writer->startElement('crm:E12_Production');
if (strlen($row['Production Place URI']) > 0){
$writer->startElement('crm:P7_took_place_at');
$writer->writeAttribute('rdf:resource', $row['Production Place URI']);
$writer->endElement();
}
if (strlen($row['Artist URI']) > 0){
$writer->startElement('crm:P14_carried_out_by');
$writer->writeAttribute('rdf:resource', $row['Artist URI']);
$writer->endElement();
}
if (strlen($row['CreEarliestDate']) > 0 && strlen($row['CreLatestDate']) > 0){
$writer->startElement('crm:P4_has_time-span');
$writer->startElement('crm:E52_Time-Span');
$writer->startElement('crm:P82a_begin_of_the_begin');
$writer->writeAttribute('rdf:datatype', 'http://www.w3.org/2001/XMLSchema#gYear');
$writer->text(number_pad($row['CreEarliestDate'], 4));
$writer->endElement();
$writer->startElement('crm:P82b_end_of_the_end');
$writer->writeAttribute('rdf:datatype', 'http://www.w3.org/2001/XMLSchema#gYear');
$writer->text(number_pad($row['CreLatestDate'], 4));
$writer->endElement();
$writer->endElement();
$writer->endElement();
}
$writer->endElement();
$writer->endElement();
}
//initiate DOMDocument call to harvest image URLs
$html = file_get_contents($row['URI']);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
$images = $xpath->query("//div[contains(@class, 'object-thumbnail')]/img");
if ($images->length > 0){
$imageURL = $images->item(0)->getAttribute('src');
/*$writer->startElement('foaf:thumbnail');
$writer->writeAttribute('rdf:resource', $imageURL);
$writer->endElement();*/
$writer->startElement('foaf:depiction');
$writer->writeAttribute('rdf:resource', str_replace('_thumb', '_full', $imageURL));
$writer->endElement();
}
//collection
$writer->startElement('crm:P50_has_current_keeper');
$writer->writeAttribute('rdf:resource', 'http://kerameikos.org/id/ima_newfields');
$writer->endElement();
//dataset
$writer->startElement('void:inDataset');
$writer->writeAttribute('rdf:resource', 'https://discovernewfields.org/');
$writer->endElement();
$writer->endElement();
//echo "{$row['URI']}\n";
}
$writer->endElement();
$writer->flush();
/***** FUNCTIONS *****/
//pad integer value from Filemaker to create a year that meets the xs:gYear specification
function number_pad($number,$n) {
if ($number > 0){
$gYear = str_pad((int) $number,$n,"0",STR_PAD_LEFT);
} elseif ($number < 0) {
$bcNum = (int)abs($number);
$gYear = '-' . str_pad($bcNum,$n,"0",STR_PAD_LEFT);
}
return $gYear;
}
function generate_json($doc){
$keys = array();
$geoData = array();
$data = csvToArray($doc, ',');
// Set number of elements (minus 1 because we shift off the first row)
$count = count($data) - 1;
//Use first row for names
$labels = array_shift($data);
foreach ($labels as $label) {
$keys[] = $label;
}
// Bring it all together
for ($j = 0; $j < $count; $j++) {
$d = array_combine($keys, $data[$j]);
$geoData[$j] = $d;
}
return $geoData;
}
// Function to convert CSV into associative array
function csvToArray($file, $delimiter) {
if (($handle = fopen($file, 'r')) !== FALSE) {
$i = 0;
while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) {
for ($j = 0; $j < count($lineArray); $j++) {
$arr[$i][$j] = $lineArray[$j];
}
$i++;
}
fclose($handle);
}
return $arr;
}
?>