Java Code Examples for org.geotools.feature.FeatureIterator#close()

The following examples show how to use org.geotools.feature.FeatureIterator#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestVectorFilter.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("nls")
public void testVectorFilter() throws Exception {
    SimpleFeatureCollection testFC = HMTestMaps.getTestFC();
    OmsVectorFilter filter = new OmsVectorFilter();
    filter.inVector = testFC;
    filter.pCql = "cat > 2";
    filter.process();
    SimpleFeatureCollection outFC = filter.outVector;

    assertTrue(outFC.size() == 1);

    FeatureIterator<SimpleFeature> featureIterator = outFC.features();
    SimpleFeature feature = featureIterator.next();
    assertNotNull(feature);

    Integer attribute = (Integer) feature.getAttribute("cat");
    assertEquals(3, attribute.intValue());
    featureIterator.close();

}
 
Example 2
Source File: TestVectorReshaper.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("nls")
public void testFeatureReshaper() throws Exception {

    SimpleFeatureCollection testFC = HMTestMaps.getTestFC();

    OmsVectorReshaper reshaper = new OmsVectorReshaper();
    reshaper.inVector = testFC;
    reshaper.pCql = "newcat=cat*2 \n newcat2=cat*4";
    reshaper.process();
    SimpleFeatureCollection outFC = reshaper.outVector;

    FeatureIterator<SimpleFeature> featureIterator = outFC.features();
    SimpleFeature feature = featureIterator.next();
    assertNotNull(feature);

    Integer attribute = (Integer) feature.getAttribute("cat");
    Double newAttribute = (Double) feature.getAttribute("newcat");
    Double newAttribute2 = (Double) feature.getAttribute("newcat2");
    assertEquals(attribute.intValue() * 2, newAttribute.intValue());
    assertEquals(attribute.intValue() * 4, newAttribute2.intValue());
    featureIterator.close();

}
 
Example 3
Source File: TestVectorReshaper.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
public void testBuffer() throws Exception {
    String cql = "the_geom=buffer(the_geom, 20.0)";

    SimpleFeatureCollection testFC = HMTestMaps.getTestFC();
    OmsVectorReshaper reshaper = new OmsVectorReshaper();
    reshaper.inVector = testFC;
    reshaper.pCql = cql;
    reshaper.process();
    SimpleFeatureCollection outFC = reshaper.outVector;
    FeatureIterator<SimpleFeature> featureIterator = outFC.features();
    SimpleFeature feature = featureIterator.next();
    Geometry geometry = (Geometry) feature.getDefaultGeometry();
    String geometryType = geometry.getGeometryType();
    assertTrue(geometryType.toUpperCase().equals("POLYGON"));
    featureIterator.close();
}
 
Example 4
Source File: TestVectorReshaper.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
public void testCentroid() throws Exception {
    String cql = "the_geom=centroid(the_geom)";

    SimpleFeatureCollection testFC = HMTestMaps.getTestLeftFC();
    OmsVectorReshaper reshaper = new OmsVectorReshaper();
    reshaper.inVector = testFC;
    reshaper.pCql = cql;
    reshaper.process();
    SimpleFeatureCollection outFC = reshaper.outVector;
    FeatureIterator<SimpleFeature> featureIterator = outFC.features();
    SimpleFeature feature = featureIterator.next();
    Geometry geometry = (Geometry) feature.getDefaultGeometry();
    String geometryType = geometry.getGeometryType();
    assertTrue(geometryType.toUpperCase().equals("POINT"));
    featureIterator.close();
}
 
Example 5
Source File: ShapeInMemFeatureModelTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	ClassLoader classloader = Thread.currentThread().getContextClassLoader();
	URL url = classloader.getResource(SHAPE_FILE);
	DataStore dataStore = new ShapefileDataStore(url);
	featureModel = new ShapeInMemFeatureModel(dataStore, LAYER_NAME, 4326, converterService);
	featureModel.setLayerInfo(layerInfo);

	FeatureSource<SimpleFeatureType, SimpleFeature> fs = featureModel.getFeatureSource();
	FeatureIterator<SimpleFeature> fi = fs.getFeatures().features();
	feature = fi.next();
	feature = fi.next();
	feature = fi.next();
	feature = fi.next();
	feature = fi.next();
	fi.close();
}
 
