Hello Ian,
Thanks a million for your reply. When I tried your Cartouche example the first
thing I noticed was that I forgot to mention that my application is SWT based
whereas Cartouche is based on swing.
I had to change some minor things to make it work in my setup however in the
swing based map view getWorldToScreen() and getScreenArea() both are working.
However the (Feature) map (and the DirectLayer) are shown only for a split
second upon e.g. reszing the map view (or panning etc.) and turn completely
white afterwards (as well as before that).
To see what's happening with SwtMapFrame instead I changed the code a bit (see
attachment, and please excuse me for hardwiring the filename). These are my
observations so far:
- Map is drawn perfectly and stays that way when panning resizing, zooming etc.
- A NullPointerException is being thrown in line 68 (println of the
getWorldToScreen() )
java.lang.NullPointerException
at
org.geotools.referencing.operation.matrix.XAffineTransform.getScaleX0(XAffineTransform.java:569)
at
org.geotools.referencing.operation.matrix.XAffineTransform.getScale(XAffineTransform.java:596)
at
org.geotools.renderer.lite.RendererUtilities.calculateOGCScaleAffine(RendererUtilities.java:507)
at org.geotools.JSurvey.SwtCartouche.draw(SwtCartouche.java:132)
at
org.geotools.renderer.lite.StreamingRenderer$RenderDirectLayerRequest.execute(StreamingRenderer.java:3450)
at
org.geotools.renderer.lite.StreamingRenderer$PainterThread.run(StreamingRenderer.java:3504)
at
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
- DirectLayer (Cartouche) is never being drawn (correctly)
- When commenting out line 68 (println...):
-- getScreenArea() returns a (0,0,0,0) rectangle as in my original application
-- Cartouche is not visible, presumably due to incorrect size values from
getScreenArea()
Does my SWT version of Cartouche work for you?
Thanks again!
Thomas
On Thursday 11 February 2016 14:47:22 you wrote:
> Here is an example of my Cartouche (
> https://en.wikipedia.org/wiki/Cartouche_(cartography)) layer -
> https://gitlab.com/snippets/14766 in case that helps
>
> Ian
>
> On 11 February 2016 at 14:43, Ian Turton <[email protected]> wrote:
> > It sounds like you have a problem somewhere, I regularly use Rectangle
> > size = viewport.getScreenArea(); in my directlayers and it is fine. Can
> > you
> > post some code and I can see if I can spot the problem for you,
> >
> > Ian
> >
> > On 11 February 2016 at 14:17, Thomas Wunder <[email protected]>
> >
> > wrote:
> >> Hello,
> >> I'm trying to extend the DirectLayer class but when I'm trying to use
> >> getWorldToScreen() on the viewport argument inside the draw(...) method
> >> it's
> >> always returning null. Is this the intended behaviour of that method in
> >> GeoTools 14? If so, how am I supposed to get the worldToScreen transform
> >> otherwise?
> >>
> >> By the way viewport.getScreenArea() always returns a (0,0,0,0) rectangle,
> >> whereas viewport.getBounds() seems to work properly. The getBounds() of
> >> my
> >> DirectLayer subclass I have implemented in a way that it returns sensible
> >> values and a valid CRS.
> >>
> >>
> >> I would really appreciate some hints on what could be going wrong here
> >> and how
> >> to get things working properly.
> >>
> >> Thanks in advance and best regards!
> >>
> >> Thomas
> >>
> >> -------------------------------------------------------------------------
> >> ----- Site24x7 APM Insight: Get Deep Visibility into Application
> >> Performance APM + Mobile APM + RUM: Monitor 3 App instances at just
> >> $35/Month
> >> Monitor end-to-end web transactions and take corrective actions now
> >> Troubleshoot faster and improve end-user experience. Signup Now!
> >> http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
> >> _______________________________________________
> >> GeoTools-GT2-Users mailing list
> >> [email protected]
> >> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
> >
> > --
> > Ian Turton
--
package org.geotools.JSurvey;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.map.DirectLayer;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.map.MapViewport;
import org.geotools.renderer.lite.RendererUtilities;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swt.SwtMapFrame;
import org.geotools.swt.control.JFileDataStoreChooser;
/**
* Make a cartouche using a directlayer to add author, copyright and other info
* to a map.
*
* @author ian
*
*/
public class SwtCartouche extends DirectLayer {
public enum Position {
TOP, BOTTOM, LEFT, RIGHT, TOPRIGHT, TOPLEFT, BOTTOMRIGHT, BOTTOMLEFT
}
public static final Position BOTTOMLEFT = Position.BOTTOMLEFT;
public static final Position BOTTOM = Position.BOTTOM;
public static final Position BOTTOMRIGHT = Position.BOTTOMRIGHT;
public static final Position RIGHT = Position.RIGHT;
public static final Position TOPRIGHT = Position.TOPRIGHT;
public static final Position TOP = Position.TOP;
public static final Position TOPLEFT = Position.TOPLEFT;
public static final Position LEFT = Position.LEFT;
//TODO make these values visible to DpiRescaleStyleVisitor
Position position = BOTTOMLEFT;
int fontSize = 12;
Font font = new Font("Liberation Sans", Font.PLAIN, fontSize);
boolean displayScale = false;
boolean heading = true;
List<String> text = new ArrayList<>();
Color bgColor = null;
Color fgColor = Color.BLACK;
@Override
public void draw(Graphics2D graphics, MapContent map, MapViewport viewport) {
Font oldFont = graphics.getFont();
Color oldColor = graphics.getColor();
Rectangle size = viewport.getScreenArea();
System.out.println( viewport.getWorldToScreen() );
graphics.setFont(font);
FontMetrics fm = graphics.getFontMetrics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int textPadding = 5;
int lineH = fm.getMaxAscent() + fm.getMaxDescent() + textPadding;
String longest = getLongestLine();
Rectangle2D rect = fm.getStringBounds(longest, graphics);
int w = (int) rect.getWidth() + 2*textPadding;
int h = lineH * text.size() + textPadding;
if(displayScale) {
h += lineH;
}
int ah = Math.max((int) (h / 10.0), (int) (w / 10.0));
int aw = ah;
int padding = 10;
int x = 0;
int y = 0;
int halfHeight = size.height/2;
int halfWidth = size.width/2;
int halfH = h/2;
int halfW = w/2;
int halfPadding = padding/2;
if (position == Position.RIGHT || position == Position.BOTTOMRIGHT || position == Position.TOPRIGHT) {
x = size.width - w - padding;
if(position == Position.RIGHT) {
y = halfHeight - halfH - halfPadding;
}
}
if (position == Position.BOTTOM || position == Position.BOTTOMLEFT || position == Position.BOTTOMRIGHT) {
y = size.height - h - padding;
if(position==Position.TOP) {
x = halfWidth - halfW - halfPadding;
}
}
if (position == Position.LEFT || position == Position.BOTTOMLEFT || position == Position.TOPLEFT) {
x = padding;
if(position == Position.LEFT) {
y = halfHeight - halfH - halfPadding;
}
}
if (position == Position.TOP || position == Position.TOPLEFT || position == Position.TOPRIGHT) {
y = padding;
if(position==Position.TOP) {
x = halfWidth - halfW - halfPadding;
}
}
if(bgColor!=null) {
graphics.setColor(bgColor);
graphics.fillRoundRect(x, y, w, h, aw, ah);
}
graphics.setColor(fgColor);
graphics.drawRoundRect(x, y, w, h, aw, ah);
int y2 = y;
for (String line : text) {
y2 += lineH;
graphics.drawString(line, x + textPadding, y2);
}
if(displayScale) {
double s = RendererUtilities.calculateOGCScaleAffine(map.getCoordinateReferenceSystem(), viewport.getWorldToScreen(), null);
y2 += lineH;
graphics.drawString(String.format("1 : %0$,1.0f",s), x + textPadding, y2);
}
graphics.setFont(oldFont);
graphics.setColor(oldColor);
//inform the label cache that we are an obstacle
}
private String getLongestLine() {
int max = 0;
String longest = "";
for (String line : text) {
if (line.length() > max) {
longest = line;
max = longest.length();
}
}
return longest;
}
public void setText(List<String> lines) {
text = lines;
}
public void addLine(String line) {
text.add(line);
}
@Override
public ReferencedEnvelope getBounds() {
return null;
}
public void setPosition(Position position) {
this.position = position;
}
public void setFontSize(int fontSize) {
this.fontSize = fontSize;
this.font = new Font(font.getFontName(), font.getStyle(), fontSize);
}
public void setFont(Font font) {
this.font = font;
}
public Color getBgColor() {
return bgColor;
}
public void setBgColor(Color bgColor) {
this.bgColor = bgColor;
}
public Color getFgColor() {
return fgColor;
}
public void setFgColor(Color fgColor) {
this.fgColor = fgColor;
}
public boolean isDisplayScale() {
return displayScale;
}
public void setDisplayScale(boolean scale) {
this.displayScale = scale;
}
public static void main(String[] args) throws IOException {
FileDataStore store = FileDataStoreFinder.getDataStore( new File( "/home/tomi/git/JSurvey/JSurveyMvn/data/countries.shp" ) );
SimpleFeatureSource featureSource = store.getFeatureSource();
// Create a map content and add our shapefile to it
MapContent mapContent = new MapContent();
mapContent.setTitle("GeoTools Mapping");
Style style = SLD.createSimpleStyle(featureSource.getSchema());
Layer layer = new FeatureLayer(featureSource, style);
mapContent.addLayer(layer);
SwtCartouche cartouche = new SwtCartouche();
cartouche.setPosition(SwtCartouche.BOTTOMRIGHT);
cartouche.addLine("Map of the World");
cartouche.addLine("Designed by Ian Turton, \u00a92015");
cartouche.addLine("Map data from Natural Earth, Public Domain");
cartouche.setDisplayScale(true);
mapContent.addLayer(cartouche);
SwtMapFrame frame = new SwtMapFrame(true, true, true, true, mapContent);
frame.setBlockOnOpen( true );
frame.open();
}
}
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
GeoTools-GT2-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users