Package org.geotools.feature

Examples of org.geotools.feature.FeatureIterator


        FeatureTypeInfo fti = getCatalog().getFeatureTypeByName(MockData.LINES.getLocalPart());
        assertEquals("EPSG:3004", fti.getSRS());
        assertEquals(ProjectionPolicy.NONE, fti.getProjectionPolicy());
        FeatureCollection fc = fti.getFeatureSource(null, null).getFeatures();
        assertEquals(CRS.decode("EPSG:32615"), fc.getSchema().getCoordinateReferenceSystem());
        FeatureIterator fi = fc.features();
        Feature f = fi.next();
       
        //test that the geometry was left in tact
        Geometry g = (Geometry) f.getDefaultGeometryProperty().getValue();
        assertTrue(g.equalsExact(WKT.read("LINESTRING(500125 500025,500175 500075)")));
       
        fi.close();
        assertEquals(CRS.decode("EPSG:32615"), f.getType().getCoordinateReferenceSystem());
    }
View Full Code Here


        FeatureTypeInfo fti = getCatalog().getFeatureTypeByName("MyPoints");
        assertEquals("EPSG:4326", fti.getSRS());
        assertEquals(ProjectionPolicy.REPROJECT_TO_DECLARED, fti.getProjectionPolicy());
        FeatureCollection fc = fti.getFeatureSource(null, null).getFeatures();
        assertEquals(CRS.decode("EPSG:4326"), fc.getSchema().getCoordinateReferenceSystem());
        FeatureIterator fi = fc.features();
        Feature f = fi.next();
       
        //test that geometry was reprojected
        Geometry g = (Geometry) f.getDefaultGeometryProperty().getValue();
        assertFalse(g.equalsExact(WKT.read("POINT(500050 500050)")));
        fi.close();
        assertEquals(CRS.decode("EPSG:4326"), f.getType().getCoordinateReferenceSystem());
    }
View Full Code Here

                featureCollection.getFeature().clear();
                featureCollection.getFeature().addAll(filtered);
               
                // Hack to initialize the threadlocals
                for(FeatureCollection fc : (List<FeatureCollection>)featureCollection.getFeature()) {
                    FeatureIterator fi = fc.features();
                    try {
                        while(fi.hasNext())
                            fi.next();
                    } finally {
                        fi.close();
                    }
                }
            }
        }
       
View Full Code Here

        VersioningDataStore synch = (VersioningDataStore) getCatalog().getDataStoreByName("synch")
                .getDataStore(null);
        FeatureSource<SimpleFeatureType, SimpleFeature> fs = synch.getFeatureSource(SYNCH_HISTORY);
        FeatureCollection fc = fs.getFeatures(ff.equals(ff.property("table_name"), ff
                .literal("restricted")));
        FeatureIterator fi = fc.features();
        SimpleFeature f = (SimpleFeature) fi.next();
        fi.close();

        assertEquals(12l, f.getAttribute("central_revision"));
    }
View Full Code Here

        int maxfeatures = request.getFeatureCount(); // will default to 1
                                                     // if not specified
                                                     // in the request

        FeatureIterator reader = null;

        try {
            final List collections = results.getFeature();
            final int size = collections.size();
            FeatureCollection fr;
            SimpleFeature f;

            SimpleFeatureType schema;
            List<AttributeDescriptor> types;
           
            for (int i = 0; i < size; i++) // for each layer queried
            {
                fr = (FeatureCollection) collections.get(i);               
                reader = fr.features();
             
                boolean startFeat = true;
                while (reader.hasNext()) {
                    Feature feature = reader.next();
                   
                    if (startFeat) {
                        writer.println("Results for FeatureType '" + fr.getSchema().getName()
                            + "':");
                        startFeat = false;
                    }
                   
                    if (featuresPrinted < maxfeatures) {
                        writer.println("--------------------------------------------");
                   
                        if (feature instanceof SimpleFeature)
                        {
                            f = (SimpleFeature) feature;
                            schema = (SimpleFeatureType) f.getType();
                            types = schema.getAttributeDescriptors();
       
                            for (AttributeDescriptor descriptor : types) {
                                final Name name = descriptor.getName();
                                if (Geometry.class.isAssignableFrom(descriptor.getType().getBinding())) {
                                    // writer.println(types[j].getName() + " =
                                    // [GEOMETRY]");
   
                                    // DJB: changed this to print out WKT - its very
                                    // nice for users
                                    // Geometry g = (Geometry)
                                    // f.getAttribute(types[j].getName());
                                    // writer.println(types[j].getName() + " =
                                    // [GEOMETRY] = "+g.toText() );
   
                                    // DJB: decided that all the geometry info was
                                    // too much - they should use GML version if
                                    // they want those details
                                    Geometry g = (Geometry) f.getAttribute(name);
                                    writer.println(name + " = [GEOMETRY (" + g.getGeometryType()
                                            + ") with " + g.getNumPoints() + " points]");
                                } else {
                                    writer.println(name + " = " + f.getAttribute(name));
                                }
                            }
                           
                        }

                        else
                        {
                            writer.println(feature.toString());
                        }
                    }
                   
                    writer.println("--------------------------------------------");
                    featuresPrinted++;
                   
                }
            }
        } catch (Exception ife) {
            LOGGER.log(Level.WARNING, "Error generating getFeaturInfo, HTML format", ife);
            writer.println("Unable to generate information " + ife);
        } finally {
            if (reader != null) {
                reader.close();
            }
        }

        if (featuresPrinted == 0) {
            writer.println("no features were found");
View Full Code Here

        List list = new ArrayList();
       
        Iterator it = collections.iterator();
        while(it.hasNext()){
            FeatureCollection col = (FeatureCollection)it.next();
            FeatureIterator it2 = col.features();
            while (it2.hasNext()){
                list.add(it.next());
            }
            it2.close();
        }
       
        return list.toArray(arg0);
    }
