On 18 February 2010 21:05, Andrea Aime wrote:
>
> A failing test case pretty please? :-)
>

Here is an example with a bounding box defined by a ReferencedEnvelope
in WGS84 and a point feature that lies within the box where the
feature's CRS is UTM.

A bounding box filter is created with the ReferencedEnvelope. The
filter is evaluated against the feature but returns false because the
test for point in bounds is done blind to the CRS - hence my question
about what is the SRS used for ?

I would have inferred from the docs that the BBox filter should take
into account the projections of both bounds and feature when doing the
test.  I'm guess this means I'm misinterpreting the docs but it still
seems odd to me.

Michael


package org.geotools.demo;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.DirectPosition2D;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

public class BBoxFilterTest {
    private static final GeometryFactory GF = new GeometryFactory();
    private final CoordinateReferenceSystem crsGeo;
    private final CoordinateReferenceSystem crsUTM;
    private final MathTransform transform;
    private final SimpleFeatureType TYPE;

    private SimpleFeatureBuilder featureBuilder;


    public static void main(String[] args) throws Exception {
        new BBoxFilterTest().testBBox();
    }


    public BBoxFilterTest() throws Exception {
        crsGeo = DefaultGeographicCRS.WGS84;
        crsUTM = CRS.decode("EPSG:32756");  // UTM Zone 55S
        transform = CRS.findMathTransform(crsGeo, crsUTM);
        TYPE = createFeatureType(crsGeo);
    }

    private void testBBox() throws Exception {
        /*
         * A bounding box (around Sydney)
         */
        ReferencedEnvelope bounds = new ReferencedEnvelope(
                151.0, 151.5, -34.0, -33.5, crsGeo);

        /*
         * Create a point feature at the centre of the envelope
         */
        DirectPosition2D pos = new DirectPosition2D(crsGeo,
bounds.getMedian(0), bounds.getMedian(1));
        transform.transform(pos, pos);
        final SimpleFeature feature = createFeature(TYPE, pos);

        /*
         * Try to catch the feature with a BBox filter where the box is the
         * lat-lon envelope.
         *
         * This will fail :-(
         */
        FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
        Filter filter = ff.bbox(ff.property("the_geom"), bounds);

        if (filter.evaluate(feature)) {
            System.out.println("Feature retrieved by filter");
        } else {
            System.out.println("Feature missed by filter");
        }
    }

    private SimpleFeatureType createFeatureType(CoordinateReferenceSystem crs) {
        SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
        tb.setName("Foo");
        tb.add("the_geom", Point.class, crs);
        return tb.buildFeatureType();
    }

    private SimpleFeature createFeature(SimpleFeatureType type,
DirectPosition2D pos) {
        if (featureBuilder == null) {
            featureBuilder = new SimpleFeatureBuilder(type);
        }

        featureBuilder.add(GF.createPoint(new Coordinate(pos.x, pos.y)));
        return featureBuilder.buildFeature(null);
    }

}

------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Geotools-devel mailing list
Geotools-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-devel

Reply via email to