hey guys,

I wrote some code but unfortunately it does not work and I do not know why. 

I want to read in a featureCollection from a PostGIS database and a shape
file given by the user. These two feature collections are going to be
intersect with each other.
I want to loop the one from the PostGIS and get all features from shape file
lying in feature from PostGIS. I want to use the attributes from the shape
file and add attributes from PostGIS.
features that are not full in the other feature are going to be intersected. 
was this understandable? I need a little help.

Here is my code:
        public File intersect(GISViewer viewer,
FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection)
throws Exception {

                // initialize feature collection 
                featColl_adminUnit = featureCollection; 
                shapefile = new Shapefile();
                crs_INSPIRE = new CoordReferSyst();

                // create new shape file and reproject old one
                file = shapefile.reprojectToShapefile(viewer); //works fine
                store = FileDataStoreFinder.getDataStore(file);
                featSource_input = store.getFeatureSource();
                featColl_input = featSource_input.getFeatures();

                // create a new shape file with a slight modified schema
                fileNew = new File("TempFile.shp");
                factory = new ShapefileDataStoreFactory();
                create = new HashMap<String, Serializable>();
                create.put("url", fileNew.toURI().toURL());
                create.put("create spatial index", Boolean.TRUE);        
                newDataStore = factory.createNewDataStore(create);
                schema = featSource_input.getSchema();
                featureType = SimpleFeatureTypeBuilder.retype(schema,
crs_INSPIRE.crs_ETRS89_LAEA());
                newDataStore.createSchema(featureType);

                // carefully open an iterator and writer to process the results
                transaction = new DefaultTransaction("Convert");
                writer = 
newDataStore.getFeatureWriterAppend(featureType.getTypeName(),
transaction);
                
                // initialize feature iterator
                iterator_adminUnit = featColl_adminUnit.features();
                iterator_input = featColl_input.features();
                
                try {
                        while(iterator_adminUnit.hasNext()){
                                feature_adminUnit = iterator_adminUnit.next();
                                boundingBox = feature_adminUnit.getBounds();
                                
                                geometryColumnName =
feature_adminUnit.getDefaultGeometryProperty().getName();
                                cqlExpression = "BBOX(" + geometryColumnName + 
"," +
boundingBox.getMinX() + ","
                                + boundingBox.getMinY() + "," + 
boundingBox.getMaxX() + "," +
boundingBox.getMaxY() + ")";
                                try {
                                        filter = CQL.toFilter(cqlExpression);
                                        collectionBoundingBox = 
featSource_input.getFeatures(filter);
                                        featureIterator = 
collectionBoundingBox.features();

                                } catch (CQLException e1) {
                                        System.out.printf("Unable to create CQL 
: {}, Reason: {}",
cqlExpression , e1);
                                        continue;
                                }
                                
                                while(featureIterator.hasNext()) {

                                        feature = featureIterator.next();
                                        
                                        java.util.List attributes = new 
ArrayList();
                                        attributes.add(feature.getAttributes());
                                        
attributes.add(feature_adminUnit.getAttributes());
                                        copy.setAttributes(attributes);
                                        
                                        Geometry geometry1 = (Geometry) 
feature.getDefaultGeometry();
                                        Geometry geometry2 = (Geometry) 
feature_adminUnit.getDefaultGeometry();

                                        if (geometry2.contains(geometry1)){
                                                copy = writer.next();
                                                
copy.setDefaultGeometry(geometry1);
                                                writer.write();
                                        }
                                        if (geometry2.intersects(geometry1)){
                                                copy = writer.next();
                                                geometry3 = 
geometry2.intersection(geometry1);
                                                
copy.setDefaultGeometry(geometry3);
                                                writer.write();
                                        }
                                }
                        }
                        transaction.commit();
                        JOptionPane.showMessageDialog(null, "Convert to 
Administrative Unit
complete");
                } catch (Exception problem) {
                        problem.printStackTrace();
                        transaction.rollback();
                        JOptionPane.showMessageDialog(null, "Convert to 
Administrative Unit
failed");
                } finally {
                        writer.close();
                        featureIterator.close();
                        iterator_adminUnit.close();
                        transaction.close();
                        //copy(fileNew, file);
                        //fileNew.deleteOnExit();
                }
                return fileNew;
        }


-- 
View this message in context: 
http://osgeo-org.1803224.n2.nabble.com/Intersection-of-PostGIS-view-and-shape-file-tp5186523p5186523.html
Sent from the geotools-gt2-users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate 
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the 
lucky parental unit.  See the prize list and enter to win: 
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to