use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.
the class IntervalStyleBuilder method genericAnalyze.
private void genericAnalyze() {
if (genericAnalyze)
return;
genericAnalyze = true;
properties.clear();
minimum = Double.POSITIVE_INFINITY;
maximum = Double.NEGATIVE_INFINITY;
count = 0;
sum = 0;
median = 0;
mean = 0;
// search the different numeric attributs
if (layer.getData() instanceof FeatureSet) {
throw new IllegalArgumentException("MapLayer resource must be a FeatureSet");
}
FeatureSet data = (FeatureSet) layer.getData();
final FeatureType schema;
try {
schema = data.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 (Number.class.isAssignableFrom(type) || type == byte.class || type == short.class || type == int.class || type == long.class || type == float.class || type == double.class) {
properties.add(ff.property(desc.getName().tip().toString()));
}
}
}
// find the geometry class for template
Class<?> geoClass = null;
try {
PropertyType geo = FeatureExt.getDefaultGeometry(schema);
geoClass = ((AttributeType) ((Operation) geo).getResult()).getValueClass();
} catch (PropertyNotFoundException | IllegalStateException ex) {
}
if (geoClass == null) {
return;
}
if (template == null) {
if (Polygon.class.isAssignableFrom(geoClass) || MultiPolygon.class.isAssignableFrom(geoClass)) {
template = createPolygonTemplate();
} else if (LineString.class.isAssignableFrom(geoClass) || MultiLineString.class.isAssignableFrom(geoClass)) {
template = createLineTemplate();
} else {
template = createPointTemplate();
}
}
// search the extreme values
final Query query = new Query(schema.getName().toString());
if (classification == null || layer == null)
return;
if (!properties.contains(classification))
return;
final Set<String> qp = new HashSet<String>();
qp.add(classification.getXPath());
if (normalize != null && !normalize.equals(noValue)) {
qp.add(normalize.getXPath());
}
query.setProperties(qp.toArray(new String[0]));
Iterator<Feature> features = null;
try (Stream<Feature> stream = data.subset(query).features(false)) {
features = stream.iterator();
List<Double> values = new ArrayList<Double>();
while (features.hasNext()) {
Feature sf = features.next();
count++;
Number classifValue = (Number) classification.apply(sf);
double value;
if (classifValue == null) {
// skip null values in analyze
continue;
}
if (normalize == null || normalize.equals(noValue)) {
value = classifValue.doubleValue();
} else {
Number normalizeValue = (Number) normalize.apply(sf);
value = classifValue.doubleValue() / normalizeValue.doubleValue();
}
values.add(value);
sum += value;
if (value < minimum) {
minimum = value;
}
if (value > maximum) {
maximum = value;
}
}
mean = (minimum + maximum) / 2;
// find the median
allValues = values.toArray(new Double[values.size()]);
Arrays.sort(allValues);
if (values.isEmpty()) {
median = 0;
} else if (values.size() % 2 == 0) {
median = (allValues[(allValues.length / 2) - 1] + allValues[allValues.length / 2]) / 2.0;
} else {
median = allValues[allValues.length / 2];
}
} catch (DataStoreException ex) {
ex.printStackTrace();
}
}
use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.
the class PostgisDemo method main.
public static void main(String[] args) throws DataStoreException {
Demos.init();
System.out.println(PostgresProvider.PARAMETERS_DESCRIPTOR);
final Parameters parameters = Parameters.castOrWrap(PostgresProvider.PARAMETERS_DESCRIPTOR.createValue());
parameters.getOrCreate(PostgresProvider.HOST).setValue("hote");
parameters.getOrCreate(PostgresProvider.PORT).setValue(5432);
parameters.getOrCreate(PostgresProvider.DATABASE).setValue("base");
parameters.getOrCreate(PostgresProvider.USER).setValue("user");
parameters.getOrCreate(PostgresProvider.PASSWORD).setValue("secret");
final FeatureStore store = (FeatureStore) DataStores.open(parameters);
final MapLayers context = MapBuilder.createContext();
for (GenericName n : store.getNames()) {
System.out.println(store.getFeatureType(n.toString()));
final FeatureSet col = store.createSession(true).getFeatureCollection(new Query(n));
final MapLayer layer = MapBuilder.createLayer(col);
layer.setStyle(RandomStyleBuilder.createRandomVectorStyle(col.getType()));
context.getComponents().add(layer);
}
// FXMapFrame.show(context);
}
use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.
the class PostgisRasterDemo method main.
public static void main(String[] args) throws Exception {
final CoordinateReferenceSystem crs = CommonCRS.defaultGeographic();
// connect to postgres feature store
final PostgresStore store = new PostgresStore("localhost", 5432, "table", "public", "user", "password");
// create a feature type with a coverage attribute type
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("SpotImages");
ftb.addAttribute(String.class).setName("name");
ftb.addAttribute(GridCoverage.class).setName("image").setCRS(crs);
FeatureType type = ftb.build();
store.createFeatureType(type);
// type migh be a little different after insertion
type = store.getFeatureType("SpotImages");
// WARNING : if you use a existing table you must ensure that the srid
// constraint is set in the raster_columns view or that the raster
// column comment contains the srid as a comment
// Create an image we will use as a coverage
final BufferedImage image = new BufferedImage(180, 360, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g = image.createGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, 180, 360);
// Create a coverage
final GridCoverageBuilder gcb = new GridCoverageBuilder();
gcb.setValues(image);
gcb.setDomain(new GridGeometry(null, PixelInCell.CELL_CORNER, new AffineTransform2D(-1, 0, 0, 1, +90, -180), crs));
final GridCoverage coverage = gcb.build();
// Create a feature
final Feature feature = type.newInstance();
feature.setPropertyValue("name", "world");
feature.setPropertyValue("image", coverage);
// Save the feature
store.addFeatures(type.getName().toString(), Collections.singletonList(feature));
// Display it
final FeatureSet col = store.createSession(false).getFeatureCollection(new Query(store.getNames().iterator().next()));
final MapLayer layer = MapBuilder.createLayer(col);
layer.setStyle(RandomStyleBuilder.createDefaultRasterStyle());
final MapLayers context = MapBuilder.createContext();
context.getComponents().add(layer);
// FXMapFrame.show(context);
}
use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.
the class DBRelationOperation method apply.
@Override
public Property apply(Feature ftr, ParameterValueGroup pvg) {
final Object key = ftr.getPropertyValue(relation.getCurrentColumn());
final Query qb = new Query();
qb.setTypeName(NamesExt.create(relation.getForeignTable()));
qb.setSelection(relation.toFilter(key));
final FeatureCollection res = store.createSession(false).getFeatureCollection(qb);
final Object value;
if (type.getMaximumOccurs() == 1) {
try (FeatureIterator ite = res.iterator()) {
if (ite.hasNext()) {
value = ite.next();
} else {
value = null;
}
}
} else {
value = res;
}
return new AbstractAssociation(type) {
@Override
public Feature getValue() throws MultiValuedPropertyException {
if (value == null || value instanceof Feature) {
return (Feature) value;
}
throw new MultiValuedPropertyException();
}
@Override
public Collection<Feature> getValues() {
if (value == null) {
return Collections.EMPTY_LIST;
} else if (value instanceof Feature) {
return Arrays.asList((Feature) value);
} else {
return res;
}
}
@Override
public void setValue(Feature value) throws InvalidPropertyValueException {
throw new UnsupportedOperationException("Not supported.");
}
};
}
use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.
the class StringToFeatureCollectionConverter method apply.
@Override
public FeatureCollection apply(final String s) throws UnconvertibleObjectException {
if (s == null)
throw new UnconvertibleObjectException("Empty FeatureCollection");
try {
String url;
if (s.startsWith("file:")) {
url = s;
} else {
url = "file:" + s;
}
final Map<String, Serializable> parameters = new HashMap<String, Serializable>();
parameters.put(DataStoreProvider.LOCATION, URI.create(url));
final FeatureStore store = (FeatureStore) DataStores.open(parameters);
if (store == null) {
throw new UnconvertibleObjectException("Invalid URL");
}
if (store.getNames().size() != 1) {
throw new UnconvertibleObjectException("More than one FeatureCollection in the file");
}
final FeatureCollection collection = store.createSession(true).getFeatureCollection(new Query(store.getNames().iterator().next()));
if (collection != null) {
return collection;
} else {
throw new UnconvertibleObjectException("Collection not found");
}
} catch (DataStoreException ex) {
throw new UnconvertibleObjectException(ex);
}
}
Aggregations