Mercurial > mxd2map
comparison src/java/de/intevation/mxd/writer/MapScriptWriter.java @ 259:8fe9ccc77962
Introduced a MapScript helper class and improved metadata handling.
author | raimund renkert <raimund.renkert@intevation.de> |
---|---|
date | Tue, 16 Aug 2011 13:09:57 +0200 |
parents | 4bae15d560d3 |
children | efab4d62ad3c |
comparison
equal
deleted
inserted
replaced
258:4bae15d560d3 | 259:8fe9ccc77962 |
---|---|
44 import edu.umn.gis.mapscript.MS_CONNECTION_TYPE; | 44 import edu.umn.gis.mapscript.MS_CONNECTION_TYPE; |
45 import edu.umn.gis.mapscript.MS_FONT_TYPE; | 45 import edu.umn.gis.mapscript.MS_FONT_TYPE; |
46 import edu.umn.gis.mapscript.MS_POSITIONS_ENUM; | 46 import edu.umn.gis.mapscript.MS_POSITIONS_ENUM; |
47 | 47 |
48 import de.intevation.mxd.utils.XMLUtils; | 48 import de.intevation.mxd.utils.XMLUtils; |
49 import de.intevation.mxd.utils.MapScriptUtils; | |
49 | 50 |
50 /** | 51 /** |
51 * The Mapfile Writer. | 52 * The Mapfile Writer. |
52 * This Writer uses the MapScript Java API to create Mapfiles from a DOM. | 53 * This Writer uses the MapScript Java API to create Mapfiles from a DOM. |
53 * | 54 * |
64 /** | 65 /** |
65 * Private member. | 66 * Private member. |
66 */ | 67 */ |
67 private Document root; | 68 private Document root; |
68 private mapObj map; | 69 private mapObj map; |
70 private MapScriptUtils msutils; | |
69 private String mapFilename; | 71 private String mapFilename; |
70 private String MS_BINDIR = "c:/ms_6.1-dev/bin"; | 72 private String MS_BINDIR = "c:/ms_6.1-dev/bin"; |
71 private String prefix = ""; | 73 private String prefix = ""; |
72 private MS_UNITS units = MS_UNITS.MS_METERS; | 74 private MS_UNITS units = MS_UNITS.MS_METERS; |
73 | 75 |
75 * Default constructor. | 77 * Default constructor. |
76 * Creates a mapscript writer object with an empty map. | 78 * Creates a mapscript writer object with an empty map. |
77 */ | 79 */ |
78 public MapScriptWriter() { | 80 public MapScriptWriter() { |
79 map = new mapObj(""); | 81 map = new mapObj(""); |
82 msutils = new MapScriptUtils(); | |
80 mapFilename = ""; | 83 mapFilename = ""; |
81 } | 84 } |
82 | 85 |
83 /** | 86 /** |
84 * Contructor with template and output filename. | 87 * Contructor with template and output filename. |
89 * @param filename Output file name. | 92 * @param filename Output file name. |
90 */ | 93 */ |
91 public MapScriptWriter(String templ, String filename) { | 94 public MapScriptWriter(String templ, String filename) { |
92 String path = System.getProperty("user.dir"); | 95 String path = System.getProperty("user.dir"); |
93 map = new mapObj(templ); | 96 map = new mapObj(templ); |
97 msutils = new MapScriptUtils(); | |
94 File f = new File(filename); | 98 File f = new File(filename); |
95 mapFilename = filename; | 99 mapFilename = filename; |
96 if(f.isAbsolute()) { | 100 if(f.isAbsolute()) { |
97 map.setMappath(mapFilename); | 101 map.setMappath(mapFilename); |
98 } | 102 } |
204 else { | 208 else { |
205 units = MS_UNITS.MS_METERS; | 209 units = MS_UNITS.MS_METERS; |
206 } | 210 } |
207 map.setUnits(units); | 211 map.setUnits(units); |
208 | 212 |
209 String srs = map.getMetaData("ows_srs"); | |
210 String mproj = mapNode.getAttribute("projection"); | 213 String mproj = mapNode.getAttribute("projection"); |
211 if(srs.indexOf(mproj) < 0) { | 214 if(mproj != null && !mproj.equals("") && ! mproj.equals("0")) { |
212 srs += " EPSG:" + mproj; | 215 MapScriptUtils msu = new MapScriptUtils(); |
213 map.setMetaData("ows_srs", srs); | 216 String wmssrs = msu.getMetaData(map, "wms_srs"); |
217 String owssrs = msu.getMetaData(map, "ows_srs"); | |
218 if(wmssrs.indexOf(mproj) < 0) { | |
219 if(wmssrs.equals("")) { | |
220 wmssrs = "EPSG:"; | |
221 } | |
222 else { | |
223 wmssrs += " EPSG:"; | |
224 } | |
225 wmssrs += mproj; | |
226 } | |
227 if(owssrs.indexOf(mproj) < 0) { | |
228 if(owssrs.equals("")) { | |
229 owssrs = "EPSG:"; | |
230 } | |
231 else { | |
232 owssrs += " EPSG:"; | |
233 } | |
234 owssrs += mproj; | |
235 } | |
236 map.setMetaData("ows_srs", owssrs); | |
237 map.setMetaData("wms_srs", wmssrs); | |
214 } | 238 } |
215 } | 239 } |
216 | 240 |
217 /** | 241 /** |
218 * Create layer objects and set the attributes. | 242 * Create layer objects and set the attributes. |
242 group = layerElement.getAttribute("group"); | 266 group = layerElement.getAttribute("group"); |
243 String[] splitted = group.split("/"); | 267 String[] splitted = group.split("/"); |
244 String cleangroup = ""; | 268 String cleangroup = ""; |
245 for(int j = 0; j < splitted.length; j++) { | 269 for(int j = 0; j < splitted.length; j++) { |
246 if(!splitted[j].equals("")) { | 270 if(!splitted[j].equals("")) { |
247 splitted[j] = validateString(splitted[j]); | 271 splitted[j] = msutils.replaceUmlauts(splitted[j]); |
248 cleangroup += "/" + validateLayerName(splitted[j]); | 272 cleangroup += "/" + |
273 msutils.removeSpecialChars(splitted[j]); | |
249 } | 274 } |
250 } | 275 } |
251 group = cleangroup; | 276 group = cleangroup; |
252 } | 277 } |
253 String lname = layerElement.getAttribute("name"); | 278 String lname = layerElement.getAttribute("name"); |
254 lname = validateLayerName(lname); | 279 lname = msutils.removeSpecialChars(lname); |
255 String ulname = group.replaceAll("/", ".") + "." + lname; | 280 String ulname = group.replaceAll("/", ".") + "." + lname; |
256 if(ulname.startsWith(".")) { | 281 if(ulname.startsWith(".")) { |
257 ulname = ulname.substring(1); | 282 ulname = ulname.substring(1); |
258 } | 283 } |
259 layer.setName(ulname); | 284 layer.setName(ulname); |
261 if(!group.equals("")) { | 286 if(!group.equals("")) { |
262 layer.setMetaData("wms_layer_group", group); | 287 layer.setMetaData("wms_layer_group", group); |
263 } | 288 } |
264 layer.setMetaData("wms_title", ulname); | 289 layer.setMetaData("wms_title", ulname); |
265 | 290 |
291 // Projection metadata. | |
292 String mproj = mapNode.getAttribute("projection"); | |
293 if(mproj != null && !mproj.equals("") && !mproj.equals("0")) { | |
294 String wmssrs = layer.getMetaData("wms_srs"); | |
295 String owssrs = layer.getMetaData("ows_srs"); | |
296 if(wmssrs == null) { | |
297 wmssrs = "EPSG:"; | |
298 } | |
299 else { | |
300 wmssrs += " EPSG:"; | |
301 } | |
302 if(owssrs == null) { | |
303 owssrs = "EPSG:"; | |
304 } | |
305 else { | |
306 owssrs += " EPSG:"; | |
307 } | |
308 wmssrs += mproj; | |
309 owssrs += mproj; | |
310 layer.setMetaData("wms_srs", wmssrs); | |
311 layer.setMetaData("ows_srs", owssrs); | |
312 } | |
313 | |
314 // The layer extent metadata. | |
266 if(layerElement.hasAttribute("extent_min_x") && | 315 if(layerElement.hasAttribute("extent_min_x") && |
267 layerElement.hasAttribute("extent_max_x") && | 316 layerElement.hasAttribute("extent_max_x") && |
268 layerElement.hasAttribute("extent_min_y") && | 317 layerElement.hasAttribute("extent_min_y") && |
269 layerElement.hasAttribute("extent_max_y")) { | 318 layerElement.hasAttribute("extent_max_y")) { |
270 layer.setMetaData( | 319 layer.setMetaData( |
473 //each class. | 522 //each class. |
474 for(int i = 0; i < list.getLength(); i++) { | 523 for(int i = 0; i < list.getLength(); i++) { |
475 Element classElement = (Element)list.item(i); | 524 Element classElement = (Element)list.item(i); |
476 classObj co = new classObj(layer); | 525 classObj co = new classObj(layer); |
477 String name = classElement.getAttribute("label"); | 526 String name = classElement.getAttribute("label"); |
478 name = validateString(name); | 527 name = msutils.replaceUmlauts(name); |
479 if (name.equals("")) { | 528 if (name.equals("")) { |
480 name = layerElement.getAttribute("name"); | 529 name = layerElement.getAttribute("name"); |
481 name = validateString(name); | 530 name = msutils.replaceUmlauts(name); |
482 if (list.getLength() > 1) { | 531 if (list.getLength() > 1) { |
483 name += "-" + i; | 532 name += "-" + i; |
484 } | 533 } |
485 } | 534 } |
486 co.setName (name); | 535 co.setName (name); |
701 } | 750 } |
702 } | 751 } |
703 } | 752 } |
704 return expression; | 753 return expression; |
705 } | 754 } |
706 | |
707 /** | |
708 * Replaces german umlauts and removes leading and trailing whitespaces. | |
709 * | |
710 * @param s String | |
711 */ | |
712 private String validateString (String s) { | |
713 if (s.equals("")) { | |
714 return ""; | |
715 } | |
716 String tmp = s.trim(); | |
717 tmp = tmp.replace ("ö", "oe"); | |
718 tmp = tmp.replace ("ä", "ae"); | |
719 tmp = tmp.replace ("ü", "ue"); | |
720 tmp = tmp.replace ("ß", "ss"); | |
721 if(!s.equals(tmp)) { | |
722 logger.info("Renamed \"" + s + "\" to \"" + tmp + "\"."); | |
723 } | |
724 return tmp; | |
725 } | |
726 | |
727 | |
728 /** | |
729 * Replaces special characters and removes leading and trailing whitespaces. | |
730 * | |
731 * @param s String | |
732 * | |
733 * @return the input string without special chars. | |
734 */ | |
735 private String validateLayerName (String s) { | |
736 if(s.equals("")) { | |
737 return ""; | |
738 } | |
739 | |
740 String tmp = s.trim(); | |
741 tmp = tmp.replace(" ", ""); | |
742 tmp = tmp.replace("/", ""); | |
743 tmp = tmp.replace(",", ""); | |
744 tmp = tmp.replace("#", ""); | |
745 tmp = tmp.replace("+", ""); | |
746 tmp = tmp.replace("~", ""); | |
747 tmp = tmp.replace("(", ""); | |
748 tmp = tmp.replace(")", ""); | |
749 tmp = tmp.replace("{", ""); | |
750 tmp = tmp.replace("}", ""); | |
751 tmp = tmp.replace("[", ""); | |
752 tmp = tmp.replace("]", ""); | |
753 tmp = tmp.replace("<", ""); | |
754 tmp = tmp.replace(">", ""); | |
755 tmp = tmp.replace("*", ""); | |
756 tmp = tmp.replace("\\\\", ""); | |
757 tmp = tmp.replace("^", ""); | |
758 if(!s.equals(tmp)) { | |
759 logger.info("Renamed \"" + s +"\" to \"" + tmp +"\"."); | |
760 } | |
761 return tmp; | |
762 } | |
763 } | 755 } |