Friday 31 August 2018

java - Weird behavior using org.geotools Filter


I am using filter to catch all the features who fall into a specific polygon. My collection is in EPSG:32632 coordinate reference system and it is located somewhere in this bounding box:


xMin, yMin 470701.73,5048820.54 : xMax,yMax 641679.97,5165204.84

Filter uses polygons expressed in EPSG:4326, but usign org.geotools ReprojectingFeatureCollection this is not a problem. Here are the two polygons:


the_geom bbox POLYGON ((-180 -90, -180 90, 0 90, 0 -90, -180 -90)),
the_geom bbox POLYGON ((0 -90, 0 90, 180 90, 180 -90, 0 -90))

who are respectively reprojected into:



the_geom bbox POLYGON ((-108523640.02189268 -187176133.93183675, -108523640.02189268 187176133.93183675, 274812009.447262 187176133.93183675, 274812009.447262 -187176133.93183675, -108523640.02189268 -187176133.93183675)), 
the_geom bbox POLYGON ((-69745878.65576684 -187176133.93183675, -69745878.65576684 187176133.93183675, 274812009.447262 187176133.93183675, 274812009.447262 -187176133.93183675, -69745878.65576684 -187176133.93183675))

What I expect is that applying filter with the first polygon I should get no features and when I apply filter with the second polygon I should get all the features. It's strange that filter gives back the whole features when is applied with both polygons! Here is a test to reproduce


public class FilterTesting {

@BeforeClass
public static void setUp() {
System.setProperty("org.geotools.referencing.forceXY", "true");
}


@Test
public void filterTesting() throws Exception {
String file = "whereisyour.shp";
FileDataStore ds = FileDataStoreFinder.getDataStore(new File(file));
SimpleFeatureCollection features = ds.getFeatureSource().getFeatures();
ReprojectingFeatureCollection rfc = new ReprojectingFeatureCollection(features, org.geotools.referencing.CRS.decode("EPSG:4326"));
Filter filter = buildFilter(-180D, -90D, 0D, 90D);
SimpleFeatureCollection fc = rfc.subCollection(filter);
Assert.assertTrue(fc.isEmpty());

filter = buildFilter(0D, -90D, 180D, 90D);
fc = rfc.subCollection(filter);
Assert.assertFalse(fc.isEmpty());
}

private BBOX buildFilter(double x1, double y1, double x2, double y2) throws Exception {
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
ReferencedEnvelope bbox = new ReferencedEnvelope(x1, x2, y1, y2,
CRS.decode("EPSG:4326"));
return ff.bbox(ff.property("the_geom"), bbox);

}
}

I am using org.geotools version 17.2.


UPDATE


You can download a shapefile to reproduce the behavior here




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...