34
33
* is transformed automatically to "EPSG 3785".
36
35
* @author Kjetil Andresen <kjetand@ifi.uio.no>
36
* @author Olai Solheim <olais@ifi.uio.no>
38
38
public class GeoToolsMap extends InternalMap {
40
// The flat list of map objects in this map.
41
List<GeoToolsMapObject> mapObjects;
41
44
* Creates an empty map.
43
46
public GeoToolsMap() {
44
this.mapObjects = new LinkedList<InternalMapObject>();
47
this.mapObjects = new LinkedList<GeoToolsMapObject>();
48
51
* Creates a map with the given initial map layer.
52
* @param layer the initial map layer
52
54
public GeoToolsMap(InternalMapLayer layer) {
53
this.mapObjects = new LinkedList<InternalMapObject>();
55
this.mapObjects = new LinkedList<GeoToolsMapObject>();
54
56
this.addMapLayer(layer);
58
60
* Creates a map with the given initial map layers.
61
* @param layers the list of initial map layers
62
63
public GeoToolsMap(List<InternalMapLayer> layers) {
63
this.mapObjects = new LinkedList<InternalMapObject>();
64
this.mapObjects = new LinkedList<GeoToolsMapObject>();
64
65
this.addMapLayers(layers);
68
69
* Adds a map object to this map.
70
* @param mapObject the map object
72
public void addMapObject(InternalMapObject mapObject) {
72
public void addMapObject(GeoToolsMapObject mapObject) {
73
73
this.mapObjects.add(mapObject);
77
* Adds all map objects contained in the collection.
77
* Adds all map objects contained in the list.
78
* @param mapObjects the list of map objects
81
public void addMapObjects(List<InternalMapObject> mapObjects) {
82
for (InternalMapObject mapObject : mapObjects) {
83
this.mapObjects.add(mapObject);
80
public void addMapObjects(List<GeoToolsMapObject> mapObjects) {
81
for (GeoToolsMapObject mapObject : mapObjects) {
82
this.mapObjects.add((GeoToolsMapObject) mapObject);
88
* Adds a map layer to this map.
86
// -------------------------------------------------------------------------
87
// InternalMap implementation
88
// -------------------------------------------------------------------------
92
90
public void addMapLayer(InternalMapLayer layer) {
93
91
for (InternalMapObject mapObject : layer.getAllMapObjects()) {
94
92
this.addMapObject((GeoToolsMapObject) mapObject);
99
* Adds all map layers contained in the list.
103
96
public void addMapLayers(List<InternalMapLayer> layers) {
104
97
for (InternalMapLayer layer : layers) {
105
98
for (InternalMapObject mapObject : layer.getAllMapObjects()) {
112
* Renders all map objects contained in this map to an image
113
* with the default image width.
117
public Image render() {
104
public BufferedImage render() {
118
105
return render(DEFAULT_IMAGEWIDTH);
122
* Renders all map objects contained in this map to an image.
127
public Image render(int imageWidth) {
108
public BufferedImage render(int imageWidth) {
128
109
MapContent map = new MapContent();
130
111
// Convert map objects to features, and add them to the map
131
for (InternalMapObject mapObject : mapObjects) {
112
for (GeoToolsMapObject mapObject : mapObjects) {
133
map.addLayer(createFeatureLayer((GeoToolsMapObject) mapObject));
114
map.addLayer(createFeatureLayerFromMapObject(mapObject));
134
115
} catch (SchemaException ex) {
135
116
System.err.println("Could not add map object: " + mapObject.toString() + ": " + ex.getMessage());
146
127
double imageHeightFactor = mapBounds.getSpan(1) / mapBounds.getSpan(0);
147
128
Rectangle imageBounds = new Rectangle(0, 0, imageWidth, (int) Math.ceil(imageWidth * imageHeightFactor));
149
Image image = new BufferedImage(
150
imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB);
130
// Create an image and get the graphics context from it
131
BufferedImage image = new BufferedImage(
132
imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_ARGB);
151
133
Graphics2D g = (Graphics2D) image.getGraphics();
153
g.setColor(background);
135
// Draw a background if the background color is specified
136
// NOTE It will be transparent otherwise, which is desired
137
if (backgroundColor != null) {
138
g.setColor(backgroundColor);
142
// Enable anti-aliasing if specified
156
143
if (isAntiAliasingEnabled) {
157
144
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
158
145
RenderingHints.VALUE_ANTIALIAS_ON);
161
148
RenderingHints.VALUE_ANTIALIAS_OFF);
164
152
renderer.paint(g, imageBounds, mapBounds);
157
// -------------------------------------------------------------------------
159
// -------------------------------------------------------------------------
170
162
* Creates a feature layer based on a map object.
175
private Layer createFeatureLayer(GeoToolsMapObject mapObject) throws SchemaException {
164
private Layer createFeatureLayerFromMapObject(GeoToolsMapObject mapObject) throws SchemaException {
176
165
SimpleFeatureType featureType;
177
166
SimpleFeatureBuilder featureBuilder;
178
167
SimpleFeature feature;
227
212
"geometries", "geometry:" + type + ":srid=3785");
216
* Creates an image with text indicating an error.
230
218
@SuppressWarnings("unused")
231
private Image createErrorImage(String error) {
219
private BufferedImage createErrorImage(String error) {
232
220
String str = "Error creating map image: " + error;
233
221
BufferedImage image = new BufferedImage(500, 25, BufferedImage.TYPE_INT_RGB);
234
222
Graphics2D g = (Graphics2D) image.createGraphics();