2020import java .util .Collection ;
2121import java .util .HashSet ;
2222import java .util .Iterator ;
23+ import java .util .LinkedHashMap ;
2324import java .util .List ;
25+ import java .util .Map ;
2426import java .util .Set ;
2527import javax .xml .namespace .QName ;
2628import net .opengis .gml .v32 .AbstractFeature ;
3941import org .sensorhub .test .TestUtils ;
4042import org .vast .ogc .gml .GenericFeatureImpl ;
4143import org .vast .util .Bbox ;
44+ import com .vividsolutions .jts .geom .Coordinate ;
45+ import com .vividsolutions .jts .geom .Geometry ;
4246import com .vividsolutions .jts .geom .GeometryFactory ;
4347import com .vividsolutions .jts .geom .Polygon ;
4448
@@ -59,15 +63,18 @@ public abstract class AbstractTestObsStorage<StorageType extends IObsStorageModu
5963 static String FOI_UID_PREFIX = "urn:domain:features:foi" ;
6064 static int NUM_FOIS = 100 ;
6165 GMLFactory gmlFac = new GMLFactory (true );
66+ Map <String , AbstractFeature > allFeatures ;
6267
6368 static String [] FOI_SET1_IDS = new String []
6469 {
65- FOI_UID_PREFIX + "001" ,
66- FOI_UID_PREFIX + "002" ,
67- FOI_UID_PREFIX + "003"
70+ FOI_UID_PREFIX + "1" ,
71+ FOI_UID_PREFIX + "2" ,
72+ FOI_UID_PREFIX + "3" ,
73+ FOI_UID_PREFIX + "4" ,
74+ FOI_UID_PREFIX + "15"
6875 };
6976
70- static int [] FOI_SET1_STARTS = new int [] {0 , 20 , 60 };
77+ static int [] FOI_SET1_STARTS = new int [] {0 , 20 , 25 , 40 , 60 };
7178
7279
7380 String [] FOI_IDS = FOI_SET1_IDS ;
@@ -77,6 +84,7 @@ public abstract class AbstractTestObsStorage<StorageType extends IObsStorageModu
7784 protected void addFoisToStorage () throws Exception
7885 {
7986 storage .setAutoCommit (false );
87+ allFeatures = new LinkedHashMap <String , AbstractFeature >(NUM_FOIS );
8088
8189 for (int foiNum = 1 ; foiNum <= NUM_FOIS ; foiNum ++)
8290 {
@@ -89,6 +97,7 @@ protected void addFoisToStorage() throws Exception
8997 Point p = gmlFac .newPoint ();
9098 p .setPos (new double [] {foiNum , foiNum , 0.0 });
9199 foi .setLocation (p );
100+ allFeatures .put (foi .getUniqueIdentifier (), foi );
92101 storage .storeFoi (producerID , foi );
93102 }
94103
@@ -278,6 +287,36 @@ protected IObsFilter buildFilterByFoiID(DataComponent recordDef, List<DataBlock>
278287 }
279288
280289
290+ protected IObsFilter buildFilterByRoi (DataComponent recordDef , List <DataBlock > dataList , final Polygon roi )
291+ {
292+ // get list of FOIs within roi
293+ ArrayList <Integer > foiIndexList = new ArrayList <Integer >();
294+ int fIndex = 0 ;
295+ for (String foiID : FOI_IDS )
296+ {
297+ AbstractFeature f = allFeatures .get (foiID );
298+ if (roi .intersects ((Geometry )f .getLocation ()))
299+ foiIndexList .add (fIndex );
300+ fIndex ++;
301+ }
302+
303+ // then just filter dataList using list of indexes
304+ int i = 0 ;
305+ int [] foiIndexes = new int [foiIndexList .size ()];
306+ for (int index : foiIndexList )
307+ foiIndexes [i ++] = index ;
308+ buildFilterByFoiID (recordDef , dataList , foiIndexes );
309+
310+ // generate filter
311+ IObsFilter filter = new ObsFilter (recordDef .getName ()) {
312+ public Polygon getRoi () { return roi ; }
313+ public Collection <String > getProducerIDs () {return producerFilterList ; };
314+ };
315+
316+ return filter ;
317+ }
318+
319+
281320 protected IObsFilter buildFilterByFoiIDAndTime (DataComponent recordDef , List <DataBlock > dataList , int [] foiIndexes , final double [] timeRange )
282321 {
283322 final Set <String > foiSet = new HashSet <String >();
@@ -346,7 +385,8 @@ protected void checkFilteredResults(IObsFilter filter, List<DataBlock> dataList)
346385 {
347386 IDataRecord dbRec = it2 .next ();
348387 TestUtils .assertEquals (dataList .get (i ), dbRec .getData ());
349- assertTrue (filter .getFoiIDs ().contains (((ObsKey )dbRec .getKey ()).foiID ));
388+ if (filter .getFoiIDs () != null )
389+ assertTrue (filter .getFoiIDs ().contains (((ObsKey )dbRec .getKey ()).foiID ));
350390 i ++;
351391 }
352392
@@ -460,6 +500,60 @@ public void testGetRecordsForMultipleFoiIDsAndTime() throws Exception
460500 testList .addAll (dataList );
461501 filter = buildFilterByFoiIDAndTime (recordDef , testList , new int [] {2 , 1 }, timeRange );
462502 checkFilteredResults (filter , testList );
503+ }
504+
505+
506+ @ Test
507+ public void testGetRecordsByRoi () throws Exception
508+ {
509+ addFoisToStorage ();
510+
511+ DataComponent recordDef = createDs2 ();
512+ List <DataBlock > dataList = addObservationsWithFoiToStorage (recordDef );
513+ List <DataBlock > testList = new ArrayList <DataBlock >(dataList .size ());
514+ IObsFilter filter ;
515+ Polygon roi ;
516+
517+ // FOI 1
518+ testList .clear ();
519+ testList .addAll (dataList );
520+ roi = new GeometryFactory ().createPolygon (new Coordinate [] {
521+ new Coordinate (0.5 , 0.5 ),
522+ new Coordinate (0.5 , 1.5 ),
523+ new Coordinate (1.5 , 1.5 ),
524+ new Coordinate (1.5 , 0.5 ),
525+ new Coordinate (0.5 , 0.5 )
526+ });
527+ filter = buildFilterByRoi (recordDef , testList , roi );
528+ checkFilteredResults (filter , testList );
529+
530+ // FOIs 1 + 3
531+ testList .clear ();
532+ testList .addAll (dataList );
533+ roi = new GeometryFactory ().createPolygon (new Coordinate [] {
534+ new Coordinate (0.5 , 0.5 ),
535+ new Coordinate (0.5 , 3.5 ),
536+ new Coordinate (3.5 , 3.5 ),
537+ new Coordinate (3.5 , 2.5 ),
538+ new Coordinate (1.5 , 2.5 ),
539+ new Coordinate (1.5 , 0.5 ),
540+ new Coordinate (0.5 , 0.5 )
541+ });
542+ filter = buildFilterByRoi (recordDef , testList , roi );
543+ checkFilteredResults (filter , testList );
544+
545+ // FOIs 1-4
546+ testList .clear ();
547+ testList .addAll (dataList );
548+ roi = new GeometryFactory ().createPolygon (new Coordinate [] {
549+ new Coordinate (0.0 , 0.0 ),
550+ new Coordinate (0.0 , 4.0 ),
551+ new Coordinate (4.0 , 4.0 ),
552+ new Coordinate (4.0 , 0.0 ),
553+ new Coordinate (0.0 , 0.0 )
554+ });
555+ filter = buildFilterByRoi (recordDef , testList , roi );
556+ checkFilteredResults (filter , testList );
463557 }
464558
465559}
0 commit comments