Hello Ian, The seemingly odd behaviour you are seeing is because MapViewport does not centre the map bounds inside the screen bounds by default when calculating its AffineTransforms. In your case you have map and screen bounds with different aspect ratios so you are getting non-intuitive output.
Add the following line to your code: mc.getViewport().setMatchingAspectRatio(true); You should then get the result that you are expecting (let me know if you don't). MapViewport was written to have this default behaviour to honour GeoServer / WFS conventions (sorry - can't quite remember the details now but Andrea Aaime can provide more info if needed). Hope this helps, Michael On 20 April 2012 23:21, Ian Mayo <[email protected]> wrote: > Hi all, > I'm having some GeoTools projection troubles - so am going back to > first principles... > > I've produced a code sample to familiarise myself with the world to > screen transforms, but I'm surprised by the results. > > I'd be most grateful for a pointer on where I'm going wrong. > > Here's the code: > =================== > public void testOne() throws NoSuchAuthorityCodeException, FactoryException > { > MapContent mc = new MapContent(); > > // set a coordinate reference system > CoordinateReferenceSystem crs = CRS.decode("EPSG:4326"); > mc.getViewport().setCoordinateReferenceSystem(crs); > > // set a data area > DirectPosition2D tlDegs = new DirectPosition2D(5, 1); > DirectPosition2D brDegs = new DirectPosition2D(1, 5); > Envelope2D env = new Envelope2D(tlDegs, brDegs); > ReferencedEnvelope rEnv = new ReferencedEnvelope(env, crs); > mc.getViewport().setBounds(rEnv); > > // set a screen area > mc.getViewport().setScreenArea(new Rectangle(0, 0, 800, 400)); > > // create a point to test > DirectPosition2D degs = new DirectPosition2D(3, 3); > > // and results object > DirectPosition2D pixels = new DirectPosition2D(); > > // transform the test point > mc.getViewport().getWorldToScreen().transform(degs, pixels); > > System.out.println("pixels:" + pixels); > > } > =================== > > Surprisingly I'm getting this output: > ================= > pixels:DirectPosition2D[299.0, -295.0] > ================= > > I would expect the location to be near the centre of the screen > coordinate space (since the supplied point is near the centre of the > data coordinate space). > > Any guidance would be much appreciated, > Ian > > ------------------------------------------------------------------------------ > For Developers, A Lot Can Happen In A Second. > Boundary is the first to Know...and Tell You. > Monitor Your Applications in Ultra-Fine Resolution. Try it FREE! > http://p.sf.net/sfu/Boundary-d2dvs2 > _______________________________________________ > GeoTools-GT2-Users mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users ------------------------------------------------------------------------------ For Developers, A Lot Can Happen In A Second. Boundary is the first to Know...and Tell You. Monitor Your Applications in Ultra-Fine Resolution. Try it FREE! http://p.sf.net/sfu/Boundary-d2dvs2 _______________________________________________ GeoTools-GT2-Users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
