-
Notifications
You must be signed in to change notification settings - Fork 1
/
GeoJsonFeatureCollectionParserPlugin.java
248 lines (219 loc) · 11.5 KB
/
GeoJsonFeatureCollectionParserPlugin.java
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Copyright 2017-2022 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
*
* This file is part of REGARDS.
*
* REGARDS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* REGARDS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with REGARDS. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.cnes.regards.modules.acquisition.service.plugins;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import fr.cnes.regards.framework.geojson.Feature;
import fr.cnes.regards.framework.geojson.FeatureCollection;
import fr.cnes.regards.framework.modules.plugins.annotations.Plugin;
import fr.cnes.regards.framework.modules.plugins.annotations.PluginParameter;
import fr.cnes.regards.framework.urn.DataType;
import fr.cnes.regards.framework.utils.file.ChecksumUtils;
import fr.cnes.regards.framework.utils.plugins.PluginUtilsRuntimeException;
import fr.cnes.regards.modules.acquisition.plugins.IScanPlugin;
import fr.cnes.regards.modules.ingest.dto.sip.SIP;
import fr.cnes.regards.modules.ingest.dto.sip.SIPBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/**
* This plugin allows to scan a directory to find geojson files and generate a new file to acquire for each feature found in it.
*
* @author Sébastien Binda
*/
@Plugin(id = "GeoJsonFeatureCollectionParserPlugin",
version = "1.0.0-SNAPSHOT",
description = "Scan directory to detect geosjson files. Generate a file to acquire for each feature found in it.",
author = "REGARDS Team",
contact = "regards@c-s.fr",
license = "GPLv3",
owner = "CSSI",
url = "https://github.com/RegardsOss")
public class GeoJsonFeatureCollectionParserPlugin implements IScanPlugin {
private static final Logger LOGGER = LoggerFactory.getLogger(GlobDiskScanning.class);
public static final String FIELD_FEATURE_ID = "featureId";
public static final String ALLOW_EMPTY_FEATURES = "allowEmptyFeatures";
@Autowired
private Gson gson;
@PluginParameter(name = FIELD_FEATURE_ID,
label = "Json path to access the identifier of each feature in the geojson file",
optional = false)
private String featureId;
@PluginParameter(name = ALLOW_EMPTY_FEATURES,
label = "Generate features with no files (raw, thumbnail & description) associated.",
optional = true,
defaultValue = "false")
private boolean allowEmptyFeature;
@Override
public List<Path> scan(Path dirPath, Optional<OffsetDateTime> scanningDate) {
List<Path> scannedFiles = new ArrayList<>();
if (Files.isDirectory(dirPath)) {
scannedFiles.addAll(scanDirectory(dirPath, scanningDate));
} else {
throw new PluginUtilsRuntimeException(String.format("Invalid directory path : %s", dirPath.toString()));
}
return scannedFiles;
}
private List<Path> scanDirectory(Path dirPath, Optional<OffsetDateTime> scanningDate) {
List<Path> genetateFeatureFiles = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dirPath, "*.geojson")) {
for (Path entry : stream) {
if (Files.isRegularFile(entry)) {
if (scanningDate.isPresent()) {
OffsetDateTime lmd = OffsetDateTime.ofInstant(Files.getLastModifiedTime(entry).toInstant(),
ZoneOffset.UTC);
if (lmd.isAfter(scanningDate.get()) || lmd.isEqual(scanningDate.get())) {
genetateFeatureFiles.addAll(generateFeatureFiles(entry));
}
} else {
genetateFeatureFiles.addAll(generateFeatureFiles(entry));
}
}
}
} catch (IOException x) {
throw new PluginUtilsRuntimeException("Scanning failure", x);
}
return genetateFeatureFiles;
}
private List<Path> generateFeatureFiles(Path entry) {
List<Path> generatedFiles = new ArrayList<>();
try {
// Check if file is a gson feature collection
File gsonFile = entry.toFile();
JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(gsonFile)));
FeatureCollection fc = gson.fromJson(reader, FeatureCollection.class);
for (Feature feature : fc.getFeatures()) {
String name = (String) feature.getProperties().get(featureId);
SIPBuilder builder = new SIPBuilder(name);
for (String property : feature.getProperties().keySet()) {
Object value = feature.getProperties().get(property);
if (value != null) {
builder.addDescriptiveInformation(property, value);
}
}
// Check for RAWDATA if any
Path rawDataFile = Paths.get(entry.getParent().toString(), name + ".dat");
Path thumbnailFilePng = Paths.get(entry.getParent().toString(), name + ".png");
Path thumbnailFileJpg = Paths.get(entry.getParent().toString(), name + ".jpg");
Path descFile = Paths.get(entry.getParent().toString(), name + ".pdf");
if (Files.exists(rawDataFile)) {
String checksum = ChecksumUtils.computeHexChecksum(new FileInputStream(rawDataFile.toFile()),
"MD5");
builder.getContentInformationBuilder()
.setDataObject(DataType.RAWDATA,
rawDataFile.toAbsolutePath(),
rawDataFile.getFileName().toString(),
"MD5",
checksum,
rawDataFile.toFile().length());
builder.getContentInformationBuilder().setSyntax(MediaType.APPLICATION_OCTET_STREAM);
builder.addContentInformation();
}
if (Files.exists(thumbnailFilePng)) {
String checksum = ChecksumUtils.computeHexChecksum(new FileInputStream(thumbnailFilePng.toFile()),
"MD5");
builder.getContentInformationBuilder()
.setDataObject(DataType.THUMBNAIL,
thumbnailFilePng.toAbsolutePath(),
thumbnailFilePng.getFileName().toString(),
"MD5",
checksum,
thumbnailFilePng.toFile().length());
builder.getContentInformationBuilder().setSyntax(MediaType.IMAGE_PNG);
builder.addContentInformation();
builder.getContentInformationBuilder()
.setDataObject(DataType.QUICKLOOK_SD,
thumbnailFilePng.toAbsolutePath(),
thumbnailFilePng.getFileName().toString(),
"MD5",
checksum,
thumbnailFilePng.toFile().length());
builder.getContentInformationBuilder().setSyntax(MediaType.IMAGE_PNG);
builder.addContentInformation();
}
if (Files.exists(thumbnailFileJpg)) {
String checksum = ChecksumUtils.computeHexChecksum(new FileInputStream(thumbnailFileJpg.toFile()),
"MD5");
builder.getContentInformationBuilder()
.setDataObject(DataType.THUMBNAIL,
thumbnailFileJpg.toAbsolutePath(),
thumbnailFileJpg.getFileName().toString(),
"MD5",
checksum,
thumbnailFileJpg.toFile().length());
builder.getContentInformationBuilder().setSyntax(MediaType.IMAGE_PNG);
builder.addContentInformation();
builder.getContentInformationBuilder()
.setDataObject(DataType.QUICKLOOK_SD,
thumbnailFileJpg.toAbsolutePath(),
thumbnailFileJpg.getFileName().toString(),
"MD5",
checksum,
thumbnailFileJpg.toFile().length());
builder.getContentInformationBuilder().setSyntax(MediaType.IMAGE_PNG);
builder.addContentInformation();
}
if (Files.exists(descFile)) {
String checksum = ChecksumUtils.computeHexChecksum(new FileInputStream(descFile.toFile()), "MD5");
builder.getContentInformationBuilder()
.setDataObject(DataType.DESCRIPTION,
descFile.toAbsolutePath(),
descFile.getFileName().toString(),
"MD5",
checksum,
descFile.toFile().length());
builder.getContentInformationBuilder().setSyntax(MediaType.APPLICATION_PDF);
builder.addContentInformation();
}
SIP sip = builder.build();
sip.setGeometry(feature.getGeometry());
if (!sip.getProperties().getContentInformations().isEmpty() || allowEmptyFeature) {
Path file = Paths.get(entry.getParent().toString(), name + ".json");
generatedFiles.add(Files.write(file, Arrays.asList(gson.toJson(sip)), Charset.forName("UTF-8")));
}
}
} catch (IOException | NoSuchAlgorithmException e) {
LOGGER.error(e.getMessage(), e);
}
return generatedFiles;
}
public void setGson(Gson gson) {
this.gson = gson;
}
public void setFeatureId(String featureId) {
this.featureId = featureId;
}
}