]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Format/ArcXML.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Format / ArcXML.js
1 /* Copyright (c) 2009 MetaCarta, Inc., published under the Clear BSD
2  * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3  * full text of the license. */
4
5 /**
6  * @requires OpenLayers/Format/XML.js
7  * @requires OpenLayers/Geometry/Polygon.js
8  * @requires OpenLayers/Geometry/Point.js
9  * @requires OpenLayers/Geometry/MultiPolygon.js
10  * @requires OpenLayers/Geometry/LinearRing.js
11  */
12
13 /**
14  * Class: OpenLayers.Format.ArcXML
15  * Read/Wite ArcXML. Create a new instance with the <OpenLayers.Format.ArcXML>
16  *     constructor.
17  * 
18  * Inherits from:
19  *  - <OpenLayers.Format>
20  */
21 OpenLayers.Format.ArcXML = OpenLayers.Class(OpenLayers.Format.XML, {
22
23     /**
24      * Property: fontStyleKeys
25      * {Array} List of keys used in font styling.
26      */
27     fontStyleKeys: [
28         'antialiasing', 'blockout', 'font', 'fontcolor','fontsize', 'fontstyle',
29         'glowing', 'interval', 'outline', 'printmode', 'shadow', 'transparency'
30     ],
31
32     /**
33      * Property: request
34      * A get_image request destined for an ArcIMS server.
35      */
36     request: null,
37     
38     /**
39      * Property: response
40      * A parsed response from an ArcIMS server.
41      */
42     response: null,
43
44     /**
45      * Constructor: OpenLayers.Format.ArcXML
46      * Create a new parser/writer for ArcXML.  Create an instance of this class
47      *    to begin authoring a request to an ArcIMS service.  This is used
48      *    primarily by the ArcIMS layer, but could be used to do other wild
49      *    stuff, like geocoding.
50      *
51      * Parameters:
52      * options - {Object} An optional object whose properties will be set on
53      *     this instance.
54      */
55     initialize: function(options) {
56         this.request = new OpenLayers.Format.ArcXML.Request();
57         this.response = new OpenLayers.Format.ArcXML.Response();
58
59         if (options) {
60             if (options.requesttype == "feature") {
61                 this.request.get_image = null;
62             
63                 var qry = this.request.get_feature.query;
64                 this.addCoordSys(qry.featurecoordsys, options.featureCoordSys);
65                 this.addCoordSys(qry.filtercoordsys, options.filterCoordSys);
66             
67                 if (options.polygon) {
68                     qry.isspatial = true;
69                     qry.spatialfilter.polygon = options.polygon;
70                 } else if (options.envelope) {
71                     qry.isspatial = true;
72                     qry.spatialfilter.envelope = {minx:0, miny:0, maxx:0, maxy:0};
73                     this.parseEnvelope(qry.spatialfilter.envelope, options.envelope);
74                 }
75             } else if (options.requesttype == "image") {
76                 this.request.get_feature = null;
77             
78                 var props = this.request.get_image.properties;
79                 this.parseEnvelope(props.envelope, options.envelope);
80             
81                 this.addLayers(props.layerlist, options.layers);
82                 this.addImageSize(props.imagesize, options.tileSize);
83                 this.addCoordSys(props.featurecoordsys, options.featureCoordSys);
84                 this.addCoordSys(props.filtercoordsys, options.filterCoordSys);
85             } else {
86                 // if an arcxml object is being created with no request type, it is
87                 // probably going to consume a response, so do not throw an error if
88                 // the requesttype is not defined
89                 this.request = null;
90             }
91         }
92         
93         OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
94     },
95     
96     /**
97      * Method: parseEnvelope
98      * Parse an array of coordinates into an ArcXML envelope structure.
99      *
100      * Parameters:
101      * env - {Object} An envelope object that will contain the parsed coordinates.
102      * arr - {Array(double)} An array of coordinates in the order: [ minx, miny, maxx, maxy ]
103      */
104     parseEnvelope: function(env, arr) {
105         if (arr && arr.length == 4) {          
106             env.minx = arr[0];
107             env.miny = arr[1];
108             env.maxx = arr[2];
109             env.maxy = arr[3];
110         }
111     },
112     
113     /** 
114      * Method: addLayers
115      * Add a collection of layers to another collection of layers. Each layer in the list is tuple of
116      * { id, visible }.  These layer collections represent the 
117      * /ARCXML/REQUEST/get_image/PROPERTIES/LAYERLIST/LAYERDEF items in ArcXML
118      *
119      * TODO: Add support for dynamic layer rendering.
120      *
121      * Parameters:
122      * ll - {Array({id,visible})} A list of layer definitions.
123      * lyrs - {Array({id,visible})} A list of layer definitions.
124      */
125     addLayers: function(ll, lyrs) {
126         for(var lind = 0, len=lyrs.length; lind < len; lind++) {
127             ll.push(lyrs[lind]);
128         }
129     },
130     
131     /**
132      * Method: addImageSize
133      * Set the size of the requested image.
134      *
135      * Parameters:
136      * imsize - {Object} An ArcXML imagesize object.
137      * olsize - {OpenLayers.Size} The image size to set.
138      */
139     addImageSize: function(imsize, olsize) {
140         if (olsize !== null) {
141             imsize.width = olsize.w;
142             imsize.height = olsize.h;
143             imsize.printwidth = olsize.w;
144             imsize.printheight = olsize.h;
145         }
146     },
147
148     /**
149      * Method: addCoordSys
150      * Add the coordinate system information to an object. The object may be 
151      *
152      * Parameters:
153      * featOrFilt - {Object} A featurecoordsys or filtercoordsys ArcXML structure.
154      * fsys - {String} or {OpenLayers.Projection} or {filtercoordsys} or 
155      * {featurecoordsys} A projection representation. If it's a {String}, 
156      * the value is assumed to be the SRID.  If it's a {OpenLayers.Projection} 
157      * AND Proj4js is available, the projection number and name are extracted 
158      * from there.  If it's a filter or feature ArcXML structure, it is copied.
159      */
160     addCoordSys: function(featOrFilt, fsys) {
161         if (typeof fsys == "string") {
162             featOrFilt.id = parseInt(fsys);
163             featOrFilt.string = fsys;
164         }
165         // is this a proj4js instance?
166         else if (typeof fsys == "object" && fsys.proj !== null){
167             featOrFilt.id = fsys.proj.srsProjNumber;
168             featOrFilt.string = fsys.proj.srsCode;
169         } else {
170             featOrFilt = fsys;
171         }
172     },
173
174     /**
175      * APIMethod: iserror
176      * Check to see if the response from the server was an error.
177      *
178      * Parameters:
179      * data - {String} or {DOMElement} data to read/parse. If nothing is supplied,
180      * the current response is examined.
181      *
182      * Returns:
183      * {Boolean} true if the response was an error.
184      */
185     iserror: function(data) {
186         var ret = null; 
187         
188         if (!data) {
189             ret = (this.response.error !== '');
190         } else {
191             data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
192             var errorNodes = data.documentElement.getElementsByTagName("ERROR");
193             ret = (errorNodes !== null && errorNodes.length > 0);
194         }
195
196         return ret;
197     },
198
199     /**
200      * APIMethod: read
201      * Read data from a string, and return an response. 
202      * 
203      * Parameters:
204      * data - {String} or {DOMElement} data to read/parse.
205      *
206      * Returns:
207      * {OpenLayers.Format.ArcXML.Response} An ArcXML response. Note that this response
208      *     data may change in the future. 
209      */
210     read: function(data) {
211         if(typeof data == "string") {
212             data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
213         }
214         
215         var arcNode = null;
216         if (data && data.documentElement) {
217             if(data.documentElement.nodeName == "ARCXML") {
218                 arcNode = data.documentElement;
219             } else {
220                 arcNode = data.documentElement.getElementsByTagName("ARCXML")[0];
221             }
222         }
223           
224         if (!arcNode) {
225             var error, source;
226             try {
227                 error = data.firstChild.nodeValue;
228                 source = data.firstChild.childNodes[1].firstChild.nodeValue;
229             } catch (err) {
230                 // pass
231             }
232             throw {
233                 message: "Error parsing the ArcXML request", 
234                 error: error,
235                 source: source
236             };
237         }
238         
239         var response = this.parseResponse(arcNode);
240         return response;
241     },
242     
243     /**
244      * APIMethod: write
245      * Generate an ArcXml document string for sending to an ArcIMS server. 
246      * 
247      * Returns:
248      * {String} A string representing the ArcXML document request.
249      */
250     write: function(request) {       
251         if (!request) {
252             request = this.request;
253         }    
254         var root = this.createElementNS("", "ARCXML");
255         root.setAttribute("version","1.1");
256
257         var reqElem = this.createElementNS("", "REQUEST");
258         
259         if (request.get_image != null) {
260             var getElem = this.createElementNS("", "GET_IMAGE");
261             reqElem.appendChild(getElem);
262
263             var propElem = this.createElementNS("", "PROPERTIES");
264             getElem.appendChild(propElem);
265
266             var props = request.get_image.properties;
267             if (props.featurecoordsys != null) {
268                 var feat = this.createElementNS("", "FEATURECOORDSYS");
269                 propElem.appendChild(feat);
270                 
271                 if (props.featurecoordsys.id === 0) {
272                     feat.setAttribute("string", props.featurecoordsys['string']);
273                 }
274                 else {
275                     feat.setAttribute("id", props.featurecoordsys.id);
276                 }
277             }
278           
279             if (props.filtercoordsys != null) {
280                 var filt = this.createElementNS("", "FILTERCOORDSYS");
281                 propElem.appendChild(filt);
282
283                 if (props.filtercoordsys.id === 0) {
284                     filt.setAttribute("string", props.filtercoordsys.string);
285                 }
286                 else {
287                     filt.setAttribute("id", props.filtercoordsys.id);
288                 }
289             }
290           
291             if (props.envelope != null) {
292                 var env = this.createElementNS("", "ENVELOPE");
293                 propElem.appendChild(env);
294
295                 env.setAttribute("minx", props.envelope.minx);
296                 env.setAttribute("miny", props.envelope.miny);
297                 env.setAttribute("maxx", props.envelope.maxx);
298                 env.setAttribute("maxy", props.envelope.maxy);
299             }        
300           
301             var imagesz = this.createElementNS("", "IMAGESIZE");
302             propElem.appendChild(imagesz);
303           
304             imagesz.setAttribute("height", props.imagesize.height);
305             imagesz.setAttribute("width", props.imagesize.width);
306           
307             if (props.imagesize.height != props.imagesize.printheight ||
308                  props.imagesize.width != props.imagesize.printwidth) {
309                 imagesz.setAttribute("printheight", props.imagesize.printheight);
310                 imagesz.setArrtibute("printwidth", props.imagesize.printwidth);
311             }
312           
313             if (props.background != null) {
314                 var backgrnd = this.createElementNS("", "BACKGROUND");
315                 propElem.appendChild(backgrnd);
316             
317                 backgrnd.setAttribute("color", 
318                     props.background.color.r + "," + 
319                     props.background.color.g + "," + 
320                     props.background.color.b);
321               
322                 if (props.background.transcolor !== null) {
323                     backgrnd.setAttribute("transcolor", 
324                         props.background.transcolor.r + "," + 
325                         props.background.transcolor.g + "," + 
326                         props.background.transcolor.b);
327                 }
328             }
329           
330             if (props.layerlist != null && props.layerlist.length > 0) {
331                 var layerlst = this.createElementNS("", "LAYERLIST");
332                 propElem.appendChild(layerlst);
333             
334                 for (var ld = 0; ld < props.layerlist.length; ld++) {
335                     var ldef = this.createElementNS("", "LAYERDEF");
336                     layerlst.appendChild(ldef);
337               
338                     ldef.setAttribute("id", props.layerlist[ld].id);
339                     ldef.setAttribute("visible", props.layerlist[ld].visible);
340               
341                     if (typeof props.layerlist[ld].query == "object") {
342                         var query = props.layerlist[ld].query;
343
344                         if (query.where.length < 0) {
345                             continue;
346                         }
347                   
348                         var queryElem = null;
349                         if (typeof query.spatialfilter == "boolean" && query.spatialfilter) {
350                             // handle spatial filter madness
351                             queryElem = this.createElementNS("", "SPATIALQUERY");
352                         }
353                         else {
354                             queryElem = this.createElementNS("", "QUERY");
355                         }
356                 
357                         queryElem.setAttribute("where", query.where);
358                 
359                         if (typeof query.accuracy == "number" && query.accuracy > 0) {
360                             queryElem.setAttribute("accuracy", query.accuracy);
361                         }
362                         if (typeof query.featurelimit == "number" && query.featurelimit < 2000) {
363                             queryElem.setAttribute("featurelimit", query.featurelimit);
364                         }
365                         if (typeof query.subfields == "string" && query.subfields != "#ALL#") {
366                             queryElem.setAttribute("subfields", query.subfields);
367                         }
368                         if (typeof query.joinexpression == "string" && query.joinexpression.length > 0) {
369                             queryElem.setAttribute("joinexpression", query.joinexpression);
370                         }
371                         if (typeof query.jointables == "string" && query.jointables.length > 0) {
372                             queryElem.setAttribute("jointables", query.jointables);
373                         }
374
375                         ldef.appendChild(queryElem);
376                     }
377               
378                     if (typeof props.layerlist[ld].renderer == "object") {
379                         this.addRenderer(ldef, props.layerlist[ld].renderer);                  
380                     }
381                 }
382             }
383         } else if (request.get_feature != null) {
384             var getElem = this.createElementNS("", "GET_FEATURES");
385             getElem.setAttribute("outputmode", "newxml");
386             getElem.setAttribute("checkesc", "true");
387           
388             if (request.get_feature.geometry) {
389                 getElem.setAttribute("geometry", request.get_feature.geometry);
390             }
391             else {
392                 getElem.setAttribute("geometry", "false");
393             }
394           
395             if (request.get_feature.compact) {
396                 getElem.setAttribute("compact", request.get_feature.compact);
397             }
398           
399             if (request.get_feature.featurelimit == "number") {
400                 getElem.setAttribute("featurelimit", request.get_feature.featurelimit);
401             }
402           
403             getElem.setAttribute("globalenvelope", "true");
404             reqElem.appendChild(getElem);
405           
406             if (request.get_feature.layer != null && request.get_feature.layer.length > 0) {
407                 var lyrElem = this.createElementNS("", "LAYER");
408                 lyrElem.setAttribute("id", request.get_feature.layer);
409                 getElem.appendChild(lyrElem);
410             }
411           
412             var fquery = request.get_feature.query;
413             if (fquery != null) {
414                 var qElem = null;
415                 if (fquery.isspatial) {
416                     qElem = this.createElementNS("", "SPATIALQUERY");
417                 } else {
418                     qElem = this.createElementNS("", "QUERY");
419                 }
420                 getElem.appendChild(qElem);
421                 
422                 if (typeof fquery.accuracy == "number") {
423                     qElem.setAttribute("accuracy", fquery.accuracy);
424                 }
425                 //qElem.setAttribute("featurelimit", "5");
426             
427                 if (fquery.featurecoordsys != null) {
428                     var fcsElem1 = this.createElementNS("", "FEATURECOORDSYS");
429               
430                     if (fquery.featurecoordsys.id == 0) {
431                         fcsElem1.setAttribute("string", fquery.featurecoordsys.string);
432                     } else {
433                         fcsElem1.setAttribute("id", fquery.featurecoordsys.id);
434                     }
435                     qElem.appendChild(fcsElem1);
436                 }
437             
438                 if (fquery.filtercoordsys != null) {
439                     var fcsElem2 = this.createElementNS("", "FILTERCOORDSYS");
440               
441                     if (fquery.filtercoordsys.id === 0) {
442                         fcsElem2.setAttribute("string", fquery.filtercoordsys.string);
443                     } else {
444                         fcsElem2.setAttribute("id", fquery.filtercoordsys.id);
445                     }
446                     qElem.appendChild(fcsElem2);
447                 }
448             
449                 if (fquery.buffer > 0) {   
450                     var bufElem = this.createElementNS("", "BUFFER");
451                     bufElem.setAttribute("distance", fquery.buffer);
452                     qElem.appendChild(bufElem);
453                 }
454             
455                 if (fquery.isspatial) {
456                     var spfElem = this.createElementNS("", "SPATIALFILTER");
457                     spfElem.setAttribute("relation", fquery.spatialfilter.relation);
458                     qElem.appendChild(spfElem);
459               
460                     if (fquery.spatialfilter.envelope) {
461                         var envElem = this.createElementNS("", "ENVELOPE"); 
462                         envElem.setAttribute("minx", fquery.spatialfilter.envelope.minx);
463                         envElem.setAttribute("miny", fquery.spatialfilter.envelope.miny);
464                         envElem.setAttribute("maxx", fquery.spatialfilter.envelope.maxx);
465                         envElem.setAttribute("maxy", fquery.spatialfilter.envelope.maxy);
466                         spfElem.appendChild(envElem);
467                     } else if(typeof fquery.spatialfilter.polygon == "object") {
468                         spfElem.appendChild(this.writePolygonGeometry(fquery.spatialfilter.polygon));                
469                     }
470                 }
471             
472                 if (fquery.where != null && fquery.where.length > 0) {
473                     qElem.setAttribute("where", fquery.where);
474                 }
475             }
476         }
477
478         root.appendChild(reqElem);
479
480         return OpenLayers.Format.XML.prototype.write.apply(this, [root]);
481     },
482     
483     
484     addGroupRenderer: function(ldef, toprenderer) {
485         var topRelem = this.createElementNS("", "GROUPRENDERER");
486         ldef.appendChild(topRelem);
487       
488         for (var rind = 0; rind < toprenderer.length; rind++) {
489             var renderer = toprenderer[rind];
490             this.addRenderer(topRelem, renderer);
491         }
492     },
493     
494     
495     addRenderer: function(topRelem, renderer) {
496         if (renderer instanceof Array) {
497             this.addGroupRenderer(topRelem, renderer);
498         } else {
499             var renderElem = this.createElementNS("", renderer.type.toUpperCase() + "RENDERER");
500             topRelem.appendChild(renderElem);
501           
502             if (renderElem.tagName == "VALUEMAPRENDERER") {
503                 this.addValueMapRenderer(renderElem, renderer);
504             } else if (renderElem.tagName == "VALUEMAPLABELRENDERER") {
505                 this.addValueMapLabelRenderer(renderElem, renderer);
506             } else if (renderElem.tagName == "SIMPLELABELRENDERER") {
507                 this.addSimpleLabelRenderer(renderElem, renderer);
508             } else if (renderElem.tagName == "SCALEDEPENDENTRENDERER") {
509                 this.addScaleDependentRenderer(renderElem, renderer);
510             }
511         }             
512     },
513     
514     
515     addScaleDependentRenderer: function(renderElem, renderer) {
516         if (typeof renderer.lower == "string" || typeof renderer.lower == "number") {
517             renderElem.setAttribute("lower", renderer.lower);
518         }
519         if (typeof renderer.upper == "string" || typeof renderer.upper == "number") {
520             renderElem.setAttribute("upper", renderer.upper);
521         }
522         
523         this.addRenderer(renderElem, renderer.renderer);
524     },
525     
526     
527     addValueMapLabelRenderer: function(renderElem, renderer) {
528         renderElem.setAttribute("lookupfield", renderer.lookupfield);
529         renderElem.setAttribute("labelfield", renderer.labelfield);
530       
531         if (typeof renderer.exacts == "object") {
532             for (var ext=0, extlen=renderer.exacts.length; ext<extlen; ext++) {
533                 var exact = renderer.exacts[ext];
534           
535                 var eelem = this.createElementNS("", "EXACT");
536           
537                 if (typeof exact.value == "string") {
538                     eelem.setAttribute("value", exact.value);
539                 }
540                 if (typeof exact.label == "string") {
541                     eelem.setAttribute("label", exact.label);
542                 }
543                 if (typeof exact.method == "string") {
544                     eelem.setAttribute("method", exact.method);
545                 }
546
547                 renderElem.appendChild(eelem);
548             
549                 if (typeof exact.symbol == "object") {
550                     var selem = null;
551                 
552                     if (exact.symbol.type == "text") {
553                         selem = this.createElementNS("", "TEXTSYMBOL");
554                     }
555                 
556                     if (selem != null) {
557                         var keys = this.fontStyleKeys;
558                         for (var i = 0, len = keys.length; i < len; i++) {
559                             var key = keys[i];
560                             if (exact.symbol[key]) {
561                                 selem.setAttribute(key, exact.symbol[key]);
562                             }
563                         }    
564                         eelem.appendChild(selem);
565                     }
566                 }
567             } // for each exact
568         }      
569     },
570     
571     addValueMapRenderer: function(renderElem, renderer) {
572         renderElem.setAttribute("lookupfield", renderer.lookupfield);
573         
574         if (typeof renderer.ranges == "object") {
575             for(var rng=0, rnglen=renderer.ranges.length; rng<rnglen; rng++) {
576                 var range = renderer.ranges[rng];
577                 
578                 var relem = this.createElementNS("", "RANGE");
579                 relem.setAttribute("lower", range.lower);
580                 relem.setAttribute("upper", range.upper);
581                 
582                 renderElem.appendChild(relem);
583                 
584                 if (typeof range.symbol == "object") {
585                     var selem = null;
586               
587                     if (range.symbol.type == "simplepolygon") {
588                         selem = this.createElementNS("", "SIMPLEPOLYGONSYMBOL");
589                     }
590               
591                     if (selem != null) {
592                         if (typeof range.symbol.boundarycolor == "string") {
593                             selem.setAttribute("boundarycolor", range.symbol.boundarycolor);
594                         }
595                         if (typeof range.symbol.fillcolor == "string") {
596                             selem.setAttribute("fillcolor", range.symbol.fillcolor);
597                         }
598                         if (typeof range.symbol.filltransparency == "number") {
599                             selem.setAttribute("filltransparency", range.symbol.filltransparency);
600                         }
601                         relem.appendChild(selem);
602                     }   
603                 }
604             } // for each range
605         } else if (typeof renderer.exacts == "object") {
606             for (var ext=0, extlen=renderer.exacts.length; ext<extlen; ext++) {
607                 var exact = renderer.exacts[ext];
608           
609                 var eelem = this.createElementNS("", "EXACT");
610                 if (typeof exact.value == "string") {
611                     eelem.setAttribute("value", exact.value);
612                 }
613                 if (typeof exact.label == "string") {
614                     eelem.setAttribute("label", exact.label);
615                 }
616                 if (typeof exact.method == "string") {
617                     eelem.setAttribute("method", exact.method);
618                 }
619             
620                 renderElem.appendChild(eelem);
621             
622                 if (typeof exact.symbol == "object") {
623                     var selem = null;
624             
625                     if (exact.symbol.type == "simplemarker") {
626                         selem = this.createElementNS("", "SIMPLEMARKERSYMBOL");
627                     }
628             
629                     if (selem != null) {
630                         if (typeof exact.symbol.antialiasing == "string") {
631                             selem.setAttribute("antialiasing", exact.symbol.antialiasing);
632                         }
633                         if (typeof exact.symbol.color == "string") {
634                             selem.setAttribute("color", exact.symbol.color);
635                         }
636                         if (typeof exact.symbol.outline == "string") {
637                             selem.setAttribute("outline", exact.symbol.outline);
638                         }
639                         if (typeof exact.symbol.overlap == "string") {
640                             selem.setAttribute("overlap", exact.symbol.overlap);
641                         }
642                         if (typeof exact.symbol.shadow == "string") {
643                             selem.setAttribute("shadow", exact.symbol.shadow);
644                         }
645                         if (typeof exact.symbol.transparency == "number") {
646                             selem.setAttribute("transparency", exact.symbol.transparency);
647                         }
648                         //if (typeof exact.symbol.type == "string")
649                         //    selem.setAttribute("type", exact.symbol.type);
650                         if (typeof exact.symbol.usecentroid == "string") {
651                             selem.setAttribute("usecentroid", exact.symbol.usecentroid);
652                         }
653                         if (typeof exact.symbol.width == "number") {
654                             selem.setAttribute("width", exact.symbol.width);
655                         }
656                 
657                         eelem.appendChild(selem);
658                     }
659                 }
660             } // for each exact
661         }
662     },
663     
664     
665     addSimpleLabelRenderer: function(renderElem, renderer) {
666         renderElem.setAttribute("field", renderer.field);
667         var keys = ['featureweight', 'howmanylabels', 'labelbufferratio', 
668                     'labelpriorities', 'labelweight', 'linelabelposition',
669                     'rotationalangles'];
670         for (var i=0, len=keys.length; i<len; i++) {
671             var key = keys[i];
672             if (renderer[key]) {
673                 renderElem.setAttribute(key, renderer[key]);
674             }
675         }     
676            
677         if (renderer.symbol.type == "text") {
678             var symbol = renderer.symbol;
679             var selem = this.createElementNS("", "TEXTSYMBOL");
680             renderElem.appendChild(selem);
681           
682             var keys = this.fontStyleKeys;
683             for (var i=0, len=keys.length; i<len; i++) {
684                 var key = keys[i];
685                 if (symbol[key]) {
686                     selem.setAttribute(key, renderer[key]);
687                 }
688             }    
689         }    
690     },
691     
692     writePolygonGeometry: function(polygon) {
693         if (!(polygon instanceof OpenLayers.Geometry.Polygon)) {
694             throw { 
695                 message:'Cannot write polygon geometry to ArcXML with an ' +
696                     polygon.CLASS_NAME + ' object.',
697                 geometry: polygon
698             };
699         }
700         
701         var polyElem = this.createElementNS("", "POLYGON");
702       
703         for (var ln=0, lnlen=polygon.components.length; ln<lnlen; ln++) {
704             var ring = polygon.components[ln];
705             var ringElem = this.createElementNS("", "RING");
706         
707             for (var rn=0, rnlen=ring.components.length; rn<rnlen; rn++) {
708                 var point = ring.components[rn];
709                 var pointElem = this.createElementNS("", "POINT");
710             
711                 pointElem.setAttribute("x", point.x);
712                 pointElem.setAttribute("y", point.y);
713             
714                 ringElem.appendChild(pointElem);
715             }
716         
717             polyElem.appendChild(ringElem);
718         }
719       
720         return polyElem;
721     },
722     
723     /**
724      * Method: parseResponse
725      * Take an ArcXML response, and parse in into this object's internal properties.
726      *
727      * Parameters:
728      * data - {String} or {DOMElement} The ArcXML response, as either a string or the
729      * top level DOMElement of the response.
730      */
731     parseResponse: function(data) {
732         if(typeof data == "string") { 
733             var newData = new OpenLayers.Format.XML();
734             data = newData.read(data);
735         }
736         var response = new OpenLayers.Format.ArcXML.Response();
737         
738         var errorNode = data.getElementsByTagName("ERROR");
739         
740         if (errorNode != null && errorNode.length > 0) {
741             response.error = this.getChildValue(errorNode, "Unknown error.");
742         } else {
743             var responseNode = data.getElementsByTagName("RESPONSE");
744           
745             if (responseNode == null || responseNode.length == 0) {
746                 response.error = "No RESPONSE tag found in ArcXML response.";
747                 return response;
748             }
749           
750             var rtype = responseNode[0].firstChild.nodeName;
751             if (rtype == "#text") {
752                 rtype = responseNode[0].firstChild.nextSibling.nodeName;
753             }
754           
755             if (rtype == "IMAGE") {
756                 var envelopeNode = data.getElementsByTagName("ENVELOPE");
757                 var outputNode = data.getElementsByTagName("OUTPUT");
758                 
759                 if (envelopeNode == null || envelopeNode.length == 0) {
760                     response.error = "No ENVELOPE tag found in ArcXML response.";
761                 } else if (outputNode == null || outputNode.length == 0) {
762                     response.error = "No OUTPUT tag found in ArcXML response.";
763                 } else {
764                     var envAttr = this.parseAttributes(envelopeNode[0]);            
765                     var outputAttr = this.parseAttributes(outputNode[0]);
766                   
767                     if (typeof outputAttr.type == "string") {
768                         response.image = { 
769                             envelope: envAttr, 
770                             output: { 
771                                 type: outputAttr.type, 
772                                 data: this.getChildValue(outputNode[0])
773                             }
774                         };
775                     } else {
776                         response.image = { envelope: envAttr, output: outputAttr };
777                     }
778                 }
779             } else if (rtype == "FEATURES") {
780                 var features = responseNode[0].getElementsByTagName("FEATURES");
781             
782                 // get the feature count
783                 var featureCount = features[0].getElementsByTagName("FEATURECOUNT");
784                 response.features.featurecount = featureCount[0].getAttribute("count");
785             
786                 if (response.features.featurecount > 0) {
787                     // get the feature envelope
788                     var envelope = features[0].getElementsByTagName("ENVELOPE");
789                     response.features.envelope = this.parseAttributes(envelope[0], typeof(0));
790
791                     // get the field values per feature
792                     var featureList = features[0].getElementsByTagName("FEATURE");
793                     for (var fn = 0; fn < featureList.length; fn++) {
794                         var feature = new OpenLayers.Feature.Vector();
795                         var fields = featureList[fn].getElementsByTagName("FIELD");
796
797                         for (var fdn = 0; fdn < fields.length; fdn++) {
798                             var fieldName = fields[fdn].getAttribute("name");
799                             var fieldValue = fields[fdn].getAttribute("value");
800                             feature.attributes[ fieldName ] = fieldValue;
801                         }
802
803                         var geom = featureList[fn].getElementsByTagName("POLYGON");
804
805                         if (geom.length > 0) {
806                             // if there is a polygon, create an openlayers polygon, and assign
807                             // it to the .geometry property of the feature
808                             var ring = geom[0].getElementsByTagName("RING");
809
810                             var polys = [];
811                             for (var rn = 0; rn < ring.length; rn++) {
812                                 var linearRings = [];
813                                 linearRings.push(this.parsePointGeometry(ring[rn]));
814
815                                 var holes = ring[rn].getElementsByTagName("HOLE");
816                                 for (var hn = 0; hn < holes.length; hn++) {
817                                     linearRings.push(this.parsePointGeometry(holes[hn]));
818                                 }
819                                 holes = null;
820                                 polys.push(new OpenLayers.Geometry.Polygon(linearRings));
821                                 linearRings = null;
822                             }
823                             ring = null;
824                           
825                             if (polys.length == 1) {
826                                 feature.geometry = polys[0];
827                             } else
828                             {
829                                 feature.geometry = new OpenLayers.Geometry.MultiPolygon(polys);
830                             }
831                         }
832
833                         response.features.feature.push(feature);
834                     }
835                 }
836             } else {
837                 response.error = "Unidentified response type.";
838             }
839         }
840         return response;
841     },
842     
843     
844     /**
845      * Method: parseAttributes
846      *
847      * Parameters:
848      * node - {<DOMElement>} An element to parse attributes from.
849      *
850      * Returns:
851      * {Object} An attributes object, with properties set to attribute values.
852      */
853     parseAttributes: function(node,type) {
854         var attributes = {};
855         for(var attr = 0; attr < node.attributes.length; attr++) {
856             if (type == "number") {
857                 attributes[node.attributes[attr].nodeName] = parseFloat(node.attributes[attr].nodeValue);
858             } else {
859                 attributes[node.attributes[attr].nodeName] = node.attributes[attr].nodeValue;
860             }
861         }
862         return attributes;
863     },
864     
865     
866     /**
867      * Method: parsePointGeometry
868      *
869      * Parameters:
870      * node - {<DOMElement>} An element to parse <COORDS> or <POINT> arcxml data from.
871      *
872      * Returns:
873      * {OpenLayers.Geometry.LinearRing} A linear ring represented by the node's points.
874      */
875     parsePointGeometry: function(node) {
876         var ringPoints = [];
877         var coords = node.getElementsByTagName("COORDS");
878
879         if (coords.length > 0) {
880             // if coords is present, it's the only coords item
881             var coordArr = this.getChildValue(coords[0]);
882             coordArr = coordArr.split(/;/);
883             for (var cn = 0; cn < coordArr.length; cn++) {
884                 var coordItems = coordArr[cn].split(/ /);
885                 ringPoints.push(new OpenLayers.Geometry.Point(parseFloat(coordItems[0]), parseFloat(coordItems[1])));
886             }
887             coords = null;
888         } else {
889             var point = node.getElementsByTagName("POINT");
890             if (point.length > 0) {
891                 for (var pn = 0; pn < point.length; pn++) {
892                     ringPoints.push(
893                         new OpenLayers.Geometry.Point(
894                             parseFloat(point[pn].getAttribute("x")),
895                             parseFloat(point[pn].getAttribute("y"))
896                         )
897                     );
898                 }
899             }
900             point = null;
901         }
902
903         return new OpenLayers.Geometry.LinearRing(ringPoints);      
904     },
905     
906     CLASS_NAME: "OpenLayers.Format.ArcXML" 
907 });
908
909 OpenLayers.Format.ArcXML.Request = OpenLayers.Class({
910     initialize: function(params) {
911         var defaults = {
912             get_image: {
913                 properties: {
914                     background: null,
915                     /*{ 
916                         color: { r:255, g:255, b:255 },
917                         transcolor: null
918                     },*/
919                     draw: true,
920                     envelope: {
921                         minx: 0, 
922                         miny: 0, 
923                         maxx: 0, 
924                         maxy: 0
925                     },
926                     featurecoordsys: { 
927                         id:0, 
928                         string:"",
929                         datumtransformid:0,
930                         datumtransformstring:""
931                     },
932                     filtercoordsys:{
933                         id:0,
934                         string:"",
935                         datumtransformid:0,
936                         datumtransformstring:""
937                     },
938                     imagesize:{
939                         height:0,
940                         width:0,
941                         dpi:96,
942                         printheight:0,
943                         printwidth:0,
944                         scalesymbols:false
945                     },
946                     layerlist:[],
947                     /* no support for legends */
948                     output:{
949                         baseurl:"",
950                         legendbaseurl:"",
951                         legendname:"",
952                         legendpath:"",
953                         legendurl:"",
954                         name:"",
955                         path:"",
956                         type:"jpg",
957                         url:""
958                     }
959                 }
960             },
961
962             get_feature: {
963                 layer: "",
964                 query: {
965                     isspatial: false,
966                     featurecoordsys: {
967                         id:0,
968                         string:"",
969                         datumtransformid:0,
970                         datumtransformstring:""
971                     },
972                     filtercoordsys: {
973                         id:0,
974                         string:"",
975                         datumtransformid:0,
976                         datumtransformstring:""
977                     },
978                     buffer:0,
979                     where:"",
980                     spatialfilter: {
981                         relation: "envelope_intersection",
982                         envelope: null
983                     }
984                 }
985             },
986       
987             environment: {
988                 separators: {
989                     cs:" ",
990                     ts:";"
991                 }
992             },
993       
994             layer: [],
995             workspaces: []
996         };
997       
998         return OpenLayers.Util.extend(this, defaults);      
999     },
1000   
1001     CLASS_NAME: "OpenLayers.Format.ArcXML.Request"
1002 });
1003
1004 OpenLayers.Format.ArcXML.Response = OpenLayers.Class({  
1005     initialize: function(params) {
1006         var defaults = {
1007             image: {
1008                 envelope:null,
1009                 output:''
1010             },
1011       
1012             features: {
1013                 featurecount: 0,
1014                 envelope: null,
1015                 feature: []
1016             },
1017       
1018             error:''
1019         };
1020   
1021         return OpenLayers.Util.extend(this, defaults);
1022     },
1023   
1024     CLASS_NAME: "OpenLayers.Format.ArcXML.Response"
1025 });