Hello Andrea,
I took my time and narrowed it down a bit. It seems the difference between
SwtMapPane and JMapPane is that the former doesn't update/set the screenArea
of the MapViewPort (of the SwtMapPane) upon panning or resizing the control
whereas the latter does by just calling setForNewSize() inside
onShownOrResized().
My workaround for this is as simple as this:
Create a MapPaneListener for capturing the onResized(MapPaneEvent) as well as
onDisplayAreaChanged(MapPaneEvent) to call MapViewPort's setScreenArea(...)
from there (see the modified cartouche example in the attachment). For the
moment this works for me. I have no idea whether this might result in
threading issues etc...
I'd be interested in contributing fixes however I fear my skills are currently
not sufficient for doing so.
Best,
Thomas
On Friday 12 February 2016 08:17:40 you wrote:
> Hi Thomas,
> I don't think the SWT module in GeoTools is maintained (cc'ing Andrea
> Antonello for
> confirmation). Wondering if you are interested in the module enough to
> contribute
> fixes and improvements to it?
>
> Cheers
> Andrea
>
>
> On Thu, Feb 11, 2016 at 11:41 PM, Thomas Wunder <[email protected]>
>
> wrote:
> > 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(XAff
> > ineTransform.java:569)>
> > at
> >
> > org.geotools.referencing.operation.matrix.XAffineTransform.getScale(XAffin
> > eTransform.java:596)>
> > at
> >
> > org.geotools.renderer.lite.RendererUtilities.calculateOGCScaleAffine(Rende
> > rerUtilities.java:507)>
> > at org.geotools.JSurvey.SwtCartouche.draw(SwtCartouche.java:132)
> > at
> >
> > org.geotools.renderer.lite.StreamingRenderer$RenderDirectLayerRequest.exec
> > ute(StreamingRenderer.java:3450)>
> > at
> >
> > org.geotools.renderer.lite.StreamingRenderer$PainterThread.run(StreamingRe
> > nderer.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
> >
> > --
> >
> >
> >
> > --------------------------------------------------------------------------
> > ---- 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
--
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.widgets.Composite;
import org.eclipse.swt.widgets.Control;
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.SwtMapPane;
import org.geotools.swt.event.MapPaneEvent;
import org.geotools.swt.event.MapPaneListener;
/**
* Make a cartouche using a directlayer to add author, copyright and other info
* to a map.
*
* @author ian
*
*/
public class SwtCartouche extends DirectLayer implements MapPaneListener {
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;
}
@Override
public void onDisplayAreaChanged( MapPaneEvent evt ){
MapContent mc = mp.getMapContent();
if( mc != null ){
MapViewport vp = mc.getViewport();
org.eclipse.swt.graphics.Rectangle visible_rect = mp.getVisibleRect();
if( vp != null ){
vp.setScreenArea( new Rectangle( visible_rect.x, visible_rect.y, visible_rect.width, visible_rect.height ) );
}
}
}
@Override
public void onNewContext( MapPaneEvent arg0 ){}
@Override
public void onNewRenderer( MapPaneEvent arg0 ){}
@Override
public void onRenderingProgress( MapPaneEvent arg0 ){}
@Override
public void onRenderingStarted( MapPaneEvent arg0 ){}
@Override
public void onRenderingStopped( MapPaneEvent arg0 ){}
@Override
public void onResized( MapPaneEvent evt ){
onDisplayAreaChanged( evt );
}
public SwtMapPane mp = null;
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);
final 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){
@Override
protected Control createContents( Composite arg0 ){
Control res = super.createContents( arg0 );
cartouche.mp = getMapPane();
cartouche.mp.addMapPaneListener( cartouche );
return( res );
}
};
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