~registry/dhis2-academy/map_generation_service-group_10

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/GeoToolsMap.java

  • Committer: Olai Solheim
  • Date: 2011-12-02 02:46:23 UTC
  • Revision ID: olai.solheim@gmail.com-20111202024623-xq9gjowdlicc996v
+ Added functionality to merge images in the service.
+ Changed where geometry is built -> now in GeoToolsMapObject where it belongs.
+ Smaller changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
import java.awt.Color;
4
4
import java.awt.Graphics2D;
5
 
import java.awt.Image;
6
5
import java.awt.Rectangle;
7
6
import java.awt.RenderingHints;
8
7
import java.awt.image.BufferedImage;
34
33
 * is transformed automatically to "EPSG 3785".
35
34
 * 
36
35
 * @author Kjetil Andresen <kjetand@ifi.uio.no>
 
36
 * @author Olai Solheim <olais@ifi.uio.no>
37
37
 */
38
38
public class GeoToolsMap extends InternalMap {
39
39
 
 
40
    // The flat list of map objects in this map.
 
41
    List<GeoToolsMapObject> mapObjects;
 
42
    
40
43
    /**
41
44
     * Creates an empty map.
42
45
     */
43
46
    public GeoToolsMap() {
44
 
        this.mapObjects = new LinkedList<InternalMapObject>();
 
47
        this.mapObjects = new LinkedList<GeoToolsMapObject>();
45
48
    }
46
49
 
47
50
    /**
48
51
     * Creates a map with the given initial map layer.
49
 
     * 
50
 
     * @param layer
 
52
     * @param layer the initial map layer
51
53
     */
52
54
    public GeoToolsMap(InternalMapLayer layer) {
53
 
        this.mapObjects = new LinkedList<InternalMapObject>();
 
55
        this.mapObjects = new LinkedList<GeoToolsMapObject>();
54
56
        this.addMapLayer(layer);
55
57
    }
56
58
 
57
59
    /**
58
60
     * Creates a map with the given initial map layers.
59
 
     * 
60
 
     * @param layers
 
61
     * @param layers the list of initial map layers
61
62
     */
62
63
    public GeoToolsMap(List<InternalMapLayer> layers) {
63
 
        this.mapObjects = new LinkedList<InternalMapObject>();
 
64
        this.mapObjects = new LinkedList<GeoToolsMapObject>();
64
65
        this.addMapLayers(layers);
65
66
    }
66
67
 
67
68
    /**
68
69
     * Adds a map object to this map.
69
 
     * 
70
 
     * @param mapObject
 
70
     * @param mapObject the map object
71
71
     */
72
 
    public void addMapObject(InternalMapObject mapObject) {
 
72
    public void addMapObject(GeoToolsMapObject mapObject) {
73
73
        this.mapObjects.add(mapObject);
74
74
    }
75
75
 
76
76
    /**
77
 
     * Adds all map objects contained in the collection.
78
 
     * 
79
 
     * @param mapObjects
 
77
     * Adds all map objects contained in the list.
 
78
     * @param mapObjects the list of map objects
80
79
     */
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);
84
83
        }
85
84
    }
86
85
 
87
 
    /**
88
 
     * Adds a map layer to this map.
89
 
     * 
90
 
     * @param layer
91
 
     */
 
86
    // -------------------------------------------------------------------------
 
87
    // InternalMap implementation
 
88
    // -------------------------------------------------------------------------
 
89
 
92
90
    public void addMapLayer(InternalMapLayer layer) {
93
91
        for (InternalMapObject mapObject : layer.getAllMapObjects()) {
94
92
            this.addMapObject((GeoToolsMapObject) mapObject);
95
93
        }
96
94
    }
97
95
 
98
 
    /**
99
 
     * Adds all map layers contained in the list.
100
 
     * 
101
 
     * @param layers
102
 
     */
103
96
    public void addMapLayers(List<InternalMapLayer> layers) {
104
97
        for (InternalMapLayer layer : layers) {
105
98
            for (InternalMapObject mapObject : layer.getAllMapObjects()) {
108
101
        }
109
102
    }
110
103
    
111
 
    /**
112
 
     * Renders all map objects contained in this map to an image 
113
 
     * with the default image width.
114
 
     * 
115
 
     * @return
116
 
     */
117
 
    public Image render() {
 
104
    public BufferedImage render() {
118
105
        return render(DEFAULT_IMAGEWIDTH);
119
106
    }
120
107
 
121
 
    /**
122
 
     * Renders all map objects contained in this map to an image.
123
 
     * 
124
 
     * @param width
125
 
     * @return
126
 
     */
127
 
    public Image render(int imageWidth) {
 
108
    public BufferedImage render(int imageWidth) {
128
109
        MapContent map = new MapContent();
129
110
 
130
111
        // Convert map objects to features, and add them to the map
131
 
        for (InternalMapObject mapObject : mapObjects) {
 
112
        for (GeoToolsMapObject mapObject : mapObjects) {
132
113
            try {
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());
136
117
            }
146
127
        double imageHeightFactor = mapBounds.getSpan(1) / mapBounds.getSpan(0);
147
128
        Rectangle imageBounds = new Rectangle(0, 0, imageWidth, (int) Math.ceil(imageWidth * imageHeightFactor));
148
129
 
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();
152
134
 
153
 
        g.setColor(background);
154
 
        g.fill(imageBounds);
 
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);
 
139
            g.fill(imageBounds);
 
140
        }
155
141
 
 
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);
162
149
        }
163
150
 
 
151
        // Render the map
164
152
        renderer.paint(g, imageBounds, mapBounds);
165
153
 
166
154
        return image;
167
155
    }
168
156
 
169
 
    /**
 
157
    // -------------------------------------------------------------------------
 
158
    // Internal
 
159
    // -------------------------------------------------------------------------
 
160
 
 
161
    /*
170
162
     * Creates a feature layer based on a map object.
171
 
     * 
172
 
     * @param mapObject
173
 
     * @return
174
163
     */
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;
203
192
        return new FeatureLayer(featureCollection, style);
204
193
    }
205
194
 
206
 
    /**
 
195
    /*
207
196
     * Creates a feature type for a GeoTools geometric primitive.
208
 
     * 
209
 
     * @param geom
210
 
     * @return
211
 
     * @throws SchemaException
212
197
     */
213
198
    private SimpleFeatureType createFeatureType(Object geom) throws SchemaException {
214
199
        String type = "";
227
212
                "geometries", "geometry:" + type + ":srid=3785");
228
213
    }
229
214
    
 
215
    /*
 
216
     * Creates an image with text indicating an error. 
 
217
     */
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();