Hi Jody,
I was getting blank images, but have solved the problem with help from
my colleague Paul Townend who has already been through this.
The method createPNGImage(MapContext,int,int,String) as below generates
a PNG type image from MapContext theMapContext, this worked in the code
I originally posted as soon as I had _MapContext. What was then in the
image was clearly the boundaries of the area I wanted to colour. Getting
the colouring working involved quite a bit more work. The bit that
caught us out for ages was setting the right feature type name in the
FeatureTypeStyle we were using. In our case, this could be got from any
of the Features as _Feature.getFeatureType().getTypeName(). Hope this
makes sense. I can post the code online or send it to anyone who wants.
I'm a little concerned that we're using deprecated methods, but am
satisfied for the time being that this works.
public void createPNGImage(MapContext theMapContext, final int sizeX,
final int sizeY, String fileName) throws IOException {
LiteRenderer theLiteRenderer = new LiteRenderer(theMapContext);
final Panel thePanel = new Panel();
thePanel.setSize(sizeX,sizeY);
final ShapefileRenderer theSFRenderer = new
ShapefileRenderer(theMapContext);
final AffineTransform theTransform =
theLiteRenderer.worldToScreenTransform(theMapContext.getAreaOfInterest()
, thePanel.getBounds());
// image creation
BufferedImage bufferedImage = new BufferedImage(sizeX, sizeY,
BufferedImage.TYPE_INT_ARGB) {
{
paint((Graphics2D) getGraphics());
}
private void paint(final Graphics2D graphics2D) {
graphics2D.setRenderingHints(new RenderingHints
(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON));
graphics2D.setColor(new Color(0,0,0,0));
graphics2D.fillRect(0, 0, sizeX, sizeY);
theSFRenderer.paint(graphics2D, thePanel.getBounds(),
theTransform);
}
};
try {
ImageIO.write(bufferedImage, "png", new File(fileName));
} catch (Exception e) {
e.printStackTrace();
}
}
Best wishes,
Andy
http://www.geog.leeds.ac.uk/people/a.turner/
-----Original Message-----
From: Jody Garnett [mailto:[EMAIL PROTECTED]
Sent: 26 May 2007 02:00
To: Andy Turner
Cc: [email protected]
Subject: Re: [Geotools-gt2-users] Producing image from shapefile and
some attribute data that can be linked
Hi Andy
There is a style maker in geotools - based on the color brewer project.
It is one of the extensions - give it a shapefile and the attribute you
want to use to color by and it will generate an Style. The code is used
in uDig ...
I am also interested in writing out a image ... I would like to export
the current screen from uDig.
You just mentioned that you had "no luck" ...
are you getting empty images? A stack trace? ... anything?
If you want you can fire up uDig in order to visualize as you work on a
SLD document. It can make the initial colors for you and then you can
hack on the xml file. You can then feed the sld file back to your code
when you are happy ... does your code draw anything?
Cheers,
Jody
Andy Turner wrote:
> Hi geotools-gt2-users,
>
> I have joined the club again after years of just lurking on the email
> list.
>
> I want to generate a load of static image files which are geographic
> maps that I can insert into web pages.
>
> Can you help?
>
> I am using GeoTools 2.3.1 (bin) and the javadocs.
>
> I can load the shapefile and iterate through the features and see the
> attribute on which I want to link. That is this bit:
>
> // Read Boundary data using ShapefileDataStore
> URL _URL = new URL( "file:///C:/temp/test.shp" );
> //boolean readDBF = true;
> ShapefileDataStoreFactory _ShapefileDataStoreFactory = new
> ShapefileDataStoreFactory();
> ShapefileDataStore _ShapefileDataStore = ( ShapefileDataStore
> ) _ShapefileDataStoreFactory.createDataStore( _URL );
> //ShapefileDataStore _ShapefileDataStore = new
> ShapefileDataStore( _URL );
> FeatureType _FeatureType = _ShapefileDataStore.getSchema();
> AttributeType[] _AttributeTypes =
> _FeatureType.getAttributeTypes();
> for ( int i = 0; i < _AttributeTypes.length; i ++ ) {
> System.out.println( _AttributeTypes[ i ].getName() );
> }
> FeatureSource _FeatureSource =
> _ShapefileDataStore.getFeatureSource();
> FeatureCollection _FeatureCollection =
> _FeatureSource.getFeatures();
> FeatureIterator _FeatureIterator =
> _FeatureCollection.features();
> // Feature _Feature;
> // while ( _FeatureIterator.hasNext() ) {
> // _Feature = _FeatureIterator.next();
> // System.out.println( _Feature.toString() );
> // }
>
> Next I wanted to create an image of the polygons in the shapefile.
> This is where I got stuck. Here is what I currently have:
>
> // Create image using _ShapefileRenderer
> ShapefileRenderer _ShapefileRenderer = new
ShapefileRenderer();
> // Set _MapContext
> // CoordinateReferenceSystem _CoordinateReferenceSystem = new
> CoordinateReferenceSystem();
> // DefaultMapContext context = new DefaultMapContext(
> _CoordinateReferenceSystem );
> DefaultMapContext _MapContext = new DefaultMapContext();
> // Set some extra information to the map
> _MapContext.setTitle( "_Title" );
> _MapContext.setContactInformation( "_ContactInformation" );
> _MapContext.setAbstract( "_Abstract" );
> _ShapefileRenderer.setContext( _MapContext );
>
> // Create _Style
> StyleFactoryImpl _StyleFactoryImpl = new StyleFactoryImpl();
> PolygonSymbolizer _PolygonSymbolizer =
> _StyleFactoryImpl.createPolygonSymbolizer();
> // Fill _Fill = new Fill();
> // Expression _Expression = new Expression();
> // _Fill.setColor( _Expression );
> // _PolygonSymbolizer.setFill( _Fill );
> Rule _Rule = _StyleFactoryImpl.createRule();
> _Rule.setSymbolizers( new Symbolizer[] { _PolygonSymbolizer }
);
> Style _Style = _StyleFactoryImpl.createStyle();
> _Style.setName( "Style" );
> _Style.addFeatureTypeStyle(
> _StyleFactoryImpl.createFeatureTypeStyle( new Rule[] { _Rule } ) );
>
> // Create _MapLayer from the _Style
> MapLayer _MapLayer = new DefaultMapLayer( _FeatureCollection,
> _Style, "_MapLayer1");
>
> // Create _BufferedImage
> BufferedImage _BufferedImage = new BufferedImage(
> _Chart_Width, _Chart_Height, BufferedImage.TYPE_INT_RGB );
> Graphics _Graphics = _BufferedImage.getGraphics();
> _Graphics.setColor( Color.white );
> _Graphics.fillRect( 0, 0, _Chart_Width, _Chart_Height );
> _ShapefileRenderer.paint( ( Graphics2D ) _Graphics , new
> Rectangle( 0, 0, _Chart_Width, _Chart_Height ), new AffineTransform()
> );
>
> // Create image file
> String type = "PNG";
> javax.imageio.ImageIO.write( _BufferedImage, type, new File(
> "C:/temp/test" + "." + type ) );
>
>
> I have tried quite a few things, but had no luck. Please advise?
>
> I am now working on linking the other attribute data that I have and
> trying to figure how to shade these in different colours. Linking the
> data I was going to do myself as I don't expect there to be anything
> in
> GT2 to help. Styling is going to be tricky unless I can figure how to
> see some output. I have tried to use JMapPane but sofar had no success
> in displaying an image on screen. Any advice much appreciated.
>
> Many thanks for your help and for GT2 etc...
>
> Best wishes,
>
> Andy
> http://www.geog.leeds.ac.uk/people/a.turner/
>
>
>
> ----------------------------------------------------------------------
> --
>
> ----------------------------------------------------------------------
> --
>
> ----------------------------------------------------------------------
> --- This SF.net email is sponsored by DB2 Express Download DB2 Express
> C - the FREE version of DB2 express and take control of your XML. No
> limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> ----------------------------------------------------------------------
> --
>
> _______________________________________________
> Geotools-gt2-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
>
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users