comparison src/java/de/intevation/mxd/writer/MapScriptWriter.java @ 298:1cbc278c956b

Whitespace Only: unify to 4 Space indention. So that i can avoid whitespace problems while working on this file.
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 04 Sep 2012 17:49:17 +0200
parents ad05f72ef9e4
children c4fb04f6422f
comparison
equal deleted inserted replaced
297:ad05f72ef9e4 298:1cbc278c956b
77 * Default constructor. 77 * Default constructor.
78 * Creates a mapscript writer object with an empty map. 78 * Creates a mapscript writer object with an empty map.
79 */ 79 */
80 public MapScriptWriter() { 80 public MapScriptWriter() {
81 map = new mapObj(""); 81 map = new mapObj("");
82 msutils = new MapScriptUtils(); 82 msutils = new MapScriptUtils();
83 mapFilename = ""; 83 mapFilename = "";
84 } 84 }
85 85
86 /** 86 /**
87 * Contructor with template and output filename. 87 * Contructor with template and output filename.
92 * @param filename Output file name. 92 * @param filename Output file name.
93 */ 93 */
94 public MapScriptWriter(String templ, String filename) { 94 public MapScriptWriter(String templ, String filename) {
95 String path = System.getProperty("user.dir"); 95 String path = System.getProperty("user.dir");
96 map = new mapObj(templ); 96 map = new mapObj(templ);
97 msutils = new MapScriptUtils(); 97 msutils = new MapScriptUtils();
98 File f = new File(filename); 98 File f = new File(filename);
99 mapFilename = filename; 99 mapFilename = filename;
100 if(f.isAbsolute()) { 100 if(f.isAbsolute()) {
101 map.setMappath(mapFilename); 101 map.setMappath(mapFilename);
102 } 102 }
132 "/mxd/file", 132 "/mxd/file",
133 XPathConstants.NODE); 133 XPathConstants.NODE);
134 //Write the map attributes. 134 //Write the map attributes.
135 writeMap(); 135 writeMap();
136 //Write the layers. 136 //Write the layers.
137 boolean ret = writeLayer(); 137 boolean ret = writeLayer();
138 //Save the map. 138 //Save the map.
139 if (ret) { 139 if (ret) {
140 mapObj cloneMap = map.cloneMap(); 140 mapObj cloneMap = map.cloneMap();
141 cloneMap.save(mapFilename); 141 cloneMap.save(mapFilename);
142 logger.info("Mapfile created: " + mapFilename); 142 logger.info("Mapfile created: " + mapFilename);
143 return true; 143 return true;
144 } 144 }
145 return false; 145 return false;
146 } 146 }
147 147
148 /** 148 /**
149 * Create the map object and set the attributes. 149 * Create the map object and set the attributes.
150 */ 150 */
166 Double.parseDouble(mapNode.getAttribute("extent_min_x")), 166 Double.parseDouble(mapNode.getAttribute("extent_min_x")),
167 Double.parseDouble(mapNode.getAttribute("extent_min_y")), 167 Double.parseDouble(mapNode.getAttribute("extent_min_y")),
168 Double.parseDouble(mapNode.getAttribute("extent_max_x")), 168 Double.parseDouble(mapNode.getAttribute("extent_max_x")),
169 Double.parseDouble(mapNode.getAttribute("extent_max_y"))); 169 Double.parseDouble(mapNode.getAttribute("extent_max_y")));
170 170
171 map.setMetaData( 171 map.setMetaData(
172 "ows_extent", 172 "ows_extent",
173 mapNode.getAttribute("extent_min_x") + ", " + 173 mapNode.getAttribute("extent_min_x") + ", " +
174 mapNode.getAttribute("extent_min_y") + ", " + 174 mapNode.getAttribute("extent_min_y") + ", " +
175 mapNode.getAttribute("extent_max_x") + ", " + 175 mapNode.getAttribute("extent_max_x") + ", " +
176 mapNode.getAttribute("extent_max_y")); 176 mapNode.getAttribute("extent_max_y"));
177 177
178 map.setSize(800, 600); 178 map.setSize(800, 600);
179 179
180 //Set the units and projection. 180 //Set the units and projection.
181 int proj = Integer.parseInt(mapNode.getAttribute("projection")); 181 int proj = Integer.parseInt(mapNode.getAttribute("projection"));
182 if(proj != 0) { 182 if(proj != 0) {
183 try { 183 try {
217 else { 217 else {
218 units = MS_UNITS.MS_METERS; 218 units = MS_UNITS.MS_METERS;
219 } 219 }
220 map.setUnits(units); 220 map.setUnits(units);
221 221
222 String mproj = mapNode.getAttribute("projection"); 222 String mproj = mapNode.getAttribute("projection");
223 if(mproj != null && !mproj.equals("") && ! mproj.equals("0")) { 223 if(mproj != null && !mproj.equals("") && ! mproj.equals("0")) {
224 MapScriptUtils msu = new MapScriptUtils(); 224 MapScriptUtils msu = new MapScriptUtils();
225 String wmssrs = msu.getMetaData(map, "wms_srs"); 225 String wmssrs = msu.getMetaData(map, "wms_srs");
226 String owssrs = msu.getMetaData(map, "ows_srs"); 226 String owssrs = msu.getMetaData(map, "ows_srs");
227 if(wmssrs.indexOf(mproj) < 0) { 227 if(wmssrs.indexOf(mproj) < 0) {
228 if(wmssrs.equals("")) { 228 if(wmssrs.equals("")) {
229 wmssrs = "EPSG:"; 229 wmssrs = "EPSG:";
230 } 230 }
231 else { 231 else {
232 wmssrs += " EPSG:"; 232 wmssrs += " EPSG:";
233 } 233 }
234 wmssrs += mproj; 234 wmssrs += mproj;
235 } 235 }
236 if(owssrs.indexOf(mproj) < 0) { 236 if(owssrs.indexOf(mproj) < 0) {
237 if(owssrs.equals("")) { 237 if(owssrs.equals("")) {
238 owssrs = "EPSG:"; 238 owssrs = "EPSG:";
239 } 239 }
240 else { 240 else {
241 owssrs += " EPSG:"; 241 owssrs += " EPSG:";
242 } 242 }
243 owssrs += mproj; 243 owssrs += mproj;
244 } 244 }
245 map.setMetaData("ows_srs", owssrs); 245 map.setMetaData("ows_srs", owssrs);
246 map.setMetaData("wms_srs", wmssrs); 246 map.setMetaData("wms_srs", wmssrs);
247 } 247 }
248 } 248 }
249 249
250 /** 250 /**
251 * Create layer objects and set the attributes. 251 * Create layer objects and set the attributes.
252 */ 252 */
259 NodeList list = mapNode.getElementsByTagName("layer"); 259 NodeList list = mapNode.getElementsByTagName("layer");
260 for(int i = 0; i < list.getLength(); i++) { 260 for(int i = 0; i < list.getLength(); i++) {
261 Element layerElement = (Element)list.item(i); 261 Element layerElement = (Element)list.item(i);
262 layerObj layer = new layerObj(map); 262 layerObj layer = new layerObj(map);
263 NodeList renderer = layerElement.getElementsByTagName("renderer"); 263 NodeList renderer = layerElement.getElementsByTagName("renderer");
264 if (renderer.getLength() == 0) { 264 if (renderer.getLength() == 0) {
265 map.removeLayer(layer.getIndex()); 265 map.removeLayer(layer.getIndex());
266 logger.warn( 266 logger.warn(
267 "Skipping empty layer " + 267 "Skipping empty layer " +
268 layerElement.getAttribute("name")); 268 layerElement.getAttribute("name"));
269 continue; 269 continue;
270 } 270 }
271 271
272 //The layer name. 272 //The layer name.
273 String group = ""; 273 String group = "";
274 if(layerElement.hasAttribute("group")) { 274 if(layerElement.hasAttribute("group")) {
275 group = layerElement.getAttribute("group"); 275 group = layerElement.getAttribute("group");
276 String[] splitted = group.split("/"); 276 String[] splitted = group.split("/");
277 String cleangroup = ""; 277 String cleangroup = "";
278 for(int j = 0; j < splitted.length; j++) { 278 for(int j = 0; j < splitted.length; j++) {
279 if(!splitted[j].equals("")) { 279 if(!splitted[j].equals("")) {
280 splitted[j] = msutils.replaceUmlauts(splitted[j]); 280 splitted[j] = msutils.replaceUmlauts(splitted[j]);
281 cleangroup += "/" + 281 cleangroup += "/" +
282 msutils.removeSpecialChars(splitted[j]); 282 msutils.removeSpecialChars(splitted[j]);
283 } 283 }
284 } 284 }
285 group = cleangroup; 285 group = cleangroup;
286 } 286 }
287 String lname = layerElement.getAttribute("name"); 287 String lname = layerElement.getAttribute("name");
288 lname = msutils.removeSpecialChars(lname); 288 lname = msutils.removeSpecialChars(lname);
289 lname = msutils.replaceUmlauts(lname); 289 lname = msutils.replaceUmlauts(lname);
290 String ulname = group.replaceAll("/", ".") + "." + lname; 290 String ulname = group.replaceAll("/", ".") + "." + lname;
291 if(ulname.startsWith(".")) { 291 if(ulname.startsWith(".")) {
292 ulname = ulname.substring(1); 292 ulname = ulname.substring(1);
293 } 293 }
294 layer.setName(ulname); 294 layer.setName(ulname);
295 layer.setUnits(units.swigValue()); 295 layer.setUnits(units.swigValue());
296 if(!group.equals("")) { 296 if(!group.equals("")) {
297 layer.setMetaData("wms_layer_group", group); 297 layer.setMetaData("wms_layer_group", group);
298 } 298 }
299 layer.setMetaData("wms_title", ulname); 299 layer.setMetaData("wms_title", ulname);
300 300
301 // Projection metadata. 301 // Projection metadata.
302 String mproj = mapNode.getAttribute("projection"); 302 String mproj = mapNode.getAttribute("projection");
303 if(mproj != null && !mproj.equals("") && !mproj.equals("0")) { 303 if(mproj != null && !mproj.equals("") && !mproj.equals("0")) {
304 String wmssrs = layer.getMetaData("wms_srs"); 304 String wmssrs = layer.getMetaData("wms_srs");
305 String owssrs = layer.getMetaData("ows_srs"); 305 String owssrs = layer.getMetaData("ows_srs");
306 if(wmssrs == null) { 306 if(wmssrs == null) {
307 wmssrs = "EPSG:"; 307 wmssrs = "EPSG:";
308 } 308 }
309 else { 309 else {
310 wmssrs += " EPSG:"; 310 wmssrs += " EPSG:";
311 } 311 }
312 if(owssrs == null) { 312 if(owssrs == null) {
313 owssrs = "EPSG:"; 313 owssrs = "EPSG:";
314 } 314 }
315 else { 315 else {
316 owssrs += " EPSG:"; 316 owssrs += " EPSG:";
317 } 317 }
318 wmssrs += mproj; 318 wmssrs += mproj;
319 owssrs += mproj; 319 owssrs += mproj;
320 layer.setMetaData("wms_srs", wmssrs); 320 layer.setMetaData("wms_srs", wmssrs);
321 layer.setMetaData("ows_srs", owssrs); 321 layer.setMetaData("ows_srs", owssrs);
322 } 322 }
323 323
324 // The layer extent metadata. 324 // The layer extent metadata.
325 if(layerElement.hasAttribute("extent_min_x") && 325 if(layerElement.hasAttribute("extent_min_x") &&
326 layerElement.hasAttribute("extent_max_x") && 326 layerElement.hasAttribute("extent_max_x") &&
327 layerElement.hasAttribute("extent_min_y") && 327 layerElement.hasAttribute("extent_min_y") &&
328 layerElement.hasAttribute("extent_max_y")) { 328 layerElement.hasAttribute("extent_max_y")) {
329 layer.setMetaData( 329 layer.setMetaData(
330 "ows_extent", 330 "ows_extent",
331 layerElement.getAttribute("extent_min_x") + ", " + 331 layerElement.getAttribute("extent_min_x") + ", " +
332 layerElement.getAttribute("extent_min_y") + ", " + 332 layerElement.getAttribute("extent_min_y") + ", " +
333 layerElement.getAttribute("extent_max_x") + ", " + 333 layerElement.getAttribute("extent_max_x") + ", " +
334 layerElement.getAttribute("extent_max_y")); 334 layerElement.getAttribute("extent_max_y"));
335 } 335 }
336 //The layer status. 336 //The layer status.
337 String stat = layerElement.getAttribute("status"); 337 String stat = layerElement.getAttribute("status");
338 if (stat.equals("on")) { 338 if (stat.equals("on")) {
339 layer.setStatus(1); 339 layer.setStatus(1);
340 } 340 }
362 //The layer type. 362 //The layer type.
363 String type = layerElement.getAttribute("type"); 363 String type = layerElement.getAttribute("type");
364 if(type.equals("point")) { 364 if(type.equals("point")) {
365 layer.setType(MS_LAYER_TYPE.MS_LAYER_POINT); 365 layer.setType(MS_LAYER_TYPE.MS_LAYER_POINT);
366 layer.setTileitem(""); 366 layer.setTileitem("");
367 } 367 }
368 else if (type.equals("line")) { 368 else if (type.equals("line")) {
369 layer.setType(MS_LAYER_TYPE.MS_LAYER_LINE); 369 layer.setType(MS_LAYER_TYPE.MS_LAYER_LINE);
370 layer.setTileitem(""); 370 layer.setTileitem("");
371 } 371 }
372 else if (type.equals("polygon")) { 372 else if (type.equals("polygon")) {
373 layer.setType(MS_LAYER_TYPE.MS_LAYER_POLYGON); 373 layer.setType(MS_LAYER_TYPE.MS_LAYER_POLYGON);
374 layer.setTileitem(""); 374 layer.setTileitem("");
375 } 375 }
376 else if(type.equals("raster")) { 376 else if(type.equals("raster")) {
377 layer.setType(MS_LAYER_TYPE.MS_LAYER_RASTER); 377 layer.setType(MS_LAYER_TYPE.MS_LAYER_RASTER);
378 } 378 }
379 379
380 //The layer datasource. 380 //The layer datasource.
381 String con_type = layerElement.getAttribute("connection_type"); 381 String con_type = layerElement.getAttribute("connection_type");
382 if(con_type.equals("local")) { 382 if(con_type.equals("local")) {
383 // The data source is a local file. (.shp) 383 // The data source is a local file. (.shp)
396 "SDE datasource found." + 396 "SDE datasource found." +
397 " Please edit password in mapfile."); 397 " Please edit password in mapfile.");
398 layer.setConnectionType( 398 layer.setConnectionType(
399 MS_CONNECTION_TYPE.MS_PLUGIN.swigValue(), 399 MS_CONNECTION_TYPE.MS_PLUGIN.swigValue(),
400 MS_BINDIR + "/ms/plugins/msplugin_sde_93.dll"); 400 MS_BINDIR + "/ms/plugins/msplugin_sde_93.dll");
401 String database_auth = layerElement.getAttribute("database"); 401 String database_auth = layerElement.getAttribute("database");
402 if (database_auth.equals("") || database_auth == null) { 402 if (database_auth.equals("") || database_auth == null) {
403 logger.warn("Could not set the database connection user." + 403 logger.warn("Could not set the database connection user." +
404 " Setting to \"sde\" as default."); 404 " Setting to \"sde\" as default.");
405 database_auth = "sde"; 405 database_auth = "sde";
406 } 406 }
407 407
408 String port = layerElement.getAttribute("instance"); 408 String port = layerElement.getAttribute("instance");
409 StringBuffer strBuff = new StringBuffer(); 409 StringBuffer strBuff = new StringBuffer();
410 char c; 410 char c;
411 411
412 for (int j = 0; j < port.length() ; j++) { 412 for (int j = 0; j < port.length() ; j++) {
413 c = port.charAt(j); 413 c = port.charAt(j);
414 414
415 if(Character.isDigit(c)) { 415 if(Character.isDigit(c)) {
416 strBuff.append(c); 416 strBuff.append(c);
417 } 417 }
418 } 418 }
419 419
420 layer.setConnection( 420 layer.setConnection(
421 layerElement.getAttribute("server") + "," + 421 layerElement.getAttribute("server") + "," +
422 "port:" + strBuff.toString() + "," + 422 "port:" + strBuff.toString() + "," +
423 database_auth + "," + 423 database_auth + "," +
424 layerElement.getAttribute("user") + ",<PASSWORD>"); 424 layerElement.getAttribute("user") + ",<PASSWORD>");
427 ",SHAPE," + 427 ",SHAPE," +
428 layerElement.getAttribute("version")); 428 layerElement.getAttribute("version"));
429 layer.setProcessing("CLOSE_CONNECTION=defer"); 429 layer.setProcessing("CLOSE_CONNECTION=defer");
430 layer.setProcessing("ATTRIBUTE_QUALIFIED=TRUE"); 430 layer.setProcessing("ATTRIBUTE_QUALIFIED=TRUE");
431 if(layerElement.hasAttribute("join_table")) { 431 if(layerElement.hasAttribute("join_table")) {
432 logger.info( 432 logger.info(
433 "The layer " + 433 "The layer " +
434 layer.getName() + 434 layer.getName() +
435 " contains a jointable attribute." + 435 " contains a jointable attribute." +
436 " Please verify the attribute parameters."); 436 " Please verify the attribute parameters.");
437 String join = layerElement.getAttribute("join_table"); 437 String join = layerElement.getAttribute("join_table");
438 438
439 layer.setProcessing( 439 layer.setProcessing(
440 "JOINTABLE=" + 440 "JOINTABLE=" +
441 layerElement.getAttribute("join_table")); 441 layerElement.getAttribute("join_table"));
442 layer.setFilter( 442 layer.setFilter(
443 "where " + 443 "where " +
456 layer.setFilter("where " + 456 layer.setFilter("where " +
457 layerElement.getAttribute("definition_query")); 457 layerElement.getAttribute("definition_query"));
458 } 458 }
459 } 459 }
460 } 460 }
461 else if(con_type.equals("SDE") && type.equals("raster")) { 461 else if(con_type.equals("SDE") && type.equals("raster")) {
462 // The data source is a sde database. 462 // The data source is a sde database.
463 logger.info( 463 logger.info(
464 "SDE datasource found." + 464 "SDE datasource found." +
465 " Please edit password in mapfile."); 465 " Please edit password in mapfile.");
466 String database_auth = layerElement.getAttribute("database"); 466 String database_auth = layerElement.getAttribute("database");
467 if (database_auth.equals("") || database_auth == null) { 467 if (database_auth.equals("") || database_auth == null) {
468 logger.warn("Could not set the database connection user." + 468 logger.warn("Could not set the database connection user." +
469 " Setting to \"sde\" as default."); 469 " Setting to \"sde\" as default.");
470 database_auth = "sde"; 470 database_auth = "sde";
471 } 471 }
472 472
473 String port = layerElement.getAttribute("instance"); 473 String port = layerElement.getAttribute("instance");
474 StringBuffer strBuff = new StringBuffer(); 474 StringBuffer strBuff = new StringBuffer();
475 char c; 475 char c;
476 476
477 for (int j = 0; j < port.length() ; j++) { 477 for (int j = 0; j < port.length() ; j++) {
478 c = port.charAt(j); 478 c = port.charAt(j);
479 if(Character.isDigit(c)) { 479 if(Character.isDigit(c)) {
480 strBuff.append(c); 480 strBuff.append(c);
481 } 481 }
482 } 482 }
483 483
484 layer.setData( 484 layer.setData(
485 "SDE:" + layerElement.getAttribute("server") + 485 "SDE:" + layerElement.getAttribute("server") +
486 "," + strBuff + 486 "," + strBuff +
487 "," + layerElement.getAttribute("database") + 487 "," + layerElement.getAttribute("database") +
488 "," + layerElement.getAttribute("user") + 488 "," + layerElement.getAttribute("user") +
489 ",<PASSWORD>," + layerElement.getAttribute("dataset")); 489 ",<PASSWORD>," + layerElement.getAttribute("dataset"));
490 layer.setType(MS_LAYER_TYPE.MS_LAYER_RASTER); 490 layer.setType(MS_LAYER_TYPE.MS_LAYER_RASTER);
491 if(layerElement.hasAttribute("definition_query") && 491 if(layerElement.hasAttribute("definition_query") &&
492 !layerElement.getAttribute("definition_query").equals("")) { 492 !layerElement.getAttribute("definition_query").equals("")) {
493 layer.setFilter("where " + 493 layer.setFilter("where " +
494 layerElement.getAttribute("definition_query")); 494 layerElement.getAttribute("definition_query"));
495 } 495 }
496 } 496 }
497 if(!type.equals("raster")) { 497 if(!type.equals("raster")) {
498 layer.setTemplate("PleaseInsertAValidTemplateForGFI"); 498 layer.setTemplate("PleaseInsertAValidTemplateForGFI");
499 } 499 }
500 500
501 // Create labelitem 501 // Create labelitem
502 NodeList labels = layerElement.getElementsByTagName("label"); 502 NodeList labels = layerElement.getElementsByTagName("label");
503 if(labels.getLength() > 0) { 503 if(labels.getLength() > 0) {
504 Element label = (Element)labels.item(0); 504 Element label = (Element)labels.item(0);
512 } 512 }
513 513
514 //Write classes. 514 //Write classes.
515 writeClass(layer, layerElement); 515 writeClass(layer, layerElement);
516 } 516 }
517 return true; 517 return true;
518 } 518 }
519 519
520 /** 520 /**
521 * Adds the classes to the layer. 521 * Adds the classes to the layer.
522 * @param layer Mapscript layer object. 522 * @param layer Mapscript layer object.
532 //each class. 532 //each class.
533 for(int i = 0; i < list.getLength(); i++) { 533 for(int i = 0; i < list.getLength(); i++) {
534 Element classElement = (Element)list.item(i); 534 Element classElement = (Element)list.item(i);
535 classObj co = new classObj(layer); 535 classObj co = new classObj(layer);
536 String name = classElement.getAttribute("label"); 536 String name = classElement.getAttribute("label");
537 name = msutils.replaceUmlauts(name); 537 name = msutils.replaceUmlauts(name);
538 if (name.equals("")) { 538 if (name.equals("")) {
539 name = layerElement.getAttribute("name"); 539 name = layerElement.getAttribute("name");
540 name = msutils.replaceUmlauts(name); 540 name = msutils.replaceUmlauts(name);
541 if (list.getLength() > 1) { 541 if (list.getLength() > 1) {
542 name += "-" + i; 542 name += "-" + i;
543 } 543 }
544 } 544 }
545 co.setName (name); 545 co.setName (name);
707 pre = prefix + "."; 707 pre = prefix + ".";
708 } 708 }
709 expression += "\"[" + pre; 709 expression += "\"[" + pre;
710 expression += ce.getAttribute("expression_field_0") + "]\""; 710 expression += ce.getAttribute("expression_field_0") + "]\"";
711 expression += " " + ce.getAttribute("expression_operator"); 711 expression += " " + ce.getAttribute("expression_operator");
712 expression += " \"" + ce.getAttribute("value") + "\")"; 712 expression += " \"" + ce.getAttribute("value") + "\")";
713 } 713 }
714 catch(Exception e) { 714 catch(Exception e) {
715 e.printStackTrace(); 715 e.printStackTrace();
716 } 716 }
717 } 717 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)