Java Code Examples for org.geotools.feature.FeatureCollection#features()

The following examples show how to use org.geotools.feature.FeatureCollection#features() . 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: geoUtils.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
public static String getFileLayerAction(String layerUrl) throws JSONException, IOException, EMFUserError {
	IFeaturesProviderFileDAO featuresProvider = DAOFactory.getFeaturesProviderFileDAO();
	FeatureCollection outputFeatureCollection = featuresProvider.getAllFeatures(layerUrl);
	FeatureIterator it = outputFeatureCollection.features();
	List<SimpleFeature> list = new ArrayList<SimpleFeature>();
	while (it.hasNext()) {
		SimpleFeature f = (SimpleFeature) it.next();
		list.add(f);
	}

	FeatureCollection<SimpleFeatureType, SimpleFeature> filteredOutputFeatureCollection = DataUtilities.collection(list);

	Monitor.start("GetTargetLayerAction.flushResponse");
	FeatureJSON featureJSON = new FeatureJSON();
	String responseFeature = featureJSON.toString(filteredOutputFeatureCollection);

	return responseFeature;
}
 
Example 2
Source File: FeatureFigureEditorApp.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void loadFigureCollection(File file, FigureCollection figureCollection) throws IOException {
    FeatureSource<SimpleFeatureType, SimpleFeature> featureFeatureSource;
    FeatureCollection<SimpleFeatureType, SimpleFeature> featureTypeSimpleFeatureFeatureCollection;
    featureFeatureSource = getFeatureSource(file);
    featureTypeSimpleFeatureFeatureCollection = featureFeatureSource.getFeatures();
    try(FeatureIterator<SimpleFeature> featureIterator = featureTypeSimpleFeatureFeatureCollection.features();) {
        while (featureIterator.hasNext()) {
            SimpleFeature simpleFeature = featureIterator.next();
            DefaultFigureStyle figureStyle = createDefaultFigureStyle();
            Object o = simpleFeature.getDefaultGeometry();
            if (o instanceof Point) {
                figureCollection.addFigure(new SimpleFeaturePointFigure(simpleFeature, sceneTransformProvider, figureStyle));
            } else {
                figureCollection.addFigure(new SimpleFeatureShapeFigure(simpleFeature, sceneTransformProvider, figureStyle));
            }
        }
    }
}
 
Example 3
Source File: GeoToolsLayer.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Envelope getBounds(Filter filter) throws LayerException {
	FeatureSource<SimpleFeatureType, SimpleFeature> source = getFeatureSource();
	if (source instanceof FeatureStore<?, ?>) {
		SimpleFeatureStore store = (SimpleFeatureStore) source;
		transactionSynchronization.synchTransaction(store);
	}
	try {
		FeatureCollection<SimpleFeatureType, SimpleFeature> fc;
		if (null == filter) {
			fc = source.getFeatures();
		} else {
			fc = source.getFeatures(filter);
		}
		FeatureIterator<SimpleFeature> it = fc.features();
		transactionSynchronization.addIterator(it);
		return fc.getBounds();
	} catch (Throwable t) { // NOSONAR avoid errors (like NPE) as well
		throw new LayerException(t, ExceptionCode.UNEXPECTED_PROBLEM);
	}
}
 
Example 4
Source File: GeoToolsLayer.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 */
@Transactional(readOnly = true)
public Iterator<?> getElements(Filter filter, int offset, int maxResultSize) throws LayerException {
	FeatureSource<SimpleFeatureType, SimpleFeature> source = getFeatureSource();
	try {
		if (source instanceof FeatureStore<?, ?>) {
			SimpleFeatureStore store = (SimpleFeatureStore) source;
			transactionSynchronization.synchTransaction(store);
		}
		Query query = new Query();
		query.setFilter(filter);
		query.setMaxFeatures(maxResultSize > 0 ? maxResultSize : Integer.MAX_VALUE);
		query.setStartIndex(offset);
		FeatureCollection<SimpleFeatureType, SimpleFeature> fc = source.getFeatures(query);
		FeatureIterator<SimpleFeature> it = fc.features();
		transactionSynchronization.addIterator(it);
		return new JavaIterator(it);
	} catch (Throwable t) { // NOSONAR avoid errors (like NPE) as well
		throw new LayerException(t, ExceptionCode.UNEXPECTED_PROBLEM);
	}
}
 
