how to use "Contains" filter

5 messages Options
Embed this post
Permalink
vurfem

how to use "Contains" filter

Reply Threaded More More options
Print post
Permalink
Hi all;

I have two shapefiles. One has buildings and the other has parcels (both of them are multipolygons). I need to check  if the given parcel has buildings inside it or not. If it has building, get the coordinates of the building.

I have some difficulty in writing the filter for checking the buildings inside the parcel.

The explanation on the javadoc for "contains" filter is;

" contains(Expression geometry1, Expression geometry2)"

What means expression here? May be The name of geometry of the parcel and the building seperately?

SimpleFeature building = buildingiterator.next();
                   Geometry geom = (Geometry) building.getDefaultGeometry();
                   String filterStr = "CONTAINS(the_geom, " +(geom) + ")";                    Filter filter2 = CQL.toFilter(filterStr);

I have found this example on mailing list. And I want to understand what the code exactly mean in bold and if it is true or wrong?

It is very urgent for my study.

Thanks.

Best regards,

Vurfem.

mbedward

Re: how to use "Contains" filter

Reply Threaded More More options
Print post
Permalink
Hi Vurfem

> The explanation on the javadoc for "contains" filter is;
>
> " contains(Expression geometry1, Expression geometry2)"

Yeeesss.... it does leave a bit to the imagination doesn't it.

(Note to self: spend a bit of time on javadocs for filter classes)

> What means expression here? May be The name of geometry of the parcel and
> the building seperately?

The filter tests if the first geometry contains the second geometry.

The first argument should be a literal expression for the geometry
attribute (almost always "the_geom").

The second argument is the actual geometry that you are testing to see
if it is contained within a given feature's geometry.

> SimpleFeature building = buildingiterator.next();
>                        Geometry geom = (Geometry) building.getDefaultGeometry();
>                        String filterStr = "CONTAINS(the_geom, " +(geom) + ")";
> Filter filter2 = CQL.toFilter(filterStr);
>

No, that doesn't look quite right. With CQL the second geometry has to
be expressed as a WKT string such as POINT(12 34) or LINESTRING(10 10,
20 10, 20 20).  You could do it like this...

        WKTWriter writer = new WKTWriter();
        Geometry geom2 = ...
        String cqlPredicate = "CONTAIMS(the_geom, " + writer.write(geom2) + ")";
        Filter filter = CQL.toFilter(cqlPredicate);

Or you could use FilterFactory2 directly which is probably easier if
you are dealing directly with the JTS Geometry objects...

        FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);

        Geometry building = ...;
        Filter filter = ff.contains(ff.literal("the_geom"),
ff.literal(building));

Hope this helps,

Michael

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
Mauricio Pazos

Re: how to use "Contains" filter

Reply Threaded More More options
Print post
Permalink
On Wednesday 04 November 2009 04:13:35 am Michael Bedward wrote:
> No, that doesn't look quite right. With CQL the second geometry has to
> be expressed as a WKT string such as POINT(12 34) or LINESTRING(10 10,
> 20 10, 20 20).  You could do it like this...
>
>         WKTWriter writer = new WKTWriter();
>         Geometry geom2 = ...
>         String cqlPredicate = "CONTAIMS(the_geom, " + writer.write(geom2) +
>  ")"; Filter filter = CQL.toFilter(cqlPredicate);
>
Exactly! from http://docs.codehaus.org/display/GEOTOOLS/CQL+Parser+Design 

<georoutine argument list> ::=  <left paren><attribute name><comma><geometry
literal><right paren>
<geometry literal> := ...

The extended syntax implemented in ECQL accepts expressions as arguments:
<georoutine argument list> ::=  "(" <expression> "," <expression> ")"
 
You could write something like

ECQL.toFilter("CONTAINS(buffer(the_geom, 10) , POINT(1 2))")


cheers
--
Mauricio Pazos
www.axios.es

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
vurfem

Re: how to use "Contains" filter

Reply Threaded More More options
Print post
Permalink
In reply to this post by mbedward
Hi Michael,

Thanks for the explanation, but I have another problem in my applicaiton. May be it is very simple for you but I can't solve it. I want to check, one of the selected parcel (polygon type) contains a building in it. With contains filter I can check the parcels that contain building. But I want to check this only for the selected parcel. So I have a problem in that case. I try some code and I am sending it. May be I need to combine two filters?

Sample Code;
 
 parcelfeature = shapefile.getFeatureSource(typeName);

