comparison src/java/de/intevation/mxd/reader/CompositeGraphicsLayerReader.java @ 315:1d77ea6a915d

Add basic support for Graphics Layers. For now this is restricted to simple TextElements and the base layer.
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 17 Sep 2012 17:08:15 +0200
parents
children 5ea3abee729d
comparison
equal deleted inserted replaced
314:cd3cb1a7f35a 315:1d77ea6a915d
1 /*
2 * Copyright (c) 2012 by Intevation GmbH, Germany <info@intevation.de>
3 *
4 * This file is part of MXD2map.
5 *
6 * This program is free software under the LGPL (>=v2.1)
7 * Read the file LICENCE.txt coming with the software for details
8 * or visit http://www.gnu.org/licenses/ if it does not exist.
9 *
10 * MXD2map has been developed on behalf of the
11 * Bundesamt fuer Seeschifffahrt und Hydrographie (BSH) in Hamburg
12 * by Intevation GmbH.
13 *
14 * Authors:
15 * Raimund Renkert <raimund.renkert@intevation.de>
16 * Bjoern Schilberg <bjoern.schilberg@intevation.de>
17 * Stephan Holl <stephan.holl@intevation.de>
18 * Andre Heinecke <aheinecke@intevation.de>
19 */
20
21 package de.intevation.mxd.reader;
22
23 import org.apache.log4j.Logger;
24
25 import com.esri.arcgis.carto.IGraphicsLayer;
26 import com.esri.arcgis.carto.CompositeGraphicsLayer;
27 import com.esri.arcgis.carto.AnnotateLayerPropertiesCollection;
28 import com.esri.arcgis.carto.IAnnotateLayerProperties;
29 import com.esri.arcgis.carto.LabelEngineLayerProperties;
30 import com.esri.arcgis.carto.IElement;
31 import com.esri.arcgis.carto.TextElement;
32 import com.esri.arcgis.system.IName;
33 import com.esri.arcgis.system.IPropertySet;
34 import com.esri.arcgis.geometry.Envelope;
35 import com.esri.arcgis.geometry.ISpatialReference;
36 import com.esri.arcgis.geometry.ProjectedCoordinateSystem;
37 import com.esri.arcgis.geometry.GeographicCoordinateSystem;
38 import com.esri.arcgis.geometry.UnknownCoordinateSystem;
39 import com.esri.arcgis.geometry.Projection;
40 import com.esri.arcgis.geometry.IPoint;
41
42 import com.esri.arcgis.display.ITextSymbol;
43 import com.esri.arcgis.display.TextSymbol;
44
45 import org.w3c.dom.Element;
46
47 import de.intevation.mxd.utils.MapToXMLUtils;
48 import java.io.IOException;
49 import com.esri.arcgis.interop.AutomationException;
50 /**
51 * Reads Layer information.
52 *
53 * @author <a href="mailto:aheinecke@intevation.de">Andre Heinecke</a>
54 */
55 public class CompositeGraphicsLayerReader
56 implements ILayerReader {
57
58 /**
59 * The logger.
60 */
61 private static final Logger logger =
62 Logger.getLogger(CompositeGraphicsLayerReader.class);
63
64 /**
65 * Privte member.
66 */
67 private CompositeGraphicsLayer layer;
68 private MapToXMLUtils util;
69
70 /**
71 * Constructor with layer.
72 *
73 * @param layer The ArcGIS layer object.
74 */
75 public CompositeGraphicsLayerReader(IGraphicsLayer layer)
76 throws Exception {
77 if(layer instanceof CompositeGraphicsLayer) {
78 this.layer = (CompositeGraphicsLayer)layer;
79 }
80 else {
81 throw new Exception("Not an instance of CompositeGraphicsLayer: " +
82 layer.getClass().toString());
83 }
84 }
85
86 /**
87 * Setter for XML document helper.
88 *
89 * @param util The helper for storing map information.
90 */
91 public void setUtil(MapToXMLUtils util) {
92 this.util = util;
93 }
94
95 /**
96 * Reads the Layer content.
97 *
98 * @return The layer XML element.
99 */
100 public Element read()
101 throws IOException{
102 logger.debug("read()");
103 Element layerElement = null;
104 try {
105 layerElement = util.addLayer();
106 }
107 catch(Exception e) {
108 logger.error("Failed to create DOM-Element for Layer.");
109 return null;
110 }
111
112 // Name
113 try {
114 layerElement.setAttribute("name", layer.getName());
115 logger.debug("Adding composite graphics layer: " + layer.getName());
116 logger.debug("Sublayers: " + layer.getCount());
117 }
118 catch(Exception e) {
119 logger.warn(
120 "Could not read layer name." +
121 " Stopped reading layer.");
122 throw new IOException("Error reading layer name.");
123 }
124
125 // Scale
126 try {
127 layerElement.setAttribute("min_scale",
128 String.valueOf(layer.getMinimumScale()));
129 }
130 catch(IOException ioe) {
131 logger.warn("Could not read minimum scale.");
132 }
133
134 try {
135 layerElement.setAttribute("max_scale",
136 String.valueOf(layer.getMaximumScale()));
137 }
138 catch(Exception e) {
139 logger.warn(
140 "Could not read maximum scale.");
141 }
142
143 // Status
144 try {
145 if(layer.isVisible()) {
146 layerElement.setAttribute("status", "on");
147 }
148 else {
149 layerElement.setAttribute("status", "off");
150 }
151 }
152 catch(Exception e) {
153 logger.warn(
154 "Could not read layer status." +
155 " Setting layer status to \"on\".");
156 layerElement.setAttribute("status", "on");
157 }
158
159 // Extent
160 try {
161 Envelope rect = (Envelope)layer.getExtent();
162 layerElement.setAttribute(
163 "extent_min_x",
164 String.valueOf(rect.getXMin ()));
165 layerElement.setAttribute(
166 "extent_max_x",
167 String.valueOf(rect.getXMax()));
168 layerElement.setAttribute(
169 "extent_min_y",
170 String.valueOf(rect.getYMin()));
171 layerElement.setAttribute(
172 "extent_max_y",
173 String.valueOf(rect.getYMax()));
174 }
175 catch(Exception e) {
176 logger.warn(
177 "Could not read extent from layer "
178 + layer.getName() + ".");
179 }
180
181 // Read the elements
182 try {
183 int count = 0;
184 IElement actElement = null;
185 IElement prevElement = null;
186 layer.reset(); // Reset the element container
187 actElement = layer.next();
188 while (actElement != prevElement) {
189 prevElement = actElement;
190 if (actElement instanceof TextElement) {
191 TextElement te = (TextElement)actElement;
192 Element xmlTextElement = util.addFeature(layerElement);
193 xmlTextElement.setAttribute("text", te.getText());
194
195 IPoint poi = te.getGeometry().getEnvelope().getLowerLeft();
196 xmlTextElement.setAttribute("X", String.valueOf(poi.getX()));
197 xmlTextElement.setAttribute("Y", String.valueOf(poi.getY()));
198
199 logger.debug("Text: " + te.getText());
200 logger.debug(" X,Y: " + poi.getX() + ", " + poi.getY());
201 try {
202 ITextSymbol sym = te.getSymbol();
203 if(sym instanceof TextSymbol) {
204 TextSymbolReader tsr = new TextSymbolReader(sym);
205 tsr.setParent(xmlTextElement);
206 tsr.setUtil(util);
207 tsr.read();
208 } else {
209 logger.warn("Unknwon Symbol of class: " +
210 sym.getClass().toString());
211 }
212 }
213 catch(Exception e) {
214 logger.warn("Could not read element text symbol.");
215 }
216 } else {
217 logger.warn("Unhandled Element of class: " +
218 actElement.getClass().toString() +
219 " in conversion of layer: " +
220 layer.getName());
221 }
222 count++;
223 }
224 logger.debug("Read " + count + " Elements");
225 }
226 catch(Exception e) {
227 logger.warn("Could not read layer elements.");
228 logger.debug(e);
229 }
230
231
232 //Read the projection.
233 try {
234 ISpatialReference sr = layer.getSpatialReference();
235 int projection = 0;
236 if(sr instanceof ProjectedCoordinateSystem) {
237 ProjectedCoordinateSystem pcs = (ProjectedCoordinateSystem)sr;
238 projection = pcs.getFactoryCode();
239 }
240 else if(sr instanceof GeographicCoordinateSystem) {
241 GeographicCoordinateSystem gcs = (GeographicCoordinateSystem)sr;
242 projection = gcs.getFactoryCode();
243 }
244 else if(sr instanceof UnknownCoordinateSystem) {
245 UnknownCoordinateSystem ucs = (UnknownCoordinateSystem)sr;
246 projection = 0;
247 }
248 else{
249 logger.debug(
250 "Unknown SpatialReference: " +
251 sr.getClass().toString());
252 }
253
254 if(projection == 0) {
255 logger.warn(
256 "Unknown projection for Layer:" + layer.getName() +
257 " Please edit projection in resulting mapfile.");
258 }
259 layerElement.setAttribute("projection", String.valueOf(projection));
260 }
261 catch(Exception e) {
262 logger.warn("Could not read layer projection.");
263 logger.debug(e);
264 }
265
266 // Static values for this layer
267 layerElement.setAttribute("type", "point");
268
269 return layerElement;
270 }
271 }
272 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)