use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.
the class KmlFeatureUtilities method BuildSimpleFeature.
/**
* Build simple feature
* @param idgeom geometry id
* @param values no geographic data
* @param finalGeom geometry need to be insert in feature
* @return a {@link SimpleFeature}
*/
private static Feature BuildSimpleFeature(int idgeom, Map<String, String> values, Geometry finalGeom) {
// Building simplefeature
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
final String name = "Geometry";
ftb.setName(name);
ftb.addAttribute(Geometry.class).setName("geometry").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
// loop on values to find data names
for (String valName : values.keySet()) {
ftb.addAttribute(String.class).setName(valName);
}
final FeatureType sft = ftb.build();
final Feature simpleFeature = sft.newInstance();
simpleFeature.setPropertyValue(AttributeConvention.IDENTIFIER, "feature" + idgeom);
// add geometry
simpleFeature.setPropertyValue("geometry", finalGeom);
// add other data
for (String valName : values.keySet()) {
simpleFeature.setPropertyValue(valName, values.get(valName));
}
return simpleFeature;
}
use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.
the class GeoJSONReader method fillFeature.
/**
* Recursively fill a ComplexAttribute with properties map
*
* @param feature
* @param properties
*/
private void fillFeature(Feature feature, Map<String, Object> properties) throws BackingStoreException {
final FeatureType featureType = feature.getType();
for (final PropertyType type : featureType.getProperties(true)) {
final String attName = type.getName().toString();
final Object value = properties.get(attName);
if (value == null) {
continue;
}
if (type instanceof FeatureAssociationRole) {
final FeatureAssociationRole asso = (FeatureAssociationRole) type;
final FeatureType assoType = asso.getValueType();
final Class<?> valueClass = value.getClass();
if (valueClass.isArray()) {
Class<?> base = value.getClass().getComponentType();
if (!Map.class.isAssignableFrom(base)) {
LOGGER.log(Level.WARNING, "Invalid complex property value " + value);
}
final int size = Array.getLength(value);
if (size > 0) {
// list of objects
final List<Feature> subs = new ArrayList<>();
for (int i = 0; i < size; i++) {
Object subValue = Array.get(value, i);
final Feature subComplexAttribute;
if (subValue instanceof Map) {
subComplexAttribute = assoType.newInstance();
fillFeature(subComplexAttribute, (Map) Array.get(value, i));
} else if (subValue instanceof GeoJSONFeature) {
subComplexAttribute = toFeature((GeoJSONFeature) subValue, assoType);
} else {
throw new IllegalArgumentException("Sub value must be a GeoJSONFeature or a map");
}
subs.add(subComplexAttribute);
}
feature.setPropertyValue(attName, subs);
}
} else if (value instanceof Map) {
final Feature subComplexAttribute = assoType.newInstance();
fillFeature(subComplexAttribute, (Map) value);
feature.setPropertyValue(attName, subComplexAttribute);
} else if (value instanceof GeoJSONFeature) {
final Feature subComplexAttribute = toFeature((GeoJSONFeature) value, assoType);
feature.setPropertyValue(attName, subComplexAttribute);
} else if (value instanceof GeoJSONFeatureCollection) {
GeoJSONFeatureCollection collection = (GeoJSONFeatureCollection) value;
final List<Feature> subFeatures = new ArrayList<>();
for (GeoJSONFeature subFeature : collection.getFeatures()) {
subFeatures.add(toFeature(subFeature, assoType));
}
feature.setPropertyValue(attName, subFeatures);
} else {
LOGGER.warning("Unexpected attribute value type:" + value.getClass());
}
} else if (type instanceof AttributeType) {
final Attribute<?> property = (Attribute<?>) feature.getProperty(type.getName().toString());
fillProperty(property, value);
}
}
}
use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.
the class GeoJSONWriter method writeProperties.
/**
* Write ComplexAttribute.
*
* @param edited
* @param fieldName
* @param writeFieldName
* @throws IOException
* @throws IllegalArgumentException
*/
private void writeProperties(Feature edited, String fieldName, boolean writeFieldName, Set<Feature> alreadyWritten) throws IOException, IllegalArgumentException {
if (writeFieldName) {
writer.writeObjectFieldStart(fieldName);
} else {
writer.writeStartObject();
}
FeatureType type = edited.getType();
PropertyType defGeom = FeatureExt.getDefaultGeometrySafe(type).flatMap(Features::toAttribute).orElse(null);
Collection<? extends PropertyType> descriptors = type.getProperties(true).stream().filter(GeoJSONUtils.IS_NOT_CONVENTION).filter(it -> !Objects.equals(defGeom, it)).collect(Collectors.toList());
for (PropertyType propType : descriptors) {
final String name = propType.getName().tip().toString();
final Object value = edited.getPropertyValue(propType.getName().toString());
if (propType instanceof AttributeType) {
final AttributeType attType = (AttributeType) propType;
if (attType.getMaximumOccurs() > 1) {
writer.writeArrayFieldStart(name);
for (Object v : (Collection) value) {
writeProperty(name, v, false, alreadyWritten);
}
writer.writeEndArray();
} else {
writeProperty(name, value, true, alreadyWritten);
}
} else if (propType instanceof FeatureAssociationRole) {
final FeatureAssociationRole asso = (FeatureAssociationRole) propType;
if (asso.getMaximumOccurs() > 1) {
writer.writeFieldName(name);
writeFeatureCollection((List<Feature>) value, asso.getValueType());
} else {
writeProperty(name, value, true, alreadyWritten);
}
} else if (propType instanceof Operation) {
writeProperty(name, value, true, alreadyWritten);
}
}
writer.writeEndObject();
}
use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.
the class Copy method execute.
/**
* {@inheritDoc }
*/
@Override
protected void execute() throws ProcessException {
final FeatureStore sourceDS = inputParameters.getValue(SOURCE_STORE);
final FeatureStore targetDS = inputParameters.getValue(TARGET_STORE);
Session targetSS = inputParameters.getValue(TARGET_SESSION);
final Boolean eraseParam = inputParameters.getValue(ERASE);
final Boolean newVersion = inputParameters.getValue(NEW_VERSION);
// Type name can be removed, it's embedded in the query param.
final String typenameParam = inputParameters.getValue(TYPE_NAME);
final Query queryParam = inputParameters.getValue(QUERY);
final boolean doCommit = targetSS == null;
final Session sourceSS = sourceDS.createSession(false);
if (targetSS == null) {
if (targetDS != null) {
targetSS = targetDS.createSession(true);
} else {
throw new ProcessException("Input target_session or target_datastore missing.", this, null);
}
}
boolean reBuildQuery = false;
final String queryName;
if (queryParam != null) {
queryName = queryParam.getTypeName();
reBuildQuery = true;
} else if (typenameParam != null) {
queryName = typenameParam;
} else {
queryName = "*";
}
final Set<GenericName> names;
if ("*".equals(queryName)) {
// all values
try {
names = sourceDS.getNames();
} catch (DataStoreException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
}
} else {
// pick only the wanted names
names = new HashSet<>();
final List<String> wanted = UnmodifiableArrayList.wrap(queryName.split(","));
for (String s : wanted) {
try {
final FeatureType type = sourceDS.getFeatureType(s);
names.add(type.getName());
} catch (DataStoreException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
}
}
}
final float size = names.size();
int inc = 0;
for (GenericName n : names) {
fireProgressing("Copying " + n + ".", (int) ((inc * 100f) / size), false);
try {
Query query;
if (reBuildQuery) {
Query builder = new Query();
builder.copy(queryParam);
builder.setTypeName(n);
query = builder;
} else {
query = queryParam != null ? queryParam : new Query(n);
}
insert(n, sourceSS, targetSS, query, eraseParam, newVersion);
} catch (DataStoreException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
}
inc++;
}
try {
Date lastVersionDate = null;
if (doCommit) {
LOGGER.log(Level.INFO, "Commit all changes");
targetSS.commit();
// find last version
for (GenericName n : names) {
if (targetSS.getFeatureStore().getQueryCapabilities().handleVersioning()) {
final List<Version> versions = targetSS.getFeatureStore().getVersioning(n.toString()).list();
if (!versions.isEmpty()) {
if (lastVersionDate == null || versions.get(versions.size() - 1).getDate().getTime() > lastVersionDate.getTime()) {
lastVersionDate = versions.get(versions.size() - 1).getDate();
}
}
}
}
}
if (lastVersionDate != null) {
outputParameters.getOrCreate(VERSION).setValue(lastVersionDate);
}
} catch (DataStoreException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
} catch (VersioningException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
}
}
use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.
the class ProjectedGeometryTest method createProjectedGeometry.
private static ProjectedGeometry createProjectedGeometry(Geometry geometry, Dimension canvasBounds, AffineTransform objToDisp) throws NoninvertibleTransformException, TransformException, FactoryException {
final int canvasWidth = canvasBounds.width;
final int canvasHeight = canvasBounds.height;
// build a maplayer
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("test");
ftb.addAttribute(Geometry.class).setName("geom").setCRS(CommonCRS.WGS84.normalizedGeographic());
final FeatureType type = ftb.build();
final Feature feature = type.newInstance();
JTS.setCRS(geometry, CommonCRS.WGS84.normalizedGeographic());
feature.setPropertyValue("geom", geometry);
final FeatureSet col = new InMemoryFeatureSet(type, Arrays.asList(feature));
final List<GraphicalSymbol> symbols = new ArrayList<>();
symbols.add(SF.mark(StyleConstants.MARK_SQUARE, SF.fill(Color.BLACK), SF.stroke(Color.BLACK, 0)));
final Graphic graphic = SF.graphic(symbols, StyleConstants.LITERAL_ONE_FLOAT, FF.literal(2), StyleConstants.LITERAL_ZERO_FLOAT, null, null);
final PointSymbolizer ps = SF.pointSymbolizer(graphic, null);
final MutableStyle style = SF.style(ps);
final MapLayer layer = MapBuilder.createLayer(col);
layer.setStyle(style);
// build a rendering canvas
final J2DCanvasBuffered canvas = new J2DCanvasBuffered(CommonCRS.WGS84.normalizedGeographic(), new Dimension(canvasWidth, canvasHeight));
canvas.applyTransform(objToDisp);
final RenderingContext2D context = canvas.prepareContext(new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_ARGB).createGraphics());
final ProjectedGeometry pg = new ProjectedGeometry(context);
pg.setDataGeometry(geometry, CommonCRS.WGS84.normalizedGeographic());
Envelope env = canvas.getVisibleEnvelope();
System.out.println(env.getMinimum(0) + " " + env.getMaximum(0));
System.out.println(env.getMinimum(1) + " " + env.getMaximum(1));
return pg;
}
Aggregations