Example 5
Source File: VectorDataLayer.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private void updateFigureCollection() {
    FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection = vectorDataNode.getFeatureCollection();

    Figure[] figures = figureCollection.getFigures();
    Map<SimpleFeature, SimpleFeatureFigure> figureMap = new HashMap<>();
    for (Figure figure : figures) {
        if (figure instanceof SimpleFeatureFigure) {
            SimpleFeatureFigure simpleFeatureFigure = (SimpleFeatureFigure) figure;
            figureMap.put(simpleFeatureFigure.getSimpleFeature(), simpleFeatureFigure);
        }
    }

    FeatureIterator<SimpleFeature> featureIterator = featureCollection.features();
    while (featureIterator.hasNext()) {
        SimpleFeature simpleFeature = featureIterator.next();
        SimpleFeatureFigure featureFigure = figureMap.get(simpleFeature);
        if (featureFigure != null) {
            figureMap.remove(simpleFeature);
            Placemark placemark = vectorDataNode.getPlacemarkGroup().getPlacemark(simpleFeature);
            if(placemark != null) {
                String css = placemark.getStyleCss();
                final FigureStyle normalStyle = DefaultFigureStyle.createFromCss(css);
                final FigureStyle selectedStyle = getFigureFactory().deriveSelectedStyle(normalStyle);
                featureFigure.setNormalStyle(normalStyle);
                featureFigure.setSelectedStyle(selectedStyle);
            }
        } else {
            featureFigure = getFigureFactory().createSimpleFeatureFigure(simpleFeature, vectorDataNode.getDefaultStyleCss());
            figureCollection.addFigure(featureFigure);
        }
        featureFigure.forceRegeneration();
    }

    Collection<SimpleFeatureFigure> remainingFigures = figureMap.values();
    figureCollection.removeFigures(remainingFigures.toArray(new Figure[remainingFigures.size()]));

}
 
Example 6
Source File: ImportTrackAction.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private static void transformFeatureCollection(FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection, CoordinateReferenceSystem targetCRS) throws TransformException {
    final GeometryCoordinateSequenceTransformer transform = FeatureUtils.getTransform(DefaultGeographicCRS.WGS84, targetCRS);
    final FeatureIterator<SimpleFeature> features = featureCollection.features();
    final GeometryFactory geometryFactory = new GeometryFactory();
    while (features.hasNext()) {
        final SimpleFeature simpleFeature = features.next();
        final Point sourcePoint = (Point) simpleFeature.getDefaultGeometry();
        final Point targetPoint = transform.transformPoint(sourcePoint, geometryFactory);
        simpleFeature.setDefaultGeometry(targetPoint);
    }
}
 
Example 7
Source File: ExportGeometryAction.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private static Map<Class<?>, List<SimpleFeature>> createGeometryToFeaturesListMap(VectorDataNode vectorNode) throws TransformException,
                                                                                                                    SchemaException {
    FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection = vectorNode.getFeatureCollection();
    CoordinateReferenceSystem crs = vectorNode.getFeatureType().getCoordinateReferenceSystem();
    if (crs == null) {   // for pins and GCPs crs is null
        crs = vectorNode.getProduct().getSceneCRS();
    }
    final CoordinateReferenceSystem modelCrs;
    if (vectorNode.getProduct().getSceneGeoCoding() instanceof CrsGeoCoding) {
        modelCrs = vectorNode.getProduct().getSceneCRS();
    } else {
        modelCrs = DefaultGeographicCRS.WGS84;
    }

    // Not using ReprojectingFeatureCollection - it is reprojecting all geometries of a feature
    // but we want to reproject the default geometry only
    GeometryCoordinateSequenceTransformer transformer = createTransformer(crs, modelCrs);

    Map<Class<?>, List<SimpleFeature>> featureListMap = new HashMap<>();
    final FeatureIterator<SimpleFeature> featureIterator = featureCollection.features();
    // The schema needs to be reprojected. We need to build a new feature be cause we can't change the schema.
    // It is necessary to have this reprojected schema, because otherwise the shapefile is not correctly georeferenced.
    SimpleFeatureType schema = featureCollection.getSchema();
    SimpleFeatureType transformedSchema = FeatureTypes.transform(schema, modelCrs);
    while (featureIterator.hasNext()) {
        SimpleFeature feature = featureIterator.next();
        Object defaultGeometry = feature.getDefaultGeometry();
        feature.setDefaultGeometry(transformer.transform((Geometry) defaultGeometry));

        Class<?> geometryType = defaultGeometry.getClass();
        List<SimpleFeature> featureList = featureListMap.computeIfAbsent(geometryType, k -> new ArrayList<>());
        SimpleFeature exportFeature = SimpleFeatureBuilder.build(transformedSchema, feature.getAttributes(), feature.getID());
        featureList.add(exportFeature);
    }
    return featureListMap;
}
 
Example 8
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 9
Source File: WmsLayer.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
private List<Feature> getGmlFeatures(InputStream stream, Version version) throws IOException, SAXException,
		ParserConfigurationException {
	List<Feature> features = new ArrayList<Feature>();
	GML gml = new GML(version);
	FeatureCollection<?, SimpleFeature> collection = gml.decodeFeatureCollection(stream);
	FeatureIterator<SimpleFeature> it = collection.features();

	while (it.hasNext()) {
		features.add(toDto(it.next()));
	}
	return features;
}