Example 6
Source File: OmsVectorReshaper.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
private SimpleFeature getSample() {
    FeatureIterator<SimpleFeature> iterator = inVector.features();
    try {
        if (!iterator.hasNext()) {
            throw new ModelsRuntimeException("Input featurecollection is empty.", this.getClass().getSimpleName());
        }
        return iterator.next();
    } finally {
        iterator.close();
    }
}
 
Example 7
Source File: TestVectorReprojector.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public void testVectorReprojector() throws Exception {

        SimpleFeatureCollection testFC = HMTestMaps.getTestFC();

        OmsVectorReprojector reprojector = new OmsVectorReprojector();
        reprojector.inVector = testFC;
        reprojector.pCode = "EPSG:4326";
        reprojector.pm = pm;
        reprojector.process();

        CoordinateReferenceSystem sourceCRS = HMTestMaps.getCrs();
        CoordinateReferenceSystem targetCRS = CrsUtilities.getCrsFromSrid(4326);

        MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS);

        SimpleFeatureCollection outFC = reprojector.outVector;
        FeatureIterator<SimpleFeature> featureIterator = outFC.features();
        SimpleFeatureIterator originalFeatureIterator = testFC.features();
        while( featureIterator.hasNext() ) {
            SimpleFeature feature = featureIterator.next();
            Geometry geometry = (Geometry) feature.getDefaultGeometry();
            Coordinate coordinate = geometry.getCoordinate();

            SimpleFeature originalFeature = originalFeatureIterator.next();
            Coordinate origCoord = ((Geometry) originalFeature.getDefaultGeometry()).getCoordinate();
            Coordinate reprojected = JTS.transform(origCoord, null, transform);

            assertEquals(reprojected.x, coordinate.x, delta);
            assertEquals(reprojected.y, coordinate.y, delta);
        }
        featureIterator.close();

    }
 
Example 8
Source File: TestVectorReshaper.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public void testConvexHull() throws Exception {
    String cql = "the_geom=convexHull(the_geom)";

    GeometryFactory gf = GeometryUtilities.gf();
    MultiPoint multiPoint = gf.createMultiPoint(new Coordinate[]{//
            HMTestMaps.getWestNorth(), HMTestMaps.getEastSouth(),
            HMTestMaps.getEastNorth()});
    SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
    b.setName("test");
    b.setCRS(HMTestMaps.getCrs());
    b.add("the_geom", MultiPoint.class);
    SimpleFeatureType type = b.buildFeatureType();
    SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);
    Object[] values = new Object[]{multiPoint};
    builder.addAll(values);
    SimpleFeature feature = builder.buildFeature(null);
    DefaultFeatureCollection newCollection = new DefaultFeatureCollection();
    newCollection.add(feature);

    OmsVectorReshaper reshaper = new OmsVectorReshaper();
    reshaper.inVector = newCollection;
    reshaper.pCql = cql;
    reshaper.process();
    SimpleFeatureCollection outFC = reshaper.outVector;
    FeatureIterator<SimpleFeature> featureIterator = outFC.features();
    SimpleFeature newFeature = featureIterator.next();
    Geometry geometry = (Geometry) newFeature.getDefaultGeometry();
    String geometryType = geometry.getGeometryType();
    assertTrue(geometryType.toUpperCase().equals("POLYGON"));
    featureIterator.close();
}
 
Example 9
Source File: OmsKriging.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Extract the coordinate of a FeatureCollection in a HashMap with an ID as
 * a key.
 * 
 * @param nStaz
 * @param collection
 * @throws Exception
 *             if a fiel of elevation isn't the same of the collection
 */
