İntersection of polygon and point layers

4 messages Options
Embed this post
Permalink
vurfem

İntersection of polygon and point layers

Reply Threaded More More options
Print post
Permalink
Hi list;

I have two layers one is Polygon layer(parcel) and the other is point layer(building). I want to find the building in a geometry . First I found the bounding box of the parcel. And create a polygon with the coordinates of the bounding box. Then I want to clip the points inside this polygon. But I don't know how to clip point layer with this. Here is my code;

     File file = new File ("/tezle ilgili/ada/ada_region.shp" );
                           
                          String parselNo = JOptionPane.showInputDialog("Lutfen parsel numarasını giriniz:");
                           
                        Map<String,Object> connect = new HashMap<String,Object>();
                        connect.put("url", file.toURI().toURL());
                        DataStore shapefile = DataStoreFinder.getDataStore(connect);
                        String typeName = shapefile.getTypeNames()[0];
                       
                             
                         
                             FeatureSource<SimpleFeatureType, SimpleFeature> featureSource;
                             FeatureCollection<SimpleFeatureType, SimpleFeature> collection;
                            // FeatureIterator<SimpleFeature> iterator;
                                     
                              featureSource = shapefile.getFeatureSource(typeName);
                               
                              Filter filter = CQL.toFilter(("ADA_NO="+parselNo));
                               collection = featureSource.getFeatures(filter);
                               
                              BoundingBox ft=collection.getBounds();
                              Double i,k,l,m;
                               i=ft.getMaxX();
                               k=ft.getMaxY();
                               l=ft.getMinX();
                               m=ft.getMinY();
                             
                               GeometryFactory geometryFactory= new GeometryFactory ();
                               
                            // System.out.print("MaxX:"+i+"MaxY:"+k+"MinX:"+l+"MinY:"+m);
                              Coordinate[] bbox=new Coordinate[5];
                              bbox[0]= new Coordinate (l+5,m+5);
                              bbox[1]= new Coordinate (l+5,k+5);
                              bbox[2]= new Coordinate (i+5,k+5);
                              bbox[3]= new Coordinate (i+5,l+5);
                              bbox[4]= new Coordinate (l+5,m+5);
                               
                               LinearRing bboxRing= geometryFactory.createLinearRing(bbox);
                               Polygon bboxPolygon = geometryFactory.createPolygon(bboxRing, null);
                             
                               File file2 = new File ("/tezle ilgili/poligon/poligon_font_point.shp" );
 
                                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> fs;
                                    FeatureCollection<SimpleFeatureType, SimpleFeature> cl;
                                    FeatureIterator<SimpleFeature> iterator;
                                    fs = shapefile2.getFeatureSource(typeName2);
                                    cl = fs.getFeatures();
                                   
                                    try {
                                         while (iterator.hasNext()) {
                                                SimpleFeature feature = iterator.next();
                                                Geometry geometry= (Geometry)feature.getDefaultGeometry();
.......

Idon't know how to continue.

Or you can suggest me anyway to find the points inside the polygon..


It is very urgent for me. thanks for any help.

Best regards.

Vurfem

mbedward

Re: İntersection of polygon and point layers

Reply Threaded More More options
Print post
Permalink
Hi Vurfem,

Leaving out the code to connect to shapefiles etc. which it looks like
you've got under control, here's one way to do it (others will suggest
much better ways I'm sure !)...

FeatureSource<SimpleFeatureType, SimpleFeature> parcelSrc = ...  //
from parcels shapefile
FeatureSource<SimpleFeatureType, SimpleFeature> buildingSrc = ...  //
from buildings shapefile

FeatureCollection<SimpleFeatureType, SimpleFeature> buildingColl =
buildingSrc.getFeatures();

WKTWriter wktWriter = new WKTWriter();
FeatureIterator<SimpleFeature> iter = buildingColl.features();
try {
    while (iter.hasNext()) {
        SimpleFeature building = iter.next();
        Geometry geom = (Geometry) building.getDefaultGeometry();
        String filterStr = "CONTAINS(the_geom, " + wktWriter.write(geom) + ")";
        Filter filter = CQL.toFilter(filterStr);

        FeatureCollection<SimpleFeatureType, SimpleFeature> parcelColl
= parcelSrc.getFeatures(filter);
        if (!parcelColl.isEmpty()) {
                    FeatureIterator<SimpleFeature> parcelIter =
parcelColl.features();
                    try {
                        while (parcelIter.hasNext()) {
                            // ... get whatever data for the parcel
that contains the building
                        }

                    } finally {
                        parcelIter.close();
                    }
                }
            }

        } finally {
            iter.close();
        }

    }

------------------------------------------------------------------------------
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
mbedward

Re: İntersection of polygon and point layers

Reply Threaded More More options
Print post
Permalink
PS. that code looks for the parcel(s) that contain each building - not
sure if that's what you want but if not it hopefully gives you the
general idea

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
vurfem

Re: İntersection of polygon and point layers

Reply Threaded More More options
Print post
Permalink
Hello Michael;

Thank you for your reply. It is not what exactly I am looking for but it is a helpfull code sample for my other works.

Best regards,
Vurfem

mbedward wrote:
PS. that code looks for the parcel(s) that contain each building - not
sure if that's what you want but if not it hopefully gives you the
general idea

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