View Full Code Here

    public String getMode() {
        return mode;
    }
   
    public FeatureIterator openIterator(FeatureCollection fc) {
        FeatureIterator fi = fc.features();
        iterators.put(fi, fi);
        return fi;
    }
View Full Code Here

    //
    // FeatureCollection - Feature Access
    //
    @SuppressWarnings("unchecked")
    public FeatureIterator<F> features() {
        FeatureIterator iter = new DelegateFeatureIterator(openIterator());
        getOpenIterators().add(iter);
        return iter;
    }
View Full Code Here

    private Set<String> computeFids(Tile tile, Connection conn)
            throws Exception {
        Tile parent = tile.getParent();
        Set<String> parentFids = getUpwardFids(parent, conn);
        Set<String> currFids = new HashSet<String>();
        FeatureIterator fi = null;
        try {
            // grab the features
            FeatureSource fs = featureType.getFeatureSource(null,null);
            GeometryDescriptor geom = fs.getSchema().getGeometryDescriptor();
            CoordinateReferenceSystem nativeCrs = geom
                    .getCoordinateReferenceSystem();

            ReferencedEnvelope nativeTileEnvelope = null;

            if (!CRS.equalsIgnoreMetadata(Tile.WGS84, nativeCrs)) {
                try {
                    nativeTileEnvelope = tile.getEnvelope().transform(nativeCrs, true);
                } catch (ProjectionException pe) {
                    // the WGS84 envelope of the tile is too big for this project,
                    // let's intersect it with the declared lat/lon bounds then
                    LOGGER.log(Level.INFO, "Could not reproject the current tile bounds "
                            + tile.getEnvelope() + " to the native SRS, intersecting with "
                            + "the layer declared lat/lon bounds and retrying");
                   
                    // let's compare against the declared data bounds then
                    ReferencedEnvelope llEnv = featureType.getLatLonBoundingBox();
                    Envelope reduced = tile.getEnvelope().intersection(llEnv);
                    if(reduced.isNull() || reduced.getWidth() == 0 || reduced.getHeight() == 0) {
                        // no overlap, no party, the tile will be empty
                        return Collections.emptySet();
                    }
                   
                    // there is some overlap, let's try the reprojection again.
                    // if even this fails, the user has evidently setup the
                    // geographics bounds improperly
                    ReferencedEnvelope refRed = new ReferencedEnvelope(reduced,
                            tile.getEnvelope().getCoordinateReferenceSystem());
                    nativeTileEnvelope = refRed.transform(nativeCrs, true);
                }
            } else {
                nativeTileEnvelope = tile.getEnvelope();
            }

            fi = getSortedFeatures(geom, tile.getEnvelope(), nativeTileEnvelope, conn);

            // if the crs is not wgs84, we'll need to transform the point
            MathTransform tx = null;
            double[] coords = new double[2];

            // scan counting how many fids we've collected
            boolean first = true;
            while (fi.hasNext() && currFids.size() < featuresPerTile) {
                // grab the feature, skip it if it's already in a parent element
                SimpleFeature f = (SimpleFeature) fi.next();
                if (parentFids.contains(f.getID()))
                    continue;

                // check the need for a transformation
                if (first) {
                    first = false;
                    CoordinateReferenceSystem nativeCRS = f.getType()
                            .getCoordinateReferenceSystem();
                    featureType.getFeatureType().getCoordinateReferenceSystem();
                    if (nativeCRS != null
                            && !CRS.equalsIgnoreMetadata(nativeCRS, Tile.WGS84)) {
                        tx = CRS.findMathTransform(nativeCRS, Tile.WGS84);
                    }
                }

                // see if the features is to be included in this tile
                Point p = ((Geometry) f.getDefaultGeometry()).getCentroid();
                coords[0] = p.getX();
                coords[1] = p.getY();
                if (tx != null)
                    tx.transform(coords, 0, coords, 0, 1);
                if (tile.contains(coords[0], coords[1]))
                    currFids.add(f.getID());
            }
        } finally {
            if (fi != null)
                fi.close();
        }
        return currFids;
    }
