Skip to content

Commit

Permalink
hotfix for bbox and footprint appear at the same time.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yen, David (398B-Affiliate) committed Oct 19, 2023
1 parent 546ec08 commit 406a5f2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 51 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>gov.nasa.podaac</groupId>
<artifactId>cumulus-metadata-aggregator</artifactId>
<version>8.5.0-rc.2</version>
<version>8.5.0-alpha.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Metadata-Aggregator</name>
Expand Down Expand Up @@ -55,18 +55,18 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
<version>4.5.14</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
<version>3.13.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ private JSONObject readToken()
}
String localTokenFilePath = s3Utils.download(this.region, this.tknBucket, this.tknFilePath,
Paths.get(this.workingDir, "token.json").toString());
AdapterLogger.LogInfo(this.className + " downloaded token file to local:" + localTokenFilePath);
JSONObject json = null;
try (FileReader reader = new FileReader(String.valueOf(localTokenFilePath))) {
json = (JSONObject) parser.parse(reader);
Expand Down Expand Up @@ -302,7 +301,6 @@ private HttpResponse send(String url, HttpEntity entity)
request.setHeader("Content-Type", content_type);
// Send the request
HttpResponse response = httpClient.execute(request);
request.releaseConnection();
return response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ public void setGranuleFileSizeAndChecksum(JSONArray input_granules) {
}
granule.getGranuleArchiveSet().add(uga);
}
AdapterLogger.LogInfo(this.className + " GranuleArchive HashSet Size:" + granule.getGranuleArchiveSet().size());
AdapterLogger.LogInfo(this.className + " GranuleArchive HashSet:" + granule.getGranuleArchiveSet());
}

