use of org.geotoolkit.storage.feature.FeatureStoreRuntimeException in project geotoolkit by Geomatys.
the class CategoryStyleBuilder method analyze.
public void analyze(final MapLayer layer) {
Resource resource = layer.getData();
if (!(resource instanceof FeatureSet)) {
throw new IllegalArgumentException("Layer resource must be a FeatureSet");
}
this.layer = layer;
fts.rules().clear();
properties.clear();
if (layer != null) {
FeatureType schema;
try {
schema = ((FeatureSet) resource).getType();
} catch (DataStoreException ex) {
throw new FeatureStoreRuntimeException(ex.getMessage(), ex);
}
for (PropertyType desc : schema.getProperties(true)) {
if (desc instanceof AttributeType) {
Class<?> type = ((AttributeType) desc).getValueClass();
if (!Geometry.class.isAssignableFrom(type)) {
properties.add(ff.property(desc.getName().tip().toString()));
}
}
}
// find the geometry class for template
Class<?> geoClass = null;
try {
PropertyType geo = FeatureExt.getDefaultGeometry(schema);
geoClass = Features.toAttribute(geo).map(AttributeType::getValueClass).orElse(null);
} catch (PropertyNotFoundException ex) {
LOGGER.log(Level.FINE, "No sis:geometry property found", ex);
}
if (geoClass == null) {
return;
}
if (Polygon.class.isAssignableFrom(geoClass) || MultiPolygon.class.isAssignableFrom(geoClass)) {
Stroke stroke = sf.stroke(Color.BLACK, 1);
Fill fill = sf.fill(Color.BLUE);
template = sf.polygonSymbolizer(stroke, fill, null);
expectedType = PolygonSymbolizer.class;
} else if (LineString.class.isAssignableFrom(geoClass) || MultiLineString.class.isAssignableFrom(geoClass)) {
Stroke stroke = sf.stroke(Color.BLUE, 2);
template = sf.lineSymbolizer(stroke, null);
expectedType = LineSymbolizer.class;
} else {
Stroke stroke = sf.stroke(Color.BLACK, 1);
Fill fill = sf.fill(Color.BLUE);
List<GraphicalSymbol> symbols = new ArrayList<>();
symbols.add(sf.mark(StyleConstants.MARK_CIRCLE, fill, stroke));
Graphic gra = sf.graphic(symbols, ff.literal(1), ff.literal(12), ff.literal(0), sf.anchorPoint(), sf.displacement());
template = sf.pointSymbolizer(gra, null);
expectedType = PointSymbolizer.class;
}
// try to rebuild the previous analyze if it was one
List<? extends FeatureTypeStyle> ftss = layer.getStyle().featureTypeStyles();
if (ftss.size() == 1) {
FeatureTypeStyle fts = ftss.get(0);
// defensive copy avoid synchronization
List<? extends Rule> candidateRules = new ArrayList<>(fts.rules());
for (Rule r : candidateRules) {
// defensive copy avoid synchronization
List<? extends Symbolizer> candidateSymbols = new ArrayList<>(r.symbolizers());
if (candidateSymbols.size() != 1)
break;
Symbolizer symbol = candidateSymbols.get(0);
if (expectedType.isInstance(symbol)) {
if (r.isElseFilter()) {
// it looks like it's a valid classification "other" rule
this.fts.rules().add((MutableRule) r);
template = symbol;
other = true;
} else {
Filter f = r.getFilter();
if (f != null && f.getOperatorType() == ComparisonOperatorName.PROPERTY_IS_EQUAL_TO) {
BinaryComparisonOperator equal = (BinaryComparisonOperator) f;
Expression exp1 = equal.getOperand1();
Expression exp2 = equal.getOperand2();
if (exp1 instanceof ValueReference && exp2 instanceof Literal) {
if (properties.contains(exp1)) {
// it looks like it's a valid classification property rule
this.fts.rules().add((MutableRule) r);
template = symbol;
currentProperty = (ValueReference) exp1;
} else {
// property is not in the schema
break;
}
} else if (exp2 instanceof ValueReference && exp1 instanceof Literal) {
if (properties.contains(exp2)) {
// it looks like it's a valid classification property rule
this.fts.rules().add((MutableRule) r);
template = symbol;
currentProperty = (ValueReference) exp2;
} else {
// property is not in the schema
break;
}
} else {
// mismatch analyze structure
break;
}
}
}
} else {
break;
}
}
}
}
}
use of org.geotoolkit.storage.feature.FeatureStoreRuntimeException in project geotoolkit by Geomatys.
the class CategoryStyleBuilder method create.
public List<MutableRule> create() {
// search the different values
final Set<Object> differentValues = new HashSet<>();
final ValueReference property = currentProperty;
final FeatureSet resource = (FeatureSet) layer.getData();
final Query query = new Query();
try {
query.setTypeName(resource.getType().getName());
} catch (DataStoreException ex) {
LOGGER.log(Level.FINE, "Error while accessing data", ex);
}
query.setProperties(new String[] { property.getXPath() });
try (Stream<Feature> stream = resource.subset(query).features(false)) {
final Iterator<Feature> features = stream.iterator();
while (features.hasNext()) {
final Feature feature = features.next();
differentValues.add(property.apply(feature));
}
} catch (DataStoreException | FeatureStoreRuntimeException ex) {
LOGGER.log(Level.FINE, "Error while accessing data", ex);
}
// generate the different rules
fts.rules().clear();
for (Object obj : differentValues) {
fts.rules().add(createRule(property, obj));
}
// generate the other rule if asked
if (other) {
MutableRule r = sf.rule(createSymbolizer());
r.setElseFilter(true);
r.setDescription(sf.description("other", "other"));
fts.rules().add(r);
}
return fts.rules();
}
use of org.geotoolkit.storage.feature.FeatureStoreRuntimeException in project geotoolkit by Geomatys.
the class MIFReader method next.
/**
* {@inheritDoc}
*/
@Override
public Feature next() throws FeatureStoreRuntimeException {
try {
checkScanners();
} catch (DataStoreException | IOException ex) {
throw new FeatureStoreRuntimeException("Cannot read data", ex);
}
Feature resFeature = null;
// We check the MIF file first, because it will define the feature count to reach the next good typed data.
if (readMif) {
String currentPattern;
while (mifScanner.hasNextLine()) {
currentPattern = mifScanner.findInLine(GEOMETRY_ID_PATTERN);
if (currentPattern == null) {
// Empty or non-compliant line. We just ignore it.
LOGGER.log(Level.FINE, "Invalid line : " + mifScanner.findInLine(".*"));
} else {
final MIFUtils.GeometryType geomType = MIFUtils.getGeometryType(currentPattern);
if (geomType == null) {
LOGGER.log(Level.WARNING, "Unrecognized geometry flag in MIF file : " + currentPattern);
} else {
resFeature = geomType.binding.buildType(master.getMifCRS(), readType).newInstance();
try {
geomType.readGeometry(mifScanner, resFeature, master.getTransform());
} catch (Exception ex) {
LOGGER.log(Level.WARNING, ex.getLocalizedMessage(), ex);
}
break;
// We must check if we're on a Geometry naming line to increment the counter of past geometries.
}
}
mifScanner.nextLine();
}
}
// No geometry has been read.
if (resFeature == null) {
resFeature = readType.newInstance();
}
if (readMid) {
// parse MID line.
while (midCounter < mifCounter) {
midScanner.nextLine();
midCounter++;
}
final String line = midScanner.nextLine();
final CharSequence[] split = CharSequences.split(line, master.mifDelimiter);
for (final Map.Entry<String, Integer> entry : propertiesToRead.entrySet()) {
final Integer idx = entry.getValue();
try {
resFeature.setPropertyValue(entry.getKey(), converters[idx].apply(split[idx]));
} catch (RuntimeException e) {
Logging.recoverableException(LOGGER, MIFReader.class, "next", e);
}
}
midCounter++;
}
if (readMif) {
mifCounter++;
}
return resFeature;
}
use of org.geotoolkit.storage.feature.FeatureStoreRuntimeException in project geotoolkit by Geomatys.
the class OMFeatureTypes method getFeatureTypes.
public static GenericNameIndex<FeatureType> getFeatureTypes(final String name) {
final GenericNameIndex<FeatureType> types = new GenericNameIndex<>();
final GenericName OM_TN_SAMPLINGPOINT = NamesExt.create(OM_NAMESPACE, name);
final FeatureTypeBuilder featureTypeBuilder = new FeatureTypeBuilder();
featureTypeBuilder.setName(OM_TN_SAMPLINGPOINT);
featureTypeBuilder.setSuperTypes(GMLConvention.ABSTRACTFEATURETYPE_31);
featureTypeBuilder.addAttribute(String.class).setName(ATT_DESC).setMinimumOccurs(0).setMaximumOccurs(1);
featureTypeBuilder.addAttribute(String.class).setName(ATT_NAME).setMinimumOccurs(1).setMaximumOccurs(Integer.MAX_VALUE);
featureTypeBuilder.addAttribute(String.class).setName(ATT_SAMPLED).setMinimumOccurs(0).setMaximumOccurs(Integer.MAX_VALUE).addCharacteristic(GMLConvention.NILLABLE_CHARACTERISTIC);
featureTypeBuilder.addAttribute(Geometry.class).setName(ATT_POSITION).addRole(AttributeRole.DEFAULT_GEOMETRY);
try {
types.add(null, OM_TN_SAMPLINGPOINT, featureTypeBuilder.build());
} catch (IllegalNameException ex) {
// won't happen
throw new FeatureStoreRuntimeException(ex);
}
return types;
}
use of org.geotoolkit.storage.feature.FeatureStoreRuntimeException in project geotoolkit by Geomatys.
the class FeatureCollectionToReferenceConverter method convert.
/**
* {@inheritDoc}
*/
@Override
public Reference convert(final FeatureCollection source, final Map<String, Object> params) throws UnconvertibleObjectException {
if (params.get(TMP_DIR_PATH) == null) {
throw new UnconvertibleObjectException("The output directory should be defined.");
}
if (params.get(TMP_DIR_URL) == null) {
throw new UnconvertibleObjectException("The output directory URL should be defined.");
}
if (source == null) {
throw new UnconvertibleObjectException("The output data should be defined.");
}
// TODO : useless test, null test above is all we need, fix this and other converters
if (!(source instanceof FeatureCollection)) {
throw new UnconvertibleObjectException("The requested output data is not an instance of FeatureCollection.");
}
Reference reference = new Reference();
reference.setMimeType((String) params.get(MIME));
reference.setEncoding((String) params.get(ENCODING));
final FeatureType ft = source.getType();
final String namespace = NamesExt.getNamespace(ft.getName());
final Map<String, String> schemaLocation = new HashMap<>();
final String randomFileName = UUID.randomUUID().toString();
if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(reference.getMimeType())) {
// create file
final Path dataFile = buildPath(params, randomFileName + ".json");
try (OutputStream fos = Files.newOutputStream(dataFile, CREATE, TRUNCATE_EXISTING, WRITE);
GeoJSONStreamWriter writer = new GeoJSONStreamWriter(fos, ft, WPSConvertersUtils.FRACTION_DIGITS);
Stream<Feature> st = source.features(false)) {
Iterator<Feature> iterator = st.iterator();
while (iterator.hasNext()) {
Feature next = iterator.next();
Feature neww = writer.next();
FeatureExt.copy(next, neww, false);
writer.write();
}
} catch (DataStoreException e) {
throw new UnconvertibleObjectException("Can't write Feature into GeoJSON output stream.", e);
} catch (IOException e) {
throw new UnconvertibleObjectException(e);
}
final String relLoc = getRelativeLocation(dataFile, params);
reference.setHref(params.get(TMP_DIR_URL) + "/" + relLoc);
reference.setSchema(null);
} else if (WPSMimeType.APP_GML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(reference.getMimeType())) {
try {
reference.setSchema(WPSConvertersUtils.writeSchema(ft, params));
schemaLocation.put(namespace, reference.getSchema());
} catch (JAXBException ex) {
throw new UnconvertibleObjectException("Can't write FeatureType into xsd schema.", ex);
} catch (IOException ex) {
throw new UnconvertibleObjectException("Can't create xsd schema file.", ex);
}
// Write Feature
final JAXPStreamFeatureWriter featureWriter = new JAXPStreamFeatureWriter(schemaLocation);
// create file
final Path dataFile = buildPath(params, randomFileName + ".xml");
try (final OutputStream dataStream = Files.newOutputStream(dataFile);
final AutoCloseable xmlCloser = () -> featureWriter.dispose()) {
// Write feature in file
featureWriter.write(source, dataStream);
final String relLoc = getRelativeLocation(dataFile, params);
reference.setHref(params.get(TMP_DIR_URL) + "/" + relLoc);
} catch (XMLStreamException ex) {
throw new UnconvertibleObjectException("Stax exception while writing the feature collection", ex);
} catch (DataStoreException ex) {
throw new UnconvertibleObjectException("FeatureStore exception while writing the feature collection", ex);
} catch (FeatureStoreRuntimeException ex) {
throw new UnconvertibleObjectException("FeatureStoreRuntimeException exception while writing the feature collection", ex);
} catch (Exception ex) {
throw new UnconvertibleObjectException(ex);
}
} else {
throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + reference.getMimeType());
}
return reference;
}
Aggregations