public static SimpleFeatureType toReShapeFeatureType( SimpleFeatureCollection delegate, List<Definition> definitionList) { SimpleFeature sample = null; SimpleFeatureIterator iterator = delegate.features(); try { if (iterator.hasNext()) { sample = iterator.next(); } } finally { iterator.close(); // good bye } SimpleFeatureTypeBuilder build = new SimpleFeatureTypeBuilder(); SimpleFeatureType origional = delegate.getSchema(); for (Definition def : definitionList) { String name = def.name; Expression expression = def.expression; Object value = null; if (sample != null) { value = expression.evaluate(sample); } Class<?> binding = def.binding; // make use of any default binding hint provided by user if (value == null) { if (expression instanceof PropertyName) { PropertyName propertyName = (PropertyName) expression; String path = propertyName.getPropertyName(); AttributeDescriptor descriptor = origional.getDescriptor(name); AttributeType attributeType = descriptor.getType(); binding = attributeType.getBinding(); } } else { binding = value.getClass(); } if (binding == null) { // note we could consider scanning through additional samples until we get a non null hit throw new IllegalArgumentException("Unable to determine type for " + name); } if (Geometry.class.isAssignableFrom(binding)) { CoordinateReferenceSystem crs; AttributeType originalAttributeType = origional.getType(name); if (originalAttributeType != null && originalAttributeType instanceof GeometryType) { GeometryType geometryType = (GeometryType) originalAttributeType; crs = geometryType.getCoordinateReferenceSystem(); } else { crs = origional.getCoordinateReferenceSystem(); } build.crs(crs); build.add(name, binding); } else { build.add(name, binding); } } build.setName(origional.getTypeName()); return build.buildFeatureType(); }
/** * Get the data type of the command attribute * * @param wfs datastore * @return data type as Class */ private Class<?> getDataType(WFSContentDataStore wfs) { SimpleFeatureType schema; Class<?> clazz = null; try { schema = wfs.getSchema(_wfsngTypeName); // get schema clazz = schema.getType(_command.getPropertyName()).getBinding(); // get data type as Class } catch (IOException e) { e.printStackTrace(); } if (clazz == null) { throw new RuntimeException( "Should never happen, we need to know what type is the attribute of"); } return clazz; }
@Override public SimpleFeature decode( final IndexedAdapterPersistenceEncoding data, final PrimaryIndex index) { final PersistentValue<Object> obj = data.getAdapterExtendedData().getValues().get(0); final byte[][] bytes = (byte[][]) obj.getValue(); int i = 0; final SimpleFeatureBuilder bldr = getBuilder(); for (final byte[] f : bytes) { if (f != null) { final FieldReader reader = FieldUtils.getDefaultReaderForClass(featureType.getType(i).getBinding()); bldr.set(i, reader.readField(f)); } i++; } return bldr.buildFeature(data.getDataId().getString()); }
public static IGlobeFeatureCollection< IVector2, ? extends IBoundedGeometry2D<? extends IFinite2DBounds<?>>> readFeatures(final DataStore dataStore, final String layerName, final GProjection projection) throws Exception { final SimpleFeatureSource featureSource = dataStore.getFeatureSource(layerName); final SimpleFeatureCollection featuresCollection = featureSource.getFeatures(); final GIntHolder validCounter = new GIntHolder(0); // final GIntHolder polygonsWithHolesCounter = new GIntHolder(0); final GIntHolder invalidCounter = new GIntHolder(0); // final GIntHolder validVerticesCounter = new GIntHolder(0); final GIntHolder polygonsCounter = new GIntHolder(0); final GIntHolder linesCounter = new GIntHolder(0); final GIntHolder pointsCounter = new GIntHolder(0); final int featuresCount = featuresCollection.size(); final ArrayList<IGlobeFeature<IVector2, IBoundedGeometry2D<? extends IFinite2DBounds<?>>>> euclidFeatures = new ArrayList< IGlobeFeature<IVector2, IBoundedGeometry2D<? extends IFinite2DBounds<?>>>>( featuresCount); final GProgress progress = new GProgress(featuresCount) { @Override public void informProgress( final double percent, final long elapsed, final long estimatedMsToFinish) { // System.out.println("Loading \"" + fileName.buildPath() + "\" " // + progressString(percent, elapsed, // estimatedMsToFinish)); System.out.println( "Loading data from data storage: " + layerName + " " + progressString(percent, elapsed, estimatedMsToFinish)); } }; final FeatureIterator<SimpleFeature> iterator = featuresCollection.features(); while (iterator.hasNext()) { final SimpleFeature feature = iterator.next(); final GeometryAttribute geometryAttribute = feature.getDefaultGeometryProperty(); final GeometryType type = geometryAttribute.getType(); if (type.getBinding() == com.vividsolutions.jts.geom.MultiPolygon.class) { polygonsCounter.increment(); final com.vividsolutions.jts.geom.MultiPolygon multipolygon = (com.vividsolutions.jts.geom.MultiPolygon) geometryAttribute.getValue(); final int geometriesCount = multipolygon.getNumGeometries(); final List<IPolygon2D> polygons = new ArrayList<IPolygon2D>(geometriesCount); for (int i = 0; i < geometriesCount; i++) { final com.vividsolutions.jts.geom.Polygon jtsPolygon = (com.vividsolutions.jts.geom.Polygon) multipolygon.getGeometryN(i); try { final IPolygon2D euclidPolygon = createEuclidPolygon(projection, jtsPolygon); if (euclidPolygon != null) { // euclidFeatures.add(createFeature(euclidPolygon, feature)); polygons.add(euclidPolygon); validCounter.increment(); } } catch (final IllegalArgumentException e) { // System.err.println(e.getMessage()); } } if (!polygons.isEmpty()) { if (polygons.size() == 1) { euclidFeatures.add(createFeature(polygons.get(0), feature)); } else { euclidFeatures.add(createFeature(new GMultiGeometry2D<IPolygon2D>(polygons), feature)); } } } else if (type.getBinding() == com.vividsolutions.jts.geom.MultiLineString.class) { linesCounter.increment(); final com.vividsolutions.jts.geom.MultiLineString multiline = (com.vividsolutions.jts.geom.MultiLineString) geometryAttribute.getValue(); final int geometriesCount = multiline.getNumGeometries(); final List<IPolygonalChain2D> lines = new ArrayList<IPolygonalChain2D>(geometriesCount); for (int i = 0; i < geometriesCount; i++) { final com.vividsolutions.jts.geom.LineString jtsLine = (com.vividsolutions.jts.geom.LineString) multiline.getGeometryN(i); try { final IPolygonalChain2D euclidLine = createLine(jtsLine.getCoordinates(), projection); // euclidFeatures.add(createFeature(euclidLines, feature)); lines.add(euclidLine); } catch (final IllegalArgumentException e) { // System.err.println(e.getMessage()); } } if (!lines.isEmpty()) { if (lines.size() == 1) { euclidFeatures.add(createFeature(lines.get(0), feature)); } else { euclidFeatures.add( createFeature(new GMultiGeometry2D<IPolygonalChain2D>(lines), feature)); } } validCounter.increment(); } else if (type.getBinding() == com.vividsolutions.jts.geom.Point.class) { pointsCounter.increment(); final IVector2 euclidPoint = createPoint( ((com.vividsolutions.jts.geom.Point) geometryAttribute.getValue()).getCoordinate(), projection); euclidFeatures.add(createFeature(euclidPoint, feature)); validCounter.increment(); } else if (type.getBinding() == com.vividsolutions.jts.geom.MultiPoint.class) { final IBoundedGeometry2D<? extends IFinite2DBounds<?>> euclidMultipoint = createEuclidMultiPoint(geometryAttribute, projection); euclidFeatures.add(createFeature(euclidMultipoint, feature)); validCounter.increment(); } else { invalidCounter.increment(); System.out.println("invalid type: " + type); } progress.stepDone(); } dataStore.dispose(); euclidFeatures.trimToSize(); System.out.println(); System.out.println("Features: " + featuresCount); System.out.println(); System.out.println("Read " + validCounter.get() + " valid geometries"); System.out.println(" => " + polygonsCounter.get() + " valid polygons"); System.out.println(" => " + linesCounter.get() + " valid lines"); System.out.println(" => " + pointsCounter.get() + " valid points"); System.out.println(); if (invalidCounter.get() > 0) { System.out.println("Ignored " + invalidCounter.get() + " invalid geometries"); } System.out.println(); final SimpleFeatureType schema = featureSource.getSchema(); final int fieldsCount = schema.getAttributeCount(); final List<GField> fields = new ArrayList<GField>(fieldsCount); System.out.println("Fields count: " + fieldsCount); for (int i = 0; i < fieldsCount; i++) { final String fieldName = schema.getType(i).getName().getLocalPart(); System.out.println("Fieldname: " + fieldName); final Class<?> fieldType = schema.getType(i).getBinding(); fields.add(new GField(fieldName, fieldType)); } return new GListFeatureCollection<IVector2, IBoundedGeometry2D<? extends IFinite2DBounds<?>>>( GProjection.EPSG_4326, fields, euclidFeatures, "uniqueId_000"); }