/** Find the next feature */ private void findNext() { if (nextFeature != null) { return; } while (nextFeature == null) { if (nextFC != null) { if (ite.hasNext()) { nextFeature = ite.next(); continue; } else { nextFC = null; ite = null; } } else { if (firstPass) { // first pass iterate on the original FeatureCollection if (originalFI.hasNext()) { nextFC = modify2(originalFI.next(), firstPass, featureList); ite = nextFC.iterator(); } else { firstPass = false; } } else { if (unionFI.hasNext()) { // second pass iterate on the union FeatureCollection nextFC = modify2(unionFI.next(), firstPass, featureList); ite = nextFC.iterator(); } else { break; } } } } }
@Test public void testRead() throws DataStoreException, NoSuchAuthorityCodeException, FactoryException { final ExtendedDataStore store = new ExtendedDataStore(dataStore); final Name name = DefaultName.valueOf("{http://www.geotoolkit.org/test}extsql"); assertFalse(store.getNames().contains(name)); // add a new query final Query query = QueryBuilder.language( JDBCDataStore.CUSTOM_SQL, "SELECT geometry as geo ,\"intProperty\" as it FROM custom", name); store.addQuery(query); assertTrue(store.getNames().contains(name)); final FeatureType ft = store.getFeatureType(name); final FeatureCollection col = store.createSession(true).getFeatureCollection(QueryBuilder.all(name)); assertEquals(ft, col.getFeatureType()); assertEquals(3, col.size()); final FeatureIterator ite = col.iterator(); try { while (ite.hasNext()) { final Feature f = ite.next(); assertEquals(f.getType(), ft); } } finally { ite.close(); } }
@Test public void testIntersection() throws ProcessException, NoSuchIdentifierException { // Inputs final FeatureCollection<?> featureList = buildFeatureList(); final FeatureCollection<?> featureUnionList = buildFeatureUnionList(); // Process ProcessDescriptor desc = ProcessFinder.getProcessDescriptor("vector", "union"); ParameterValueGroup in = desc.getInputDescriptor().createValue(); in.parameter("feature_in").setValue(featureList); in.parameter("feature_union").setValue(featureUnionList); in.parameter("input_geometry_name").setValue("geom1"); org.geotoolkit.process.Process proc = desc.createProcess(in); // Features out final FeatureCollection<?> featureListOut = (FeatureCollection<?>) proc.call().parameter("feature_out").getValue(); // Expected Features out final FeatureCollection<?> featureListResult = buildResultList(); assertEquals(featureListOut.getFeatureType(), featureListResult.getFeatureType()); assertEquals(featureListOut.getID(), featureListResult.getID()); assertEquals(featureListOut.size(), featureListResult.size()); assertTrue(featureListOut.containsAll(featureListResult)); }
public void testReadFeatures() throws Exception { FeatureCollection fc = dataStore.createSession(false).getFeatureCollection(QueryBuilder.all(nsname(PERSON))); assertEquals(2, fc.size()); FeatureIterator<SimpleFeature> fr = fc.iterator(); assertTrue(fr.hasNext()); SimpleFeature f = fr.next(); assertTrue(fr.hasNext()); f = fr.next(); assertFalse(fr.hasNext()); fr.close(); }
public static FeatureCollection wrap(final FeatureCollection col, final Query query) { return new AbstractFeatureCollection("wrap", col.getSource()) { private FeatureType type = null; @Override public FeatureType getFeatureType() { if (type == null) { try (FeatureReader ite = iterator(null)) { type = ite.getFeatureType(); } } return type; } @Override public FeatureReader iterator(Hints hints) throws FeatureStoreRuntimeException { final FeatureReader ite = (FeatureReader) col.iterator(); try { return wrap(ite, query); } catch (DataStoreException ex) { throw new FeatureStoreRuntimeException(ex); } } @Override public void update(Filter filter, Map values) throws DataStoreException { throw new DataStoreException("Not supported."); } @Override public void remove(Filter filter) throws DataStoreException { throw new DataStoreException("Not supported."); } }; }
/** * Connect to the original FeatureConnection * * @param originalFC FeatureCollection * @param intersList FeatureCollection */ public UnionFeatureCollection( final FeatureCollection<Feature> inputFC, final FeatureCollection<Feature> unionFC, final String inputGeomName, final String unionGeomName) { super(inputFC); this.unionFC = unionFC; /* * We ensure that inputGeomName and unionGeomName are not null and we get the CRS of * these geometry. This in order to genereate the new FeatureType based on two FeatureType * and with only one geometry (in this case inputGeomName) * If inputGeomName or unionGeomName is null, we use default geometry name and CRS. */ CoordinateReferenceSystem geometryCRS; if (inputGeomName == null) { this.inputGeomName = inputFC.getFeatureType().getGeometryDescriptor().getName().getLocalPart(); geometryCRS = inputFC.getFeatureType().getGeometryDescriptor().getCoordinateReferenceSystem(); } else { this.inputGeomName = inputGeomName; final GeometryDescriptor buffDesc = (GeometryDescriptor) inputFC.getFeatureType().getDescriptor(inputGeomName); geometryCRS = buffDesc.getCoordinateReferenceSystem(); } if (unionGeomName == null) { this.unionGeomName = unionFC.getFeatureType().getGeometryDescriptor().getName().getLocalPart(); } else { this.unionGeomName = unionGeomName; } // Create the new FeatureType which concatenate two FeatureCollection FeatureType but with only // one geometry // (inputGeomName) this.newFeatureType = Union.mergeType( inputFC.getFeatureType(), unionFC.getFeatureType(), this.inputGeomName, geometryCRS); }
/** {@inheritDoc } */ @Override public FeatureIterator<Feature> iterator(final Hints hints) throws DataStoreRuntimeException { return new IntersectionFeatureIterator( getOriginalFeatureCollection().iterator(), unionFC.iterator()); }
/** * @param featureCollection * @param writer * @param fragment : true if we write in a stream, dont write start and end elements * @throws DataStoreException */ public Element writeFeatureCollection( final FeatureCollection featureCollection, final boolean fragment, final boolean wfs) throws DataStoreException, ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // then we have to create document-loader: factory.setNamespaceAware(false); DocumentBuilder loader = factory.newDocumentBuilder(); // creating a new DOM-document... Document document = loader.newDocument(); // the XML header if (!fragment) { document.setXmlVersion("1.0"); // writer.writeStartDocument("UTF-8", "1.0"); } // the root Element final Element rootElement; if (wfs) { rootElement = document.createElementNS("http://www.opengis.net/wfs", "FeatureCollection"); rootElement.setPrefix("wfs"); } else { rootElement = document.createElementNS("http://www.opengis.net/gml", "FeatureCollection"); rootElement.setPrefix("gml"); } document.appendChild(rootElement); String collectionID = ""; if (featureCollection.getID() != null) { collectionID = featureCollection.getID(); } final Attr idAttribute = document.createAttributeNS(Namespaces.GML, "id"); idAttribute.setValue(collectionID); idAttribute.setPrefix("gml"); rootElement.setAttributeNodeNS(idAttribute); if (schemaLocation != null && !schemaLocation.equals("")) { rootElement.setAttributeNS( "http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", schemaLocation); } /*FeatureType type = featureCollection.getFeatureType(); if (type != null && type.getName() != null) { String namespace = type.getName().getNamespaceURI(); if (namespace != null && !namespace.equals(Namespaces.GML)) { Prefix prefix = getPrefix(namespace); writer.writeNamespace(prefix.prefix, namespace); } }*/ /* * The boundedby part */ final Element boundElement = writeBounds(featureCollection.getEnvelope(), document); if (boundElement != null) { rootElement.appendChild(boundElement); } // we write each feature member of the collection FeatureIterator iterator = featureCollection.iterator(); try { while (iterator.hasNext()) { final Feature f = iterator.next(); final Element memberElement = document.createElementNS(Namespaces.GML, "featureMember"); memberElement.setPrefix("gml"); memberElement.appendChild(writeFeature(f, document, true)); rootElement.appendChild(memberElement); } } finally { // we close the stream iterator.close(); } return rootElement; }
private static FeatureCollection<?> buildResultList() { try { type = createSimpleResultType(); } catch (NoSuchAuthorityCodeException ex) { Logger.getLogger(UnionTest.class.getName()).log(Level.SEVERE, null, ex); } catch (FactoryException ex) { Logger.getLogger(UnionTest.class.getName()).log(Level.SEVERE, null, ex); } final FeatureCollection<Feature> featureList = DataUtilities.collection("", type); Feature myFeature; LinearRing ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(4, 7), new Coordinate(6, 7), new Coordinate(6, 5), new Coordinate(4, 5), new Coordinate(4, 7) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature1"); sfb.set("color", "red"); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); sfb.set("att", 20); myFeature = sfb.buildFeature("id-01-id-11"); featureList.add(myFeature); LineString str = geometryFactory.createLineString( new Coordinate[] {new Coordinate(3, 5), new Coordinate(3, 6)}); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature1"); sfb.set("color", "grey"); sfb.set("geom1", str); sfb.set("att", 12); myFeature = sfb.buildFeature("id-01-id-15"); featureList.add(myFeature); ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(3, 5), new Coordinate(3, 6), new Coordinate(3, 7), new Coordinate(4, 7), new Coordinate(4, 5), new Coordinate(3, 5) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature1"); sfb.set("color", null); sfb.set("att", null); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); myFeature = sfb.buildFeature("id-01"); featureList.add(myFeature); ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(7, 5), new Coordinate(8, 5), new Coordinate(8, 4), new Coordinate(7, 4), new Coordinate(7, 5) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature3"); sfb.set("color", "blue"); sfb.set("att", 20); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); myFeature = sfb.buildFeature("id-03-id-12"); featureList.add(myFeature); ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(6, 4), new Coordinate(6, 5), new Coordinate(7, 5), new Coordinate(7, 4), new Coordinate(6, 4) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature3"); sfb.set("color", "red"); sfb.set("att", 20); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); myFeature = sfb.buildFeature("id-03-id-11"); featureList.add(myFeature); ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(6, 2), new Coordinate(6, 4), new Coordinate(8, 4), new Coordinate(8, 2), new Coordinate(6, 2) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature3"); sfb.set("color", "grey"); sfb.set("att", 10); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); myFeature = sfb.buildFeature("id-03-id-13"); featureList.add(myFeature); ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(7, 7), new Coordinate(8, 7), new Coordinate(8, 5), new Coordinate(7, 5), new Coordinate(7, 7) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature2"); sfb.set("color", "blue"); sfb.set("att", 20); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); myFeature = sfb.buildFeature("id-02-id-12"); featureList.add(myFeature); ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(6, 5), new Coordinate(6, 7), new Coordinate(7, 7), new Coordinate(7, 5), new Coordinate(6, 5) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature2"); sfb.set("color", "red"); sfb.set("att", 20); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); myFeature = sfb.buildFeature("id-02-id-11"); featureList.add(myFeature); ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(2, 3), new Coordinate(2, 4), new Coordinate(3, 4), new Coordinate(3, 3), new Coordinate(2, 3) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature4"); sfb.set("color", null); sfb.set("att", null); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); myFeature = sfb.buildFeature("id-04"); featureList.add(myFeature); ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(7, 7), new Coordinate(7, 8), new Coordinate(9, 8), new Coordinate(9, 4), new Coordinate(8, 4), new Coordinate(8, 5), new Coordinate(8, 7), new Coordinate(7, 7) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature12"); sfb.set("color", "blue"); sfb.set("att", 20); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); myFeature = sfb.buildFeature("id-12"); featureList.add(myFeature); ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(8, 4), new Coordinate(9, 4), new Coordinate(9, 2), new Coordinate(8, 2), new Coordinate(8, 4) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature13"); sfb.set("color", "grey"); sfb.set("att", 10); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); myFeature = sfb.buildFeature("id-13"); featureList.add(myFeature); ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(4, 2), new Coordinate(4, 3), new Coordinate(5, 3), new Coordinate(5, 2), new Coordinate(4, 2) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature14"); sfb.set("color", "grey"); sfb.set("att", 12); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); myFeature = sfb.buildFeature("id-14"); featureList.add(myFeature); ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(4, 4), new Coordinate(4, 5), new Coordinate(6, 5), new Coordinate(6, 4), new Coordinate(4, 4) }); LinearRing ring2 = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(6, 7), new Coordinate(4, 7), new Coordinate(4, 8), new Coordinate(7, 8), new Coordinate(7, 7), new Coordinate(6, 7) }); Polygon poly1 = geometryFactory.createPolygon(ring, null); Polygon poly2 = geometryFactory.createPolygon(ring2, null); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature11"); sfb.set("color", "red"); sfb.set("att", 20); sfb.set("geom1", geometryFactory.createMultiPolygon(new Polygon[] {poly1, poly2})); myFeature = sfb.buildFeature("id-11"); featureList.add(myFeature); Feature myFeature5; ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(2.0, 5.0), new Coordinate(2.0, 6.0), new Coordinate(3.0, 6.0), new Coordinate(3.0, 5.0), new Coordinate(2.0, 5.0) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature15"); sfb.set("color", "grey"); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); sfb.set("att", 12); myFeature5 = sfb.buildFeature("id-15"); featureList.add(myFeature5); return featureList; }
private static FeatureCollection<?> buildFeatureUnionList() { try { type = createSimpleType2(); } catch (NoSuchAuthorityCodeException ex) { Logger.getLogger(UnionTest.class.getName()).log(Level.SEVERE, null, ex); } catch (FactoryException ex) { Logger.getLogger(UnionTest.class.getName()).log(Level.SEVERE, null, ex); } final FeatureCollection<Feature> featureList = DataUtilities.collection("", type); Feature myFeature1; LinearRing ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(4.0, 4.0), new Coordinate(4.0, 8.0), new Coordinate(7.0, 8.0), new Coordinate(7.0, 4.0), new Coordinate(4.0, 4.0) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature11"); sfb.set("color", "red"); sfb.set("geom3", geometryFactory.createPolygon(ring, null)); sfb.set("att", 20); myFeature1 = sfb.buildFeature("id-11"); featureList.add(myFeature1); Feature myFeature2; ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(7.0, 4.0), new Coordinate(7.0, 8.0), new Coordinate(9.0, 8.0), new Coordinate(9.0, 4.0), new Coordinate(7.0, 4.0) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature12"); sfb.set("color", "blue"); sfb.set("geom3", geometryFactory.createPolygon(ring, null)); sfb.set("att", 20); myFeature2 = sfb.buildFeature("id-12"); featureList.add(myFeature2); Feature myFeature3; ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(6.0, 2.0), new Coordinate(6.0, 4.0), new Coordinate(9.0, 4.0), new Coordinate(9.0, 2.0), new Coordinate(6.0, 2.0) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature13"); sfb.set("color", "grey"); sfb.set("geom3", geometryFactory.createPolygon(ring, null)); sfb.set("att", 10); myFeature3 = sfb.buildFeature("id-13"); featureList.add(myFeature3); Feature myFeature4; ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(4.0, 2.0), new Coordinate(4.0, 3.0), new Coordinate(5.0, 3.0), new Coordinate(5.0, 2.0), new Coordinate(4.0, 2.0) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature14"); sfb.set("color", "grey"); sfb.set("geom3", geometryFactory.createPolygon(ring, null)); sfb.set("att", 12); myFeature4 = sfb.buildFeature("id-14"); featureList.add(myFeature4); Feature myFeature5; ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(2.0, 5.0), new Coordinate(2.0, 6.0), new Coordinate(3.0, 6.0), new Coordinate(3.0, 5.0), new Coordinate(2.0, 5.0) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature15"); sfb.set("color", "grey"); sfb.set("geom3", geometryFactory.createPolygon(ring, null)); sfb.set("att", 12); myFeature5 = sfb.buildFeature("id-15"); featureList.add(myFeature5); return featureList; }
private static FeatureCollection<?> buildFeatureList() { try { type = createSimpleType(); } catch (NoSuchAuthorityCodeException ex) { Logger.getLogger(UnionTest.class.getName()).log(Level.SEVERE, null, ex); } catch (FactoryException ex) { Logger.getLogger(UnionTest.class.getName()).log(Level.SEVERE, null, ex); } final FeatureCollection<Feature> featureList = DataUtilities.collection("", type); Feature myFeature1; LinearRing ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(3.0, 5.0), new Coordinate(3.0, 7.0), new Coordinate(6.0, 7.0), new Coordinate(6.0, 5.0), new Coordinate(3.0, 5.0) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature1"); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); myFeature1 = sfb.buildFeature("id-01"); featureList.add(myFeature1); Feature myFeature2; ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(6.0, 5.0), new Coordinate(6.0, 7.0), new Coordinate(8.0, 7.0), new Coordinate(8.0, 5.0), new Coordinate(6.0, 5.0) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature2"); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); myFeature2 = sfb.buildFeature("id-02"); featureList.add(myFeature2); Feature myFeature3; ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(6.0, 2.0), new Coordinate(6.0, 5.0), new Coordinate(8.0, 5.0), new Coordinate(8.0, 2.0), new Coordinate(6.0, 2.0) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature3"); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); // sfb.set("geom2", line); myFeature3 = sfb.buildFeature("id-03"); featureList.add(myFeature3); Feature myFeature4; ring = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(2.0, 3.0), new Coordinate(2.0, 4.0), new Coordinate(3.0, 4.0), new Coordinate(3.0, 3.0), new Coordinate(2.0, 3.0) }); sfb = new SimpleFeatureBuilder(type); sfb.set("name", "feature4"); sfb.set("geom1", geometryFactory.createPolygon(ring, null)); myFeature4 = sfb.buildFeature("id-04"); featureList.add(myFeature4); return featureList; }