use of org.apache.sis.portrayal.MapItem in project geotoolkit by Geomatys.
the class OwcXmlIO method findItem.
private static MapItem findItem(MapLayers parent, String name) {
for (MapItem mi : parent.getComponents()) {
if (mi.getIdentifier().equals(name)) {
return mi;
}
}
// does not exist, create it
final MapLayers np = MapBuilder.createItem();
parent.getComponents().add(np);
return np;
}
use of org.apache.sis.portrayal.MapItem in project geotoolkit by Geomatys.
the class OwcXmlIO method readEntry.
private static MapItem readEntry(final EntryType entry) throws JAXBException, FactoryException, DataStoreException {
final List<Object> entryContent = entry.getAuthorOrCategoryOrContent();
String layerName = "";
String layerTitle = "";
String layerAbstract = "";
boolean visible = true;
boolean selectable = true;
double layerOpacity = 1.0;
MapItem mapItem = null;
MutableStyle baseStyle = null;
MutableStyle selectionStyle = null;
final List<MapItem> children = new ArrayList<>();
for (Object o : entryContent) {
QName name = null;
if (o instanceof JAXBElement) {
final JAXBElement jax = (JAXBElement) o;
name = jax.getName();
o = jax.getValue();
if (GEOTK_FACTORY._Visible_QNAME.equals(name)) {
visible = (Boolean) o;
} else if (GEOTK_FACTORY._Selectable_QNAME.equals(name)) {
selectable = (Boolean) o;
} else if (GEOTK_FACTORY._Opacity_QNAME.equals(name)) {
layerOpacity = (Double) o;
}
}
if (o instanceof OfferingType) {
final OfferingType offering = (OfferingType) o;
for (OwcExtension ext : getExtensions()) {
if (ext.getCode().equals(offering.getCode())) {
mapItem = ext.createLayer(offering);
break;
}
}
// search for styles
baseStyle = readStyle(offering, true);
selectionStyle = readStyle(offering, false);
} else if (o instanceof ContentType) {
// decode children
final ContentType content = (ContentType) o;
final List<Object> contentContent = content.getContent();
for (Object co : contentContent) {
if (co instanceof JAXBElement) {
co = ((JAXBElement) o).getValue();
}
if (co instanceof EntryType) {
children.add(readEntry((EntryType) co));
}
}
} else if (o instanceof IdType) {
final IdType idType = (IdType) o;
final String value = idType.getValue();
layerName = value;
} else if (o instanceof TextType) {
final TextType tt = (TextType) o;
if (ATOM_FACTORY._EntryTypeTitle_QNAME.equals(name)) {
if (!tt.getContent().isEmpty()) {
layerTitle = (String) tt.getContent().get(0);
}
} else if (ATOM_FACTORY._EntryTypeSummary_QNAME.equals(name)) {
if (!tt.getContent().isEmpty()) {
layerAbstract = (String) tt.getContent().get(0);
}
}
}
}
if (mapItem == null) {
mapItem = MapBuilder.createItem();
} else if (mapItem instanceof MapLayer) {
if (baseStyle != null) {
((MapLayer) mapItem).setStyle(baseStyle);
}
((MapLayer) mapItem).setOpacity(layerOpacity);
}
mapItem.setIdentifier(layerName);
mapItem.setTitle(layerTitle);
mapItem.setAbstract(layerAbstract);
mapItem.setVisible(visible);
if (mapItem instanceof MapLayers) {
((MapLayers) mapItem).getComponents().addAll(children);
} else if (!children.isEmpty()) {
throw new IllegalArgumentException("MapLayer can not have children layers.");
}
return mapItem;
}
use of org.apache.sis.portrayal.MapItem in project geotoolkit by Geomatys.
the class OwcXmlIO method write.
private static FeedType write(final MapLayers context) throws FactoryException {
final FeedType feed = ATOM_FACTORY.createFeedType();
final LinkType link = ATOM_FACTORY.createLinkType();
link.setRel("profile");
link.setHref("http://www.opengis.net/spec/owc-atom/1.0/req/core");
link.setTitle(context.getIdentifier() == null ? "" : context.getIdentifier());
feed.getAuthorOrCategoryOrContributor().add(ATOM_FACTORY.createFeedTypeLink(link));
final TextType title = ATOM_FACTORY.createTextType();
title.getContent().add(context.getIdentifier() == null ? "" : context.getIdentifier());
feed.getAuthorOrCategoryOrContributor().add(ATOM_FACTORY.createFeedTypeTitle(title));
final Envelope aoi = context.getAreaOfInterest();
if (aoi != null) {
final String ogc = IdentifiedObjects.lookupURN(aoi.getCoordinateReferenceSystem(), null);
final WhereType where = GEORSS_FACTORY.createWhereType();
final DirectPositionType lowerCorner = new DirectPositionType(aoi.getLowerCorner());
final DirectPositionType upperCorner = new DirectPositionType(aoi.getUpperCorner());
final EnvelopeType envelopeType = new EnvelopeType(null, lowerCorner, upperCorner, ogc);
envelopeType.setSrsDimension(2);
where.setEnvelope(envelopeType);
feed.getAuthorOrCategoryOrContributor().add(GEORSS_FACTORY.createWhere(where));
}
for (final MapItem mapItem : context.getComponents()) {
toEntry(null, mapItem, feed.getAuthorOrCategoryOrContributor());
}
return feed;
}
use of org.apache.sis.portrayal.MapItem in project geotoolkit by Geomatys.
the class J2DLegendUtilities method estimateNoTemplate.
/**
* Estimate the size (pixels) needed to render the given {@link MapItem} legend
* without using any rendering hint (see {@link LegendTemplate}).
* @param source the map item to paint legend for.
* @param toSet the dimension used to store estimation result.
*/
private static void estimateNoTemplate(MapItem source, Dimension toSet) {
if (source instanceof MapLayers) {
final MapLayers mc = (MapLayers) source;
for (MapItem childItem : mc.getComponents()) {
if (childItem instanceof MapLayer) {
final MapLayer ml = (MapLayer) childItem;
final Dimension preferred = new Dimension(0, 0);
DefaultGlyphService.glyphPreferredSize(ml.getStyle(), preferred, ml);
if (preferred != null) {
if (preferred.width > toSet.width) {
toSet.width = preferred.width;
}
toSet.height += preferred.height;
}
} else {
estimateNoTemplate(childItem, toSet);
}
}
}
}
use of org.apache.sis.portrayal.MapItem in project geotoolkit by Geomatys.
the class J2DLegendUtilities method paintWithTemplate.
/**
* Draw legend using given {@link LegendTemplate} as rendering hint. At the
* end of the drawning, input {@link Graphics2D} origin is reset at the origin
* it was when given as parameter.
* @param item The map item to draw legend for.
* @param g2d The graphic object to draw legend in.
* @param bounds Drawing authorized rectangle.
* @param template Rendering hints.
* @param legendResults useless. Store drawn images for each coverage layer
* @return The number of lines which have been drawn on y axis
*/
private static int paintWithTemplate(final MapItem item, final Graphics2D g2d, final Rectangle bounds, final LegendTemplate template, final Map<GenericName, BufferedImage> legendResults) {
final AffineTransform origin = g2d.getTransform();
final FontMetrics layerFontMetric = g2d.getFontMetrics(template.getLayerFont());
final FontMetrics ruleFontMetric = g2d.getFontMetrics(template.getRuleFont());
final int ruleFontHeight = ruleFontMetric.getHeight();
final float gapSize = template.getGapSize();
final Dimension glyphSize = template.getGlyphSize();
final Rectangle2D rectangle = new Rectangle2D.Float();
float moveY = 0;
final List<MapItem> layers = (item instanceof MapLayers) ? ((MapLayers) item).getComponents() : Collections.EMPTY_LIST;
for (int l = 0, n = layers.size(); l < n; l++) {
final MapItem currentItem = layers.get(l);
// check if the given layer is visible, and if we should display invisible layers.
if (template.displayOnlyVisibleLayers() && !isVisible(currentItem)) {
continue;
}
// Check for current item title.
if (template.isLayerVisible()) {
if (l != 0) {
moveY += gapSize;
}
String title = "";
final CharSequence titleTmp = currentItem.getTitle();
if (titleTmp != null) {
title = titleTmp.toString().replace("{}", "");
}
if (title.isEmpty()) {
title = currentItem.getIdentifier();
}
if (title != null && !title.isEmpty()) {
moveY += layerFontMetric.getLeading() + layerFontMetric.getAscent();
g2d.setFont(template.getLayerFont());
Color layerFontColor = template.getLayerFontColor();
if (layerFontColor != null) {
if (template.getLayerFontOpacity() != null) {
layerFontColor = new Color(layerFontColor.getRed(), layerFontColor.getGreen(), layerFontColor.getBlue(), template.getLayerFontOpacity());
}
} else {
layerFontColor = Color.BLACK;
}
g2d.setColor(layerFontColor);
g2d.drawString(title, 0, moveY);
moveY += layerFontMetric.getDescent();
moveY += gapSize;
}
}
// If we're not on a leaf, try to display this node children.
if (!(currentItem instanceof MapLayer)) {
// Using doubles allows current position relative translation.
final double nodeInset = template.getBackground().getBackgroundInsets().left;
g2d.translate(nodeInset, moveY);
final int itemDim = paintWithTemplate(currentItem, g2d, bounds, template, legendResults);
g2d.translate(-nodeInset, -moveY);
// Previous function reset graphic position at the top of drawn map item. We Add its size to vertical offset, so next item knows how much pixels it should jump.
moveY += itemDim;
continue;
}
final MapLayer layer = (MapLayer) layers.get(l);
// default style defined on the WMS service for this layer
wmscase: if (layer.getData() instanceof GridCoverageResource) {
// Get the image from the ones previously stored, to not resend a get legend graphic request.
BufferedImage image = null;
try {
image = legendResults.get(layer.getData().getIdentifier().get());
} catch (DataStoreException ex) {
// do nothing
}
if (image == null) {
break wmscase;
}
if (l != 0) {
moveY += gapSize;
}
g2d.drawImage(image, null, 0, Math.round(moveY));
moveY += image.getHeight();
continue;
}
final Style style = layer.getStyle();
if (style == null) {
continue;
}
int numElement = 0;
for (final FeatureTypeStyle fts : style.featureTypeStyles()) {
for (final Rule rule : fts.rules()) {
if (numElement != 0) {
moveY += gapSize;
}
// calculate the rule text displacement with the glyph size
final float stepRuleTitle;
final float glyphHeight;
final float glyphWidth;
final Dimension preferred = DefaultGlyphService.glyphPreferredSize(rule, null, layer);
if (glyphSize == null) {
// find the best size
glyphHeight = preferred.height;
glyphWidth = preferred.width;
} else {
// Use the biggest size between preferred one and default one.
glyphHeight = Math.max(glyphSize.height, preferred.height);
glyphWidth = Math.max(glyphSize.width, preferred.width);
}
if (glyphHeight > ruleFontHeight) {
stepRuleTitle = ruleFontMetric.getLeading() + ruleFontMetric.getAscent() + (glyphHeight - ruleFontHeight) / 2;
} else {
stepRuleTitle = ruleFontMetric.getLeading() + ruleFontMetric.getAscent();
}
rectangle.setRect(0, moveY, glyphWidth, glyphHeight);
DefaultGlyphService.render(rule, rectangle, g2d, layer);
String title = "";
final Description description = rule.getDescription();
if (description != null) {
final InternationalString titleTmp = description.getTitle();
if (titleTmp != null) {
title = titleTmp.toString();
}
}
if (title.isEmpty()) {
moveY += glyphHeight;
} else {
g2d.setFont(template.getRuleFont());
g2d.setColor(Color.BLACK);
g2d.drawString(title, glyphWidth + GLYPH_SPACE, moveY + stepRuleTitle);
moveY += (glyphHeight > ruleFontHeight) ? glyphHeight : ruleFontHeight;
}
numElement++;
}
}
}
g2d.setTransform(origin);
return (int) moveY;
}
Aggregations