Skip to content

Commit 24fd4f7

Browse files
committed
L3TimeAxis, geometry support for multilinestrings
1 parent 4e5318b commit 24fd4f7

File tree

5 files changed

+97
-51
lines changed

5 files changed

+97
-51
lines changed

core/src/main/java/com/bc/fiduceo/geometry/L3TimeAxis.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ public class L3TimeAxis implements TimeAxis {
88

99
private final Date startDate;
1010
private final Date stopDate;
11+
private final Geometry geometry;
1112

12-
public L3TimeAxis(Date startDate, Date stopDate) {
13+
public L3TimeAxis(Date startDate, Date stopDate, Geometry geometry) {
1314
this.startDate = startDate;
1415
this.stopDate = stopDate;
16+
this.geometry = geometry;
1517
}
1618

1719
@Override
@@ -26,7 +28,9 @@ public TimeInterval getProjectionTime(LineString polygonSide) {
2628

2729
@Override
2830
public Date getTime(Point coordinate) {
29-
return null;
31+
final long avgTime = (stopDate.getTime() + startDate.getTime()) / 2;
32+
33+
return new Date(avgTime);
3034
}
3135

3236
@Override
@@ -41,7 +45,7 @@ public Date getEndTime() {
4145

4246
@Override
4347
public Geometry getGeometry() {
44-
return null;
48+
return geometry;
4549
}
4650

4751
@Override

core/src/main/java/com/bc/fiduceo/geometry/s2/BcS2GeometryFactory.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,18 @@ public Geometry parse(String wkt) {
4848
} else if (geometry instanceof S2Point) {
4949
return BcS2Point.createFrom((S2Point) geometry);
5050
} else if (geometry instanceof List) {
51-
final ArrayList<Polygon> polygonList = new ArrayList<>();
52-
List<S2Polygon> googlePolygonList = (List<S2Polygon>) geometry;
53-
for (S2Polygon googlePolygon : googlePolygonList) {
54-
polygonList.add(new BcS2Polygon(googlePolygon));
51+
final Object geoObject = ((List<?>) geometry).get(0);
52+
if (geoObject instanceof S2Polygon) {
53+
final ArrayList<Polygon> polygonList = new ArrayList<>();
54+
final List<S2Polygon> googlePolygonList = (List<S2Polygon>) geometry;
55+
for (S2Polygon googlePolygon : googlePolygonList) {
56+
polygonList.add(new BcS2Polygon(googlePolygon));
57+
}
58+
return new BcS2MultiPolygon(polygonList);
59+
} else if (geoObject instanceof S2Polyline) {
60+
final List<S2Polyline> googlePolylineList = (List<S2Polyline>) geometry;
61+
return new BcS2MultiLineString(googlePolylineList);
5562
}
56-
return new BcS2MultiPolygon(polygonList);
5763
}
5864
throw new RuntimeException("Unsupported geometry type");
5965
}
@@ -111,7 +117,6 @@ public MultiPolygon createMultiPolygon(List<Polygon> polygonList) {
111117
return new BcS2MultiPolygon(polygonList);
112118
}
113119

114-
115120
@Override
116121
public TimeAxis createTimeAxis(LineString lineString, Date startTime, Date endTime) {
117122
return new BcS2TimeAxis(lineString, startTime, endTime);

core/src/main/java/com/bc/fiduceo/reader/smos/SmosL1CDailyGriddedReader.java

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import ucar.nc2.Variable;
2323

2424
import java.io.*;
25+
import java.nio.file.Files;
2526
import java.util.ArrayList;
2627
import java.util.Calendar;
2728
import java.util.List;
@@ -72,48 +73,11 @@ public AcquisitionInfo read() throws IOException {
7273
final Polygon polygon = extractPolygonFromMinMax(longitudes, latitudes, readerContext.getGeometryFactory());
7374
acquisitionInfo.setBoundingGeometry(polygon);
7475

75-
final String location = netcdfFile.getLocation();
76-
final String filename = FileUtils.getFilenameFromPath(location);
77-
final int[] ymd = extractYearMonthDayFromFilename(filename);
78-
final Calendar utcCalendar = TimeUtils.getUTCCalendar();
79-
utcCalendar.set(Calendar.YEAR, ymd[0]);
80-
utcCalendar.set(Calendar.MONTH, ymd[1] - 1); // month is zero-based tb 2022-09-15
81-
utcCalendar.set(Calendar.DAY_OF_MONTH, ymd[2]);
82-
83-
acquisitionInfo.setSensingStart(utcCalendar.getTime());
84-
85-
utcCalendar.set(Calendar.HOUR, 23);
86-
utcCalendar.set(Calendar.MINUTE, 59);
87-
utcCalendar.set(Calendar.SECOND, 59);
88-
89-
acquisitionInfo.setSensingStop(utcCalendar.getTime());
76+
setSensingTimes(acquisitionInfo);
9077

9178
return acquisitionInfo;
9279
}
9380

94-
// package access for testing only tb 2022-09-15
95-
static Polygon extractPolygonFromMinMax(Array longitudes, Array latitudes, GeometryFactory geometryFactory) {
96-
int size = (int) longitudes.getSize();
97-
final double lonMin = longitudes.getDouble(0);
98-
final double lonMax = longitudes.getDouble(size - 1);
99-
100-
size = (int) latitudes.getSize();
101-
final double latMin = latitudes.getDouble(0);
102-
final double latMax = latitudes.getDouble(size - 1);
103-
104-
final Point ll = geometryFactory.createPoint(lonMin, latMin);
105-
final Point ul = geometryFactory.createPoint(lonMin, latMax);
106-
final Point ur = geometryFactory.createPoint(lonMax, latMax);
107-
final Point lr = geometryFactory.createPoint(lonMax, latMin);
108-
final ArrayList<Point> polygonPoints = new ArrayList<>();
109-
polygonPoints.add(ll);
110-
polygonPoints.add(ul);
111-
polygonPoints.add(ur);
112-
polygonPoints.add(lr);
113-
polygonPoints.add(ll);
114-
return geometryFactory.createPolygon(polygonPoints);
115-
}
116-
11781
@Override
11882
public String getRegEx() {
11983
return "SM_RE07_MIR_CDF3T[AD]_(\\d{8}T\\d{6}_){2}\\d{3}_\\d{3}_\\d{1}.tgz";
@@ -185,7 +149,7 @@ private File extractFromTar(File file) throws IOException {
185149
final int oneMb = 1024 * 1024;
186150

187151
try {
188-
final BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
152+
final BufferedInputStream inputStream = new BufferedInputStream(Files.newInputStream(file.toPath()));
189153
final GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(inputStream);
190154
tarIn = new TarArchiveInputStream(gzipIn);
191155

@@ -214,4 +178,46 @@ private File extractFromTar(File file) throws IOException {
214178
}
215179
}
216180
}
181+
182+
private void setSensingTimes(AcquisitionInfo acquisitionInfo) {
183+
final String location = netcdfFile.getLocation();
184+
final String filename = FileUtils.getFilenameFromPath(location);
185+
final int[] ymd = extractYearMonthDayFromFilename(filename);
186+
187+
final Calendar utcCalendar = TimeUtils.getUTCCalendar();
188+
utcCalendar.set(Calendar.YEAR, ymd[0]);
189+
utcCalendar.set(Calendar.MONTH, ymd[1] - 1); // month is zero-based tb 2022-09-15
190+
utcCalendar.set(Calendar.DAY_OF_MONTH, ymd[2]);
191+
192+
acquisitionInfo.setSensingStart(utcCalendar.getTime());
193+
194+
utcCalendar.set(Calendar.HOUR, 23);
195+
utcCalendar.set(Calendar.MINUTE, 59);
196+
utcCalendar.set(Calendar.SECOND, 59);
197+
198+
acquisitionInfo.setSensingStop(utcCalendar.getTime());
199+
}
200+
201+
// package access for testing only tb 2022-09-15
202+
static Polygon extractPolygonFromMinMax(Array longitudes, Array latitudes, GeometryFactory geometryFactory) {
203+
int size = (int) longitudes.getSize();
204+
final double lonMin = longitudes.getDouble(0);
205+
final double lonMax = longitudes.getDouble(size - 1);
206+
207+
size = (int) latitudes.getSize();
208+
final double latMin = latitudes.getDouble(0);
209+
final double latMax = latitudes.getDouble(size - 1);
210+
211+
final Point ll = geometryFactory.createPoint(lonMin, latMin);
212+
final Point ul = geometryFactory.createPoint(lonMin, latMax);
213+
final Point ur = geometryFactory.createPoint(lonMax, latMax);
214+
final Point lr = geometryFactory.createPoint(lonMax, latMin);
215+
final ArrayList<Point> polygonPoints = new ArrayList<>();
216+
polygonPoints.add(ll);
217+
polygonPoints.add(ul);
218+
polygonPoints.add(ur);
219+
polygonPoints.add(lr);
220+
polygonPoints.add(ll);
221+
return geometryFactory.createPolygon(polygonPoints);
222+
}
217223
}

core/src/test/java/com/bc/fiduceo/geometry/L3TimeAxisTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@
99
import java.util.Date;
1010

1111
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertTrue;
1213

1314
public class L3TimeAxisTest {
1415

1516
private L3TimeAxis timeAxis;
1617

1718
@Before
18-
public void setUp(){
19+
public void setUp() {
1920
final Date startDate = TimeUtils.parse("20190107T000000", "yyyyMMdd'T'HHmmss");
2021
final Date stopDate = TimeUtils.parse("20190107T235959", "yyyyMMdd'T'HHmmss");
21-
timeAxis = new L3TimeAxis(startDate, stopDate);
22+
23+
final GeometryFactory geometryFactory = new GeometryFactory(GeometryFactory.Type.S2);
24+
final Geometry multiLineString = geometryFactory.parse("MULTILINESTRING((-180 0, 180 0), (0 -90, 0 90))");
25+
timeAxis = new L3TimeAxis(startDate, stopDate, multiLineString);
2226
}
2327

2428
@Test
@@ -50,6 +54,17 @@ public void testGetEndTime() {
5054
@Test
5155
public void testGetDurationInMillis() {
5256
assertEquals(86399000, timeAxis.getDurationInMillis());
57+
}
58+
59+
@Test
60+
public void testGetTime() {
61+
final Date time = timeAxis.getTime(null);// the location is not important tb 2022-09-23
62+
TestUtil.assertCorrectUTCDate(2019, 1, 7, 11, 59, 59, 500, time);
63+
}
5364

65+
@Test
66+
public void testGetGeometry() {
67+
final Geometry geometry = timeAxis.getGeometry();
68+
assertTrue(geometry instanceof MultiLineString);
5469
}
5570
}

core/src/test/java/com/bc/fiduceo/geometry/s2/BcS2GeometryFactoryTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,30 @@ public void testParseLineString() {
6565
assertNotNull(geometry);
6666
assertTrue(geometry instanceof BcS2LineString);
6767

68-
Point[] coordinates = geometry.getCoordinates();
68+
final Point[] coordinates = geometry.getCoordinates();
69+
assertEquals(4, coordinates.length);
6970
assertEquals(coordinates[0].getLon(), 1.9999999999999996, 1e-8);
7071
assertEquals(coordinates[0].getLat(), 1.0, 1e-8);
7172

7273
assertEquals(coordinates[3].getLon(), 5.0, 1e-8);
7374
assertEquals(coordinates[3].getLat(), 4.0, 1e-8);
7475
}
7576

77+
@Test
78+
public void testMultiLineString() {
79+
final Geometry geometry = factory.parse("MULTILINESTRING((0 1, 1 1), (1 4, 3 4))");
80+
assertNotNull(geometry);
81+
assertTrue(geometry instanceof BcS2MultiLineString);
82+
83+
Point[] coordinates = geometry.getCoordinates();
84+
assertEquals(4, coordinates.length);
85+
assertEquals(0.0, coordinates[0].getLon(), 1e-8);
86+
assertEquals( 1.0, coordinates[0].getLat(),1e-8);
87+
88+
assertEquals( 3.0, coordinates[3].getLon(),1e-8);
89+
assertEquals( 4.0, coordinates[3].getLat(),1e-8);
90+
}
91+
7692
@Test
7793
public void testParseMultiPolygon() {
7894
BcS2MultiPolygon bcS2MultiPolygon = (BcS2MultiPolygon) factory.parse("MULTIPOLYGON(((30 20, 100 10)),((100 10, 300 10)),((30 20,100 10)))");

0 commit comments

Comments
 (0)