Expand Down Expand Up @@ -324,12 +323,12 @@ public void readIsoMetadataFile(String file, String s3Location) throws ParserCon
// if we get here, we have the bare minimum fields already populated,
// so try and parse the rest of the granule metadata...
try {
((IsoGranule) this.granule).setIsoType(isoType);
if (isoType == IsoType.MENDS) {
AdapterLogger.LogInfo("Found MENDS file");
readIsoMendsMetadataFile(s3Location, doc, xpath);
} else if (isoType == IsoType.SMAP) {
AdapterLogger.LogInfo("Found SMAP file");
((IsoGranule) this.granule).setIsoType(isoType);
readIsoSmapMetadataFile(s3Location, doc, xpath);
} else {
AdapterLogger.LogWarning(isoType.name() + " didn't match any expected ISO type, skipping optional " +
Expand Down
100 changes: 57 additions & 43 deletions src/main/java/gov/nasa/cumulus/metadata/aggregator/UMMGranuleFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;

import javax.swing.text.html.Option;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
Expand Down Expand Up @@ -231,7 +230,7 @@ public JSONObject defineGranule()
private JSONObject UMMGPostProcessing(JSONObject granuleJson)
throws ParseException, IOException, URISyntaxException{
// remove GPolygon in case the polygon came from line formatted xml and spatial validation failed
AdapterLogger.LogDebug(this.className + " final UMMG json:" + granuleJson.toJSONString());
AdapterLogger.LogDebug(this.className + " final UMMG json for UMMGPostProcessing:" + granuleJson.toJSONString());
boolean shouldAppendRelatedUrl = isMissingRelatedUrls(granuleJson);
if(shouldAppendRelatedUrl) {
granuleJson = appendFakeRelatedUrl(granuleJson);
Expand Down Expand Up @@ -329,10 +328,10 @@ private JSONObject exportDataGranule() {
dataGranule.put("ArchiveAndDistributionInformation", archiveArray);

Set<GranuleArchive> archiveSet = granule.getGranuleArchiveSet();
AdapterLogger.LogInfo(this.className + " Granule archiveSet: " + archiveSet);
for (GranuleArchive archive : archiveSet) {
JSONObject archiveJson = new JSONObject();
archiveJson.put("Name", archive.getName());
AdapterLogger.LogInfo(this.className + " Granule Archive Name: " + archive.getName());
JSONObject checksum = new JSONObject();
if (archive instanceof IsoGranuleArchive) {
checksum.put("Value", archive.getChecksum());
Expand Down Expand Up @@ -433,15 +432,54 @@ private boolean shouldAddBBx(Granule granule) {
return shouldAddBBx;
}

private JSONObject appendISOTrack(JSONObject horizontalSpatialDomain) {
if (((IsoGranule) granule).getSwotTrack() != "") {
JSONObject track = new JSONObject();
horizontalSpatialDomain.put("Track", track);
Pattern trackPattern = Pattern.compile("Cycle:\\s(.*)\\sPass:\\s(.*)\\sTile:\\s(.*)");
Matcher trackMatcher = trackPattern.matcher(((IsoGranule) granule).getSwotTrack());
if (trackMatcher.find()) {
track.put("Cycle", Integer.parseInt(trackMatcher.group(1)));
JSONArray passes = new JSONArray();
track.put("Passes", passes);

String passString = trackMatcher.group(2);
if (passString.contains("-")) {
String[] passNumbers = passString.split("-");
int beginPass = Integer.parseInt(passNumbers[0]);
int endPass = Integer.parseInt(passNumbers[1]);
for (int i = beginPass; i <= endPass; i++) {
JSONObject pass = new JSONObject();
pass.put("Pass", i);
pass.put("Tiles", parseTrackTiles(trackMatcher.group(3)));
passes.add(pass);
}
} else {
JSONObject pass = new JSONObject();
pass.put("Pass", Integer.parseInt(trackMatcher.group(2)));
pass.put("Tiles", parseTrackTiles(trackMatcher.group(3)));
passes.add(pass);
}
}
}
return horizontalSpatialDomain;
}

private JSONObject exportSpatial() throws ParseException{
JSONObject spatialExtent = new JSONObject();
JSONObject geometry = new JSONObject();
JSONObject horizontalSpatialDomain = new JSONObject();
Boolean foundOrbitalData = false;
boolean isoBBoxAlreadyProcessed = false;
boolean geometryAlreadyProcessed = false;
spatialExtent.put("HorizontalSpatialDomain", horizontalSpatialDomain);

if (granule instanceof IsoGranule) {
/**
* if isoXmlSpatialType is configured in cumulus collection configuration and the code is processing IsoGranule
*/
if (granule instanceof IsoGranule &&
(this.isoXMLSpatialTypeEnumHashSet.contains(MENDsIsoXMLSpatialTypeEnum.FOOTPRINT) ||
this.isoXMLSpatialTypeEnumHashSet.contains(MENDsIsoXMLSpatialTypeEnum.BBOX) ||
this.isoXMLSpatialTypeEnumHashSet.contains(MENDsIsoXMLSpatialTypeEnum.ORBIT))) {
/**
* Export Footprint, Orbit or Bounding Box
* UMM v1.5 only allows for either Geometry or Orbit not both. Only process orbit if the orbitString stored
Expand All @@ -450,7 +488,7 @@ private JSONObject exportSpatial() throws ParseException{
if(this.isoXMLSpatialTypeEnumHashSet.contains(MENDsIsoXMLSpatialTypeEnum.FOOTPRINT)) {
AdapterLogger.LogDebug(this.className + "UMMGranuleFile.exportSpatial FOOTPRINT Processing");
String polygon = ((IsoGranule) granule).getPolygon();
AdapterLogger.LogInfo(this.className + " nc.iso.xml footprint processing ... ");
geometryAlreadyProcessed = true;
this.isLineFormattedPolygon = true;
geometry = line2Polygons(geometry,polygon);
}
Expand All @@ -475,49 +513,22 @@ private JSONObject exportSpatial() throws ParseException{
if(this.isoXMLSpatialTypeEnumHashSet.contains(MENDsIsoXMLSpatialTypeEnum.BBOX)) {
// Extract the stored IsoGranule bounding box and put into SpatialExtent
AdapterLogger.LogDebug(this.className + "UMMGranuleFile.exportSpatial BBOX Processing");
isoBBoxAlreadyProcessed = true;
geometryAlreadyProcessed = true;
horizontalSpatialDomain = this.appendBoundingRectangles(geometry, horizontalSpatialDomain);
}
// Export track
if (((IsoGranule) granule).getSwotTrack() != "") {
JSONObject track = new JSONObject();
horizontalSpatialDomain.put("Track", track);
Pattern trackPattern = Pattern.compile("Cycle:\\s(.*)\\sPass:\\s(.*)\\sTile:\\s(.*)");
Matcher trackMatcher = trackPattern.matcher(((IsoGranule) granule).getSwotTrack());
if (trackMatcher.find()) {
track.put("Cycle", Integer.parseInt(trackMatcher.group(1)));
JSONArray passes = new JSONArray();
track.put("Passes", passes);

String passString = trackMatcher.group(2);
if (passString.contains("-")) {
String[] passNumbers = passString.split("-");
int beginPass = Integer.parseInt(passNumbers[0]);
int endPass = Integer.parseInt(passNumbers[1]);
for (int i = beginPass; i <= endPass; i++) {
JSONObject pass = new JSONObject();
pass.put("Pass", i);
pass.put("Tiles", parseTrackTiles(trackMatcher.group(3)));
passes.add(pass);
}
} else {
JSONObject pass = new JSONObject();
pass.put("Pass", Integer.parseInt(trackMatcher.group(2)));
pass.put("Tiles", parseTrackTiles(trackMatcher.group(3)));
passes.add(pass);
}
}
}
// Commented out Export track
horizontalSpatialDomain.put("Geometry", geometry);
horizontalSpatialDomain = appendISOTrack(horizontalSpatialDomain);
} // end of processing IsoGranule

// We can only include orbital or bounding-box data, not both
// if iso Bounding Box already processed in logic above, then don't enter this block
if (foundOrbitalData == false && !isoBBoxAlreadyProcessed) {
// if geometry already processed in logic above (which includes the case of either footprint or bbox
// , then don't enter this block
if (foundOrbitalData == false && !geometryAlreadyProcessed) {

horizontalSpatialDomain.put("Geometry", geometry);

JSONArray boundingRectangles = new JSONArray();
geometry.put("BoundingRectangles", boundingRectangles);

double north = 0, south = 0, east = 0, west = 0;
if(granule !=null && granule instanceof gov.nasa.cumulus.metadata.aggregator.UMMGranule) {
Expand Down Expand Up @@ -580,6 +591,7 @@ private JSONObject exportSpatial() throws ParseException{
// If we get here, it means we have valid values for spatial data, so continue
// with the spatial extent export.
if (rangeIs360 && shouldAddBBx(granule)) {
AdapterLogger.LogInfo("Entered shouldAddBBx 1");
BigDecimal bdeast = BoundingTools.convertBoundingVal(est, true);
BigDecimal bdwest = BoundingTools.convertBoundingVal(wst, true);

Expand All @@ -595,6 +607,7 @@ private JSONObject exportSpatial() throws ParseException{
boundingRectangles.add(createBoundingBoxJson(nrth, sth, BoundingTools.convertBoundingVal(est, true), BigDecimal.valueOf(-180)));
}
} else if (shouldAddBBx(granule)){
AdapterLogger.LogInfo("Entered shouldAddBBx 2");
if (est.doubleValue() >= wst.doubleValue()) {
boundingRectangles.add(createBoundingBoxJson(nrth, sth, est, wst));
} else {
Expand All @@ -609,6 +622,9 @@ private JSONObject exportSpatial() throws ParseException{
}
}
}
if(boundingRectangles.size() >0) {
geometry.put("BoundingRectangles", boundingRectangles);
}
}

// Export track if cycle and pass exists
Expand Down Expand Up @@ -733,7 +749,7 @@ public JSONObject line2Polygons(JSONObject geometry, String line) {
}
if (dividedSize == 3) {
// reconstruct 2 polygons divided over IDL
AdapterLogger.LogInfo(this.className + " Divided to 3 GEOs, connect 1st and 3rd to polygon");
AdapterLogger.LogInfo(this.className + " Divided to 3 GEOs, connecting 1st and 3rd to polygon");
ArrayList<Coordinate> polygon1 = UMMUtils.reconstructPolygonsOver2Lines(
(ArrayList<Coordinate>) splittedGeos.get(0), (ArrayList<Coordinate>) splittedGeos.get(2));
ArrayList<ArrayList<Coordinate>> polygons = new ArrayList<>();
Expand Down Expand Up @@ -777,7 +793,6 @@ public JSONObject addPolygons(JSONObject geometry, ArrayList<ArrayList<Coordinat
Polygon polygon = geometryFactory.createPolygon(geo.stream().toArray(Coordinate[]::new));
AdapterLogger.LogInfo(this.className + " Polygon is valid: " + polygon.isValid());
AdapterLogger.LogInfo(this.className + " Polygon WKT: " + UMMUtils.getWKT(polygon));
AdapterLogger.LogInfo(this.className + " ------------------------------------------");
List<Coordinate> counterClockwiseCoordinates = Arrays.asList(
UMMUtils.ensureOrientation(CGAlgorithms.COUNTERCLOCKWISE, geo.toArray(new Coordinate[geo.size()]))
);
Expand All @@ -801,7 +816,6 @@ public JSONObject addPolygons(JSONObject geometry, ArrayList<ArrayList<Coordinat
// If any polygon is not valid, create a global bounding box and exit
AdapterLogger.LogInfo(this.className + " Polygon is NOT valid: " + polygon);
AdapterLogger.LogInfo(this.className + " Polygon WKT: " + UMMUtils.getWKT(polygon));
AdapterLogger.LogInfo(this.className + " ------------------------------------------");
geometry.remove("GPolygons");
geometry = addGlobalBoundingBox2Geometry(geometry);
break;
Expand Down

0 comments on commit 406a5f2

Please sign in to comment.