//Here it select the parcel according to the input id

             Filter filter1 = CQL.toFilter(("K1_ID="+parselNo));
             parcelcoll = parcelfeature.getFeatures(filter1);

           
             Map<String,Object> connect2 = new HashMap<String,Object>();
                connect2.put("url", file2.toURI().toURL());
                DataStore shapefile2 = DataStoreFinder.getDataStore(connect2);
                String typeName2 = shapefile2.getTypeNames()[0];
               
             FeatureSource<SimpleFeatureType, SimpleFeature> buildingfeature;
             FeatureCollection<SimpleFeatureType, SimpleFeature> buildingcoll;
             FeatureIterator<SimpleFeature> buildingiterator;
             
             buildingfeature=shapefile2.getFeatureSource(typeName2);
             buildingcoll=buildingfeature.getFeatures();
             
             buildingiterator=buildingcoll.features();
                 
              try {
               while (buildingiterator.hasNext()) {
               
                   SimpleFeature building = buildingiterator.next();
                   Geometry geom = (Geometry) building.getDefaultGeometry();
                 
                                //Filter for buildings contained in the parcels          
                   FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
                   Filter filter = ff.contains(ff.literal("the_geom"), ff.literal(geom));
                   
                               //How can I get the buildings only in the selected feature in top?
                   parceliterator=parcelcoll.features();
                           if (!parcelcoll.isEmpty()) {
                                       FeatureIterator<SimpleFeature> parcelIter = parcelcoll.features();
                                       
It is very urgent for my study. Thanks for any help or idea.

Vurfem.


mbedward wrote:
Hi Vurfem

> The explanation on the javadoc for "contains" filter is;
>
> " contains(Expression geometry1, Expression geometry2)"

Yeeesss.... it does leave a bit to the imagination doesn't it.

(Note to self: spend a bit of time on javadocs for filter classes)

> What means expression here? May be The name of geometry of the parcel and
> the building seperately?

The filter tests if the first geometry contains the second geometry.

The first argument should be a literal expression for the geometry
attribute (almost always "the_geom").

The second argument is the actual geometry that you are testing to see
if it is contained within a given feature's geometry.

> SimpleFeature building = buildingiterator.next();
>                        Geometry geom = (Geometry) building.getDefaultGeometry();
>                        String filterStr = "CONTAINS(the_geom, " +(geom) + ")";
> Filter filter2 = CQL.toFilter(filterStr);
>

No, that doesn't look quite right. With CQL the second geometry has to
be expressed as a WKT string such as POINT(12 34) or LINESTRING(10 10,
20 10, 20 20).  You could do it like this...

        WKTWriter writer = new WKTWriter();
        Geometry geom2 = ...
        String cqlPredicate = "CONTAIMS(the_geom, " + writer.write(geom2) + ")";
        Filter filter = CQL.toFilter(cqlPredicate);

Or you could use FilterFactory2 directly which is probably easier if
you are dealing directly with the JTS Geometry objects...

        FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);

        Geometry building = ...;
        Filter filter = ff.contains(ff.literal("the_geom"),
ff.literal(building));

Hope this helps,

Michael

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Geotools-gt2-users mailing list
Geotools-gt2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
mbedward

Re: how to use "Contains" filter

Reply Threaded More More options
Print post
Permalink
Hi Vurfem,

> May be it is very simple for you but I can't solve it. I want to check, one
> of the selected parcel (polygon type) contains a building in it. With
> contains filter I can check the parcels that contain building. But I want to
> check this only for the selected parcel.

Here's one way that you might do it...

   public void checkParcel(SimpleFeature parcel, FeatureSource buildings) {
       FilterFactory2 filterFactory =
CommonFactoryFinder.getFilterFactory2(null);
       Geometry parcelGeom = (Geometry) parcel.getDefaultGeometry();

       Filter withinFilter = filterFactory.within(
               filterFactory.property("the_geom"),
               filterFactory.literal(parcelGeom));

       try {
           FeatureCollection buildingsInParcel =
buildings.getFeatures(withinFilter);
           if (buildingsInParcel.isEmpty()) {
               System.out.println("parcel does not contain any buildings");
           } else {
               System.out.println("parcel has these buildings...");
               FeatureIterator iter = buildingsInParcel.features();
               try {
                   SimpleFeature building = (SimpleFeature) iter.next();
                   // print out info about building...

               } catch (Exception ex) {
                   // do something

               } finally {
                   iter.close();
               }
           }

       } catch (IOException ex) {
           ex.printStackTrace();
       }
   }


Hope this helps,
Michael

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users