private LinkedHashMap<Integer, Coordinate> getCoordinate( int nStaz, SimpleFeatureCollection collection, String idField )
        throws Exception {
    LinkedHashMap<Integer, Coordinate> id2CoordinatesMap = new LinkedHashMap<Integer, Coordinate>();
    FeatureIterator<SimpleFeature> iterator = collection.features();
    Coordinate coordinate = null;
    try {
        while( iterator.hasNext() ) {
            SimpleFeature feature = iterator.next();
            int name = ((Number) feature.getAttribute(idField)).intValue();
            coordinate = ((Geometry) feature.getDefaultGeometry()).getCentroid().getCoordinate();
            double z = 0;
            if (fPointZ != null) {
                try {
                    z = ((Number) feature.getAttribute(fPointZ)).doubleValue();
                } catch (NullPointerException e) {
                    pm.errorMessage(msg.message("kriging.noPointZ"));
                    throw new Exception(msg.message("kriging.noPointZ"));
                }
            }
            coordinate.z = z;
            id2CoordinatesMap.put(name, coordinate);
        }
    } finally {
        iterator.close();
    }

    return id2CoordinatesMap;
}
 
Example 10
Source File: ShapeInMemLayer.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Finish initializing the layer.
 *
 * @throws LayerException oops
 */
@PostConstruct
protected void initFeatures() throws LayerException {
	crs = geoService.getCrs2(layerInfo.getCrs());
	try {
		setFeatureSourceName(layerInfo.getFeatureInfo().getDataSourceName());
		featureModel = new ShapeInMemFeatureModel(getDataStore(), layerInfo.getFeatureInfo().getDataSourceName(),
				geoService.getSridFromCrs(layerInfo.getCrs()), converterService);
		featureModel.setLayerInfo(layerInfo);
		FeatureCollection<SimpleFeatureType, SimpleFeature> col = getFeatureSource().getFeatures();
		FeatureIterator<SimpleFeature> iterator = col.features();
		int lastIndex = 0;
		while (iterator.hasNext()) {
			SimpleFeature feature = iterator.next();
			String id = featureModel.getId(feature);
			features.put(id, feature);
			int intId = Integer.parseInt(id.substring(id.lastIndexOf('.') + 1));
			if (intId > lastIndex) {
				lastIndex = intId;
			}
		}
		iterator.close();
		((ShapeInMemFeatureModel) featureModel).setNextId(++lastIndex);
	} catch (NumberFormatException nfe) {
		throw new LayerException(nfe, ExceptionCode.FEATURE_MODEL_PROBLEM, url);
	} catch (MalformedURLException e) {
		throw new LayerException(e, ExceptionCode.INVALID_SHAPE_FILE_URL, url);
	} catch (IOException ioe) {
		throw new LayerException(ioe, ExceptionCode.CANNOT_CREATE_LAYER_MODEL, url);
	} catch (GeomajasException ge) {
		throw new LayerException(ge, ExceptionCode.CANNOT_CREATE_LAYER_MODEL, url);
	}
}
 
