Java Code Examples for org.geotools.data.simple.SimpleFeatureIterator#hasNext()
The following examples show how to use
org.geotools.data.simple.SimpleFeatureIterator#hasNext() .
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: MultiPolygons.java From amodeus with GNU General Public License v2.0 | 6 votes |
private static Set<MultiPolygon> initializeFrom(File shapeFile) throws IOException { URL shapeFileURL = shapeFile.toURI().toURL(); Map<String, URL> inputMap = new HashMap<>(); inputMap.put("url", shapeFileURL); DataStore dataStore = DataStoreFinder.getDataStore(inputMap); SimpleFeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]); SimpleFeatureCollection collection = DataUtilities.collection(featureSource.getFeatures()); dataStore.dispose(); Set<MultiPolygon> polygons = new HashSet<>(); SimpleFeatureIterator iterator = collection.features(); while (iterator.hasNext()) polygons.add((MultiPolygon) iterator.next().getDefaultGeometry()); return polygons; }
Example 2
Source File: InLineFeatureModel.java From sldeditor with GNU General Public License v3.0 | 6 votes |
/** * Gets the feature. * * @param row the row * @return the feature */ private SimpleFeature getFeature(int row) { if (featureCollection != null) { if (row != lastRow) { SimpleFeatureIterator iterator = featureCollection.features(); SimpleFeature feature = iterator.next(); int index = 0; while (iterator.hasNext() && (index < row)) { feature = iterator.next(); index++; } lastRow = row; cachedFeature = feature; } return cachedFeature; } return null; }
Example 3
Source File: ElasticTemporalFilterIT.java From elasticgeo with GNU General Public License v3.0 | 6 votes |
@Test public void testGreaterDateFilterLong() throws Exception { init(); Date testDate = new Date(1005912798000L); FilterFactory ff = dataStore.getFilterFactory(); Filter f = ff.greaterOrEqual(ff.property("installed_td"), ff.literal(testDate.getTime())); SimpleFeatureCollection features = featureSource.getFeatures(f); assertEquals(7, features.size()); SimpleFeatureIterator it = features.features(); while (it.hasNext()) { SimpleFeature next = it.next(); Date date = (Date) next.getAttribute("installed_td"); assertTrue(date.after(testDate) || date.equals(testDate)); } it.close(); }
Example 4
Source File: OmsAdige.java From hortonmachine with GNU General Public License v3.0 | 5 votes |
private void linkBasinWithNetwork() throws Exception { FeatureExtender fExt = new FeatureExtender(inNetwork.getSchema(), new String[]{NetworkChannel.NETNUMNAME}, new Class[]{Integer.class}); DefaultFeatureCollection newCollection = new DefaultFeatureCollection(); SimpleFeatureIterator hillslopeFeatures = inHillslope.features(); while( hillslopeFeatures.hasNext() ) { SimpleFeature hFeature = hillslopeFeatures.next(); Object netNum = hFeature.getAttribute(NetworkChannel.NETNUMNAME); Geometry hGeometry = (Geometry) hFeature.getDefaultGeometry(); PreparedGeometry preparedHGeometry = PreparedGeometryFactory.prepare(hGeometry); SimpleFeatureIterator netFeatures = inNetwork.features(); while( netFeatures.hasNext() ) { SimpleFeature nFeature = netFeatures.next(); Geometry geometry = (Geometry) nFeature.getDefaultGeometry(); if (geometry.getNumGeometries() != 1) { throw new ModelsRuntimeException("The network geometries have to be single lines.", this); } LineString nLine = (LineString) geometry.getGeometryN(0); Point startPoint = nLine.getStartPoint(); if (preparedHGeometry.contains(startPoint)) { SimpleFeature extendFeature = fExt.extendFeature(nFeature, new Object[]{netNum}); newCollection.add(extendFeature); break; } } } inNetwork = newCollection; }
Example 5
Source File: StyleGenerator.java From constellation with Apache License 2.0 | 5 votes |
private static Style createPolygonStyle(final SimpleFeatureCollection features) { // get name based rule names final List<Rule> ruleList = new ArrayList<>(); final Set<String> nameSet = new HashSet<>(); // setup custom rules for polygons final SimpleFeatureIterator featureIterator = features.features(); while (featureIterator.hasNext()) { final SimpleFeature feature = featureIterator.next(); if (nameSet.add((String) feature.getAttribute(ATTRIBUTE))) { ruleList.add(makeFillRule(feature)); } } // create a partially opaque outline stroke final Rule defaultRule = makeFillRule(); defaultRule.setElseFilter(true); defaultRule.setName("Default Rule"); ruleList.add(defaultRule); // Create rule defined style final Rule[] rules = ruleList.toArray(new Rule[ruleList.size()]); final StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(); final FeatureTypeStyle featureTypeStyle = styleFactory.createFeatureTypeStyle(rules); final StyleBuilder builder = new StyleBuilder(); final Style style = builder.createStyle(); style.getDescription().setTitle("Polygon Style"); style.featureTypeStyles().add(featureTypeStyle); return style; }
Example 6
Source File: ElasticFeatureFilterIT.java From elasticgeo with GNU General Public License v3.0 | 5 votes |
@Test public void testNaturalSortingDesc() throws Exception { init(); Query q = new Query(featureSource.getSchema().getTypeName()); q.setSortBy(new SortBy[] { SortBy.REVERSE_ORDER }); SimpleFeatureIterator features = featureSource.getFeatures(q).features(); String prevId = null; while (features.hasNext()) { String currId = features.next().getID(); if (prevId != null) assertTrue(prevId.compareTo(currId) >= 0); prevId = currId; } features.close(); }
Example 7
Source File: OmsVectorMerger.java From hortonmachine with GNU General Public License v3.0 | 5 votes |
@Execute public void process() throws Exception { checkNull(inVectors); SimpleFeatureType firstType = null; FeatureExtender fEx = null; pm.beginTask("Merging features...", inVectors.size()); try { outVector = new DefaultFeatureCollection(); for( SimpleFeatureCollection featureCollection : inVectors ) { if (firstType == null) { firstType = featureCollection.getSchema(); fEx = new FeatureExtender(firstType, new String[0], new Class< ? >[0]); } else { SimpleFeatureType schema = featureCollection.getSchema(); int compare = DataUtilities.compare(firstType, schema); if (compare != 0) { throw new ModelsIllegalargumentException("Merging is done only on same feature types.", this, pm); } } SimpleFeatureIterator featureIterator = featureCollection.features(); while( featureIterator.hasNext() ) { SimpleFeature f = featureIterator.next(); SimpleFeature extendFeature = fEx.extendFeature(f, new Object[0]); ((DefaultFeatureCollection) outVector).add(extendFeature); } pm.worked(1); } } finally { pm.done(); } }
Example 8
Source File: ElasticFeatureFilterIT.java From elasticgeo with GNU General Public License v3.0 | 5 votes |
@Test public void testReadNumericArrayWithCsvStrategy() throws Exception { init(); dataStore.setArrayEncoding(ArrayEncoding.CSV); FilterFactory ff = dataStore.getFilterFactory(); PropertyIsBetween between = ff.between(ff.property("speed_is"), ff.literal(160), ff.literal(300)); SimpleFeatureCollection features = featureSource.getFeatures(between); assertEquals(5, features.size()); SimpleFeatureIterator iterator = features.features(); while (iterator.hasNext()) { SimpleFeature f = iterator.next(); assertFalse(f.getAttribute("speed_is") instanceof List); } }
Example 9
Source File: FeatureCollectionPolygonLayer.java From hortonmachine with GNU General Public License v3.0 | 5 votes |
public void run() { removeAllRenderables(); SimpleFeatureIterator featureIterator = featureCollectionLL.features(); while( featureIterator.hasNext() ) { SimpleFeature polygonAreaFeature = featureIterator.next(); if (mApplyExtrusion && (mHeightFieldName != null || mHasConstantHeight)) { addExtrudedPolygon(polygonAreaFeature); } else { addPolygon(polygonAreaFeature); } } featureIterator.close(); }
Example 10
Source File: ElasticTestSupport.java From elasticgeo with GNU General Public License v3.0 | 5 votes |
List<SimpleFeature> readFeatures(SimpleFeatureIterator iterator) { final List<SimpleFeature> features = new ArrayList<>(); try { while (iterator.hasNext()) { features.add(iterator.next()); } } finally { iterator.close(); } return features; }
Example 11
Source File: FeatureUtilities.java From hortonmachine with GNU General Public License v3.0 | 5 votes |
/** * Extracts features from a {@link FeatureCollection} into an {@link ArrayList} of {@link FeatureMate}s. * * @param collection the feature collection. * @return the list with the features or an empty list if no features present. */ public static List<FeatureMate> featureCollectionToMatesList( SimpleFeatureCollection collection ) { List<FeatureMate> featuresList = new ArrayList<FeatureMate>(); if (collection == null) { return featuresList; } SimpleFeatureIterator featureIterator = collection.features(); while( featureIterator.hasNext() ) { SimpleFeature feature = featureIterator.next(); featuresList.add(new FeatureMate(feature)); } featureIterator.close(); return featuresList; }
Example 12
Source File: InlineFeatureUtils.java From sldeditor with GNU General Public License v3.0 | 5 votes |
/** * Determine geometry type. * * @param geometryDescriptor the geometry descriptor * @param simpleFeatureCollection the simple feature collection * @return the geometry type enum */ public static GeometryTypeEnum determineGeometryType( GeometryDescriptor geometryDescriptor, SimpleFeatureCollection simpleFeatureCollection) { if (geometryDescriptor == null) { return GeometryTypeEnum.UNKNOWN; } if (simpleFeatureCollection == null) { return GeometryTypeEnum.UNKNOWN; } Class<?> bindingType = geometryDescriptor.getType().getBinding(); if (bindingType == Geometry.class) { Name geometryName = geometryDescriptor.getName(); SimpleFeatureIterator iterator = simpleFeatureCollection.features(); List<GeometryTypeEnum> geometryFeatures = new ArrayList<>(); while (iterator.hasNext()) { SimpleFeature feature = iterator.next(); Object value = feature.getAttribute(geometryName); if (value != null) { GeometryTypeEnum geometryType = GeometryTypeMapping.getGeometryType(value.getClass()); if (!geometryFeatures.contains(geometryType)) { geometryFeatures.add(geometryType); } } } return (combineGeometryType(geometryFeatures)); } else { return GeometryTypeMapping.getGeometryType(bindingType); } }
Example 13
Source File: DataSourceImpl.java From sldeditor with GNU General Public License v3.0 | 5 votes |
/** * Read attributes. * * @param attributeData the attribute data */ /* (non-Javadoc) * @see com.sldeditor.datasource.DataSourceInterface#updateAttributes(com.sldeditor.render.iface.RenderAttributeDataInterface) */ @Override public void readAttributes(DataSourceAttributeListInterface attributeData) { if(attributeData == null) { return; } List<DataSourceAttributeData> valueMap = new ArrayList<DataSourceAttributeData>(); SimpleFeatureCollection featureCollection = dataSourceInfo.getFeatureCollection(); if(featureCollection != null) { SimpleFeatureIterator iterator = featureCollection.features(); if(iterator.hasNext()) { SimpleFeature feature = iterator.next(); List<Object> attributes = feature.getAttributes(); for (int i = 0; i < attributes.size(); i++) { Name fieldName = fieldNameMap.get(i); DataSourceAttributeData data = new DataSourceAttributeData(fieldName, fieldTypeMap.get(i), attributes.get(i)); valueMap.add(data); } } } attributeData.setData(valueMap); }
Example 14
Source File: InLineFeatureModel.java From sldeditor with GNU General Public License v3.0 | 4 votes |
/** * Removes the feature. * * @param selectedRow the selected row */ public void removeFeature(int selectedRow) { if ((selectedRow < 0) || (selectedRow >= getRowCount())) { return; } SimpleFeatureType featureType = userLayer.getInlineFeatureType(); String typeName = userLayer.getInlineFeatureType().getTypeName(); try { SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName); SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(featureType); ArrayList<SimpleFeature> featureList = new ArrayList<>(); SimpleFeatureIterator it = featureSource.getFeatures().features(); try { int index = 0; while (it.hasNext()) { SimpleFeature sf = it.next(); if (index != selectedRow) { List<Object> attributeValueList = sf.getAttributes(); sfb.addAll(attributeValueList); featureList.add(sfb.buildFeature(null)); } index++; } } finally { it.close(); } SimpleFeatureCollection collection = new ListFeatureCollection(featureType, featureList); featureCollection = collection; cachedFeature = null; lastRow = -1; DataStore dataStore = DataUtilities.dataStore(collection); userLayer.setInlineFeatureDatastore(dataStore); } catch (IOException e) { ConsoleManager.getInstance().exception(this, e); } this.fireTableStructureChanged(); this.fireTableDataChanged(); if (parentObj != null) { parentObj.inlineFeatureUpdated(); } }
Example 15
Source File: InLineFeatureModel.java From sldeditor with GNU General Public License v3.0 | 4 votes |
/** Adds the new feature. */ public void addNewFeature() { SimpleFeatureType featureType = userLayer.getInlineFeatureType(); String typeName = userLayer.getInlineFeatureType().getTypeName(); try { SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName); SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(featureType); ArrayList<SimpleFeature> featureList = new ArrayList<>(); SimpleFeatureIterator it = featureSource.getFeatures().features(); try { while (it.hasNext()) { SimpleFeature sf = it.next(); List<Object> attributeValueList = sf.getAttributes(); sfb.addAll(attributeValueList); featureList.add(sfb.buildFeature(null)); } // Add new feature String wktString = "wkt://POINT(0 0)"; Geometry geometry = WKTConversion.convertToGeometry(wktString, getSelectedCRSCode()); sfb.add(geometry); featureList.add(sfb.buildFeature(null)); } finally { it.close(); } SimpleFeatureCollection collection = new ListFeatureCollection(featureType, featureList); featureCollection = collection; cachedFeature = null; lastRow = -1; DataStore dataStore = DataUtilities.dataStore(collection); userLayer.setInlineFeatureDatastore(dataStore); } catch (IOException e) { ConsoleManager.getInstance().exception(this, e); } this.fireTableStructureChanged(); this.fireTableDataChanged(); if (parentObj != null) { parentObj.inlineFeatureUpdated(); } }
Example 16
Source File: InLineFeatureModel.java From sldeditor with GNU General Public License v3.0 | 4 votes |
/** * Update CRS. * * @param selectedValue the selected value */ public void updateCRS(ValueComboBoxData selectedValue) { if (selectedValue != null) { String crsCode = selectedValue.getKey(); CoordinateReferenceSystem newCRS = CoordManager.getInstance().getCRS(crsCode); SimpleFeatureType newFeatureType = SimpleFeatureTypeBuilder.retype(featureCollection.getSchema(), newCRS); String typeName = userLayer.getInlineFeatureType().getTypeName(); try { SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName); SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(newFeatureType); ArrayList<SimpleFeature> featureList = new ArrayList<>(); SimpleFeatureIterator it = featureSource.getFeatures().features(); try { while (it.hasNext()) { SimpleFeature sf = it.next(); List<Object> attributeValueList = sf.getAttributes(); sfb.addAll(attributeValueList); featureList.add(sfb.buildFeature(null)); } } finally { it.close(); } SimpleFeatureCollection collection = new ListFeatureCollection(newFeatureType, featureList); featureCollection = collection; cachedFeature = null; lastRow = -1; DataStore dataStore = DataUtilities.dataStore(collection); userLayer.setInlineFeatureDatastore(dataStore); userLayer.setInlineFeatureType(newFeatureType); } catch (IOException e) { ConsoleManager.getInstance().exception(this, e); } this.fireTableStructureChanged(); this.fireTableDataChanged(); if (parentObj != null) { parentObj.inlineFeatureUpdated(); } } }
Example 17
Source File: TestUtils.java From geowave with Apache License 2.0 | 4 votes |
public static ExpectedResults getExpectedResults( final URL[] expectedResultsResources, final CoordinateReferenceSystem crs) throws IOException { final Map<String, Object> map = new HashMap<>(); DataStore dataStore = null; final Set<Long> hashedCentroids = new HashSet<>(); int expectedResultCount = 0; final MathTransform mathTransform = transformFromCrs(crs); final TWKBWriter writer = new TWKBWriter(); final TWKBReader reader = new TWKBReader(); for (final URL expectedResultsResource : expectedResultsResources) { map.put("url", expectedResultsResource); SimpleFeatureIterator featureIterator = null; try { dataStore = DataStoreFinder.getDataStore(map); if (dataStore == null) { LOGGER.error("Could not get dataStore instance, getDataStore returned null"); throw new IOException("Could not get dataStore instance, getDataStore returned null"); } final SimpleFeatureCollection expectedResults = dataStore.getFeatureSource(dataStore.getNames().get(0)).getFeatures(); expectedResultCount += expectedResults.size(); // unwrap the expected results into a set of features IDs so its // easy to check against featureIterator = expectedResults.features(); while (featureIterator.hasNext()) { final SimpleFeature feature = featureIterator.next(); final Geometry geometry = (Geometry) feature.getDefaultGeometry(); // TODO: Geometry has to be serialized and deserialized here // to make the centroid match the one coming out of the // database. final long centroid = hashCentroid( reader.read( writer.write( mathTransform != null ? JTS.transform(geometry, mathTransform) : geometry))); hashedCentroids.add(centroid); } } catch (MismatchedDimensionException | TransformException | ParseException e) { LOGGER.warn("Unable to transform geometry", e); Assert.fail("Unable to transform geometry to CRS: " + crs.toString()); } finally { IOUtils.closeQuietly(featureIterator); if (dataStore != null) { dataStore.dispose(); } } } return new ExpectedResults(hashedCentroids, expectedResultCount); }
Example 18
Source File: InLineFeatureModel.java From sldeditor with GNU General Public License v3.0 | 4 votes |
/** * Removes the column. * * @param columnName the column name */ public void removeColumn(String columnName) { if (featureCollection != null) { if (columnList.contains(columnName)) { columnList.remove(columnName); // Find field name to remote SimpleFeatureTypeBuilder featureTypeBuilder = new SimpleFeatureTypeBuilder(); featureTypeBuilder.init(featureCollection.getSchema()); featureTypeBuilder.remove(columnName); SimpleFeatureType newFeatureType = featureTypeBuilder.buildFeatureType(); int attributeToRemoveIndex = 0; for (AttributeDescriptor descriptor : newFeatureType.getAttributeDescriptors()) { if (descriptor.getLocalName().compareTo(columnName) == 0) { break; } attributeToRemoveIndex++; } String typeName = userLayer.getInlineFeatureType().getTypeName(); try { SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName); SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(newFeatureType); ArrayList<SimpleFeature> featureList = new ArrayList<>(); SimpleFeatureIterator it = featureSource.getFeatures().features(); try { while (it.hasNext()) { SimpleFeature sf = it.next(); List<Object> attributes = sf.getAttributes(); attributes.remove(attributeToRemoveIndex); sfb.addAll(attributes); featureList.add(sfb.buildFeature(null)); } } finally { it.close(); } SimpleFeatureCollection collection = new ListFeatureCollection(newFeatureType, featureList); featureCollection = collection; cachedFeature = null; lastRow = -1; DataStore dataStore = DataUtilities.dataStore(collection); userLayer.setInlineFeatureDatastore(dataStore); userLayer.setInlineFeatureType(newFeatureType); } catch (IOException e) { ConsoleManager.getInstance().exception(this, e); } this.fireTableStructureChanged(); this.fireTableDataChanged(); if (parentObj != null) { parentObj.inlineFeatureUpdated(); } } } }
Example 19
Source File: InLineFeatureModel.java From sldeditor with GNU General Public License v3.0 | 4 votes |
/** Adds the new column. */ public void addNewColumn() { if (featureCollection != null) { String attributeName = getUniqueAttributeName(); columnList.add(attributeName); // Populate field names SimpleFeatureTypeBuilder featureTypeBuilder = new SimpleFeatureTypeBuilder(); featureTypeBuilder.init(featureCollection.getSchema()); featureTypeBuilder.add(attributeName, String.class); SimpleFeatureType newFeatureType = featureTypeBuilder.buildFeatureType(); String typeName = userLayer.getInlineFeatureType().getTypeName(); try { SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName); SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(newFeatureType); ArrayList<SimpleFeature> featureList = new ArrayList<>(); SimpleFeatureIterator it = featureSource.getFeatures().features(); try { while (it.hasNext()) { SimpleFeature sf = it.next(); sfb.addAll(sf.getAttributes()); sfb.add(new String("")); featureList.add(sfb.buildFeature(null)); } } finally { it.close(); } SimpleFeatureCollection collection = new ListFeatureCollection(newFeatureType, featureList); featureCollection = collection; cachedFeature = null; lastRow = -1; DataStore dataStore = DataUtilities.dataStore(collection); userLayer.setInlineFeatureDatastore(dataStore); userLayer.setInlineFeatureType(newFeatureType); } catch (IOException e) { ConsoleManager.getInstance().exception(this, e); } this.fireTableStructureChanged(); this.fireTableDataChanged(); if (parentObj != null) { parentObj.inlineFeatureUpdated(); } } }
Example 20
Source File: WRS2GeometryStore.java From geowave with Apache License 2.0 | 4 votes |
private void init() throws MalformedURLException, IOException { if (!wrs2Shape.exists()) { if (!wrs2Directory.delete()) { LOGGER.warn("Unable to delete '" + wrs2Directory.getAbsolutePath() + "'"); } final File wsDir = wrs2Directory.getParentFile(); if (!wsDir.exists() && !wsDir.mkdirs()) { LOGGER.warn("Unable to create directory '" + wsDir.getAbsolutePath() + "'"); } if (!wrs2Directory.mkdirs()) { LOGGER.warn("Unable to create directory '" + wrs2Directory.getAbsolutePath() + "'"); } // download and unzip the shapefile final File targetFile = new File(wrs2Directory, WRS2_SHAPE_ZIP); if (targetFile.exists()) { if (!targetFile.delete()) { LOGGER.warn("Unable to delete file '" + targetFile.getAbsolutePath() + "'"); } } FileUtils.copyURLToFile(new URL(WRS2_SHAPE_URL), targetFile); final ZipFile zipFile = new ZipFile(targetFile); try { final Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); while (entries.hasMoreElements()) { final ZipArchiveEntry entry = entries.nextElement(); if (!entry.isDirectory()) { FileUtils.copyInputStreamToFile( zipFile.getInputStream(entry), new File(wrs2Directory, entry.getName())); // HP Fortify "Path Traversal" false positive // What Fortify considers "user input" comes only // from users with OS-level access anyway } } } finally { zipFile.close(); } } // read the shapefile and cache the features for quick lookup by path // and row try { final Map<String, Object> map = new HashMap<>(); map.put("url", wrs2Shape.toURI().toURL()); final DataStore dataStore = DataStoreFinder.getDataStore(map); if (dataStore == null) { LOGGER.error("Unable to get a datastore instance, getDataStore returned null"); return; } final SimpleFeatureSource source = dataStore.getFeatureSource(WRS2_TYPE_NAME); final SimpleFeatureCollection featureCollection = source.getFeatures(); wrs2Type = featureCollection.getSchema(); final SimpleFeatureIterator iterator = featureCollection.features(); while (iterator.hasNext()) { final SimpleFeature feature = iterator.next(); final Number path = (Number) feature.getAttribute("PATH"); final Number row = (Number) feature.getAttribute("ROW"); featureCache.put( new WRS2Key(path.intValue(), row.intValue()), (MultiPolygon) feature.getDefaultGeometry()); } } catch (final IOException e) { LOGGER.error( "Unable to read wrs2_asc_desc shapefile '" + wrs2Shape.getAbsolutePath() + "'", e); throw (e); } }