Hi,
There are 2 possible issues here:
1. I think that the drop coordinates are with respect to the Display, Not the Map. So first thing that has to be done is it has to be converted to the map coordinates. Fortunately the Viewport is a Composite so it is easy. Here's a snippet for you:
@Override public void perform( IProgressMonitor monitor ) { final String string=getData().toString();
// Since I declared my target to be a ViewportPane then I know that is what the // destination is going to be. MapEditor editor=(MapEditor) getDestination();
// In order to know where to draw the string we have to calculate where the // drop event has taken place with respect to the ViewportPane. // The Event returns the location but it is w.r.t the Display. // All SWT widgets have helper methods to determine this so: Control control=editor.getComposite(); final Point drawLocation = control.toControl(getEvent().x, getEvent().y);
// By Adding a custom draw command we can draw on the viewport model... editor.getMap().sendCommandASync(new AbstractDrawCommand(){
public Rectangle getValidArea() { // I'm being lazy and returning null so that this will be re-drawn every time the // Viewport is updated. return null; }
public void run( IProgressMonitor monitor ) throws Exception { // draw the string graphics.drawString(string, drawLocation.x, drawLocation.y, ViewportGraphics.ALIGN_LEFT, ViewportGraphics.ALIGN_BOTTOM); }
});
}
For this example to work the target of the operation must be a MapEditor.
2. PixelToWorld Converts from the Screen to the Map (the CRS provided by the ViewportModel). You have to convert the coordinate from the map CRS to the layer CRS. I made a code example that you can look at here:
Hope this helps,
Jesse
On 16-Jan-07, at 7:51 AM, ukpete wrote:
Hello all,
I have a noob kind of problem, pixelToWorld doesnt seem to be converting my screen coordinates correctly to the specific layer I am dropping on. The coordinates are "off" sometimes by a significant percentage. Here is an example of the code I first attempted:
clickedLocation = ApplicationGIS.createContext(ApplicationGIS.getActiveMap()).pixelToWorld(this.getEvent().x, this.getEvent().y);
I've also used this:
clickedLocation = currentMap.getViewportModel().pixelToWorld(this.getEvent().x, this.getEvent().y);
I suspect its something to do with the CRS of the layer I am drawing on - is there a snippet of code out there that will resolve this regardless of the layer I am dropping onto? This code is embedded inside a DropAction object added through the usual extension mechanism, thus the event I am using is a DropEvent - if that makes any difference...
Kind Regards,
Pete Cornwell -- Sent from the udig-devel mailing list archive at Nabble.com.
_______________________________________________ User-friendly Desktop Internet GIS (uDig)
|