Example 11
Source File: SeaRouting.java    From searoute with European Union Public License 1.2 4 votes vote down vote up
public SeaRouting(int resKM) {
	try {

		//load marnet
		DataStore store = null;
		SimpleFeatureCollection fc = null;
		/*try {
			URL url = getClass().getResource("/marnet/marnet_plus_"+resKM+"km.gpkg");
			HashMap<String, Object> map = new HashMap<>();
			map.put(GeoPkgDataStoreFactory.DBTYPE.key, "geopkg");
			map.put(GeoPkgDataStoreFactory.DATABASE.key, url.getFile());
			map.put("url", url);
			store = DataStoreFinder.getDataStore(map);
			fc = store.getFeatureSource(store.getTypeNames()[0]).getFeatures();
		} catch (Exception e) {*/
			//URL url = new URL(path);
			HashMap<String, Object> map = new HashMap<>();
			map.put(GeoPkgDataStoreFactory.DBTYPE.key, "geopkg");
			map.put(GeoPkgDataStoreFactory.DATABASE.key, "marnet/marnet_plus_"+resKM+"km.gpkg");
			map.put("url", "marnet/marnet_plus_"+resKM+"km.gpkg");
			store = DataStoreFinder.getDataStore(map);
			fc = store.getFeatureSource(store.getTypeNames()[0]).getFeatures();
		//}

		//build graph
		FeatureIterator<?> it = fc.features();
		FeatureGraphGenerator gGen = new FeatureGraphGenerator(new LineStringGraphGenerator());
		while(it.hasNext()) gGen.add(it.next());
		g = gGen.getGraph();
		it.close();
		store.dispose();
	} catch (Exception e) { e.printStackTrace(); }

	//link nodes around the globe
	for(Object o : g.getNodes()){
		Node n = (Node)o;
		Coordinate c = ((Point)n.getObject()).getCoordinate();
		if(c.x==180) {
			Node n_ = getNode(new Coordinate(-c.x,c.y));
			if(LOGGER.isTraceEnabled()) LOGGER.trace(c + " -> " + ((Point)n_.getObject()).getCoordinate());
			BasicEdge be = new BasicEdge(n, n_);
			n.add(be);
			n_.add(be);
			g.getEdges().add(be);
		}
	}

	//define weighters
	defaultWeighter = buildEdgeWeighter(true, true);
	noSuezWeighter = buildEdgeWeighter(false, true);
	noPanamaWeighter = buildEdgeWeighter(true, false);
	noSuezNoPanamaWeighter = buildEdgeWeighter(false, false);
}
 
Example 12
Source File: TestBasinShape.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("nls")
public void testBasinShape() throws Exception {
    HashMap<String, Double> envelopeParams = HMTestMaps.getEnvelopeparams();
    CoordinateReferenceSystem crs = HMTestMaps.getCrs();

    double[][] pitData = HMTestMaps.pitData;
    GridCoverage2D pitCoverage = CoverageUtilities.buildCoverage("pit", pitData, envelopeParams, crs, true);
    double[][] basinsData = HMTestMaps.basinShapeData;
    GridCoverage2D basinsCoverage = CoverageUtilities.buildCoverage("basins", basinsData, envelopeParams, crs, true);

    OmsBasinShape basin = new OmsBasinShape();
    basin.inElev = pitCoverage;
    basin.inBasins = basinsCoverage;
    basin.pm = pm;

    basin.process();

    SimpleFeatureCollection basinsFC = basin.outBasins;

    FeatureIterator<SimpleFeature> basinsIter = basinsFC.features();
    while( basinsIter.hasNext() ) {
        SimpleFeature feature = basinsIter.next();
        Geometry line = (Geometry) feature.getDefaultGeometry();

        int numGeometries = line.getNumGeometries();
        for( int i = 0; i < numGeometries; i++ ) {
            Geometry geometryN = line.getGeometryN(i);
            int length = geometryN.getCoordinates().length;
            if (length == 9) {
                Geometry g1 = new WKTReader().read(geom1Txt);
                assertTrue(geometryN.equals(g1));
            }
            if (length == 5) {
                Geometry g2 = new WKTReader().read(geom2Txt);
                assertTrue(geometryN.equals(g2));
            }
        }
    }
    basinsIter.close();

}
 
Example 13
Source File: PutStatisticsIntoVectorDataAction.java    From snap-desktop with GNU General Public License v3.0 4 votes vote down vote up
private void setMaskRegionName(Mask selectedMask, VectorDataNode vectorDataNode) {
    FeatureIterator<SimpleFeature> features = vectorDataNode.getFeatureCollection().features();
    mask2RegionName.put(selectedMask, Util.getFeatureName(features.next()));
    features.close();
}