View Full Code Here

    }
   
    void buildIndex(Connection conn) throws Exception {
        Statement st = null;
        PreparedStatement ps = null;
        FeatureIterator fi = null;
        try {
            st = conn.createStatement();
            st.execute("CREATE TABLE FEATUREIDX(" //
                    + "X NUMBER, " //
                    + "Y NUMBER, " //
                    + "FID VARCHAR(64), " //
                    + "ORDER_FIELD " + h2Type + ")");
            st.execute("CREATE INDEX FEATUREIDX_COORDS ON FEATUREIDX(X, Y)");
            st.execute("CREATE INDEX FEATUREIDX_ORDER_FIELD ON FEATUREIDX(ORDER_FIELD)");

            // prepare this statement so that the sql parser has to deal
            // with it just once
            ps = conn.prepareStatement("INSERT INTO "
                    + "FEATUREIDX(X, Y, FID, ORDER_FIELD) VALUES (?, ?, ?, ?)");

            // build an optimized query, loading only the necessary attributes
            GeometryDescriptor geom = fs.getSchema()
                    .getGeometryDescriptor();
            CoordinateReferenceSystem nativeCrs = geom
                    .getCoordinateReferenceSystem();
            Query q = new Query();
           
            if (geom.getLocalName().equals(attribute)) {
                q.setPropertyNames(new String[] { geom.getLocalName() });
            } else {
                q.setPropertyNames(new String[] { attribute, geom.getLocalName() });
            }
           
            // setup the eventual transform
            MathTransform tx = null;
            double[] coords = new double[2];
            if (!CRS.equalsIgnoreMetadata(nativeCrs, Tile.WGS84))
                tx = CRS.findMathTransform(nativeCrs, Tile.WGS84, true);

            // read all the features and fill the index table
            // make it so the insertion is a single big transaction, should
            // be faster,
            // provided it does not kill H2...
            conn.setAutoCommit(false);
            fi = fs.getFeatures(q).features();
            while (fi.hasNext()) {
                // grab the centroid and transform it in 4326 if necessary
                SimpleFeature f = (SimpleFeature) fi.next();
                Geometry g = (Geometry) f.getDefaultGeometry();
                if(g.isEmpty()) {
                    continue;
                }
                Point centroid = g.getCentroid();
               
                //robustness check for bad geometries
                if ( Double.isNaN( centroid.getX() ) || Double.isNaN( centroid.getY() ) ) {
                    LOGGER.warning( "Could not calculate centroid for feature " + f.getID() + "; g =  " + g.toText() );
                    continue;
                }
               
                coords[0] = centroid.getX();
                coords[1] = centroid.getY();
                if (tx != null)
                    tx.transform(coords, 0, coords, 0, 1);

                // insert
                ps.setDouble(1, coords[0]);
                ps.setDouble(2, coords[1]);
                ps.setString(3, f.getID());
                ps.setObject(4, getSortAttributeValue(f));
                ps.execute();
            }
            // todo: commit every 1000 features or so. No transaction is
            // slower, but too big transaction imposes a big overhead on the db
            conn.commit();
           
            // hum, shall we kick H2 so that it updates the statistics?
        } finally {
            conn.setAutoCommit(true);
            JDBCUtils.close(st);
            JDBCUtils.close(ps);
            if (fi != null)
                fi.close();
        }
    }
View Full Code Here

TOP

Related Classes of org.geotools.feature.FeatureIterator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.