source: trunk/zoo-api/js/ZOO-api.js @ 86

Last change on this file since 86 was 86, checked in by reluc, 13 years ago

Correct a bug

File size: 184.7 KB
Line 
1/**
2 * Author : René-Luc D'Hont
3 *
4 * Copyright 2010 3liz SARL. All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25/**
26 * Class: ZOO
27 */
28ZOO = {
29  /**
30   * Constant: SERVICE_ACCEPTED
31   * {Integer} used for
32   */
33  SERVICE_ACCEPTED: 0,
34  /**
35   * Constant: SERVICE_STARTED
36   * {Integer} used for
37   */
38  SERVICE_STARTED: 1,
39  /**
40   * Constant: SERVICE_PAUSED
41   * {Integer} used for
42   */
43  SERVICE_PAUSED: 2,
44  /**
45   * Constant: SERVICE_SUCCEEDED
46   * {Integer} used for
47   */
48  SERVICE_SUCCEEDED: 3,
49  /**
50   * Constant: SERVICE_FAILED
51   * {Integer} used for
52   */
53  SERVICE_FAILED: 4,
54  /**
55   * Function: removeItem
56   * Remove an object from an array. Iterates through the array
57   *     to find the item, then removes it.
58   *
59   * Parameters:
60   * array - {Array}
61   * item - {Object}
62   *
63   * Return
64   * {Array} A reference to the array
65   */
66  removeItem: function(array, item) {
67    for(var i = array.length - 1; i >= 0; i--) {
68        if(array[i] == item) {
69            array.splice(i,1);
70        }
71    }
72    return array;
73  },
74  /**
75   * Function: indexOf
76   *
77   * Parameters:
78   * array - {Array}
79   * obj - {Object}
80   *
81   * Returns:
82   * {Integer} The index at, which the first object was found in the array.
83   *           If not found, returns -1.
84   */
85  indexOf: function(array, obj) {
86    for(var i=0, len=array.length; i<len; i++) {
87      if (array[i] == obj)
88        return i;
89    }
90    return -1;   
91  },
92  /**
93   * Function: extend
94   * Copy all properties of a source object to a destination object. Modifies
95   *     the passed in destination object.  Any properties on the source object
96   *     that are set to undefined will not be (re)set on the destination object.
97   *
98   * Parameters:
99   * destination - {Object} The object that will be modified
100   * source - {Object} The object with properties to be set on the destination
101   *
102   * Returns:
103   * {Object} The destination object.
104   */
105  extend: function(destination, source) {
106    destination = destination || {};
107    if(source) {
108      for(var property in source) {
109        var value = source[property];
110        if(value !== undefined)
111          destination[property] = value;
112      }
113    }
114    return destination;
115  },
116  /**
117   * Function: rad
118   *
119   * Parameters:
120   * x - {Float}
121   *
122   * Returns:
123   * {Float}
124   */
125  rad: function(x) {return x*Math.PI/180;},
126  /**
127   * Function: distVincenty
128   * Given two objects representing points with geographic coordinates, this
129   *     calculates the distance between those points on the surface of an
130   *     ellipsoid.
131   *
132   * Parameters:
133   * p1 - {<ZOO.Geometry.Point>} (or any object with both .x, .y properties)
134   * p2 - {<ZOO.Geometry.Point>} (or any object with both .x, .y properties)
135   *
136   * Returns:
137   * {Float} The distance (in km) between the two input points as measured on an
138   *     ellipsoid.  Note that the input point objects must be in geographic
139   *     coordinates (decimal degrees) and the return distance is in kilometers.
140   */
141  distVincenty: function(p1, p2) {
142    var a = 6378137, b = 6356752.3142,  f = 1/298.257223563;
143    var L = ZOO.rad(p2.x - p1.y);
144    var U1 = Math.atan((1-f) * Math.tan(ZOO.rad(p1.y)));
145    var U2 = Math.atan((1-f) * Math.tan(ZOO.rad(p2.y)));
146    var sinU1 = Math.sin(U1), cosU1 = Math.cos(U1);
147    var sinU2 = Math.sin(U2), cosU2 = Math.cos(U2);
148    var lambda = L, lambdaP = 2*Math.PI;
149    var iterLimit = 20;
150    while (Math.abs(lambda-lambdaP) > 1e-12 && --iterLimit>0) {
151        var sinLambda = Math.sin(lambda), cosLambda = Math.cos(lambda);
152        var sinSigma = Math.sqrt((cosU2*sinLambda) * (cosU2*sinLambda) +
153        (cosU1*sinU2-sinU1*cosU2*cosLambda) * (cosU1*sinU2-sinU1*cosU2*cosLambda));
154        if (sinSigma==0) {
155            return 0;  // co-incident points
156        }
157        var cosSigma = sinU1*sinU2 + cosU1*cosU2*cosLambda;
158        var sigma = Math.atan2(sinSigma, cosSigma);
159        var alpha = Math.asin(cosU1 * cosU2 * sinLambda / sinSigma);
160        var cosSqAlpha = Math.cos(alpha) * Math.cos(alpha);
161        var cos2SigmaM = cosSigma - 2*sinU1*sinU2/cosSqAlpha;
162        var C = f/16*cosSqAlpha*(4+f*(4-3*cosSqAlpha));
163        lambdaP = lambda;
164        lambda = L + (1-C) * f * Math.sin(alpha) *
165        (sigma + C*sinSigma*(cos2SigmaM+C*cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)));
166    }
167    if (iterLimit==0) {
168        return NaN;  // formula failed to converge
169    }
170    var uSq = cosSqAlpha * (a*a - b*b) / (b*b);
171    var A = 1 + uSq/16384*(4096+uSq*(-768+uSq*(320-175*uSq)));
172    var B = uSq/1024 * (256+uSq*(-128+uSq*(74-47*uSq)));
173    var deltaSigma = B*sinSigma*(cos2SigmaM+B/4*(cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)-
174        B/6*cos2SigmaM*(-3+4*sinSigma*sinSigma)*(-3+4*cos2SigmaM*cos2SigmaM)));
175    var s = b*A*(sigma-deltaSigma);
176    var d = s.toFixed(3)/1000; // round to 1mm precision
177    return d;
178  },
179  /**
180   * Function: Class
181   * Method used to create ZOO classes. Includes support for
182   *     multiple inheritance.
183   */
184  Class: function() {
185    var Class = function() {
186      this.initialize.apply(this, arguments);
187    };
188    var extended = {};
189    var parent;
190    for(var i=0; i<arguments.length; ++i) {
191      if(typeof arguments[i] == "function") {
192        // get the prototype of the superclass
193        parent = arguments[i].prototype;
194      } else {
195        // in this case we're extending with the prototype
196        parent = arguments[i];
197      }
198      ZOO.extend(extended, parent);
199    }
200    Class.prototype = extended;
201
202    return Class;
203  },
204  /**
205   * Function: UpdateStatus
206   * Method used to update the status of the process
207   *
208   * Parameters:
209   * env - {Object} The environment object
210   * value - {Float} the status value between 0 to 100
211   */
212  UpdateStatus: function(env,value) {
213    return ZOOUpdateStatus(env,value);
214  }
215};
216
217/**
218 * Class: ZOO.String
219 * Contains convenience methods for string manipulation
220 */
221ZOO.String = {
222  /**
223   * Function: startsWith
224   * Test whether a string starts with another string.
225   *
226   * Parameters:
227   * str - {String} The string to test.
228   * sub - {Sring} The substring to look for.
229   * 
230   * Returns:
231   * {Boolean} The first string starts with the second.
232   */
233  startsWith: function(str, sub) {
234    return (str.indexOf(sub) == 0);
235  },
236  /**
237   * Function: contains
238   * Test whether a string contains another string.
239   *
240   * Parameters:
241   * str - {String} The string to test.
242   * sub - {String} The substring to look for.
243   *
244   * Returns:
245   * {Boolean} The first string contains the second.
246   */
247  contains: function(str, sub) {
248    return (str.indexOf(sub) != -1);
249  },
250  /**
251   * Function: trim
252   * Removes leading and trailing whitespace characters from a string.
253   *
254   * Parameters:
255   * str - {String} The (potentially) space padded string.  This string is not
256   *     modified.
257   *
258   * Returns:
259   * {String} A trimmed version of the string with all leading and
260   *     trailing spaces removed.
261   */
262  trim: function(str) {
263    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
264  },
265  /**
266   * Function: camelize
267   * Camel-case a hyphenated string.
268   *     Ex. "chicken-head" becomes "chickenHead", and
269   *     "-chicken-head" becomes "ChickenHead".
270   *
271   * Parameters:
272   * str - {String} The string to be camelized.  The original is not modified.
273   *
274   * Returns:
275   * {String} The string, camelized
276   *
277   */
278  camelize: function(str) {
279    var oStringList = str.split('-');
280    var camelizedString = oStringList[0];
281    for (var i=1, len=oStringList.length; i<len; i++) {
282      var s = oStringList[i];
283      camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
284    }
285    return camelizedString;
286  },
287  /**
288   * Property: tokenRegEx
289   * Used to find tokens in a string.
290   * Examples: ${a}, ${a.b.c}, ${a-b}, ${5}
291   */
292  tokenRegEx:  /\$\{([\w.]+?)\}/g,
293  /**
294   * Property: numberRegEx
295   * Used to test strings as numbers.
296   */
297  numberRegEx: /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/,
298  /**
299   * Function: isNumeric
300   * Determine whether a string contains only a numeric value.
301   *
302   * Examples:
303   * (code)
304   * ZOO.String.isNumeric("6.02e23") // true
305   * ZOO.String.isNumeric("12 dozen") // false
306   * ZOO.String.isNumeric("4") // true
307   * ZOO.String.isNumeric(" 4 ") // false
308   * (end)
309   *
310   * Returns:
311   * {Boolean} String contains only a number.
312   */
313  isNumeric: function(value) {
314    return ZOO.String.numberRegEx.test(value);
315  },
316  /**
317   * Function: numericIf
318   * Converts a string that appears to be a numeric value into a number.
319   *
320   * Returns
321   * {Number|String} a Number if the passed value is a number, a String
322   *     otherwise.
323   */
324  numericIf: function(value) {
325    return ZOO.String.isNumeric(value) ? parseFloat(value) : value;
326  }
327};
328
329/**
330 * Class: ZOO.Request
331 * Contains convenience methods for working with ZOORequest which
332 *     replace XMLHttpRequest. Because of we are not in a browser
333 *     JavaScript environment, ZOO Project provides a method to
334 *     query servers which is based on curl : ZOORequest.
335 */
336ZOO.Request = {
337  /**
338   * Function: GET
339   * Send an HTTP GET request.
340   *
341   * Parameters:
342   * url - {String} The URL to request.
343   * params - {Object} Params to add to the url
344   *
345   * Returns:
346   * {String} Request result.
347   */
348  Get: function(url,params) {
349    var paramsArray = [];
350    for (var key in params) {
351      var value = params[key];
352      if ((value != null) && (typeof value != 'function')) {
353        var encodedValue;
354        if (typeof value == 'object' && value.constructor == Array) {
355          /* value is an array; encode items and separate with "," */
356          var encodedItemArray = [];
357          for (var itemIndex=0, len=value.length; itemIndex<len; itemIndex++) {
358            encodedItemArray.push(encodeURIComponent(value[itemIndex]));
359          }
360          encodedValue = encodedItemArray.join(",");
361        }
362        else {
363          /* value is a string; simply encode */
364          encodedValue = encodeURIComponent(value);
365        }
366        paramsArray.push(encodeURIComponent(key) + "=" + encodedValue);
367      }
368    }
369    var paramString = paramsArray.join("&");
370    if(paramString.length > 0) {
371      var separator = (url.indexOf('?') > -1) ? '&' : '?';
372      url += separator + paramString;
373    }
374    return ZOORequest('GET',url);
375  },
376  /**
377   * Function: POST
378   * Send an HTTP POST request.
379   *
380   * Parameters:
381   * url - {String} The URL to request.
382   * body - {String} The request's body to send.
383   * headers - {Object} A key-value object of headers to push to
384   *     the request's head
385   *
386   * Returns:
387   * {String} Request result.
388   */
389  Post: function(url,body,headers) {
390    if(!(headers instanceof Array)) {
391      var headersArray = [];
392      for (var name in headers) {
393        headersArray.push(name+': '+headers[name]); 
394      }
395      headers = headersArray;
396    }
397    return ZOORequest('POST',url,body,headers);
398  }
399};
400
401/**
402 * Class: ZOO.Bounds
403 * Instances of this class represent bounding boxes.  Data stored as left,
404 *     bottom, right, top floats. All values are initialized to null,
405 *     however, you should make sure you set them before using the bounds
406 *     for anything.
407 */
408ZOO.Bounds = ZOO.Class({
409  /**
410   * Property: left
411   * {Number} Minimum horizontal coordinate.
412   */
413  left: null,
414  /**
415   * Property: bottom
416   * {Number} Minimum vertical coordinate.
417   */
418  bottom: null,
419  /**
420   * Property: right
421   * {Number} Maximum horizontal coordinate.
422   */
423  right: null,
424  /**
425   * Property: top
426   * {Number} Maximum vertical coordinate.
427   */
428  top: null,
429  /**
430   * Constructor: ZOO.Bounds
431   * Construct a new bounds object.
432   *
433   * Parameters:
434   * left - {Number} The left bounds of the box.  Note that for width
435   *        calculations, this is assumed to be less than the right value.
436   * bottom - {Number} The bottom bounds of the box.  Note that for height
437   *          calculations, this is assumed to be more than the top value.
438   * right - {Number} The right bounds.
439   * top - {Number} The top bounds.
440   */
441  initialize: function(left, bottom, right, top) {
442    if (left != null)
443      this.left = parseFloat(left);
444    if (bottom != null)
445      this.bottom = parseFloat(bottom);
446    if (right != null)
447      this.right = parseFloat(right);
448    if (top != null)
449      this.top = parseFloat(top);
450  },
451  /**
452   * Method: clone
453   * Create a cloned instance of this bounds.
454   *
455   * Returns:
456   * {<ZOO.Bounds>} A fresh copy of the bounds
457   */
458  clone:function() {
459    return new ZOO.Bounds(this.left, this.bottom, 
460                          this.right, this.top);
461  },
462  /**
463   * Method: equals
464   * Test a two bounds for equivalence.
465   *
466   * Parameters:
467   * bounds - {<ZOO.Bounds>}
468   *
469   * Returns:
470   * {Boolean} The passed-in bounds object has the same left,
471   *           right, top, bottom components as this.  Note that if bounds
472   *           passed in is null, returns false.
473   */
474  equals:function(bounds) {
475    var equals = false;
476    if (bounds != null)
477        equals = ((this.left == bounds.left) && 
478                  (this.right == bounds.right) &&
479                  (this.top == bounds.top) && 
480                  (this.bottom == bounds.bottom));
481    return equals;
482  },
483  /**
484   * Method: toString
485   *
486   * Returns:
487   * {String} String representation of bounds object.
488   *          (ex.<i>"left-bottom=(5,42) right-top=(10,45)"</i>)
489   */
490  toString:function() {
491    return ( "left-bottom=(" + this.left + "," + this.bottom + ")"
492              + " right-top=(" + this.right + "," + this.top + ")" );
493  },
494  /**
495   * APIMethod: toArray
496   *
497   * Returns:
498   * {Array} array of left, bottom, right, top
499   */
500  toArray: function() {
501    return [this.left, this.bottom, this.right, this.top];
502  },
503  /**
504   * Method: toBBOX
505   *
506   * Parameters:
507   * decimal - {Integer} How many significant digits in the bbox coords?
508   *                     Default is 6
509   *
510   * Returns:
511   * {String} Simple String representation of bounds object.
512   *          (ex. <i>"5,42,10,45"</i>)
513   */
514  toBBOX:function(decimal) {
515    if (decimal== null)
516      decimal = 6; 
517    var mult = Math.pow(10, decimal);
518    var bbox = Math.round(this.left * mult) / mult + "," + 
519               Math.round(this.bottom * mult) / mult + "," + 
520               Math.round(this.right * mult) / mult + "," + 
521               Math.round(this.top * mult) / mult;
522    return bbox;
523  },
524  /**
525   * Method: toGeometry
526   * Create a new polygon geometry based on this bounds.
527   *
528   * Returns:
529   * {<ZOO.Geometry.Polygon>} A new polygon with the coordinates
530   *     of this bounds.
531   */
532  toGeometry: function() {
533    return new ZOO.Geometry.Polygon([
534      new ZOO.Geometry.LinearRing([
535        new ZOO.Geometry.Point(this.left, this.bottom),
536        new ZOO.Geometry.Point(this.right, this.bottom),
537        new ZOO.Geometry.Point(this.right, this.top),
538        new ZOO.Geometry.Point(this.left, this.top)
539      ])
540    ]);
541  },
542  /**
543   * Method: getWidth
544   *
545   * Returns:
546   * {Float} The width of the bounds
547   */
548  getWidth:function() {
549    return (this.right - this.left);
550  },
551  /**
552   * Method: getHeight
553   *
554   * Returns:
555   * {Float} The height of the bounds (top minus bottom).
556   */
557  getHeight:function() {
558    return (this.top - this.bottom);
559  },
560  /**
561   * Method: add
562   *
563   * Parameters:
564   * x - {Float}
565   * y - {Float}
566   *
567   * Returns:
568   * {<ZOO.Bounds>} A new bounds whose coordinates are the same as
569   *     this, but shifted by the passed-in x and y values.
570   */
571  add:function(x, y) {
572    if ( (x == null) || (y == null) )
573      return null;
574    return new ZOO.Bounds(this.left + x, this.bottom + y,
575                                 this.right + x, this.top + y);
576  },
577  /**
578   * Method: extend
579   * Extend the bounds to include the point, lonlat, or bounds specified.
580   *     Note, this function assumes that left < right and bottom < top.
581   *
582   * Parameters:
583   * object - {Object} Can be Point, or Bounds
584   */
585  extend:function(object) {
586    var bounds = null;
587    if (object) {
588      // clear cached center location
589      switch(object.CLASS_NAME) {
590        case "ZOO.Geometry.Point":
591          bounds = new ZOO.Bounds(object.x, object.y,
592                                         object.x, object.y);
593          break;
594        case "ZOO.Bounds":   
595          bounds = object;
596          break;
597      }
598      if (bounds) {
599        if ( (this.left == null) || (bounds.left < this.left))
600          this.left = bounds.left;
601        if ( (this.bottom == null) || (bounds.bottom < this.bottom) )
602          this.bottom = bounds.bottom;
603        if ( (this.right == null) || (bounds.right > this.right) )
604          this.right = bounds.right;
605        if ( (this.top == null) || (bounds.top > this.top) )
606          this.top = bounds.top;
607      }
608    }
609  },
610  /**
611   * APIMethod: contains
612   *
613   * Parameters:
614   * x - {Float}
615   * y - {Float}
616   * inclusive - {Boolean} Whether or not to include the border.
617   *     Default is true.
618   *
619   * Returns:
620   * {Boolean} Whether or not the passed-in coordinates are within this
621   *     bounds.
622   */
623  contains:function(x, y, inclusive) {
624     //set default
625     if (inclusive == null)
626       inclusive = true;
627     if (x == null || y == null)
628       return false;
629     x = parseFloat(x);
630     y = parseFloat(y);
631
632     var contains = false;
633     if (inclusive)
634       contains = ((x >= this.left) && (x <= this.right) && 
635                   (y >= this.bottom) && (y <= this.top));
636     else
637       contains = ((x > this.left) && (x < this.right) && 
638                   (y > this.bottom) && (y < this.top));
639     return contains;
640  },
641  /**
642   * Method: intersectsBounds
643   * Determine whether the target bounds intersects this bounds.  Bounds are
644   *     considered intersecting if any of their edges intersect or if one
645   *     bounds contains the other.
646   *
647   * Parameters:
648   * bounds - {<ZOO.Bounds>} The target bounds.
649   * inclusive - {Boolean} Treat coincident borders as intersecting.  Default
650   *     is true.  If false, bounds that do not overlap but only touch at the
651   *     border will not be considered as intersecting.
652   *
653   * Returns:
654   * {Boolean} The passed-in bounds object intersects this bounds.
655   */
656  intersectsBounds:function(bounds, inclusive) {
657    if (inclusive == null)
658      inclusive = true;
659    var intersects = false;
660    var mightTouch = (
661        this.left == bounds.right ||
662        this.right == bounds.left ||
663        this.top == bounds.bottom ||
664        this.bottom == bounds.top
665    );
666    if (inclusive || !mightTouch) {
667      var inBottom = (
668          ((bounds.bottom >= this.bottom) && (bounds.bottom <= this.top)) ||
669          ((this.bottom >= bounds.bottom) && (this.bottom <= bounds.top))
670          );
671      var inTop = (
672          ((bounds.top >= this.bottom) && (bounds.top <= this.top)) ||
673          ((this.top > bounds.bottom) && (this.top < bounds.top))
674          );
675      var inLeft = (
676          ((bounds.left >= this.left) && (bounds.left <= this.right)) ||
677          ((this.left >= bounds.left) && (this.left <= bounds.right))
678          );
679      var inRight = (
680          ((bounds.right >= this.left) && (bounds.right <= this.right)) ||
681          ((this.right >= bounds.left) && (this.right <= bounds.right))
682          );
683      intersects = ((inBottom || inTop) && (inLeft || inRight));
684    }
685    return intersects;
686  },
687  /**
688   * Method: containsBounds
689   * Determine whether the target bounds is contained within this bounds.
690   *
691   * bounds - {<ZOO.Bounds>} The target bounds.
692   * partial - {Boolean} If any of the target corners is within this bounds
693   *     consider the bounds contained.  Default is false.  If true, the
694   *     entire target bounds must be contained within this bounds.
695   * inclusive - {Boolean} Treat shared edges as contained.  Default is
696   *     true.
697   *
698   * Returns:
699   * {Boolean} The passed-in bounds object is contained within this bounds.
700   */
701  containsBounds:function(bounds, partial, inclusive) {
702    if (partial == null)
703      partial = false;
704    if (inclusive == null)
705      inclusive = true;
706    var bottomLeft  = this.contains(bounds.left, bounds.bottom, inclusive);
707    var bottomRight = this.contains(bounds.right, bounds.bottom, inclusive);
708    var topLeft  = this.contains(bounds.left, bounds.top, inclusive);
709    var topRight = this.contains(bounds.right, bounds.top, inclusive);
710    return (partial) ? (bottomLeft || bottomRight || topLeft || topRight)
711                     : (bottomLeft && bottomRight && topLeft && topRight);
712  },
713  CLASS_NAME: 'ZOO.Bounds'
714});
715
716/**
717 * Class: ZOO.Projection
718 * Class for coordinate transforms between coordinate systems.
719 *     Depends on the zoo-proj4js library. zoo-proj4js library
720 *     is loaded by the ZOO Kernel with zoo-api.
721 */
722ZOO.Projection = ZOO.Class({
723  /**
724   * Property: proj
725   * {Object} Proj4js.Proj instance.
726   */
727  proj: null,
728  /**
729   * Property: projCode
730   * {String}
731   */
732  projCode: null,
733  /**
734   * Constructor: ZOO.Projection
735   * This class offers several methods for interacting with a wrapped
736   *     zoo-pro4js projection object.
737   *
738   * Parameters:
739   * projCode - {String} A string identifying the Well Known Identifier for
740   *    the projection.
741   * options - {Object} An optional object to set additional properties.
742   *
743   * Returns:
744   * {<ZOO.Projection>} A projection object.
745   */
746  initialize: function(projCode, options) {
747    ZOO.extend(this, options);
748    this.projCode = projCode;
749    if (Proj4js) {
750      this.proj = new Proj4js.Proj(projCode);
751    }
752  },
753  /**
754   * Method: getCode
755   * Get the string SRS code.
756   *
757   * Returns:
758   * {String} The SRS code.
759   */
760  getCode: function() {
761    return this.proj ? this.proj.srsCode : this.projCode;
762  },
763  /**
764   * Method: getUnits
765   * Get the units string for the projection -- returns null if
766   *     zoo-proj4js is not available.
767   *
768   * Returns:
769   * {String} The units abbreviation.
770   */
771  getUnits: function() {
772    return this.proj ? this.proj.units : null;
773  },
774  /**
775   * Method: toString
776   * Convert projection to string (getCode wrapper).
777   *
778   * Returns:
779   * {String} The projection code.
780   */
781  toString: function() {
782    return this.getCode();
783  },
784  /**
785   * Method: equals
786   * Test equality of two projection instances.  Determines equality based
787   *     soley on the projection code.
788   *
789   * Returns:
790   * {Boolean} The two projections are equivalent.
791   */
792  equals: function(projection) {
793    if (projection && projection.getCode)
794      return this.getCode() == projection.getCode();
795    else
796      return false;
797  },
798  /* Method: destroy
799   * Destroy projection object.
800   */
801  destroy: function() {
802    this.proj = null;
803    this.projCode = null;
804  },
805  CLASS_NAME: 'ZOO.Projection'
806});
807/**
808 * Method: transform
809 * Transform a point coordinate from one projection to another.  Note that
810 *     the input point is transformed in place.
811 *
812 * Parameters:
813 * point - {{ZOO.Geometry.Point> | Object} An object with x and y
814 *     properties representing coordinates in those dimensions.
815 * sourceProj - {ZOO.Projection} Source map coordinate system
816 * destProj - {ZOO.Projection} Destination map coordinate system
817 *
818 * Returns:
819 * point - {object} A transformed coordinate.  The original point is modified.
820 */
821ZOO.Projection.transform = function(point, source, dest) {
822    if (source.proj && dest.proj)
823        point = Proj4js.transform(source.proj, dest.proj, point);
824    return point;
825};
826
827/**
828 * Class: ZOO.Format
829 * Base class for format reading/writing a variety of formats. Subclasses
830 *     of ZOO.Format are expected to have read and write methods.
831 */
832ZOO.Format = ZOO.Class({
833  /**
834   * Property: options
835   * {Object} A reference to options passed to the constructor.
836   */
837  options:null,
838  /**
839   * Property: externalProjection
840   * {<ZOO.Projection>} When passed a externalProjection and
841   *     internalProjection, the format will reproject the geometries it
842   *     reads or writes. The externalProjection is the projection used by
843   *     the content which is passed into read or which comes out of write.
844   *     In order to reproject, a projection transformation function for the
845   *     specified projections must be available. This support is provided
846   *     via zoo-proj4js.
847   */
848  externalProjection: null,
849  /**
850   * Property: internalProjection
851   * {<ZOO.Projection>} When passed a externalProjection and
852   *     internalProjection, the format will reproject the geometries it
853   *     reads or writes. The internalProjection is the projection used by
854   *     the geometries which are returned by read or which are passed into
855   *     write.  In order to reproject, a projection transformation function
856   *     for the specified projections must be available. This support is
857   *     provided via zoo-proj4js.
858   */
859  internalProjection: null,
860  /**
861   * Property: data
862   * {Object} When <keepData> is true, this is the parsed string sent to
863   *     <read>.
864   */
865  data: null,
866  /**
867   * Property: keepData
868   * {Object} Maintain a reference (<data>) to the most recently read data.
869   *     Default is false.
870   */
871  keepData: false,
872  /**
873   * Constructor: ZOO.Format
874   * Instances of this class are not useful.  See one of the subclasses.
875   *
876   * Parameters:
877   * options - {Object} An optional object with properties to set on the
878   *           format
879   *
880   * Valid options:
881   * keepData - {Boolean} If true, upon <read>, the data property will be
882   *     set to the parsed object (e.g. the json or xml object).
883   *
884   * Returns:
885   * An instance of ZOO.Format
886   */
887  initialize: function(options) {
888    ZOO.extend(this, options);
889    this.options = options;
890  },
891  /**
892   * Method: destroy
893   * Clean up.
894   */
895  destroy: function() {
896  },
897  /**
898   * Method: read
899   * Read data from a string, and return an object whose type depends on the
900   * subclass.
901   *
902   * Parameters:
903   * data - {string} Data to read/parse.
904   *
905   * Returns:
906   * Depends on the subclass
907   */
908  read: function(data) {
909  },
910  /**
911   * Method: write
912   * Accept an object, and return a string.
913   *
914   * Parameters:
915   * object - {Object} Object to be serialized
916   *
917   * Returns:
918   * {String} A string representation of the object.
919   */
920  write: function(data) {
921  },
922  CLASS_NAME: 'ZOO.Format'
923});
924/**
925 * Class: ZOO.Format.WKT
926 * Class for reading and writing Well-Known Text. Create a new instance
927 * with the <ZOO.Format.WKT> constructor.
928 *
929 * Inherits from:
930 *  - <ZOO.Format>
931 */
932ZOO.Format.WKT = ZOO.Class(ZOO.Format, {
933  /**
934   * Constructor: ZOO.Format.WKT
935   * Create a new parser for WKT
936   *
937   * Parameters:
938   * options - {Object} An optional object whose properties will be set on
939   *           this instance
940   *
941   * Returns:
942   * {<ZOO.Format.WKT>} A new WKT parser.
943   */
944  initialize: function(options) {
945    this.regExes = {
946      'typeStr': /^\s*(\w+)\s*\(\s*(.*)\s*\)\s*$/,
947      'spaces': /\s+/,
948      'parenComma': /\)\s*,\s*\(/,
949      'doubleParenComma': /\)\s*\)\s*,\s*\(\s*\(/,  // can't use {2} here
950      'trimParens': /^\s*\(?(.*?)\)?\s*$/
951    };
952    ZOO.Format.prototype.initialize.apply(this, [options]);
953  },
954  /**
955   * Method: read
956   * Deserialize a WKT string and return a vector feature or an
957   *     array of vector features.  Supports WKT for POINT,
958   *     MULTIPOINT, LINESTRING, MULTILINESTRING, POLYGON,
959   *     MULTIPOLYGON, and GEOMETRYCOLLECTION.
960   *
961   * Parameters:
962   * wkt - {String} A WKT string
963   *
964   * Returns:
965   * {<ZOO.Feature.Vector>|Array} A feature or array of features for
966   *     GEOMETRYCOLLECTION WKT.
967   */
968  read: function(wkt) {
969    var features, type, str;
970    var matches = this.regExes.typeStr.exec(wkt);
971    if(matches) {
972      type = matches[1].toLowerCase();
973      str = matches[2];
974      if(this.parse[type]) {
975        features = this.parse[type].apply(this, [str]);
976      }
977      if (this.internalProjection && this.externalProjection) {
978        if (features && 
979            features.CLASS_NAME == "ZOO.Feature") {
980          features.geometry.transform(this.externalProjection,
981                                      this.internalProjection);
982        } else if (features &&
983            type != "geometrycollection" &&
984            typeof features == "object") {
985          for (var i=0, len=features.length; i<len; i++) {
986            var component = features[i];
987            component.geometry.transform(this.externalProjection,
988                                         this.internalProjection);
989          }
990        }
991      }
992    }   
993    return features;
994  },
995  /**
996   * Method: write
997   * Serialize a feature or array of features into a WKT string.
998   *
999   * Parameters:
1000   * features - {<ZOO.Feature.Vector>|Array} A feature or array of
1001   *            features
1002   *
1003   * Returns:
1004   * {String} The WKT string representation of the input geometries
1005   */
1006  write: function(features) {
1007    var collection, geometry, type, data, isCollection;
1008    if(features.constructor == Array) {
1009      collection = features;
1010      isCollection = true;
1011    } else {
1012      collection = [features];
1013      isCollection = false;
1014    }
1015    var pieces = [];
1016    if(isCollection)
1017      pieces.push('GEOMETRYCOLLECTION(');
1018    for(var i=0, len=collection.length; i<len; ++i) {
1019      if(isCollection && i>0)
1020        pieces.push(',');
1021      geometry = collection[i].geometry;
1022      type = geometry.CLASS_NAME.split('.')[2].toLowerCase();
1023      if(!this.extract[type])
1024        return null;
1025      if (this.internalProjection && this.externalProjection) {
1026        geometry = geometry.clone();
1027        geometry.transform(this.internalProjection, 
1028                          this.externalProjection);
1029      }                       
1030      data = this.extract[type].apply(this, [geometry]);
1031      pieces.push(type.toUpperCase() + '(' + data + ')');
1032    }
1033    if(isCollection)
1034      pieces.push(')');
1035    return pieces.join('');
1036  },
1037  /**
1038   * Property: extract
1039   * Object with properties corresponding to the geometry types.
1040   * Property values are functions that do the actual data extraction.
1041   */
1042  extract: {
1043    /**
1044     * Return a space delimited string of point coordinates.
1045     * @param {<ZOO.Geometry.Point>} point
1046     * @returns {String} A string of coordinates representing the point
1047     */
1048    'point': function(point) {
1049      return point.x + ' ' + point.y;
1050    },
1051    /**
1052     * Return a comma delimited string of point coordinates from a multipoint.
1053     * @param {<ZOO.Geometry.MultiPoint>} multipoint
1054     * @returns {String} A string of point coordinate strings representing
1055     *                  the multipoint
1056     */
1057    'multipoint': function(multipoint) {
1058      var array = [];
1059      for(var i=0, len=multipoint.components.length; i<len; ++i) {
1060        array.push(this.extract.point.apply(this, [multipoint.components[i]]));
1061      }
1062      return array.join(',');
1063    },
1064    /**
1065     * Return a comma delimited string of point coordinates from a line.
1066     * @param {<ZOO.Geometry.LineString>} linestring
1067     * @returns {String} A string of point coordinate strings representing
1068     *                  the linestring
1069     */
1070    'linestring': function(linestring) {
1071      var array = [];
1072      for(var i=0, len=linestring.components.length; i<len; ++i) {
1073        array.push(this.extract.point.apply(this, [linestring.components[i]]));
1074      }
1075      return array.join(',');
1076    },
1077    /**
1078     * Return a comma delimited string of linestring strings from a multilinestring.
1079     * @param {<ZOO.Geometry.MultiLineString>} multilinestring
1080     * @returns {String} A string of of linestring strings representing
1081     *                  the multilinestring
1082     */
1083    'multilinestring': function(multilinestring) {
1084      var array = [];
1085      for(var i=0, len=multilinestring.components.length; i<len; ++i) {
1086        array.push('(' +
1087            this.extract.linestring.apply(this, [multilinestring.components[i]]) +
1088            ')');
1089      }
1090      return array.join(',');
1091    },
1092    /**
1093     * Return a comma delimited string of linear ring arrays from a polygon.
1094     * @param {<ZOO.Geometry.Polygon>} polygon
1095     * @returns {String} An array of linear ring arrays representing the polygon
1096     */
1097    'polygon': function(polygon) {
1098      var array = [];
1099      for(var i=0, len=polygon.components.length; i<len; ++i) {
1100        array.push('(' +
1101            this.extract.linestring.apply(this, [polygon.components[i]]) +
1102            ')');
1103      }
1104      return array.join(',');
1105    },
1106    /**
1107     * Return an array of polygon arrays from a multipolygon.
1108     * @param {<ZOO.Geometry.MultiPolygon>} multipolygon
1109     * @returns {Array} An array of polygon arrays representing
1110     *                  the multipolygon
1111     */
1112    'multipolygon': function(multipolygon) {
1113      var array = [];
1114      for(var i=0, len=multipolygon.components.length; i<len; ++i) {
1115        array.push('(' +
1116            this.extract.polygon.apply(this, [multipolygon.components[i]]) +
1117            ')');
1118      }
1119      return array.join(',');
1120    }
1121  },
1122  /**
1123   * Property: parse
1124   * Object with properties corresponding to the geometry types.
1125   *     Property values are functions that do the actual parsing.
1126   */
1127  parse: {
1128    /**
1129     * Method: parse.point
1130     * Return point feature given a point WKT fragment.
1131     *
1132     * Parameters:
1133     * str - {String} A WKT fragment representing the point
1134     * Returns:
1135     * {<ZOO.Feature>} A point feature
1136     */
1137    'point': function(str) {
1138       var coords = ZOO.String.trim(str).split(this.regExes.spaces);
1139            return new ZOO.Feature(
1140                new ZOO.Geometry.Point(coords[0], coords[1])
1141            );
1142    },
1143    /**
1144     * Method: parse.multipoint
1145     * Return a multipoint feature given a multipoint WKT fragment.
1146     *
1147     * Parameters:
1148     * str - {String} A WKT fragment representing the multipoint
1149     *
1150     * Returns:
1151     * {<ZOO.Feature>} A multipoint feature
1152     */
1153    'multipoint': function(str) {
1154       var points = ZOO.String.trim(str).split(',');
1155       var components = [];
1156       for(var i=0, len=points.length; i<len; ++i) {
1157         components.push(this.parse.point.apply(this, [points[i]]).geometry);
1158       }
1159       return new ZOO.Feature(
1160           new ZOO.Geometry.MultiPoint(components)
1161           );
1162    },
1163    /**
1164     * Method: parse.linestring
1165     * Return a linestring feature given a linestring WKT fragment.
1166     *
1167     * Parameters:
1168     * str - {String} A WKT fragment representing the linestring
1169     *
1170     * Returns:
1171     * {<ZOO.Feature>} A linestring feature
1172     */
1173    'linestring': function(str) {
1174      var points = ZOO.String.trim(str).split(',');
1175      var components = [];
1176      for(var i=0, len=points.length; i<len; ++i) {
1177        components.push(this.parse.point.apply(this, [points[i]]).geometry);
1178      }
1179      return new ZOO.Feature(
1180          new ZOO.Geometry.LineString(components)
1181          );
1182    },
1183    /**
1184     * Method: parse.multilinestring
1185     * Return a multilinestring feature given a multilinestring WKT fragment.
1186     *
1187     * Parameters:
1188     * str - {String} A WKT fragment representing the multilinestring
1189     *
1190     * Returns:
1191     * {<ZOO.Feature>} A multilinestring feature
1192     */
1193    'multilinestring': function(str) {
1194      var line;
1195      var lines = ZOO.String.trim(str).split(this.regExes.parenComma);
1196      var components = [];
1197      for(var i=0, len=lines.length; i<len; ++i) {
1198        line = lines[i].replace(this.regExes.trimParens, '$1');
1199        components.push(this.parse.linestring.apply(this, [line]).geometry);
1200      }
1201      return new ZOO.Feature(
1202          new ZOO.Geometry.MultiLineString(components)
1203          );
1204    },
1205    /**
1206     * Method: parse.polygon
1207     * Return a polygon feature given a polygon WKT fragment.
1208     *
1209     * Parameters:
1210     * str - {String} A WKT fragment representing the polygon
1211     *
1212     * Returns:
1213     * {<ZOO.Feature>} A polygon feature
1214     */
1215    'polygon': function(str) {
1216       var ring, linestring, linearring;
1217       var rings = ZOO.String.trim(str).split(this.regExes.parenComma);
1218       var components = [];
1219       for(var i=0, len=rings.length; i<len; ++i) {
1220         ring = rings[i].replace(this.regExes.trimParens, '$1');
1221         linestring = this.parse.linestring.apply(this, [ring]).geometry;
1222         linearring = new ZOO.Geometry.LinearRing(linestring.components);
1223         components.push(linearring);
1224       }
1225       return new ZOO.Feature(
1226           new ZOO.Geometry.Polygon(components)
1227           );
1228    },
1229    /**
1230     * Method: parse.multipolygon
1231     * Return a multipolygon feature given a multipolygon WKT fragment.
1232     *
1233     * Parameters:
1234     * str - {String} A WKT fragment representing the multipolygon
1235     *
1236     * Returns:
1237     * {<ZOO.Feature>} A multipolygon feature
1238     */
1239    'multipolygon': function(str) {
1240      var polygon;
1241      var polygons = ZOO.String.trim(str).split(this.regExes.doubleParenComma);
1242      var components = [];
1243      for(var i=0, len=polygons.length; i<len; ++i) {
1244        polygon = polygons[i].replace(this.regExes.trimParens, '$1');
1245        components.push(this.parse.polygon.apply(this, [polygon]).geometry);
1246      }
1247      return new ZOO.Feature(
1248          new ZOO.Geometry.MultiPolygon(components)
1249          );
1250    },
1251    /**
1252     * Method: parse.geometrycollection
1253     * Return an array of features given a geometrycollection WKT fragment.
1254     *
1255     * Parameters:
1256     * str - {String} A WKT fragment representing the geometrycollection
1257     *
1258     * Returns:
1259     * {Array} An array of ZOO.Feature
1260     */
1261    'geometrycollection': function(str) {
1262      // separate components of the collection with |
1263      str = str.replace(/,\s*([A-Za-z])/g, '|$1');
1264      var wktArray = ZOO.String.trim(str).split('|');
1265      var components = [];
1266      for(var i=0, len=wktArray.length; i<len; ++i) {
1267        components.push(ZOO.Format.WKT.prototype.read.apply(this,[wktArray[i]]));
1268      }
1269      return components;
1270    }
1271  },
1272  CLASS_NAME: 'ZOO.Format.WKT'
1273});
1274/**
1275 * Class: ZOO.Format.JSON
1276 * A parser to read/write JSON safely. Create a new instance with the
1277 *     <ZOO.Format.JSON> constructor.
1278 *
1279 * Inherits from:
1280 *  - <ZOO.Format>
1281 */
1282ZOO.Format.JSON = ZOO.Class(ZOO.Format, {
1283  /**
1284   * Property: indent
1285   * {String} For "pretty" printing, the indent string will be used once for
1286   *     each indentation level.
1287   */
1288  indent: "    ",
1289  /**
1290   * Property: space
1291   * {String} For "pretty" printing, the space string will be used after
1292   *     the ":" separating a name/value pair.
1293   */
1294  space: " ",
1295  /**
1296   * Property: newline
1297   * {String} For "pretty" printing, the newline string will be used at the
1298   *     end of each name/value pair or array item.
1299   */
1300  newline: "\n",
1301  /**
1302   * Property: level
1303   * {Integer} For "pretty" printing, this is incremented/decremented during
1304   *     serialization.
1305   */
1306  level: 0,
1307  /**
1308   * Property: pretty
1309   * {Boolean} Serialize with extra whitespace for structure.  This is set
1310   *     by the <write> method.
1311   */
1312  pretty: false,
1313  /**
1314   * Constructor: ZOO.Format.JSON
1315   * Create a new parser for JSON.
1316   *
1317   * Parameters:
1318   * options - {Object} An optional object whose properties will be set on
1319   *     this instance.
1320   */
1321  initialize: function(options) {
1322    ZOO.Format.prototype.initialize.apply(this, [options]);
1323  },
1324  /**
1325   * Method: read
1326   * Deserialize a json string.
1327   *
1328   * Parameters:
1329   * json - {String} A JSON string
1330   * filter - {Function} A function which will be called for every key and
1331   *     value at every level of the final result. Each value will be
1332   *     replaced by the result of the filter function. This can be used to
1333   *     reform generic objects into instances of classes, or to transform
1334   *     date strings into Date objects.
1335   *     
1336   * Returns:
1337   * {Object} An object, array, string, or number .
1338   */
1339  read: function(json, filter) {
1340    /**
1341     * Parsing happens in three stages. In the first stage, we run the text
1342     *     against a regular expression which looks for non-JSON
1343     *     characters. We are especially concerned with '()' and 'new'
1344     *     because they can cause invocation, and '=' because it can cause
1345     *     mutation. But just to be safe, we will reject all unexpected
1346     *     characters.
1347     */
1348    try {
1349      if (/^[\],:{}\s]*$/.test(json.replace(/\\["\\\/bfnrtu]/g, '@').
1350                          replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
1351                          replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
1352        /**
1353         * In the second stage we use the eval function to compile the
1354         *     text into a JavaScript structure. The '{' operator is
1355         *     subject to a syntactic ambiguity in JavaScript - it can
1356         *     begin a block or an object literal. We wrap the text in
1357         *     parens to eliminate the ambiguity.
1358         */
1359        var object = eval('(' + json + ')');
1360        /**
1361         * In the optional third stage, we recursively walk the new
1362         *     structure, passing each name/value pair to a filter
1363         *     function for possible transformation.
1364         */
1365        if(typeof filter === 'function') {
1366          function walk(k, v) {
1367            if(v && typeof v === 'object') {
1368              for(var i in v) {
1369                if(v.hasOwnProperty(i)) {
1370                  v[i] = walk(i, v[i]);
1371                }
1372              }
1373            }
1374            return filter(k, v);
1375          }
1376          object = walk('', object);
1377        }
1378        if(this.keepData) {
1379          this.data = object;
1380        }
1381        return object;
1382      }
1383    } catch(e) {
1384      // Fall through if the regexp test fails.
1385    }
1386    return null;
1387  },
1388  /**
1389   * Method: write
1390   * Serialize an object into a JSON string.
1391   *
1392   * Parameters:
1393   * value - {String} The object, array, string, number, boolean or date
1394   *     to be serialized.
1395   * pretty - {Boolean} Structure the output with newlines and indentation.
1396   *     Default is false.
1397   *
1398   * Returns:
1399   * {String} The JSON string representation of the input value.
1400   */
1401  write: function(value, pretty) {
1402    this.pretty = !!pretty;
1403    var json = null;
1404    var type = typeof value;
1405    if(this.serialize[type]) {
1406      try {
1407        json = this.serialize[type].apply(this, [value]);
1408      } catch(err) {
1409        //OpenLayers.Console.error("Trouble serializing: " + err);
1410      }
1411    }
1412    return json;
1413  },
1414  /**
1415   * Method: writeIndent
1416   * Output an indentation string depending on the indentation level.
1417   *
1418   * Returns:
1419   * {String} An appropriate indentation string.
1420   */
1421  writeIndent: function() {
1422    var pieces = [];
1423    if(this.pretty) {
1424      for(var i=0; i<this.level; ++i) {
1425        pieces.push(this.indent);
1426      }
1427    }
1428    return pieces.join('');
1429  },
1430  /**
1431   * Method: writeNewline
1432   * Output a string representing a newline if in pretty printing mode.
1433   *
1434   * Returns:
1435   * {String} A string representing a new line.
1436   */
1437  writeNewline: function() {
1438    return (this.pretty) ? this.newline : '';
1439  },
1440  /**
1441   * Method: writeSpace
1442   * Output a string representing a space if in pretty printing mode.
1443   *
1444   * Returns:
1445   * {String} A space.
1446   */
1447  writeSpace: function() {
1448    return (this.pretty) ? this.space : '';
1449  },
1450  /**
1451   * Property: serialize
1452   * Object with properties corresponding to the serializable data types.
1453   *     Property values are functions that do the actual serializing.
1454   */
1455  serialize: {
1456    /**
1457     * Method: serialize.object
1458     * Transform an object into a JSON string.
1459     *
1460     * Parameters:
1461     * object - {Object} The object to be serialized.
1462     *
1463     * Returns:
1464     * {String} A JSON string representing the object.
1465     */
1466    'object': function(object) {
1467       // three special objects that we want to treat differently
1468       if(object == null)
1469         return "null";
1470       if(object.constructor == Date)
1471         return this.serialize.date.apply(this, [object]);
1472       if(object.constructor == Array)
1473         return this.serialize.array.apply(this, [object]);
1474       var pieces = ['{'];
1475       this.level += 1;
1476       var key, keyJSON, valueJSON;
1477
1478       var addComma = false;
1479       for(key in object) {
1480         if(object.hasOwnProperty(key)) {
1481           // recursive calls need to allow for sub-classing
1482           keyJSON = ZOO.Format.JSON.prototype.write.apply(this,
1483                                                           [key, this.pretty]);
1484           valueJSON = ZOO.Format.JSON.prototype.write.apply(this,
1485                                                             [object[key], this.pretty]);
1486           if(keyJSON != null && valueJSON != null) {
1487             if(addComma)
1488               pieces.push(',');
1489             pieces.push(this.writeNewline(), this.writeIndent(),
1490                         keyJSON, ':', this.writeSpace(), valueJSON);
1491             addComma = true;
1492           }
1493         }
1494       }
1495       this.level -= 1;
1496       pieces.push(this.writeNewline(), this.writeIndent(), '}');
1497       return pieces.join('');
1498    },
1499    /**
1500     * Method: serialize.array
1501     * Transform an array into a JSON string.
1502     *
1503     * Parameters:
1504     * array - {Array} The array to be serialized
1505     *
1506     * Returns:
1507     * {String} A JSON string representing the array.
1508     */
1509    'array': function(array) {
1510      var json;
1511      var pieces = ['['];
1512      this.level += 1;
1513      for(var i=0, len=array.length; i<len; ++i) {
1514        // recursive calls need to allow for sub-classing
1515        json = ZOO.Format.JSON.prototype.write.apply(this,
1516                                                     [array[i], this.pretty]);
1517        if(json != null) {
1518          if(i > 0)
1519            pieces.push(',');
1520          pieces.push(this.writeNewline(), this.writeIndent(), json);
1521        }
1522      }
1523      this.level -= 1;   
1524      pieces.push(this.writeNewline(), this.writeIndent(), ']');
1525      return pieces.join('');
1526    },
1527    /**
1528     * Method: serialize.string
1529     * Transform a string into a JSON string.
1530     *
1531     * Parameters:
1532     * string - {String} The string to be serialized
1533     *
1534     * Returns:
1535     * {String} A JSON string representing the string.
1536     */
1537    'string': function(string) {
1538      var m = {
1539                '\b': '\\b',
1540                '\t': '\\t',
1541                '\n': '\\n',
1542                '\f': '\\f',
1543                '\r': '\\r',
1544                '"' : '\\"',
1545                '\\': '\\\\'
1546      };
1547      if(/["\\\x00-\x1f]/.test(string)) {
1548        return '"' + string.replace(/([\x00-\x1f\\"])/g, function(a, b) {
1549            var c = m[b];
1550            if(c)
1551              return c;
1552            c = b.charCodeAt();
1553            return '\\u00' +
1554            Math.floor(c / 16).toString(16) +
1555            (c % 16).toString(16);
1556        }) + '"';
1557      }
1558      return '"' + string + '"';
1559    },
1560    /**
1561     * Method: serialize.number
1562     * Transform a number into a JSON string.
1563     *
1564     * Parameters:
1565     * number - {Number} The number to be serialized.
1566     *
1567     * Returns:
1568     * {String} A JSON string representing the number.
1569     */
1570    'number': function(number) {
1571      return isFinite(number) ? String(number) : "null";
1572    },
1573    /**
1574     * Method: serialize.boolean
1575     * Transform a boolean into a JSON string.
1576     *
1577     * Parameters:
1578     * bool - {Boolean} The boolean to be serialized.
1579     *
1580     * Returns:
1581     * {String} A JSON string representing the boolean.
1582     */
1583    'boolean': function(bool) {
1584      return String(bool);
1585    },
1586    /**
1587     * Method: serialize.date
1588     * Transform a date into a JSON string.
1589     *
1590     * Parameters:
1591     * date - {Date} The date to be serialized.
1592     *
1593     * Returns:
1594     * {String} A JSON string representing the date.
1595     */
1596    'date': function(date) {   
1597      function format(number) {
1598        // Format integers to have at least two digits.
1599        return (number < 10) ? '0' + number : number;
1600      }
1601      return '"' + date.getFullYear() + '-' +
1602        format(date.getMonth() + 1) + '-' +
1603        format(date.getDate()) + 'T' +
1604        format(date.getHours()) + ':' +
1605        format(date.getMinutes()) + ':' +
1606        format(date.getSeconds()) + '"';
1607    }
1608  },
1609  CLASS_NAME: 'ZOO.Format.JSON'
1610});
1611/**
1612 * Class: ZOO.Format.GeoJSON
1613 * Read and write GeoJSON. Create a new parser with the
1614 *     <ZOO.Format.GeoJSON> constructor.
1615 *
1616 * Inherits from:
1617 *  - <ZOO.Format.JSON>
1618 */
1619ZOO.Format.GeoJSON = ZOO.Class(ZOO.Format.JSON, {
1620  /**
1621   * Constructor: ZOO.Format.GeoJSON
1622   * Create a new parser for GeoJSON.
1623   *
1624   * Parameters:
1625   * options - {Object} An optional object whose properties will be set on
1626   *     this instance.
1627   */
1628  initialize: function(options) {
1629    ZOO.Format.JSON.prototype.initialize.apply(this, [options]);
1630  },
1631  /**
1632   * Method: read
1633   * Deserialize a GeoJSON string.
1634   *
1635   * Parameters:
1636   * json - {String} A GeoJSON string
1637   * type - {String} Optional string that determines the structure of
1638   *     the output.  Supported values are "Geometry", "Feature", and
1639   *     "FeatureCollection".  If absent or null, a default of
1640   *     "FeatureCollection" is assumed.
1641   * filter - {Function} A function which will be called for every key and
1642   *     value at every level of the final result. Each value will be
1643   *     replaced by the result of the filter function. This can be used to
1644   *     reform generic objects into instances of classes, or to transform
1645   *     date strings into Date objects.
1646   *
1647   * Returns:
1648   * {Object} The return depends on the value of the type argument. If type
1649   *     is "FeatureCollection" (the default), the return will be an array
1650   *     of <ZOO.Feature>. If type is "Geometry", the input json
1651   *     must represent a single geometry, and the return will be an
1652   *     <ZOO.Geometry>.  If type is "Feature", the input json must
1653   *     represent a single feature, and the return will be an
1654   *     <ZOO.Feature>.
1655   */
1656  read: function(json, type, filter) {
1657    type = (type) ? type : "FeatureCollection";
1658    var results = null;
1659    var obj = null;
1660    if (typeof json == "string")
1661      obj = ZOO.Format.JSON.prototype.read.apply(this,[json, filter]);
1662    else
1663      obj = json;
1664    if(!obj) {
1665      //ZOO.Console.error("Bad JSON: " + json);
1666    } else if(typeof(obj.type) != "string") {
1667      //ZOO.Console.error("Bad GeoJSON - no type: " + json);
1668    } else if(this.isValidType(obj, type)) {
1669      switch(type) {
1670        case "Geometry":
1671          try {
1672            results = this.parseGeometry(obj);
1673          } catch(err) {
1674            //ZOO.Console.error(err);
1675          }
1676          break;
1677        case "Feature":
1678          try {
1679            results = this.parseFeature(obj);
1680            results.type = "Feature";
1681          } catch(err) {
1682            //ZOO.Console.error(err);
1683          }
1684          break;
1685        case "FeatureCollection":
1686          // for type FeatureCollection, we allow input to be any type
1687          results = [];
1688          switch(obj.type) {
1689            case "Feature":
1690              try {
1691                results.push(this.parseFeature(obj));
1692              } catch(err) {
1693                results = null;
1694                //ZOO.Console.error(err);
1695              }
1696              break;
1697            case "FeatureCollection":
1698              for(var i=0, len=obj.features.length; i<len; ++i) {
1699                try {
1700                  results.push(this.parseFeature(obj.features[i]));
1701                } catch(err) {
1702                  results = null;
1703                  //ZOO.Console.error(err);
1704                }
1705              }
1706              break;
1707            default:
1708              try {
1709                var geom = this.parseGeometry(obj);
1710                results.push(new ZOO.Feature(geom));
1711              } catch(err) {
1712                results = null;
1713                //ZOO.Console.error(err);
1714              }
1715          }
1716          break;
1717      }
1718    }
1719    return results;
1720  },
1721  /**
1722   * Method: isValidType
1723   * Check if a GeoJSON object is a valid representative of the given type.
1724   *
1725   * Returns:
1726   * {Boolean} The object is valid GeoJSON object of the given type.
1727   */
1728  isValidType: function(obj, type) {
1729    var valid = false;
1730    switch(type) {
1731      case "Geometry":
1732        if(ZOO.indexOf(
1733              ["Point", "MultiPoint", "LineString", "MultiLineString",
1734              "Polygon", "MultiPolygon", "Box", "GeometryCollection"],
1735              obj.type) == -1) {
1736          // unsupported geometry type
1737          //ZOO.Console.error("Unsupported geometry type: " +obj.type);
1738        } else {
1739          valid = true;
1740        }
1741        break;
1742      case "FeatureCollection":
1743        // allow for any type to be converted to a feature collection
1744        valid = true;
1745        break;
1746      default:
1747        // for Feature types must match
1748        if(obj.type == type) {
1749          valid = true;
1750        } else {
1751          //ZOO.Console.error("Cannot convert types from " +obj.type + " to " + type);
1752        }
1753    }
1754    return valid;
1755  },
1756  /**
1757   * Method: parseFeature
1758   * Convert a feature object from GeoJSON into an
1759   *     <ZOO.Feature>.
1760   *
1761   * Parameters:
1762   * obj - {Object} An object created from a GeoJSON object
1763   *
1764   * Returns:
1765   * {<ZOO.Feature>} A feature.
1766   */
1767  parseFeature: function(obj) {
1768    var feature, geometry, attributes, bbox;
1769    attributes = (obj.properties) ? obj.properties : {};
1770    bbox = (obj.geometry && obj.geometry.bbox) || obj.bbox;
1771    try {
1772      geometry = this.parseGeometry(obj.geometry);
1773    } catch(err) {
1774      // deal with bad geometries
1775      throw err;
1776    }
1777    feature = new ZOO.Feature(geometry, attributes);
1778    if(bbox)
1779      feature.bounds = ZOO.Bounds.fromArray(bbox);
1780    if(obj.id)
1781      feature.fid = obj.id;
1782    return feature;
1783  },
1784  /**
1785   * Method: parseGeometry
1786   * Convert a geometry object from GeoJSON into an <ZOO.Geometry>.
1787   *
1788   * Parameters:
1789   * obj - {Object} An object created from a GeoJSON object
1790   *
1791   * Returns:
1792   * {<ZOO.Geometry>} A geometry.
1793   */
1794  parseGeometry: function(obj) {
1795    if (obj == null)
1796      return null;
1797    var geometry, collection = false;
1798    if(obj.type == "GeometryCollection") {
1799      if(!(obj.geometries instanceof Array)) {
1800        throw "GeometryCollection must have geometries array: " + obj;
1801      }
1802      var numGeom = obj.geometries.length;
1803      var components = new Array(numGeom);
1804      for(var i=0; i<numGeom; ++i) {
1805        components[i] = this.parseGeometry.apply(
1806            this, [obj.geometries[i]]
1807            );
1808      }
1809      geometry = new ZOO.Geometry.Collection(components);
1810      collection = true;
1811    } else {
1812      if(!(obj.coordinates instanceof Array)) {
1813        throw "Geometry must have coordinates array: " + obj;
1814      }
1815      if(!this.parseCoords[obj.type.toLowerCase()]) {
1816        throw "Unsupported geometry type: " + obj.type;
1817      }
1818      try {
1819        geometry = this.parseCoords[obj.type.toLowerCase()].apply(
1820            this, [obj.coordinates]
1821            );
1822      } catch(err) {
1823        // deal with bad coordinates
1824        throw err;
1825      }
1826    }
1827        // We don't reproject collections because the children are reprojected
1828        // for us when they are created.
1829    if (this.internalProjection && this.externalProjection && !collection) {
1830      geometry.transform(this.externalProjection, 
1831          this.internalProjection); 
1832    }                       
1833    return geometry;
1834  },
1835  /**
1836   * Property: parseCoords
1837   * Object with properties corresponding to the GeoJSON geometry types.
1838   *     Property values are functions that do the actual parsing.
1839   */
1840  parseCoords: {
1841    /**
1842     * Method: parseCoords.point
1843     * Convert a coordinate array from GeoJSON into an
1844     *     <ZOO.Geometry.Point>.
1845     *
1846     * Parameters:
1847     * array - {Object} The coordinates array from the GeoJSON fragment.
1848     *
1849     * Returns:
1850     * {<ZOO.Geometry.Point>} A geometry.
1851     */
1852    "point": function(array) {
1853      if(array.length != 2) {
1854        throw "Only 2D points are supported: " + array;
1855      }
1856      return new ZOO.Geometry.Point(array[0], array[1]);
1857    },
1858    /**
1859     * Method: parseCoords.multipoint
1860     * Convert a coordinate array from GeoJSON into an
1861     *     <ZOO.Geometry.MultiPoint>.
1862     *
1863     * Parameters:
1864     * array - {Object} The coordinates array from the GeoJSON fragment.
1865     *
1866     * Returns:
1867     * {<ZOO.Geometry.MultiPoint>} A geometry.
1868     */
1869    "multipoint": function(array) {
1870      var points = [];
1871      var p = null;
1872      for(var i=0, len=array.length; i<len; ++i) {
1873        try {
1874          p = this.parseCoords["point"].apply(this, [array[i]]);
1875        } catch(err) {
1876          throw err;
1877        }
1878        points.push(p);
1879      }
1880      return new ZOO.Geometry.MultiPoint(points);
1881    },
1882    /**
1883     * Method: parseCoords.linestring
1884     * Convert a coordinate array from GeoJSON into an
1885     *     <ZOO.Geometry.LineString>.
1886     *
1887     * Parameters:
1888     * array - {Object} The coordinates array from the GeoJSON fragment.
1889     *
1890     * Returns:
1891     * {<ZOO.Geometry.LineString>} A geometry.
1892     */
1893    "linestring": function(array) {
1894      var points = [];
1895      var p = null;
1896      for(var i=0, len=array.length; i<len; ++i) {
1897        try {
1898          p = this.parseCoords["point"].apply(this, [array[i]]);
1899        } catch(err) {
1900          throw err;
1901        }
1902        points.push(p);
1903      }
1904      return new ZOO.Geometry.LineString(points);
1905    },
1906    /**
1907     * Method: parseCoords.multilinestring
1908     * Convert a coordinate array from GeoJSON into an
1909     *     <ZOO.Geometry.MultiLineString>.
1910     *
1911     * Parameters:
1912     * array - {Object} The coordinates array from the GeoJSON fragment.
1913     *
1914     * Returns:
1915     * {<ZOO.Geometry.MultiLineString>} A geometry.
1916     */
1917    "multilinestring": function(array) {
1918      var lines = [];
1919      var l = null;
1920      for(var i=0, len=array.length; i<len; ++i) {
1921        try {
1922          l = this.parseCoords["linestring"].apply(this, [array[i]]);
1923        } catch(err) {
1924          throw err;
1925        }
1926        lines.push(l);
1927      }
1928      return new ZOO.Geometry.MultiLineString(lines);
1929    },
1930    /**
1931     * Method: parseCoords.polygon
1932     * Convert a coordinate array from GeoJSON into an
1933     *     <ZOO.Geometry.Polygon>.
1934     *
1935     * Parameters:
1936     * array - {Object} The coordinates array from the GeoJSON fragment.
1937     *
1938     * Returns:
1939     * {<ZOO.Geometry.Polygon>} A geometry.
1940     */
1941    "polygon": function(array) {
1942      var rings = [];
1943      var r, l;
1944      for(var i=0, len=array.length; i<len; ++i) {
1945        try {
1946          l = this.parseCoords["linestring"].apply(this, [array[i]]);
1947        } catch(err) {
1948          throw err;
1949        }
1950        r = new ZOO.Geometry.LinearRing(l.components);
1951        rings.push(r);
1952      }
1953      return new ZOO.Geometry.Polygon(rings);
1954    },
1955    /**
1956     * Method: parseCoords.multipolygon
1957     * Convert a coordinate array from GeoJSON into an
1958     *     <ZOO.Geometry.MultiPolygon>.
1959     *
1960     * Parameters:
1961     * array - {Object} The coordinates array from the GeoJSON fragment.
1962     *
1963     * Returns:
1964     * {<ZOO.Geometry.MultiPolygon>} A geometry.
1965     */
1966    "multipolygon": function(array) {
1967      var polys = [];
1968      var p = null;
1969      for(var i=0, len=array.length; i<len; ++i) {
1970        try {
1971          p = this.parseCoords["polygon"].apply(this, [array[i]]);
1972        } catch(err) {
1973          throw err;
1974        }
1975        polys.push(p);
1976      }
1977      return new ZOO.Geometry.MultiPolygon(polys);
1978    },
1979    /**
1980     * Method: parseCoords.box
1981     * Convert a coordinate array from GeoJSON into an
1982     *     <ZOO.Geometry.Polygon>.
1983     *
1984     * Parameters:
1985     * array - {Object} The coordinates array from the GeoJSON fragment.
1986     *
1987     * Returns:
1988     * {<ZOO.Geometry.Polygon>} A geometry.
1989     */
1990    "box": function(array) {
1991      if(array.length != 2) {
1992        throw "GeoJSON box coordinates must have 2 elements";
1993      }
1994      return new ZOO.Geometry.Polygon([
1995          new ZOO.Geometry.LinearRing([
1996            new ZOO.Geometry.Point(array[0][0], array[0][1]),
1997            new ZOO.Geometry.Point(array[1][0], array[0][1]),
1998            new ZOO.Geometry.Point(array[1][0], array[1][1]),
1999            new ZOO.Geometry.Point(array[0][0], array[1][1]),
2000            new Z0O.Geometry.Point(array[0][0], array[0][1])
2001          ])
2002      ]);
2003    }
2004  },
2005  /**
2006   * Method: write
2007   * Serialize a feature, geometry, array of features into a GeoJSON string.
2008   *
2009   * Parameters:
2010   * obj - {Object} An <ZOO.Feature>, <ZOO.Geometry>,
2011   *     or an array of features.
2012   * pretty - {Boolean} Structure the output with newlines and indentation.
2013   *     Default is false.
2014   *
2015   * Returns:
2016   * {String} The GeoJSON string representation of the input geometry,
2017   *     features, or array of features.
2018   */
2019  write: function(obj, pretty) {
2020    var geojson = {
2021      "type": null
2022    };
2023    if(obj instanceof Array) {
2024      geojson.type = "FeatureCollection";
2025      var numFeatures = obj.length;
2026      geojson.features = new Array(numFeatures);
2027      for(var i=0; i<numFeatures; ++i) {
2028        var element = obj[i];
2029        if(!element instanceof ZOO.Feature) {
2030          var msg = "FeatureCollection only supports collections " +
2031            "of features: " + element;
2032          throw msg;
2033        }
2034        geojson.features[i] = this.extract.feature.apply(this, [element]);
2035      }
2036    } else if (obj.CLASS_NAME.indexOf("ZOO.Geometry") == 0) {
2037      geojson = this.extract.geometry.apply(this, [obj]);
2038    } else if (obj instanceof ZOO.Feature) {
2039      geojson = this.extract.feature.apply(this, [obj]);
2040      /*
2041      if(obj.layer && obj.layer.projection) {
2042        geojson.crs = this.createCRSObject(obj);
2043      }
2044      */
2045    }
2046    return ZOO.Format.JSON.prototype.write.apply(this,
2047                                                 [geojson, pretty]);
2048  },
2049  /**
2050   * Method: createCRSObject
2051   * Create the CRS object for an object.
2052   *
2053   * Parameters:
2054   * object - {<ZOO.Feature>}
2055   *
2056   * Returns:
2057   * {Object} An object which can be assigned to the crs property
2058   * of a GeoJSON object.
2059   */
2060  createCRSObject: function(object) {
2061    //var proj = object.layer.projection.toString();
2062    var proj = object.projection.toString();
2063    var crs = {};
2064    if (proj.match(/epsg:/i)) {
2065      var code = parseInt(proj.substring(proj.indexOf(":") + 1));
2066      if (code == 4326) {
2067        crs = {
2068          "type": "OGC",
2069          "properties": {
2070            "urn": "urn:ogc:def:crs:OGC:1.3:CRS84"
2071          }
2072        };
2073      } else {   
2074        crs = {
2075          "type": "EPSG",
2076          "properties": {
2077            "code": code 
2078          }
2079        };
2080      }   
2081    }
2082    return crs;
2083  },
2084  /**
2085   * Property: extract
2086   * Object with properties corresponding to the GeoJSON types.
2087   *     Property values are functions that do the actual value extraction.
2088   */
2089  extract: {
2090    /**
2091     * Method: extract.feature
2092     * Return a partial GeoJSON object representing a single feature.
2093     *
2094     * Parameters:
2095     * feature - {<ZOO.Feature>}
2096     *
2097     * Returns:
2098     * {Object} An object representing the point.
2099     */
2100    'feature': function(feature) {
2101      var geom = this.extract.geometry.apply(this, [feature.geometry]);
2102      return {
2103        "type": "Feature",
2104        "id": feature.fid == null ? feature.id : feature.fid,
2105        "properties": feature.attributes,
2106        "geometry": geom
2107      };
2108    },
2109    /**
2110     * Method: extract.geometry
2111     * Return a GeoJSON object representing a single geometry.
2112     *
2113     * Parameters:
2114     * geometry - {<ZOO.Geometry>}
2115     *
2116     * Returns:
2117     * {Object} An object representing the geometry.
2118     */
2119    'geometry': function(geometry) {
2120      if (geometry == null)
2121        return null;
2122      if (this.internalProjection && this.externalProjection) {
2123        geometry = geometry.clone();
2124        geometry.transform(this.internalProjection, 
2125            this.externalProjection);
2126      }                       
2127      var geometryType = geometry.CLASS_NAME.split('.')[2];
2128      var data = this.extract[geometryType.toLowerCase()].apply(this, [geometry]);
2129      var json;
2130      if(geometryType == "Collection")
2131        json = {
2132          "type": "GeometryCollection",
2133          "geometries": data
2134        };
2135      else
2136        json = {
2137          "type": geometryType,
2138          "coordinates": data
2139        };
2140      return json;
2141    },
2142    /**
2143     * Method: extract.point
2144     * Return an array of coordinates from a point.
2145     *
2146     * Parameters:
2147     * point - {<ZOO.Geometry.Point>}
2148     *
2149     * Returns:
2150     * {Array} An array of coordinates representing the point.
2151     */
2152    'point': function(point) {
2153      return [point.x, point.y];
2154    },
2155    /**
2156     * Method: extract.multipoint
2157     * Return an array of coordinates from a multipoint.
2158     *
2159     * Parameters:
2160     * multipoint - {<ZOO.Geometry.MultiPoint>}
2161     *
2162     * Returns:
2163     * {Array} An array of point coordinate arrays representing
2164     *     the multipoint.
2165     */
2166    'multipoint': function(multipoint) {
2167      var array = [];
2168      for(var i=0, len=multipoint.components.length; i<len; ++i) {
2169        array.push(this.extract.point.apply(this, [multipoint.components[i]]));
2170      }
2171      return array;
2172    },
2173    /**
2174     * Method: extract.linestring
2175     * Return an array of coordinate arrays from a linestring.
2176     *
2177     * Parameters:
2178     * linestring - {<ZOO.Geometry.LineString>}
2179     *
2180     * Returns:
2181     * {Array} An array of coordinate arrays representing
2182     *     the linestring.
2183     */
2184    'linestring': function(linestring) {
2185      var array = [];
2186      for(var i=0, len=linestring.components.length; i<len; ++i) {
2187        array.push(this.extract.point.apply(this, [linestring.components[i]]));
2188      }
2189      return array;
2190    },
2191    /**
2192     * Method: extract.multilinestring
2193     * Return an array of linestring arrays from a linestring.
2194     *
2195     * Parameters:
2196     * multilinestring - {<ZOO.Geometry.MultiLineString>}
2197     *
2198     * Returns:
2199     * {Array} An array of linestring arrays representing
2200     *     the multilinestring.
2201     */
2202    'multilinestring': function(multilinestring) {
2203      var array = [];
2204      for(var i=0, len=multilinestring.components.length; i<len; ++i) {
2205        array.push(this.extract.linestring.apply(this, [multilinestring.components[i]]));
2206      }
2207      return array;
2208    },
2209    /**
2210     * Method: extract.polygon
2211     * Return an array of linear ring arrays from a polygon.
2212     *
2213     * Parameters:
2214     * polygon - {<ZOO.Geometry.Polygon>}
2215     *
2216     * Returns:
2217     * {Array} An array of linear ring arrays representing the polygon.
2218     */
2219    'polygon': function(polygon) {
2220      var array = [];
2221      for(var i=0, len=polygon.components.length; i<len; ++i) {
2222        array.push(this.extract.linestring.apply(this, [polygon.components[i]]));
2223      }
2224      return array;
2225    },
2226    /**
2227     * Method: extract.multipolygon
2228     * Return an array of polygon arrays from a multipolygon.
2229     *
2230     * Parameters:
2231     * multipolygon - {<ZOO.Geometry.MultiPolygon>}
2232     *
2233     * Returns:
2234     * {Array} An array of polygon arrays representing
2235     *     the multipolygon
2236     */
2237    'multipolygon': function(multipolygon) {
2238      var array = [];
2239      for(var i=0, len=multipolygon.components.length; i<len; ++i) {
2240        array.push(this.extract.polygon.apply(this, [multipolygon.components[i]]));
2241      }
2242      return array;
2243    },
2244    /**
2245     * Method: extract.collection
2246     * Return an array of geometries from a geometry collection.
2247     *
2248     * Parameters:
2249     * collection - {<ZOO.Geometry.Collection>}
2250     *
2251     * Returns:
2252     * {Array} An array of geometry objects representing the geometry
2253     *     collection.
2254     */
2255    'collection': function(collection) {
2256      var len = collection.components.length;
2257      var array = new Array(len);
2258      for(var i=0; i<len; ++i) {
2259        array[i] = this.extract.geometry.apply(
2260            this, [collection.components[i]]
2261            );
2262      }
2263      return array;
2264    }
2265  },
2266  CLASS_NAME: 'ZOO.Format.GeoJSON'
2267});
2268/**
2269 * Class: ZOO.Format.KML
2270 * Read/Write KML. Create a new instance with the <ZOO.Format.KML>
2271 *     constructor.
2272 *
2273 * Inherits from:
2274 *  - <ZOO.Format>
2275 */
2276ZOO.Format.KML = ZOO.Class(ZOO.Format, {
2277  /**
2278   * Property: kmlns
2279   * {String} KML Namespace to use. Defaults to 2.2 namespace.
2280   */
2281  kmlns: "http://www.opengis.net/kml/2.2",
2282  /**
2283   * Property: foldersName
2284   * {String} Name of the folders.  Default is "ZOO export".
2285   *          If set to null, no name element will be created.
2286   */
2287  foldersName: "ZOO export",
2288  /**
2289   * Property: foldersDesc
2290   * {String} Description of the folders. Default is "Exported on [date]."
2291   *          If set to null, no description element will be created.
2292   */
2293  foldersDesc: "Created on " + new Date(),
2294  /**
2295   * Property: placemarksDesc
2296   * {String} Name of the placemarks.  Default is "No description available".
2297   */
2298  placemarksDesc: "No description available",
2299  /**
2300   * Property: extractAttributes
2301   * {Boolean} Extract attributes from KML.  Default is true.
2302   *           Extracting styleUrls requires this to be set to true
2303   */
2304  extractAttributes: true,
2305  /**
2306   * Constructor: ZOO.Format.KML
2307   * Create a new parser for KML.
2308   *
2309   * Parameters:
2310   * options - {Object} An optional object whose properties will be set on
2311   *     this instance.
2312   */
2313  initialize: function(options) {
2314    // compile regular expressions once instead of every time they are used
2315    this.regExes = {
2316           trimSpace: (/^\s*|\s*$/g),
2317           removeSpace: (/\s*/g),
2318           splitSpace: (/\s+/),
2319           trimComma: (/\s*,\s*/g),
2320           kmlColor: (/(\w{2})(\w{2})(\w{2})(\w{2})/),
2321           kmlIconPalette: (/root:\/\/icons\/palette-(\d+)(\.\w+)/),
2322           straightBracket: (/\$\[(.*?)\]/g)
2323    };
2324    // KML coordinates are always in longlat WGS84
2325    this.externalProjection = new ZOO.Projection("EPSG:4326");
2326    ZOO.Format.prototype.initialize.apply(this, [options]);
2327  },
2328  /**
2329   * APIMethod: read
2330   * Read data from a string, and return a list of features.
2331   *
2332   * Parameters:
2333   * data    - {String} data to read/parse.
2334   *
2335   * Returns:
2336   * {Array(<ZOO.Feature>)} List of features.
2337   */
2338  read: function(data) {
2339    this.features = [];
2340    data = data.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/, "");
2341    data = new XML(data);
2342    var placemarks = data..*::Placemark;
2343    this.parseFeatures(placemarks);
2344    return this.features;
2345  },
2346  /**
2347   * Method: parseFeatures
2348   * Loop through all Placemark nodes and parse them.
2349   * Will create a list of features
2350   *
2351   * Parameters:
2352   * nodes    - {Array} of {E4XElement} data to read/parse.
2353   * options  - {Object} Hash of options
2354   *
2355   */
2356  parseFeatures: function(nodes) {
2357    var features = new Array(nodes.length());
2358    for(var i=0, len=nodes.length(); i<len; i++) {
2359      var featureNode = nodes[i];
2360      var feature = this.parseFeature.apply(this,[featureNode]) ;
2361      features[i] = feature;
2362    }
2363    this.features = this.features.concat(features);
2364  },
2365  /**
2366   * Method: parseFeature
2367   * This function is the core of the KML parsing code in ZOO.
2368   *     It creates the geometries that are then attached to the returned
2369   *     feature, and calls parseAttributes() to get attribute data out.
2370   *
2371   * Parameters:
2372   * node - {E4XElement}
2373   *
2374   * Returns:
2375   * {<ZOO.Feature>} A vector feature.
2376   */
2377  parseFeature: function(node) {
2378    // only accept one geometry per feature - look for highest "order"
2379    var order = ["MultiGeometry", "Polygon", "LineString", "Point"];
2380    var type, nodeList, geometry, parser;
2381    for(var i=0, len=order.length; i<len; ++i) {
2382      type = order[i];
2383      nodeList = node.descendants(QName(null,type));
2384      if (nodeList.length()> 0) {
2385        var parser = this.parseGeometry[type.toLowerCase()];
2386        if(parser) {
2387          geometry = parser.apply(this, [nodeList[0]]);
2388          if (this.internalProjection && this.externalProjection) {
2389            geometry.transform(this.externalProjection, 
2390                               this.internalProjection); 
2391          }                       
2392        }
2393        // stop looking for different geometry types
2394        break;
2395      }
2396    }
2397    // construct feature (optionally with attributes)
2398    var attributes;
2399    if(this.extractAttributes) {
2400      attributes = this.parseAttributes(node);
2401    }
2402    var feature = new ZOO.Feature(geometry, attributes);
2403    var fid = node.@id || node.@name;
2404    if(fid != null)
2405      feature.fid = fid;
2406    return feature;
2407  },
2408  /**
2409   * Property: parseGeometry
2410   * Properties of this object are the functions that parse geometries based
2411   *     on their type.
2412   */
2413  parseGeometry: {
2414    /**
2415     * Method: parseGeometry.point
2416     * Given a KML node representing a point geometry, create a ZOO
2417     *     point geometry.
2418     *
2419     * Parameters:
2420     * node - {E4XElement} A KML Point node.
2421     *
2422     * Returns:
2423     * {<ZOO.Geometry.Point>} A point geometry.
2424     */
2425    'point': function(node) {
2426      var coordString = node.*::coordinates.toString();
2427      coordString = coordString.replace(this.regExes.removeSpace, "");
2428      coords = coordString.split(",");
2429      var point = null;
2430      if(coords.length > 1) {
2431        // preserve third dimension
2432        if(coords.length == 2) {
2433          coords[2] = null;
2434        }
2435        point = new ZOO.Geometry.Point(coords[0], coords[1], coords[2]);
2436      }
2437      return point;
2438    },
2439    /**
2440     * Method: parseGeometry.linestring
2441     * Given a KML node representing a linestring geometry, create a
2442     *     ZOO linestring geometry.
2443     *
2444     * Parameters:
2445     * node - {E4XElement} A KML LineString node.
2446     *
2447     * Returns:
2448     * {<ZOO.Geometry.LineString>} A linestring geometry.
2449     */
2450    'linestring': function(node, ring) {
2451      var line = null;
2452      var coordString = node.*::coordinates.toString();
2453      coordString = coordString.replace(this.regExes.trimSpace,
2454          "");
2455      coordString = coordString.replace(this.regExes.trimComma,
2456          ",");
2457      var pointList = coordString.split(this.regExes.splitSpace);
2458      var numPoints = pointList.length;
2459      var points = new Array(numPoints);
2460      var coords, numCoords;
2461      for(var i=0; i<numPoints; ++i) {
2462        coords = pointList[i].split(",");
2463        numCoords = coords.length;
2464        if(numCoords > 1) {
2465          if(coords.length == 2) {
2466            coords[2] = null;
2467          }
2468          points[i] = new ZOO.Geometry.Point(coords[0],
2469                                             coords[1],
2470                                             coords[2]);
2471        }
2472      }
2473      if(numPoints) {
2474        if(ring) {
2475          line = new ZOO.Geometry.LinearRing(points);
2476        } else {
2477          line = new ZOO.Geometry.LineString(points);
2478        }
2479      } else {
2480        throw "Bad LineString coordinates: " + coordString;
2481      }
2482      return line;
2483    },
2484    /**
2485     * Method: parseGeometry.polygon
2486     * Given a KML node representing a polygon geometry, create a
2487     *     ZOO polygon geometry.
2488     *
2489     * Parameters:
2490     * node - {E4XElement} A KML Polygon node.
2491     *
2492     * Returns:
2493     * {<ZOO.Geometry.Polygon>} A polygon geometry.
2494     */
2495    'polygon': function(node) {
2496      var nodeList = node..*::LinearRing;
2497      var numRings = nodeList.length();
2498      var components = new Array(numRings);
2499      if(numRings > 0) {
2500        // this assumes exterior ring first, inner rings after
2501        var ring;
2502        for(var i=0, len=nodeList.length(); i<len; ++i) {
2503          ring = this.parseGeometry.linestring.apply(this,
2504                                                     [nodeList[i], true]);
2505          if(ring) {
2506            components[i] = ring;
2507          } else {
2508            throw "Bad LinearRing geometry: " + i;
2509          }
2510        }
2511      }
2512      return new ZOO.Geometry.Polygon(components);
2513    },
2514    /**
2515     * Method: parseGeometry.multigeometry
2516     * Given a KML node representing a multigeometry, create a
2517     *     ZOO geometry collection.
2518     *
2519     * Parameters:
2520     * node - {E4XElement} A KML MultiGeometry node.
2521     *
2522     * Returns:
2523     * {<ZOO.Geometry.Collection>} A geometry collection.
2524     */
2525    'multigeometry': function(node) {
2526      var child, parser;
2527      var parts = [];
2528      var children = node.*::*;
2529      for(var i=0, len=children.length(); i<len; ++i ) {
2530        child = children[i];
2531        var type = child.localName();
2532        var parser = this.parseGeometry[type.toLowerCase()];
2533        if(parser) {
2534          parts.push(parser.apply(this, [child]));
2535        }
2536      }
2537      return new ZOO.Geometry.Collection(parts);
2538    }
2539  },
2540  /**
2541   * Method: parseAttributes
2542   *
2543   * Parameters:
2544   * node - {E4XElement}
2545   *
2546   * Returns:
2547   * {Object} An attributes object.
2548   */
2549  parseAttributes: function(node) {
2550    var attributes = {};
2551    var edNodes = node.*::ExtendedData;
2552    if (edNodes.length() > 0) {
2553      attributes = this.parseExtendedData(edNodes[0])
2554    }
2555    var child, grandchildren;
2556    var children = node.*::*;
2557    for(var i=0, len=children.length(); i<len; ++i) {
2558      child = children[i];
2559      grandchildren = child..*::*;
2560      if(grandchildren.length() == 1) {
2561        var name = child.localName();
2562        var value = child.toString();
2563        if (value) {
2564          value = value.replace(this.regExes.trimSpace, "");
2565          attributes[name] = value;
2566        }
2567      }
2568    }
2569    return attributes;
2570  },
2571  /**
2572   * Method: parseExtendedData
2573   * Parse ExtendedData from KML. Limited support for schemas/datatypes.
2574   *     See http://code.google.com/apis/kml/documentation/kmlreference.html#extendeddata
2575   *     for more information on extendeddata.
2576   *
2577   * Parameters:
2578   * node - {E4XElement}
2579   *
2580   * Returns:
2581   * {Object} An attributes object.
2582   */
2583  parseExtendedData: function(node) {
2584    var attributes = {};
2585    var dataNodes = node.*::Data;
2586    for (var i = 0, len = dataNodes.length(); i < len; i++) {
2587      var data = dataNodes[i];
2588      var key = data.@name;
2589      var ed = {};
2590      var valueNode = data.*::value;
2591      if (valueNode.length() > 0)
2592        ed['value'] = valueNode[0].toString();
2593      var nameNode = data.*::displayName;
2594      if (nameNode.length() > 0)
2595        ed['displayName'] = valueNode[0].toString();
2596      attributes[key] = ed;
2597    }
2598    return attributes;
2599  },
2600  /**
2601   * Method: write
2602   * Accept Feature Collection, and return a string.
2603   *
2604   * Parameters:
2605   * features - {Array(<ZOO.Feature>} An array of features.
2606   *
2607   * Returns:
2608   * {String} A KML string.
2609   */
2610  write: function(features) {
2611    if(!(features instanceof Array))
2612      features = [features];
2613    var kml = new XML('<kml xmlns="'+this.kmlns+'"></kml>');
2614    var folder = kml.Document.Folder;
2615    folder.name = this.foldersName;
2616    folder.description = this.foldersDesc;
2617    for(var i=0, len=features.length; i<len; ++i) {
2618      folder.Placemark[i] = this.createPlacemark(features[i]);
2619    }
2620    return kml.toXMLString();
2621  },
2622  /**
2623   * Method: createPlacemark
2624   * Creates and returns a KML placemark node representing the given feature.
2625   *
2626   * Parameters:
2627   * feature - {<ZOO.Feature>}
2628   *
2629   * Returns:
2630   * {E4XElement}
2631   */
2632  createPlacemark: function(feature) {
2633    var placemark = new XML('<Placemark xmlns="'+this.kmlns+'"></Placemark>');
2634    placemark.name = (feature.attributes.name) ?
2635                    feature.attributes.name : feature.id;
2636    placemark.description = (feature.attributes.description) ?
2637                             feature.attributes.description : this.placemarksDesc;
2638    if(feature.fid != null)
2639      placemark.@id = feature.fid;
2640    placemark.*[2] = this.buildGeometryNode(feature.geometry);
2641    return placemark;
2642  },
2643  /**
2644   * Method: buildGeometryNode
2645   * Builds and returns a KML geometry node with the given geometry.
2646   *
2647   * Parameters:
2648   * geometry - {<ZOO.Geometry>}
2649   *
2650   * Returns:
2651   * {E4XElement}
2652   */
2653  buildGeometryNode: function(geometry) {
2654    if (this.internalProjection && this.externalProjection) {
2655      geometry = geometry.clone();
2656      geometry.transform(this.internalProjection, 
2657                         this.externalProjection);
2658    }
2659    var className = geometry.CLASS_NAME;
2660    var type = className.substring(className.lastIndexOf(".") + 1);
2661    var builder = this.buildGeometry[type.toLowerCase()];
2662    var node = null;
2663    if(builder) {
2664      node = builder.apply(this, [geometry]);
2665    }
2666    return node;
2667  },
2668  /**
2669   * Property: buildGeometry
2670   * Object containing methods to do the actual geometry node building
2671   *     based on geometry type.
2672   */
2673  buildGeometry: {
2674    /**
2675     * Method: buildGeometry.point
2676     * Given a ZOO point geometry, create a KML point.
2677     *
2678     * Parameters:
2679     * geometry - {<ZOO.Geometry.Point>} A point geometry.
2680     *
2681     * Returns:
2682     * {E4XElement} A KML point node.
2683     */
2684    'point': function(geometry) {
2685      var kml = new XML('<Point xmlns="'+this.kmlns+'"></Point>');
2686      kml.coordinates = this.buildCoordinatesNode(geometry);
2687      return kml;
2688    },
2689    /**
2690     * Method: buildGeometry.multipoint
2691     * Given a ZOO multipoint geometry, create a KML
2692     *     GeometryCollection.
2693     *
2694     * Parameters:
2695     * geometry - {<ZOO.Geometry.MultiPoint>} A multipoint geometry.
2696     *
2697     * Returns:
2698     * {E4XElement} A KML GeometryCollection node.
2699     */
2700    'multipoint': function(geometry) {
2701      return this.buildGeometry.collection.apply(this, [geometry]);
2702    },
2703    /**
2704     * Method: buildGeometry.linestring
2705     * Given a ZOO linestring geometry, create a KML linestring.
2706     *
2707     * Parameters:
2708     * geometry - {<ZOO.Geometry.LineString>} A linestring geometry.
2709     *
2710     * Returns:
2711     * {E4XElement} A KML linestring node.
2712     */
2713    'linestring': function(geometry) {
2714      var kml = new XML('<LineString xmlns="'+this.kmlns+'"></LineString>');
2715      kml.coordinates = this.buildCoordinatesNode(geometry);
2716      return kml;
2717    },
2718    /**
2719     * Method: buildGeometry.multilinestring
2720     * Given a ZOO multilinestring geometry, create a KML
2721     *     GeometryCollection.
2722     *
2723     * Parameters:
2724     * geometry - {<ZOO.Geometry.MultiLineString>} A multilinestring geometry.
2725     *
2726     * Returns:
2727     * {E4XElement} A KML GeometryCollection node.
2728     */
2729    'multilinestring': function(geometry) {
2730      return this.buildGeometry.collection.apply(this, [geometry]);
2731    },
2732    /**
2733     * Method: buildGeometry.linearring
2734     * Given a ZOO linearring geometry, create a KML linearring.
2735     *
2736     * Parameters:
2737     * geometry - {<ZOO.Geometry.LinearRing>} A linearring geometry.
2738     *
2739     * Returns:
2740     * {E4XElement} A KML linearring node.
2741     */
2742    'linearring': function(geometry) {
2743      var kml = new XML('<LinearRing xmlns="'+this.kmlns+'"></LinearRing>');
2744      kml.coordinates = this.buildCoordinatesNode(geometry);
2745      return kml;
2746    },
2747    /**
2748     * Method: buildGeometry.polygon
2749     * Given a ZOO polygon geometry, create a KML polygon.
2750     *
2751     * Parameters:
2752     * geometry - {<ZOO.Geometry.Polygon>} A polygon geometry.
2753     *
2754     * Returns:
2755     * {E4XElement} A KML polygon node.
2756     */
2757    'polygon': function(geometry) {
2758      var kml = new XML('<Polygon xmlns="'+this.kmlns+'"></Polygon>');
2759      var rings = geometry.components;
2760      var ringMember, ringGeom, type;
2761      for(var i=0, len=rings.length; i<len; ++i) {
2762        type = (i==0) ? "outerBoundaryIs" : "innerBoundaryIs";
2763        ringMember = new XML('<'+type+' xmlns="'+this.kmlns+'"></'+type+'>');
2764        ringMember.LinearRing = this.buildGeometry.linearring.apply(this,[rings[i]]);
2765        kml.*[i] = ringMember;
2766      }
2767      return kml;
2768    },
2769    /**
2770     * Method: buildGeometry.multipolygon
2771     * Given a ZOO multipolygon geometry, create a KML
2772     *     GeometryCollection.
2773     *
2774     * Parameters:
2775     * geometry - {<ZOO.Geometry.Point>} A multipolygon geometry.
2776     *
2777     * Returns:
2778     * {E4XElement} A KML GeometryCollection node.
2779     */
2780    'multipolygon': function(geometry) {
2781      return this.buildGeometry.collection.apply(this, [geometry]);
2782    },
2783    /**
2784     * Method: buildGeometry.collection
2785     * Given a ZOO geometry collection, create a KML MultiGeometry.
2786     *
2787     * Parameters:
2788     * geometry - {<ZOO.Geometry.Collection>} A geometry collection.
2789     *
2790     * Returns:
2791     * {E4XElement} A KML MultiGeometry node.
2792     */
2793    'collection': function(geometry) {
2794      var kml = new XML('<MultiGeometry xmlns="'+this.kmlns+'"></MultiGeometry>');
2795      var child;
2796      for(var i=0, len=geometry.components.length; i<len; ++i) {
2797        kml.*[i] = this.buildGeometryNode.apply(this,[geometry.components[i]]);
2798      }
2799      return kml;
2800    }
2801  },
2802  /**
2803   * Method: buildCoordinatesNode
2804   * Builds and returns the KML coordinates node with the given geometry
2805   *     <coordinates>...</coordinates>
2806   *
2807   * Parameters:
2808   * geometry - {<ZOO.Geometry>}
2809   *
2810   * Return:
2811   * {E4XElement}
2812   */
2813  buildCoordinatesNode: function(geometry) {
2814    var cooridnates = new XML('<coordinates xmlns="'+this.kmlns+'"></coordinates>');
2815    var points = geometry.components;
2816    if(points) {
2817      // LineString or LinearRing
2818      var point;
2819      var numPoints = points.length;
2820      var parts = new Array(numPoints);
2821      for(var i=0; i<numPoints; ++i) {
2822        point = points[i];
2823        parts[i] = point.x + "," + point.y;
2824      }
2825      coordinates = parts.join(" ");
2826    } else {
2827      // Point
2828      coordinates = geometry.x + "," + geometry.y;
2829    }
2830    return coordinates;
2831  },
2832  CLASS_NAME: 'ZOO.Format.KML'
2833});
2834/**
2835 * Class: ZOO.Format.GML
2836 * Read/Write GML. Create a new instance with the <ZOO.Format.GML>
2837 *     constructor.  Supports the GML simple features profile.
2838 *
2839 * Inherits from:
2840 *  - <ZOO.Format>
2841 */
2842ZOO.Format.GML = ZOO.Class(ZOO.Format, {
2843  /**
2844   * Property: schemaLocation
2845   * {String} Schema location for a particular minor version.
2846   */
2847  schemaLocation: "http://www.opengis.net/gml http://schemas.opengis.net/gml/2.1.2/feature.xsd",
2848  /**
2849   * Property: namespaces
2850   * {Object} Mapping of namespace aliases to namespace URIs.
2851   */
2852  namespaces: {
2853    ogr: "http://ogr.maptools.org/",
2854    gml: "http://www.opengis.net/gml",
2855    xlink: "http://www.w3.org/1999/xlink",
2856    xsi: "http://www.w3.org/2001/XMLSchema-instance",
2857    wfs: "http://www.opengis.net/wfs" // this is a convenience for reading wfs:FeatureCollection
2858  },
2859  /**
2860   * Property: defaultPrefix
2861   */
2862  defaultPrefix: 'ogr',
2863  /**
2864   * Property: collectionName
2865   * {String} Name of featureCollection element.
2866   */
2867  collectionName: "FeatureCollection",
2868  /*
2869   * Property: featureName
2870   * {String} Element name for features. Default is "sql_statement".
2871   */
2872  featureName: "sql_statement",
2873  /**
2874   * Property: geometryName
2875   * {String} Name of geometry element.  Defaults to "geometryProperty".
2876   */
2877  geometryName: "geometryProperty",
2878  /**
2879   * Property: xy
2880   * {Boolean} Order of the GML coordinate true:(x,y) or false:(y,x)
2881   * Changing is not recommended, a new Format should be instantiated.
2882   */
2883  xy: true,
2884  /**
2885   * Property: extractAttributes
2886   * {Boolean} Could we extract attributes
2887   */
2888  extractAttributes: true,
2889  /**
2890   * Constructor: ZOO.Format.GML
2891   * Create a new parser for GML.
2892   *
2893   * Parameters:
2894   * options - {Object} An optional object whose properties will be set on
2895   *     this instance.
2896   */
2897  initialize: function(options) {
2898    // compile regular expressions once instead of every time they are used
2899    this.regExes = {
2900      trimSpace: (/^\s*|\s*$/g),
2901      removeSpace: (/\s*/g),
2902      splitSpace: (/\s+/),
2903      trimComma: (/\s*,\s*/g)
2904    };
2905    ZOO.Format.prototype.initialize.apply(this, [options]);
2906  },
2907  /**
2908   * Method: read
2909   * Read data from a string, and return a list of features.
2910   *
2911   * Parameters:
2912   * data - {String} data to read/parse.
2913   *
2914   * Returns:
2915   * {Array(<ZOO.Feature>)} An array of features.
2916   */
2917  read: function(data) {
2918    this.features = [];
2919    data = data.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/, "");
2920    data = new XML(data);
2921
2922    var gmlns = Namespace(this.namespaces['gml']);
2923    var featureNodes = data..gmlns::featureMember;
2924    if (data.localName() == 'featureMember')
2925      featureNodes = data;
2926    var features = [];
2927    for(var i=0,len=featureNodes.length(); i<len; i++) {
2928      var feature = this.parseFeature(featureNodes[i]);
2929      if(feature) {
2930        features.push(feature);
2931      }
2932    }
2933    return features;
2934  },
2935  /**
2936   * Method: parseFeature
2937   * This function is the core of the GML parsing code in ZOO.
2938   *    It creates the geometries that are then attached to the returned
2939   *    feature, and calls parseAttributes() to get attribute data out.
2940   *   
2941   * Parameters:
2942   * node - {E4XElement} A GML feature node.
2943   */
2944  parseFeature: function(node) {
2945    // only accept one geometry per feature - look for highest "order"
2946    var gmlns = Namespace(this.namespaces['gml']);
2947    var order = ["MultiPolygon", "Polygon",
2948                 "MultiLineString", "LineString",
2949                 "MultiPoint", "Point", "Envelope", "Box"];
2950    var type, nodeList, geometry, parser;
2951    for(var i=0; i<order.length; ++i) {
2952      type = order[i];
2953      nodeList = node.descendants(QName(gmlns,type));
2954      if (nodeList.length() > 0) {
2955        var parser = this.parseGeometry[type.toLowerCase()];
2956        if(parser) {
2957          geometry = parser.apply(this, [nodeList[0]]);
2958          if (this.internalProjection && this.externalProjection) {
2959            geometry.transform(this.externalProjection, 
2960                               this.internalProjection); 
2961          }                       
2962        }
2963        // stop looking for different geometry types
2964        break;
2965      }
2966    }
2967    var attributes;
2968    if(this.extractAttributes) {
2969      attributes = this.parseAttributes(node);
2970    }
2971    var feature = new ZOO.Feature(geometry, attributes);
2972    return feature;
2973  },
2974  /**
2975   * Property: parseGeometry
2976   * Properties of this object are the functions that parse geometries based
2977   *     on their type.
2978   */
2979  parseGeometry: {
2980    /**
2981     * Method: parseGeometry.point
2982     * Given a GML node representing a point geometry, create a ZOO
2983     *     point geometry.
2984     *
2985     * Parameters:
2986     * node - {E4XElement} A GML node.
2987     *
2988     * Returns:
2989     * {<ZOO.Geometry.Point>} A point geometry.
2990     */
2991    'point': function(node) {
2992      /**
2993       * Three coordinate variations to consider:
2994       * 1) <gml:pos>x y z</gml:pos>
2995       * 2) <gml:coordinates>x, y, z</gml:coordinates>
2996       * 3) <gml:coord><gml:X>x</gml:X><gml:Y>y</gml:Y></gml:coord>
2997       */
2998      var nodeList, coordString;
2999      var coords = [];
3000      // look for <gml:pos>
3001      var nodeList = node..*::pos;
3002      if(nodeList.length() > 0) {
3003        coordString = nodeList[0].toString();
3004        coordString = coordString.replace(this.regExes.trimSpace, "");
3005        coords = coordString.split(this.regExes.splitSpace);
3006      }
3007      // look for <gml:coordinates>
3008      if(coords.length == 0) {
3009        nodeList = node..*::coordinates;
3010        if(nodeList.length() > 0) {
3011          coordString = nodeList[0].toString();
3012          coordString = coordString.replace(this.regExes.removeSpace,"");
3013          coords = coordString.split(",");
3014        }
3015      }
3016      // look for <gml:coord>
3017      if(coords.length == 0) {
3018        nodeList = node..*::coord;
3019        if(nodeList.length() > 0) {
3020          var xList = nodeList[0].*::X;
3021          var yList = nodeList[0].*::Y;
3022          if(xList.length() > 0 && yList.length() > 0)
3023            coords = [xList[0].toString(),
3024                      yList[0].toString()];
3025        }
3026      }
3027      // preserve third dimension
3028      if(coords.length == 2)
3029        coords[2] = null;
3030      if (this.xy)
3031        return new ZOO.Geometry.Point(coords[0],coords[1],coords[2]);
3032      else
3033        return new ZOO.Geometry.Point(coords[1],coords[0],coords[2]);
3034    },
3035    /**
3036     * Method: parseGeometry.multipoint
3037     * Given a GML node representing a multipoint geometry, create a
3038     *     ZOO multipoint geometry.
3039     *
3040     * Parameters:
3041     * node - {E4XElement} A GML node.
3042     *
3043     * Returns:
3044     * {<ZOO.Geometry.MultiPoint>} A multipoint geometry.
3045     */
3046    'multipoint': function(node) {
3047      var nodeList = node..*::Point;
3048      var components = [];
3049      if(nodeList.length() > 0) {
3050        var point;
3051        for(var i=0, len=nodeList.length(); i<len; ++i) {
3052          point = this.parseGeometry.point.apply(this, [nodeList[i]]);
3053          if(point)
3054            components.push(point);
3055        }
3056      }
3057      return new ZOO.Geometry.MultiPoint(components);
3058    },
3059    /**
3060     * Method: parseGeometry.linestring
3061     * Given a GML node representing a linestring geometry, create a
3062     *     ZOO linestring geometry.
3063     *
3064     * Parameters:
3065     * node - {E4XElement} A GML node.
3066     *
3067     * Returns:
3068     * {<ZOO.Geometry.LineString>} A linestring geometry.
3069     */
3070    'linestring': function(node, ring) {
3071      /**
3072       * Two coordinate variations to consider:
3073       * 1) <gml:posList dimension="d">x0 y0 z0 x1 y1 z1</gml:posList>
3074       * 2) <gml:coordinates>x0, y0, z0 x1, y1, z1</gml:coordinates>
3075       */
3076      var nodeList, coordString;
3077      var coords = [];
3078      var points = [];
3079      // look for <gml:posList>
3080      nodeList = node..*::posList;
3081      if(nodeList.length() > 0) {
3082        coordString = nodeList[0].toString();
3083        coordString = coordString.replace(this.regExes.trimSpace, "");
3084        coords = coordString.split(this.regExes.splitSpace);
3085        var dim = parseInt(nodeList[0].@dimension);
3086        var j, x, y, z;
3087        for(var i=0; i<coords.length/dim; ++i) {
3088          j = i * dim;
3089          x = coords[j];
3090          y = coords[j+1];
3091          z = (dim == 2) ? null : coords[j+2];
3092          if (this.xy)
3093            points.push(new ZOO.Geometry.Point(x, y, z));
3094          else
3095            points.push(new Z0O.Geometry.Point(y, x, z));
3096        }
3097      }
3098      // look for <gml:coordinates>
3099      if(coords.length == 0) {
3100        nodeList = node..*::coordinates;
3101        if(nodeList.length() > 0) {
3102          coordString = nodeList[0].toString();
3103          coordString = coordString.replace(this.regExes.trimSpace,"");
3104          coordString = coordString.replace(this.regExes.trimComma,",");
3105          var pointList = coordString.split(this.regExes.splitSpace);
3106          for(var i=0; i<pointList.length; ++i) {
3107            coords = pointList[i].split(",");
3108            if(coords.length == 2)
3109              coords[2] = null;
3110            if (this.xy)
3111              points.push(new ZOO.Geometry.Point(coords[0],coords[1],coords[2]));
3112            else
3113              points.push(new ZOO.Geometry.Point(coords[1],coords[0],coords[2]));
3114          }
3115        }
3116      }
3117      var line = null;
3118      if(points.length != 0) {
3119        if(ring)
3120          line = new ZOO.Geometry.LinearRing(points);
3121        else
3122          line = new ZOO.Geometry.LineString(points);
3123      }
3124      return line;
3125    },
3126    /**
3127     * Method: parseGeometry.multilinestring
3128     * Given a GML node representing a multilinestring geometry, create a
3129     *     ZOO multilinestring geometry.
3130     *
3131     * Parameters:
3132     * node - {E4XElement} A GML node.
3133     *
3134     * Returns:
3135     * {<ZOO.Geometry.MultiLineString>} A multilinestring geometry.
3136     */
3137    'multilinestring': function(node) {
3138      var nodeList = node..*::LineString;
3139      var components = [];
3140      if(nodeList.length() > 0) {
3141        var line;
3142        for(var i=0, len=nodeList.length(); i<len; ++i) {
3143          line = this.parseGeometry.linestring.apply(this, [nodeList[i]]);
3144          if(point)
3145            components.push(point);
3146        }
3147      }
3148      return new ZOO.Geometry.MultiLineString(components);
3149    },
3150    /**
3151     * Method: parseGeometry.polygon
3152     * Given a GML node representing a polygon geometry, create a
3153     *     ZOO polygon geometry.
3154     *
3155     * Parameters:
3156     * node - {E4XElement} A GML node.
3157     *
3158     * Returns:
3159     * {<ZOO.Geometry.Polygon>} A polygon geometry.
3160     */
3161    'polygon': function(node) {
3162      nodeList = node..*::LinearRing;
3163      var components = [];
3164      if(nodeList.length() > 0) {
3165        // this assumes exterior ring first, inner rings after
3166        var ring;
3167        for(var i=0, len = nodeList.length(); i<len; ++i) {
3168          ring = this.parseGeometry.linestring.apply(this,[nodeList[i], true]);
3169          if(ring)
3170            components.push(ring);
3171        }
3172      }
3173      return new ZOO.Geometry.Polygon(components);
3174    },
3175    /**
3176     * Method: parseGeometry.multipolygon
3177     * Given a GML node representing a multipolygon geometry, create a
3178     *     ZOO multipolygon geometry.
3179     *
3180     * Parameters:
3181     * node - {E4XElement} A GML node.
3182     *
3183     * Returns:
3184     * {<ZOO.Geometry.MultiPolygon>} A multipolygon geometry.
3185     */
3186    'multipolygon': function(node) {
3187      var nodeList = node..*::Polygon;
3188      var components = [];
3189      if(nodeList.length() > 0) {
3190        var polygon;
3191        for(var i=0, len=nodeList.length(); i<len; ++i) {
3192          polygon = this.parseGeometry.polygon.apply(this, [nodeList[i]]);
3193          if(polygon)
3194            components.push(polygon);
3195        }
3196      }
3197      return new ZOO.Geometry.MultiPolygon(components);
3198    },
3199    /**
3200     * Method: parseGeometry.polygon
3201     * Given a GML node representing an envelope, create a
3202     *     ZOO polygon geometry.
3203     *
3204     * Parameters:
3205     * node - {E4XElement} A GML node.
3206     *
3207     * Returns:
3208     * {<ZOO.Geometry.Polygon>} A polygon geometry.
3209     */
3210    'envelope': function(node) {
3211      var components = [];
3212      var coordString;
3213      var envelope;
3214      var lpoint = node..*::lowerCorner;
3215      if (lpoint.length() > 0) {
3216        var coords = [];
3217        if(lpoint.length() > 0) {
3218          coordString = lpoint[0].toString();
3219          coordString = coordString.replace(this.regExes.trimSpace, "");
3220          coords = coordString.split(this.regExes.splitSpace);
3221        }
3222        if(coords.length == 2)
3223          coords[2] = null;
3224        if (this.xy)
3225          var lowerPoint = new ZOO.Geometry.Point(coords[0], coords[1],coords[2]);
3226        else
3227          var lowerPoint = new ZOO.Geometry.Point(coords[1], coords[0],coords[2]);
3228      }
3229      var upoint = node..*::upperCorner;
3230      if (upoint.length() > 0) {
3231        var coords = [];
3232        if(upoint.length > 0) {
3233          coordString = upoint[0].toString();
3234          coordString = coordString.replace(this.regExes.trimSpace, "");
3235          coords = coordString.split(this.regExes.splitSpace);
3236        }
3237        if(coords.length == 2)
3238          coords[2] = null;
3239        if (this.xy)
3240          var upperPoint = new ZOO.Geometry.Point(coords[0], coords[1],coords[2]);
3241        else
3242          var upperPoint = new ZOO.Geometry.Point(coords[1], coords[0],coords[2]);
3243      }
3244      if (lowerPoint && upperPoint) {
3245        components.push(new ZOO.Geometry.Point(lowerPoint.x, lowerPoint.y));
3246        components.push(new ZOO.Geometry.Point(upperPoint.x, lowerPoint.y));
3247        components.push(new ZOO.Geometry.Point(upperPoint.x, upperPoint.y));
3248        components.push(new ZOO.Geometry.Point(lowerPoint.x, upperPoint.y));
3249        components.push(new ZOO.Geometry.Point(lowerPoint.x, lowerPoint.y));
3250        var ring = new ZOO.Geometry.LinearRing(components);
3251        envelope = new ZOO.Geometry.Polygon([ring]);
3252      }
3253      return envelope;
3254    }
3255  },
3256  /**
3257   * Method: parseAttributes
3258   *
3259   * Parameters:
3260   * node - {<E4XElement>}
3261   *
3262   * Returns:
3263   * {Object} An attributes object.
3264   */
3265  parseAttributes: function(node) {
3266    var attributes = {};
3267    // assume attributes are children of the first type 1 child
3268    var childNode = node.*::*[0];
3269    var child, grandchildren;
3270    var children = childNode.*::*;
3271    for(var i=0, len=children.length(); i<len; ++i) {
3272      child = children[i];
3273      grandchildren = child..*::*;
3274      if(grandchildren.length() == 1) {
3275        var name = child.localName();
3276        var value = child.toString();
3277        if (value) {
3278          value = value.replace(this.regExes.trimSpace, "");
3279          attributes[name] = value;
3280        } else
3281          attributes[name] = null;
3282      }
3283    }
3284    return attributes;
3285  },
3286  /**
3287   * Method: write
3288   * Generate a GML document string given a list of features.
3289   *
3290   * Parameters:
3291   * features - {Array(<ZOO.Feature>)} List of features to
3292   *     serialize into a string.
3293   *
3294   * Returns:
3295   * {String} A string representing the GML document.
3296   */
3297  write: function(features) {
3298    if(!(features instanceof Array)) {
3299      features = [features];
3300    }
3301    var pfx = this.defaultPrefix;
3302    var name = pfx+':'+this.collectionName;
3303    var gml = new XML('<'+name+' xmlns:'+pfx+'="'+this.namespaces[pfx]+'" xmlns:gml="'+this.namespaces['gml']+'" xmlns:xsi="'+this.namespaces['xsi']+'" xsi:schemaLocation="'+this.schemaLocation+'"></'+name+'>');
3304    for(var i=0; i<features.length; i++) {
3305      gml.*::*[i] = this.createFeature(features[i]);
3306    }
3307    return gml.toXMLString();
3308  },
3309  /**
3310   * Method: createFeature
3311   * Accept an ZOO.Feature, and build a GML node for it.
3312   *
3313   * Parameters:
3314   * feature - {<ZOO.Feature>} The feature to be built as GML.
3315   *
3316   * Returns:
3317   * {E4XElement} A node reprensting the feature in GML.
3318   */
3319  createFeature: function(feature) {
3320    var pfx = this.defaultPrefix;
3321    var name = pfx+':'+this.featureName;
3322    var fid = feature.fid || feature.id;
3323    var gml = new XML('<gml:featureMember xmlns:gml="'+this.namespaces['gml']+'"><'+name+' xmlns:'+pfx+'="'+this.namespaces[pfx]+'" fid="'+fid+'"></'+name+'></gml:featureMember>');
3324    var geometry = feature.geometry;
3325    gml.*::*[0].*::* = this.buildGeometryNode(geometry);
3326    for(var attr in feature.attributes) {
3327      var attrNode = new XML('<'+pfx+':'+attr+' xmlns:'+pfx+'="'+this.namespaces[pfx]+'">'+feature.attributes[attr]+'</'+pfx+':'+attr+'>');
3328      gml.*::*[0].appendChild(attrNode);
3329    }
3330    return gml;
3331  },
3332  /**
3333   * Method: buildGeometryNode
3334   *
3335   * Parameters:
3336   * geometry - {<ZOO.Geometry>} The geometry to be built as GML.
3337   *
3338   * Returns:
3339   * {E4XElement} A node reprensting the geometry in GML.
3340   */
3341  buildGeometryNode: function(geometry) {
3342    if (this.externalProjection && this.internalProjection) {
3343      geometry = geometry.clone();
3344      geometry.transform(this.internalProjection, 
3345          this.externalProjection);
3346    }   
3347    var className = geometry.CLASS_NAME;
3348    var type = className.substring(className.lastIndexOf(".") + 1);
3349    var builder = this.buildGeometry[type.toLowerCase()];
3350    var pfx = this.defaultPrefix;
3351    var name = pfx+':'+this.geometryName;
3352    var gml = new XML('<'+name+' xmlns:'+pfx+'="'+this.namespaces[pfx]+'"></'+name+'>');
3353    if (builder)
3354      gml.*::* = builder.apply(this, [geometry]);
3355    return gml;
3356  },
3357  /**
3358   * Property: buildGeometry
3359   * Object containing methods to do the actual geometry node building
3360   *     based on geometry type.
3361   */
3362  buildGeometry: {
3363    /**
3364     * Method: buildGeometry.point
3365     * Given a ZOO point geometry, create a GML point.
3366     *
3367     * Parameters:
3368     * geometry - {<ZOO.Geometry.Point>} A point geometry.
3369     *
3370     * Returns:
3371     * {E4XElement} A GML point node.
3372     */
3373    'point': function(geometry) {
3374      var gml = new XML('<gml:Point xmlns:gml="'+this.namespaces['gml']+'"></gml:Point>');
3375      gml.*::*[0] = this.buildCoordinatesNode(geometry);
3376      return gml;
3377    },
3378    /**
3379     * Method: buildGeometry.multipoint
3380     * Given a ZOO multipoint geometry, create a GML multipoint.
3381     *
3382     * Parameters:
3383     * geometry - {<ZOO.Geometry.MultiPoint>} A multipoint geometry.
3384     *
3385     * Returns:
3386     * {E4XElement} A GML multipoint node.
3387     */
3388    'multipoint': function(geometry) {
3389      var gml = new XML('<gml:MultiPoint xmlns:gml="'+this.namespaces['gml']+'"></gml:MultiPoint>');
3390      var points = geometry.components;
3391      var pointMember;
3392      for(var i=0; i<points.length; i++) { 
3393        pointMember = new XML('<gml:pointMember xmlns:gml="'+this.namespaces['gml']+'"></gml:pointMember>');
3394        pointMember.*::* = this.buildGeometry.point.apply(this,[points[i]]);
3395        gml.*::*[i] = pointMember;
3396      }
3397      return gml;           
3398    },
3399    /**
3400     * Method: buildGeometry.linestring
3401     * Given a ZOO linestring geometry, create a GML linestring.
3402     *
3403     * Parameters:
3404     * geometry - {<ZOO.Geometry.LineString>} A linestring geometry.
3405     *
3406     * Returns:
3407     * {E4XElement} A GML linestring node.
3408     */
3409    'linestring': function(geometry) {
3410      var gml = new XML('<gml:LineString xmlns:gml="'+this.namespaces['gml']+'"></gml:LineString>');
3411      gml.*::*[0] = this.buildCoordinatesNode(geometry);
3412      return gml;
3413    },
3414    /**
3415     * Method: buildGeometry.multilinestring
3416     * Given a ZOO multilinestring geometry, create a GML
3417     *     multilinestring.
3418     *
3419     * Parameters:
3420     * geometry - {<ZOO.Geometry.MultiLineString>} A multilinestring
3421     *     geometry.
3422     *
3423     * Returns:
3424     * {E4XElement} A GML multilinestring node.
3425     */
3426    'multilinestring': function(geometry) {
3427      var gml = new XML('<gml:MultiLineString xmlns:gml="'+this.namespaces['gml']+'"></gml:MultiLineString>');
3428      var lines = geometry.components;
3429      var lineMember;
3430      for(var i=0; i<lines.length; i++) { 
3431        lineMember = new XML('<gml:lineStringMember xmlns:gml="'+this.namespaces['gml']+'"></gml:lineStringMember>');
3432        lineMember.*::* = this.buildGeometry.linestring.apply(this,[lines[i]]);
3433        gml.*::*[i] = lineMember;
3434      }
3435      return gml;           
3436    },
3437    /**
3438     * Method: buildGeometry.linearring
3439     * Given a ZOO linearring geometry, create a GML linearring.
3440     *
3441     * Parameters:
3442     * geometry - {<ZOO.Geometry.LinearRing>} A linearring geometry.
3443     *
3444     * Returns:
3445     * {E4XElement} A GML linearring node.
3446     */
3447    'linearring': function(geometry) {
3448      var gml = new XML('<gml:LinearRing xmlns:gml="'+this.namespaces['gml']+'"></gml:LinearRing>');
3449      gml.*::*[0] = this.buildCoordinatesNode(geometry);
3450      return gml;
3451    },
3452    /**
3453     * Method: buildGeometry.polygon
3454     * Given an ZOO polygon geometry, create a GML polygon.
3455     *
3456     * Parameters:
3457     * geometry - {<ZOO.Geometry.Polygon>} A polygon geometry.
3458     *
3459     * Returns:
3460     * {E4XElement} A GML polygon node.
3461     */
3462    'polygon': function(geometry) {
3463      var gml = new XML('<gml:Polygon xmlns:gml="'+this.namespaces['gml']+'"></gml:Polygon>');
3464      var rings = geometry.components;
3465      var ringMember, type;
3466      for(var i=0; i<rings.length; ++i) {
3467        type = (i==0) ? "outerBoundaryIs" : "innerBoundaryIs";
3468        var ringMember = new XML('<gml:'+type+' xmlns:gml="'+this.namespaces['gml']+'"></gml:'+type+'>');
3469        ringMember.*::* = this.buildGeometry.linearring.apply(this,[rings[i]]);
3470        gml.*::*[i] = ringMember;
3471      }
3472      return gml;
3473    },
3474    /**
3475     * Method: buildGeometry.multipolygon
3476     * Given a ZOO multipolygon geometry, create a GML multipolygon.
3477     *
3478     * Parameters:
3479     * geometry - {<ZOO.Geometry.MultiPolygon>} A multipolygon
3480     *     geometry.
3481     *
3482     * Returns:
3483     * {E4XElement} A GML multipolygon node.
3484     */
3485    'multipolygon': function(geometry) {
3486      var gml = new XML('<gml:MultiPolygon xmlns:gml="'+this.namespaces['gml']+'"></gml:MultiPolygon>');
3487      var polys = geometry.components;
3488      var polyMember;
3489      for(var i=0; i<polys.length; i++) { 
3490        polyMember = new XML('<gml:polygonMember xmlns:gml="'+this.namespaces['gml']+'"></gml:polygonMember>');
3491        polyMember.*::* = this.buildGeometry.polygon.apply(this,[polys[i]]);
3492        gml.*::*[i] = polyMember;
3493      }
3494      return gml;           
3495    }
3496  },
3497  /**
3498   * Method: buildCoordinatesNode
3499   * builds the coordinates XmlNode
3500   * (code)
3501   * <gml:coordinates decimal="." cs="," ts=" ">...</gml:coordinates>
3502   * (end)
3503   * Parameters:
3504   * geometry - {<ZOO.Geometry>}
3505   *
3506   * Returns:
3507   * {E4XElement} created E4XElement
3508   */
3509  buildCoordinatesNode: function(geometry) {
3510    var parts = [];
3511    if(geometry instanceof ZOO.Bounds){
3512      parts.push(geometry.left + "," + geometry.bottom);
3513      parts.push(geometry.right + "," + geometry.top);
3514    } else {
3515      var points = (geometry.components) ? geometry.components : [geometry];
3516      for(var i=0; i<points.length; i++) {
3517        parts.push(points[i].x + "," + points[i].y);               
3518      }           
3519    }
3520    return new XML('<gml:coordinates xmlns:gml="'+this.namespaces['gml']+'" decimal="." cs=", " ts=" ">'+parts.join(" ")+'</gml:coordinates>');
3521  },
3522  CLASS_NAME: 'ZOO.Format.GML'
3523});
3524/**
3525 * Class: ZOO.Format.WPS
3526 * Read/Write WPS. Create a new instance with the <ZOO.Format.WPS>
3527 *     constructor. Supports only parseExecuteResponse.
3528 *
3529 * Inherits from:
3530 *  - <ZOO.Format>
3531 */
3532ZOO.Format.WPS = ZOO.Class(ZOO.Format, {
3533  /**
3534   * Property: schemaLocation
3535   * {String} Schema location for a particular minor version.
3536   */
3537  schemaLocation: "http://www.opengis.net/wps/1.0.0/../wpsExecute_request.xsd",
3538  /**
3539   * Property: namespaces
3540   * {Object} Mapping of namespace aliases to namespace URIs.
3541   */
3542  namespaces: {
3543    ows: "http://www.opengis.net/ows/1.1",
3544    wps: "http://www.opengis.net/wps/1.0.0",
3545    xlink: "http://www.w3.org/1999/xlink",
3546    xsi: "http://www.w3.org/2001/XMLSchema-instance",
3547  },
3548  /**
3549   * Method: read
3550   *
3551   * Parameters:
3552   * data - {String} A WPS xml document
3553   *
3554   * Returns:
3555   * {Object} Execute response.
3556   */
3557  read:function(data) {
3558    data = data.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/, "");
3559    data = new XML(data);
3560    switch (data.localName()) {
3561      case 'ExecuteResponse':
3562        return this.parseExecuteResponse(data);
3563      default:
3564        return null;
3565    }
3566  },
3567  /**
3568   * Method: parseExecuteResponse
3569   *
3570   * Parameters:
3571   * node - {E4XElement} A WPS ExecuteResponse document
3572   *
3573   * Returns:
3574   * {Object} Execute response.
3575   */
3576  parseExecuteResponse: function(node) {
3577    var outputs = node.*::ProcessOutputs.*::Output;
3578    if (outputs.length() > 0) {
3579      var data = outputs[0].*::Data.*::*[0];
3580      var builder = this.parseData[data.localName().toLowerCase()];
3581      if (builder)
3582        return builder.apply(this,[data]);
3583      else
3584        return null;
3585    } else
3586      return null;
3587  },
3588  /**
3589   * Property: parseData
3590   * Object containing methods to analyse data response.
3591   */
3592  parseData: {
3593    /**
3594     * Method: parseData.complexdata
3595     * Given an Object representing the WPS complex data response.
3596     *
3597     * Parameters:
3598     * node - {E4XElement} A WPS node.
3599     *
3600     * Returns:
3601     * {Object} A WPS complex data response.
3602     */
3603    'complexdata': function(node) {
3604      var result = {value:node.toString()};
3605      if (node.@mimeType.length()>0)
3606        result.mimeType = node.@mimeType;
3607      if (node.@encoding.length()>0)
3608        result.encoding = node.@encoding;
3609      if (node.@schema.length()>0)
3610        result.schema = node.@schema;
3611      return result;
3612    },
3613    /**
3614     * Method: parseData.literaldata
3615     * Given an Object representing the WPS literal data response.
3616     *
3617     * Parameters:
3618     * node - {E4XElement} A WPS node.
3619     *
3620     * Returns:
3621     * {Object} A WPS literal data response.
3622     */
3623    'literaldata': function(node) {
3624      var result = {value:node.toString()};
3625      if (node.@dataType.length()>0)
3626        result.dataType = node.@dataType;
3627      if (node.@uom.length()>0)
3628        result.uom = node.@uom;
3629      return result;
3630    }
3631  },
3632  CLASS_NAME: 'ZOO.Format.WPS'
3633});
3634
3635/**
3636 * Class: ZOO.Feature
3637 * Vector features use the ZOO.Geometry classes as geometry description.
3638 * They have an 'attributes' property, which is the data object
3639 */
3640ZOO.Feature = ZOO.Class({
3641  /**
3642   * Property: fid
3643   * {String}
3644   */
3645  fid: null,
3646  /**
3647   * Property: geometry
3648   * {<ZOO.Geometry>}
3649   */
3650  geometry: null,
3651  /**
3652   * Property: attributes
3653   * {Object} This object holds arbitrary properties that describe the
3654   *     feature.
3655   */
3656  attributes: null,
3657  /**
3658   * Property: bounds
3659   * {<ZOO.Bounds>} The box bounding that feature's geometry, that
3660   *     property can be set by an <ZOO.Format> object when
3661   *     deserializing the feature, so in most cases it represents an
3662   *     information set by the server.
3663   */
3664  bounds: null,
3665  /**
3666   * Constructor: ZOO.Feature
3667   * Create a vector feature.
3668   *
3669   * Parameters:
3670   * geometry - {<ZOO.Geometry>} The geometry that this feature
3671   *     represents.
3672   * attributes - {Object} An optional object that will be mapped to the
3673   *     <attributes> property.
3674   */
3675  initialize: function(geometry, attributes) {
3676    this.geometry = geometry ? geometry : null;
3677    this.attributes = {};
3678    if (attributes)
3679      this.attributes = ZOO.extend(this.attributes,attributes);
3680  },
3681  /**
3682   * Method: destroy
3683   * nullify references to prevent circular references and memory leaks
3684   */
3685  destroy: function() {
3686    this.geometry = null;
3687  },
3688  /**
3689   * Method: clone
3690   * Create a clone of this vector feature.  Does not set any non-standard
3691   *     properties.
3692   *
3693   * Returns:
3694   * {<ZOO.Feature>} An exact clone of this vector feature.
3695   */
3696  clone: function () {
3697    return new ZOO.Feature(this.geometry ? this.geometry.clone() : null,
3698            this.attributes);
3699  },
3700  /**
3701   * Method: move
3702   * Moves the feature and redraws it at its new location
3703   *
3704   * Parameters:
3705   * x - {Float}
3706   * y - {Float}
3707   */
3708  move: function(x, y) {
3709    if(!this.geometry.move)
3710      return;
3711
3712    this.geometry.move(x,y);
3713    return this.geometry;
3714  },
3715  CLASS_NAME: 'ZOO.Feature'
3716});
3717
3718/**
3719 * Class: ZOO.Geometry
3720 * A Geometry is a description of a geographic object. Create an instance
3721 * of this class with the <ZOO.Geometry> constructor. This is a base class,
3722 * typical geometry types are described by subclasses of this class.
3723 */
3724ZOO.Geometry = ZOO.Class({
3725  /**
3726   * Property: id
3727   * {String} A unique identifier for this geometry.
3728   */
3729  id: null,
3730  /**
3731   * Property: parent
3732   * {<ZOO.Geometry>}This is set when a Geometry is added as component
3733   * of another geometry
3734   */
3735  parent: null,
3736  /**
3737   * Property: bounds
3738   * {<ZOO.Bounds>} The bounds of this geometry
3739   */
3740  bounds: null,
3741  /**
3742   * Constructor: ZOO.Geometry
3743   * Creates a geometry object. 
3744   */
3745  initialize: function() {
3746    //generate unique id
3747  },
3748  /**
3749   * Method: destroy
3750   * Destroy this geometry.
3751   */
3752  destroy: function() {
3753    this.id = null;
3754    this.bounds = null;
3755  },
3756  /**
3757   * Method: clone
3758   * Create a clone of this geometry.  Does not set any non-standard
3759   *     properties of the cloned geometry.
3760   *
3761   * Returns:
3762   * {<ZOO.Geometry>} An exact clone of this geometry.
3763   */
3764  clone: function() {
3765    return new ZOO.Geometry();
3766  },
3767  /**
3768   * Method: extendBounds
3769   * Extend the existing bounds to include the new bounds.
3770   * If geometry's bounds is not yet set, then set a new Bounds.
3771   *
3772   * Parameters:
3773   * newBounds - {<ZOO.Bounds>}
3774   */
3775  extendBounds: function(newBounds){
3776    var bounds = this.getBounds();
3777    if (!bounds)
3778      this.setBounds(newBounds);
3779    else
3780      this.bounds.extend(newBounds);
3781  },
3782  /**
3783   * Set the bounds for this Geometry.
3784   *
3785   * Parameters:
3786   * bounds - {<ZOO.Bounds>}
3787   */
3788  setBounds: function(bounds) {
3789    if (bounds)
3790      this.bounds = bounds.clone();
3791  },
3792  /**
3793   * Method: clearBounds
3794   * Nullify this components bounds and that of its parent as well.
3795   */
3796  clearBounds: function() {
3797    this.bounds = null;
3798    if (this.parent)
3799      this.parent.clearBounds();
3800  },
3801  /**
3802   * Method: getBounds
3803   * Get the bounds for this Geometry. If bounds is not set, it
3804   * is calculated again, this makes queries faster.
3805   *
3806   * Returns:
3807   * {<ZOO.Bounds>}
3808   */
3809  getBounds: function() {
3810    if (this.bounds == null) {
3811      this.calculateBounds();
3812    }
3813    return this.bounds;
3814  },
3815  /**
3816   * Method: calculateBounds
3817   * Recalculate the bounds for the geometry.
3818   */
3819  calculateBounds: function() {
3820    // This should be overridden by subclasses.
3821    return this.bounds;
3822  },
3823  distanceTo: function(geometry, options) {
3824  },
3825  getVertices: function(nodes) {
3826  },
3827  getLength: function() {
3828    return 0.0;
3829  },
3830  getArea: function() {
3831    return 0.0;
3832  },
3833  getCentroid: function() {
3834    return null;
3835  },
3836  /**
3837   * Method: toString
3838   * Returns the Well-Known Text representation of a geometry
3839   *
3840   * Returns:
3841   * {String} Well-Known Text
3842   */
3843  toString: function() {
3844    return ZOO.Format.WKT.prototype.write(
3845        new ZOO.Feature(this)
3846    );
3847  },
3848  CLASS_NAME: 'ZOO.Geometry'
3849});
3850/**
3851 * Function: OpenLayers.Geometry.fromWKT
3852 * Generate a geometry given a Well-Known Text string.
3853 *
3854 * Parameters:
3855 * wkt - {String} A string representing the geometry in Well-Known Text.
3856 *
3857 * Returns:
3858 * {<ZOO.Geometry>} A geometry of the appropriate class.
3859 */
3860ZOO.Geometry.fromWKT = function(wkt) {
3861  var format = arguments.callee.format;
3862  if(!format) {
3863    format = new ZOO.Format.WKT();
3864    arguments.callee.format = format;
3865  }
3866  var geom;
3867  var result = format.read(wkt);
3868  if(result instanceof ZOO.Feature) {
3869    geom = result.geometry;
3870  } else if(result instanceof Array) {
3871    var len = result.length;
3872    var components = new Array(len);
3873    for(var i=0; i<len; ++i) {
3874      components[i] = result[i].geometry;
3875    }
3876    geom = new ZOO.Geometry.Collection(components);
3877  }
3878  return geom;
3879};
3880ZOO.Geometry.segmentsIntersect = function(seg1, seg2, options) {
3881  var point = options && options.point;
3882  var tolerance = options && options.tolerance;
3883  var intersection = false;
3884  var x11_21 = seg1.x1 - seg2.x1;
3885  var y11_21 = seg1.y1 - seg2.y1;
3886  var x12_11 = seg1.x2 - seg1.x1;
3887  var y12_11 = seg1.y2 - seg1.y1;
3888  var y22_21 = seg2.y2 - seg2.y1;
3889  var x22_21 = seg2.x2 - seg2.x1;
3890  var d = (y22_21 * x12_11) - (x22_21 * y12_11);
3891  var n1 = (x22_21 * y11_21) - (y22_21 * x11_21);
3892  var n2 = (x12_11 * y11_21) - (y12_11 * x11_21);
3893  if(d == 0) {
3894    // parallel
3895    if(n1 == 0 && n2 == 0) {
3896      // coincident
3897      intersection = true;
3898    }
3899  } else {
3900    var along1 = n1 / d;
3901    var along2 = n2 / d;
3902    if(along1 >= 0 && along1 <= 1 && along2 >=0 && along2 <= 1) {
3903      // intersect
3904      if(!point) {
3905        intersection = true;
3906      } else {
3907        // calculate the intersection point
3908        var x = seg1.x1 + (along1 * x12_11);
3909        var y = seg1.y1 + (along1 * y12_11);
3910        intersection = new ZOO.Geometry.Point(x, y);
3911      }
3912    }
3913  }
3914  if(tolerance) {
3915    var dist;
3916    if(intersection) {
3917      if(point) {
3918        var segs = [seg1, seg2];
3919        var seg, x, y;
3920        // check segment endpoints for proximity to intersection
3921        // set intersection to first endpoint within the tolerance
3922        outer: for(var i=0; i<2; ++i) {
3923          seg = segs[i];
3924          for(var j=1; j<3; ++j) {
3925            x = seg["x" + j];
3926            y = seg["y" + j];
3927            dist = Math.sqrt(
3928                Math.pow(x - intersection.x, 2) +
3929                Math.pow(y - intersection.y, 2)
3930            );
3931            if(dist < tolerance) {
3932              intersection.x = x;
3933              intersection.y = y;
3934              break outer;
3935            }
3936          }
3937        }
3938      }
3939    } else {
3940      // no calculated intersection, but segments could be within
3941      // the tolerance of one another
3942      var segs = [seg1, seg2];
3943      var source, target, x, y, p, result;
3944      // check segment endpoints for proximity to intersection
3945      // set intersection to first endpoint within the tolerance
3946      outer: for(var i=0; i<2; ++i) {
3947        source = segs[i];
3948        target = segs[(i+1)%2];
3949        for(var j=1; j<3; ++j) {
3950          p = {x: source["x"+j], y: source["y"+j]};
3951          result = ZOO.Geometry.distanceToSegment(p, target);
3952          if(result.distance < tolerance) {
3953            if(point) {
3954              intersection = new ZOO.Geometry.Point(p.x, p.y);
3955            } else {
3956              intersection = true;
3957            }
3958            break outer;
3959          }
3960        }
3961      }
3962    }
3963  }
3964  return intersection;
3965};
3966ZOO.Geometry.distanceToSegment = function(point, segment) {
3967  var x0 = point.x;
3968  var y0 = point.y;
3969  var x1 = segment.x1;
3970  var y1 = segment.y1;
3971  var x2 = segment.x2;
3972  var y2 = segment.y2;
3973  var dx = x2 - x1;
3974  var dy = y2 - y1;
3975  var along = ((dx * (x0 - x1)) + (dy * (y0 - y1))) /
3976               (Math.pow(dx, 2) + Math.pow(dy, 2));
3977  var x, y;
3978  if(along <= 0.0) {
3979    x = x1;
3980    y = y1;
3981  } else if(along >= 1.0) {
3982    x = x2;
3983    y = y2;
3984  } else {
3985    x = x1 + along * dx;
3986    y = y1 + along * dy;
3987  }
3988  return {
3989    distance: Math.sqrt(Math.pow(x - x0, 2) + Math.pow(y - y0, 2)),
3990    x: x, y: y
3991  };
3992};
3993/**
3994 * Class: OpenLayers.Geometry.Collection
3995 * A Collection is exactly what it sounds like: A collection of different
3996 * Geometries. These are stored in the local parameter <components> (which
3997 * can be passed as a parameter to the constructor).
3998 *
3999 * As new geometries are added to the collection, they are NOT cloned.
4000 * When removing geometries, they need to be specified by reference (ie you
4001 * have to pass in the *exact* geometry to be removed).
4002 *
4003 * The <getArea> and <getLength> functions here merely iterate through
4004 * the components, summing their respective areas and lengths.
4005 *
4006 * Create a new instance with the <ZOO.Geometry.Collection> constructor.
4007 *
4008 * Inerhits from:
4009 *  - <ZOO.Geometry>
4010 */
4011ZOO.Geometry.Collection = ZOO.Class(ZOO.Geometry, {
4012  /**
4013   * Property: components
4014   * {Array(<ZOO.Geometry>)} The component parts of this geometry
4015   */
4016  components: null,
4017  /**
4018   * Property: componentTypes
4019   * {Array(String)} An array of class names representing the types of
4020   * components that the collection can include.  A null value means the
4021   * component types are not restricted.
4022   */
4023  componentTypes: null,
4024  /**
4025   * Constructor: ZOO.Geometry.Collection
4026   * Creates a Geometry Collection -- a list of geoms.
4027   *
4028   * Parameters:
4029   * components - {Array(<ZOO.Geometry>)} Optional array of geometries
4030   *
4031   */
4032  initialize: function (components) {
4033    ZOO.Geometry.prototype.initialize.apply(this, arguments);
4034    this.components = [];
4035    if (components != null) {
4036      this.addComponents(components);
4037    }
4038  },
4039  /**
4040   * Method: destroy
4041   * Destroy this geometry.
4042   */
4043  destroy: function () {
4044    this.components.length = 0;
4045    this.components = null;
4046  },
4047  /**
4048   * Method: clone
4049   * Clone this geometry.
4050   *
4051   * Returns:
4052   * {<ZOO.Geometry.Collection>} An exact clone of this collection
4053   */
4054  clone: function() {
4055    var geometry = eval("new " + this.CLASS_NAME + "()");
4056    for(var i=0, len=this.components.length; i<len; i++) {
4057      geometry.addComponent(this.components[i].clone());
4058    }
4059    return geometry;
4060  },
4061  /**
4062   * Method: getComponentsString
4063   * Get a string representing the components for this collection
4064   *
4065   * Returns:
4066   * {String} A string representation of the components of this geometry
4067   */
4068  getComponentsString: function(){
4069    var strings = [];
4070    for(var i=0, len=this.components.length; i<len; i++) {
4071      strings.push(this.components[i].toShortString()); 
4072    }
4073    return strings.join(",");
4074  },
4075  /**
4076   * Method: calculateBounds
4077   * Recalculate the bounds by iterating through the components and
4078   * calling calling extendBounds() on each item.
4079   */
4080  calculateBounds: function() {
4081    this.bounds = null;
4082    if ( this.components && this.components.length > 0) {
4083      this.setBounds(this.components[0].getBounds());
4084      for (var i=1, len=this.components.length; i<len; i++) {
4085        this.extendBounds(this.components[i].getBounds());
4086      }
4087    }
4088    return this.bounds
4089  },
4090  /**
4091   * APIMethod: addComponents
4092   * Add components to this geometry.
4093   *
4094   * Parameters:
4095   * components - {Array(<ZOO.Geometry>)} An array of geometries to add
4096   */
4097  addComponents: function(components){
4098    if(!(components instanceof Array))
4099      components = [components];
4100    for(var i=0, len=components.length; i<len; i++) {
4101      this.addComponent(components[i]);
4102    }
4103  },
4104  /**
4105   * Method: addComponent
4106   * Add a new component (geometry) to the collection.  If this.componentTypes
4107   * is set, then the component class name must be in the componentTypes array.
4108   *
4109   * The bounds cache is reset.
4110   *
4111   * Parameters:
4112   * component - {<ZOO.Geometry>} A geometry to add
4113   * index - {int} Optional index into the array to insert the component
4114   *
4115   * Returns:
4116   * {Boolean} The component geometry was successfully added
4117   */
4118  addComponent: function(component, index) {
4119    var added = false;
4120    if(component) {
4121      if(this.componentTypes == null ||
4122          (ZOO.indexOf(this.componentTypes,
4123                       component.CLASS_NAME) > -1)) {
4124        if(index != null && (index < this.components.length)) {
4125          var components1 = this.components.slice(0, index);
4126          var components2 = this.components.slice(index, 
4127                                                  this.components.length);
4128          components1.push(component);
4129          this.components = components1.concat(components2);
4130        } else {
4131          this.components.push(component);
4132        }
4133        component.parent = this;
4134        this.clearBounds();
4135        added = true;
4136      }
4137    }
4138    return added;
4139  },
4140  /**
4141   * Method: removeComponents
4142   * Remove components from this geometry.
4143   *
4144   * Parameters:
4145   * components - {Array(<ZOO.Geometry>)} The components to be removed
4146   */
4147  removeComponents: function(components) {
4148    if(!(components instanceof Array))
4149      components = [components];
4150    for(var i=components.length-1; i>=0; --i) {
4151      this.removeComponent(components[i]);
4152    }
4153  },
4154  /**
4155   * Method: removeComponent
4156   * Remove a component from this geometry.
4157   *
4158   * Parameters:
4159   * component - {<ZOO.Geometry>}
4160   */
4161  removeComponent: function(component) {     
4162    ZOO.removeItem(this.components, component);
4163    // clearBounds() so that it gets recalculated on the next call
4164    // to this.getBounds();
4165    this.clearBounds();
4166  },
4167  /**
4168   * Method: getLength
4169   * Calculate the length of this geometry
4170   *
4171   * Returns:
4172   * {Float} The length of the geometry
4173   */
4174  getLength: function() {
4175    var length = 0.0;
4176    for (var i=0, len=this.components.length; i<len; i++) {
4177      length += this.components[i].getLength();
4178    }
4179    return length;
4180  },
4181  /**
4182   * APIMethod: getArea
4183   * Calculate the area of this geometry. Note how this function is
4184   * overridden in <ZOO.Geometry.Polygon>.
4185   *
4186   * Returns:
4187   * {Float} The area of the collection by summing its parts
4188   */
4189  getArea: function() {
4190    var area = 0.0;
4191    for (var i=0, len=this.components.length; i<len; i++) {
4192      area += this.components[i].getArea();
4193    }
4194    return area;
4195  },
4196  /**
4197   * APIMethod: getGeodesicArea
4198   * Calculate the approximate area of the polygon were it projected onto
4199   *     the earth.
4200   *
4201   * Parameters:
4202   * projection - {<ZOO.Projection>} The spatial reference system
4203   *     for the geometry coordinates.  If not provided, Geographic/WGS84 is
4204   *     assumed.
4205   *
4206   * Reference:
4207   * Robert. G. Chamberlain and William H. Duquette, "Some Algorithms for
4208   *     Polygons on a Sphere", JPL Publication 07-03, Jet Propulsion
4209   *     Laboratory, Pasadena, CA, June 2007 http://trs-new.jpl.nasa.gov/dspace/handle/2014/40409
4210   *
4211   * Returns:
4212   * {float} The approximate geodesic area of the geometry in square meters.
4213   */
4214  getGeodesicArea: function(projection) {
4215    var area = 0.0;
4216    for(var i=0, len=this.components.length; i<len; i++) {
4217      area += this.components[i].getGeodesicArea(projection);
4218    }
4219    return area;
4220  },
4221  /**
4222   * Method: getCentroid
4223   *
4224   * Returns:
4225   * {<ZOO.Geometry.Point>} The centroid of the collection
4226   */
4227  getCentroid: function() {
4228    return this.components.length && this.components[0].getCentroid();
4229  },
4230  /**
4231   * Method: getGeodesicLength
4232   * Calculate the approximate length of the geometry were it projected onto
4233   *     the earth.
4234   *
4235   * projection - {<ZOO.Projection>} The spatial reference system
4236   *     for the geometry coordinates.  If not provided, Geographic/WGS84 is
4237   *     assumed.
4238   *
4239   * Returns:
4240   * {Float} The appoximate geodesic length of the geometry in meters.
4241   */
4242  getGeodesicLength: function(projection) {
4243    var length = 0.0;
4244    for(var i=0, len=this.components.length; i<len; i++) {
4245      length += this.components[i].getGeodesicLength(projection);
4246    }
4247    return length;
4248  },
4249  /**
4250   * Method: move
4251   * Moves a geometry by the given displacement along positive x and y axes.
4252   *     This modifies the position of the geometry and clears the cached
4253   *     bounds.
4254   *
4255   * Parameters:
4256   * x - {Float} Distance to move geometry in positive x direction.
4257   * y - {Float} Distance to move geometry in positive y direction.
4258   */
4259  move: function(x, y) {
4260    for(var i=0, len=this.components.length; i<len; i++) {
4261      this.components[i].move(x, y);
4262    }
4263  },
4264  /**
4265   * Method: rotate
4266   * Rotate a geometry around some origin
4267   *
4268   * Parameters:
4269   * angle - {Float} Rotation angle in degrees (measured counterclockwise
4270   *                 from the positive x-axis)
4271   * origin - {<ZOO.Geometry.Point>} Center point for the rotation
4272   */
4273  rotate: function(angle, origin) {
4274    for(var i=0, len=this.components.length; i<len; ++i) {
4275      this.components[i].rotate(angle, origin);
4276    }
4277  },
4278  /**
4279   * Method: resize
4280   * Resize a geometry relative to some origin.  Use this method to apply
4281   *     a uniform scaling to a geometry.
4282   *
4283   * Parameters:
4284   * scale - {Float} Factor by which to scale the geometry.  A scale of 2
4285   *                 doubles the size of the geometry in each dimension
4286   *                 (lines, for example, will be twice as long, and polygons
4287   *                 will have four times the area).
4288   * origin - {<ZOO.Geometry.Point>} Point of origin for resizing
4289   * ratio - {Float} Optional x:y ratio for resizing.  Default ratio is 1.
4290   *
4291   * Returns:
4292   * {ZOO.Geometry} - The current geometry.
4293   */
4294  resize: function(scale, origin, ratio) {
4295    for(var i=0; i<this.components.length; ++i) {
4296      this.components[i].resize(scale, origin, ratio);
4297    }
4298    return this;
4299  },
4300  distanceTo: function(geometry, options) {
4301    var edge = !(options && options.edge === false);
4302    var details = edge && options && options.details;
4303    var result, best;
4304    var min = Number.POSITIVE_INFINITY;
4305    for(var i=0, len=this.components.length; i<len; ++i) {
4306      result = this.components[i].distanceTo(geometry, options);
4307      distance = details ? result.distance : result;
4308      if(distance < min) {
4309        min = distance;
4310        best = result;
4311        if(min == 0)
4312          break;
4313      }
4314    }
4315    return best;
4316  },
4317  /**
4318   * Method: equals
4319   * Determine whether another geometry is equivalent to this one.  Geometries
4320   *     are considered equivalent if all components have the same coordinates.
4321   *
4322   * Parameters:
4323   * geom - {<ZOO.Geometry>} The geometry to test.
4324   *
4325   * Returns:
4326   * {Boolean} The supplied geometry is equivalent to this geometry.
4327   */
4328  equals: function(geometry) {
4329    var equivalent = true;
4330    if(!geometry || !geometry.CLASS_NAME ||
4331       (this.CLASS_NAME != geometry.CLASS_NAME))
4332      equivalent = false;
4333    else if(!(geometry.components instanceof Array) ||
4334             (geometry.components.length != this.components.length))
4335      equivalent = false;
4336    else
4337      for(var i=0, len=this.components.length; i<len; ++i) {
4338        if(!this.components[i].equals(geometry.components[i])) {
4339          equivalent = false;
4340          break;
4341        }
4342      }
4343    return equivalent;
4344  },
4345  /**
4346   * Method: transform
4347   * Reproject the components geometry from source to dest.
4348   *
4349   * Parameters:
4350   * source - {<ZOO.Projection>}
4351   * dest - {<ZOO.Projection>}
4352   *
4353   * Returns:
4354   * {<ZOO.Geometry>}
4355   */
4356  transform: function(source, dest) {
4357    if (source && dest) {
4358      for (var i=0, len=this.components.length; i<len; i++) { 
4359        var component = this.components[i];
4360        component.transform(source, dest);
4361      }
4362      this.bounds = null;
4363    }
4364    return this;
4365  },
4366  /**
4367   * Method: intersects
4368   * Determine if the input geometry intersects this one.
4369   *
4370   * Parameters:
4371   * geometry - {<ZOO.Geometry>} Any type of geometry.
4372   *
4373   * Returns:
4374   * {Boolean} The input geometry intersects this one.
4375   */
4376  intersects: function(geometry) {
4377    var intersect = false;
4378    for(var i=0, len=this.components.length; i<len; ++ i) {
4379      intersect = geometry.intersects(this.components[i]);
4380      if(intersect)
4381        break;
4382    }
4383    return intersect;
4384  },
4385  /**
4386   * Method: getVertices
4387   * Return a list of all points in this geometry.
4388   *
4389   * Parameters:
4390   * nodes - {Boolean} For lines, only return vertices that are
4391   *     endpoints.  If false, for lines, only vertices that are not
4392   *     endpoints will be returned.  If not provided, all vertices will
4393   *     be returned.
4394   *
4395   * Returns:
4396   * {Array} A list of all vertices in the geometry.
4397   */
4398  getVertices: function(nodes) {
4399    var vertices = [];
4400    for(var i=0, len=this.components.length; i<len; ++i) {
4401      Array.prototype.push.apply(
4402          vertices, this.components[i].getVertices(nodes)
4403          );
4404    }
4405    return vertices;
4406  },
4407  CLASS_NAME: 'ZOO.Geometry.Collection'
4408});
4409/**
4410 * Class: ZOO.Geometry.Point
4411 * Point geometry class.
4412 *
4413 * Inherits from:
4414 *  - <ZOO.Geometry>
4415 */
4416ZOO.Geometry.Point = ZOO.Class(ZOO.Geometry, {
4417  /**
4418   * Property: x
4419   * {float}
4420   */
4421  x: null,
4422  /**
4423   * Property: y
4424   * {float}
4425   */
4426  y: null,
4427  /**
4428   * Constructor: ZOO.Geometry.Point
4429   * Construct a point geometry.
4430   *
4431   * Parameters:
4432   * x - {float}
4433   * y - {float}
4434   *
4435   */
4436  initialize: function(x, y) {
4437    ZOO.Geometry.prototype.initialize.apply(this, arguments);
4438    this.x = parseFloat(x);
4439    this.y = parseFloat(y);
4440  },
4441  /**
4442   * Method: clone
4443   *
4444   * Returns:
4445   * {<ZOO.Geometry.Point>} An exact clone of this ZOO.Geometry.Point
4446   */
4447  clone: function(obj) {
4448    if (obj == null)
4449      obj = new ZOO.Geometry.Point(this.x, this.y);
4450    // catch any randomly tagged-on properties
4451    // ZOO.Util.applyDefaults(obj, this);
4452    return obj;
4453  },
4454  /**
4455   * Method: calculateBounds
4456   * Create a new Bounds based on the x/y
4457   */
4458  calculateBounds: function () {
4459    this.bounds = new ZOO.Bounds(this.x, this.y,
4460                                        this.x, this.y);
4461  },
4462  distanceTo: function(geometry, options) {
4463    var edge = !(options && options.edge === false);
4464    var details = edge && options && options.details;
4465    var distance, x0, y0, x1, y1, result;
4466    if(geometry instanceof ZOO.Geometry.Point) {
4467      x0 = this.x;
4468      y0 = this.y;
4469      x1 = geometry.x;
4470      y1 = geometry.y;
4471      distance = Math.sqrt(Math.pow(x0 - x1, 2) + Math.pow(y0 - y1, 2));
4472      result = !details ?
4473        distance : {x0: x0, y0: y0, x1: x1, y1: y1, distance: distance};
4474    } else {
4475      result = geometry.distanceTo(this, options);
4476      if(details) {
4477        // switch coord order since this geom is target
4478        result = {
4479          x0: result.x1, y0: result.y1,
4480          x1: result.x0, y1: result.y0,
4481          distance: result.distance
4482        };
4483      }
4484    }
4485    return result;
4486  },
4487  /**
4488   * Method: equals
4489   * Determine whether another geometry is equivalent to this one.  Geometries
4490   *     are considered equivalent if all components have the same coordinates.
4491   *
4492   * Parameters:
4493   * geom - {<ZOO.Geometry.Point>} The geometry to test.
4494   *
4495   * Returns:
4496   * {Boolean} The supplied geometry is equivalent to this geometry.
4497   */
4498  equals: function(geom) {
4499    var equals = false;
4500    if (geom != null)
4501      equals = ((this.x == geom.x && this.y == geom.y) ||
4502                (isNaN(this.x) && isNaN(this.y) && isNaN(geom.x) && isNaN(geom.y)));
4503    return equals;
4504  },
4505  /**
4506   * Method: toShortString
4507   *
4508   * Returns:
4509   * {String} Shortened String representation of Point object.
4510   *         (ex. <i>"5, 42"</i>)
4511   */
4512  toShortString: function() {
4513    return (this.x + ", " + this.y);
4514  },
4515  /**
4516   * Method: move
4517   * Moves a geometry by the given displacement along positive x and y axes.
4518   *     This modifies the position of the geometry and clears the cached
4519   *     bounds.
4520   *
4521   * Parameters:
4522   * x - {Float} Distance to move geometry in positive x direction.
4523   * y - {Float} Distance to move geometry in positive y direction.
4524   */
4525  move: function(x, y) {
4526    this.x = this.x + x;
4527    this.y = this.y + y;
4528    this.clearBounds();
4529  },
4530  /**
4531   * Method: rotate
4532   * Rotate a point around another.
4533   *
4534   * Parameters:
4535   * angle - {Float} Rotation angle in degrees (measured counterclockwise
4536   *                 from the positive x-axis)
4537   * origin - {<ZOO.Geometry.Point>} Center point for the rotation
4538   */
4539  rotate: function(angle, origin) {
4540        angle *= Math.PI / 180;
4541        var radius = this.distanceTo(origin);
4542        var theta = angle + Math.atan2(this.y - origin.y, this.x - origin.x);
4543        this.x = origin.x + (radius * Math.cos(theta));
4544        this.y = origin.y + (radius * Math.sin(theta));
4545        this.clearBounds();
4546  },
4547  /**
4548   * Method: getCentroid
4549   *
4550   * Returns:
4551   * {<ZOO.Geometry.Point>} The centroid of the collection
4552   */
4553  getCentroid: function() {
4554    return new ZOO.Geometry.Point(this.x, this.y);
4555  },
4556  /**
4557   * Method: resize
4558   * Resize a point relative to some origin.  For points, this has the effect
4559   *     of scaling a vector (from the origin to the point).  This method is
4560   *     more useful on geometry collection subclasses.
4561   *
4562   * Parameters:
4563   * scale - {Float} Ratio of the new distance from the origin to the old
4564   *                 distance from the origin.  A scale of 2 doubles the
4565   *                 distance between the point and origin.
4566   * origin - {<ZOO.Geometry.Point>} Point of origin for resizing
4567   * ratio - {Float} Optional x:y ratio for resizing.  Default ratio is 1.
4568   *
4569   * Returns:
4570   * {ZOO.Geometry} - The current geometry.
4571   */
4572  resize: function(scale, origin, ratio) {
4573    ratio = (ratio == undefined) ? 1 : ratio;
4574    this.x = origin.x + (scale * ratio * (this.x - origin.x));
4575    this.y = origin.y + (scale * (this.y - origin.y));
4576    this.clearBounds();
4577    return this;
4578  },
4579  /**
4580   * Method: intersects
4581   * Determine if the input geometry intersects this one.
4582   *
4583   * Parameters:
4584   * geometry - {<ZOO.Geometry>} Any type of geometry.
4585   *
4586   * Returns:
4587   * {Boolean} The input geometry intersects this one.
4588   */
4589  intersects: function(geometry) {
4590    var intersect = false;
4591    if(geometry.CLASS_NAME == "ZOO.Geometry.Point") {
4592      intersect = this.equals(geometry);
4593    } else {
4594      intersect = geometry.intersects(this);
4595    }
4596    return intersect;
4597  },
4598  /**
4599   * Method: transform
4600   * Translate the x,y properties of the point from source to dest.
4601   *
4602   * Parameters:
4603   * source - {<ZOO.Projection>}
4604   * dest - {<ZOO.Projection>}
4605   *
4606   * Returns:
4607   * {<ZOO.Geometry>}
4608   */
4609  transform: function(source, dest) {
4610    if ((source && dest)) {
4611      ZOO.Projection.transform(
4612          this, source, dest); 
4613      this.bounds = null;
4614    }       
4615    return this;
4616  },
4617  /**
4618   * Method: getVertices
4619   * Return a list of all points in this geometry.
4620   *
4621   * Parameters:
4622   * nodes - {Boolean} For lines, only return vertices that are
4623   *     endpoints.  If false, for lines, only vertices that are not
4624   *     endpoints will be returned.  If not provided, all vertices will
4625   *     be returned.
4626   *
4627   * Returns:
4628   * {Array} A list of all vertices in the geometry.
4629   */
4630  getVertices: function(nodes) {
4631    return [this];
4632  },
4633  CLASS_NAME: 'ZOO.Geometry.Point'
4634});
4635/**
4636 * Class: ZOO.Geometry.Surface
4637 * Surface geometry class.
4638 *
4639 * Inherits from:
4640 *  - <ZOO.Geometry>
4641 */
4642ZOO.Geometry.Surface = ZOO.Class(ZOO.Geometry, {
4643  initialize: function() {
4644    ZOO.Geometry.prototype.initialize.apply(this, arguments);
4645  },
4646  CLASS_NAME: "ZOO.Geometry.Surface"
4647});
4648/**
4649 * Class: ZOO.Geometry.MultiPoint
4650 * MultiPoint is a collection of Points. Create a new instance with the
4651 * <ZOO.Geometry.MultiPoint> constructor.
4652 *
4653 * Inherits from:
4654 *  - <ZOO.Geometry.Collection>
4655 */
4656ZOO.Geometry.MultiPoint = ZOO.Class(
4657  ZOO.Geometry.Collection, {
4658  /**
4659   * Property: componentTypes
4660   * {Array(String)} An array of class names representing the types of
4661   * components that the collection can include.  A null value means the
4662   * component types are not restricted.
4663   */
4664  componentTypes: ["ZOO.Geometry.Point"],
4665  /**
4666   * Constructor: ZOO.Geometry.MultiPoint
4667   * Create a new MultiPoint Geometry
4668   *
4669   * Parameters:
4670   * components - {Array(<ZOO.Geometry.Point>)}
4671   *
4672   * Returns:
4673   * {<ZOO.Geometry.MultiPoint>}
4674   */
4675  initialize: function(components) {
4676    ZOO.Geometry.Collection.prototype.initialize.apply(this,arguments);
4677  },
4678  /**
4679   * Method: addPoint
4680   * Wrapper for <ZOO.Geometry.Collection.addComponent>
4681   *
4682   * Parameters:
4683   * point - {<ZOO.Geometry.Point>} Point to be added
4684   * index - {Integer} Optional index
4685   */
4686  addPoint: function(point, index) {
4687    this.addComponent(point, index);
4688  },
4689  /**
4690   * Method: removePoint
4691   * Wrapper for <ZOO.Geometry.Collection.removeComponent>
4692   *
4693   * Parameters:
4694   * point - {<ZOO.Geometry.Point>} Point to be removed
4695   */
4696  removePoint: function(point){
4697    this.removeComponent(point);
4698  },
4699  CLASS_NAME: "ZOO.Geometry.MultiPoint"
4700});
4701/**
4702 * Class: ZOO.Geometry.Curve
4703 * A Curve is a MultiPoint, whose points are assumed to be connected. To
4704 * this end, we provide a "getLength()" function, which iterates through
4705 * the points, summing the distances between them.
4706 *
4707 * Inherits:
4708 *  - <ZOO.Geometry.MultiPoint>
4709 */
4710ZOO.Geometry.Curve = ZOO.Class(ZOO.Geometry.MultiPoint, {
4711  /**
4712   * Property: componentTypes
4713   * {Array(String)} An array of class names representing the types of
4714   *                 components that the collection can include.  A null
4715   *                 value means the component types are not restricted.
4716   */
4717  componentTypes: ["ZOO.Geometry.Point"],
4718  /**
4719   * Constructor: ZOO.Geometry.Curve
4720   *
4721   * Parameters:
4722   * point - {Array(<ZOO.Geometry.Point>)}
4723   */
4724  initialize: function(points) {
4725    ZOO.Geometry.MultiPoint.prototype.initialize.apply(this,arguments);
4726  },
4727  /**
4728   * Method: getLength
4729   *
4730   * Returns:
4731   * {Float} The length of the curve
4732   */
4733  getLength: function() {
4734    var length = 0.0;
4735    if ( this.components && (this.components.length > 1)) {
4736      for(var i=1, len=this.components.length; i<len; i++) {
4737        length += this.components[i-1].distanceTo(this.components[i]);
4738      }
4739    }
4740    return length;
4741  },
4742  /**
4743     * APIMethod: getGeodesicLength
4744     * Calculate the approximate length of the geometry were it projected onto
4745     *     the earth.
4746     *
4747     * projection - {<ZOO.Projection>} The spatial reference system
4748     *     for the geometry coordinates.  If not provided, Geographic/WGS84 is
4749     *     assumed.
4750     *
4751     * Returns:
4752     * {Float} The appoximate geodesic length of the geometry in meters.
4753     */
4754    getGeodesicLength: function(projection) {
4755      var geom = this;  // so we can work with a clone if needed
4756      if(projection) {
4757        var gg = new ZOO.Projection("EPSG:4326");
4758        if(!gg.equals(projection)) {
4759          geom = this.clone().transform(projection, gg);
4760       }
4761     }
4762     var length = 0.0;
4763     if(geom.components && (geom.components.length > 1)) {
4764       var p1, p2;
4765       for(var i=1, len=geom.components.length; i<len; i++) {
4766         p1 = geom.components[i-1];
4767         p2 = geom.components[i];
4768        // this returns km and requires x/y properties
4769        length += ZOO.distVincenty(p1,p2);
4770      }
4771    }
4772    // convert to m
4773    return length * 1000;
4774  },
4775  CLASS_NAME: "ZOO.Geometry.Curve"
4776});
4777/**
4778 * Class: ZOO.Geometry.LineString
4779 * A LineString is a Curve which, once two points have been added to it, can
4780 * never be less than two points long.
4781 *
4782 * Inherits from:
4783 *  - <ZOO.Geometry.Curve>
4784 */
4785ZOO.Geometry.LineString = ZOO.Class(ZOO.Geometry.Curve, {
4786  /**
4787   * Constructor: ZOO.Geometry.LineString
4788   * Create a new LineString geometry
4789   *
4790   * Parameters:
4791   * points - {Array(<ZOO.Geometry.Point>)} An array of points used to
4792   *          generate the linestring
4793   *
4794   */
4795  initialize: function(points) {
4796    ZOO.Geometry.Curve.prototype.initialize.apply(this, arguments);       
4797  },
4798  /**
4799   * Method: removeComponent
4800   * Only allows removal of a point if there are three or more points in
4801   * the linestring. (otherwise the result would be just a single point)
4802   *
4803   * Parameters:
4804   * point - {<ZOO.Geometry.Point>} The point to be removed
4805   */
4806  removeComponent: function(point) {
4807    if ( this.components && (this.components.length > 2))
4808      ZOO.Geometry.Collection.prototype.removeComponent.apply(this,arguments);
4809  },
4810  /**
4811   * Method: intersects
4812   * Test for instersection between two geometries.  This is a cheapo
4813   *     implementation of the Bently-Ottmann algorigithm.  It doesn't
4814   *     really keep track of a sweep line data structure.  It is closer
4815   *     to the brute force method, except that segments are sorted and
4816   *     potential intersections are only calculated when bounding boxes
4817   *     intersect.
4818   *
4819   * Parameters:
4820   * geometry - {<ZOO.Geometry>}
4821   *
4822   * Returns:
4823   * {Boolean} The input geometry intersects this geometry.
4824   */
4825  intersects: function(geometry) {
4826    var intersect = false;
4827    var type = geometry.CLASS_NAME;
4828    if(type == "ZOO.Geometry.LineString" ||
4829       type == "ZOO.Geometry.LinearRing" ||
4830       type == "ZOO.Geometry.Point") {
4831      var segs1 = this.getSortedSegments();
4832      var segs2;
4833      if(type == "ZOO.Geometry.Point")
4834        segs2 = [{
4835          x1: geometry.x, y1: geometry.y,
4836          x2: geometry.x, y2: geometry.y
4837        }];
4838      else
4839        segs2 = geometry.getSortedSegments();
4840      var seg1, seg1x1, seg1x2, seg1y1, seg1y2,
4841          seg2, seg2y1, seg2y2;
4842      // sweep right
4843      outer: for(var i=0, len=segs1.length; i<len; ++i) {
4844         seg1 = segs1[i];
4845         seg1x1 = seg1.x1;
4846         seg1x2 = seg1.x2;
4847         seg1y1 = seg1.y1;
4848         seg1y2 = seg1.y2;
4849         inner: for(var j=0, jlen=segs2.length; j<jlen; ++j) {
4850           seg2 = segs2[j];
4851           if(seg2.x1 > seg1x2)
4852             break;
4853           if(seg2.x2 < seg1x1)
4854             continue;
4855           seg2y1 = seg2.y1;
4856           seg2y2 = seg2.y2;
4857           if(Math.min(seg2y1, seg2y2) > Math.max(seg1y1, seg1y2))
4858             continue;
4859           if(Math.max(seg2y1, seg2y2) < Math.min(seg1y1, seg1y2))
4860             continue;
4861           if(ZOO.Geometry.segmentsIntersect(seg1, seg2)) {
4862             intersect = true;
4863             break outer;
4864           }
4865         }
4866      }
4867    } else {
4868      intersect = geometry.intersects(this);
4869    }
4870    return intersect;
4871  },
4872  /**
4873   * Method: getSortedSegments
4874   *
4875   * Returns:
4876   * {Array} An array of segment objects.  Segment objects have properties
4877   *     x1, y1, x2, and y2.  The start point is represented by x1 and y1.
4878   *     The end point is represented by x2 and y2.  Start and end are
4879   *     ordered so that x1 < x2.
4880   */
4881  getSortedSegments: function() {
4882    var numSeg = this.components.length - 1;
4883    var segments = new Array(numSeg);
4884    for(var i=0; i<numSeg; ++i) {
4885      point1 = this.components[i];
4886      point2 = this.components[i + 1];
4887      if(point1.x < point2.x)
4888        segments[i] = {
4889          x1: point1.x,
4890          y1: point1.y,
4891          x2: point2.x,
4892          y2: point2.y
4893        };
4894      else
4895        segments[i] = {
4896          x1: point2.x,
4897          y1: point2.y,
4898          x2: point1.x,
4899          y2: point1.y
4900        };
4901    }
4902    // more efficient to define this somewhere static
4903    function byX1(seg1, seg2) {
4904      return seg1.x1 - seg2.x1;
4905    }
4906    return segments.sort(byX1);
4907  },
4908  /**
4909   * Method: splitWithSegment
4910   * Split this geometry with the given segment.
4911   *
4912   * Parameters:
4913   * seg - {Object} An object with x1, y1, x2, and y2 properties referencing
4914   *     segment endpoint coordinates.
4915   * options - {Object} Properties of this object will be used to determine
4916   *     how the split is conducted.
4917   *
4918   * Valid options:
4919   * edge - {Boolean} Allow splitting when only edges intersect.  Default is
4920   *     true.  If false, a vertex on the source segment must be within the
4921   *     tolerance distance of the intersection to be considered a split.
4922   * tolerance - {Number} If a non-null value is provided, intersections
4923   *     within the tolerance distance of one of the source segment's
4924   *     endpoints will be assumed to occur at the endpoint.
4925   *
4926   * Returns:
4927   * {Object} An object with *lines* and *points* properties.  If the given
4928   *     segment intersects this linestring, the lines array will reference
4929   *     geometries that result from the split.  The points array will contain
4930   *     all intersection points.  Intersection points are sorted along the
4931   *     segment (in order from x1,y1 to x2,y2).
4932   */
4933  splitWithSegment: function(seg, options) {
4934    var edge = !(options && options.edge === false);
4935    var tolerance = options && options.tolerance;
4936    var lines = [];
4937    var verts = this.getVertices();
4938    var points = [];
4939    var intersections = [];
4940    var split = false;
4941    var vert1, vert2, point;
4942    var node, vertex, target;
4943    var interOptions = {point: true, tolerance: tolerance};
4944    var result = null;
4945    for(var i=0, stop=verts.length-2; i<=stop; ++i) {
4946      vert1 = verts[i];
4947      points.push(vert1.clone());
4948      vert2 = verts[i+1];
4949      target = {x1: vert1.x, y1: vert1.y, x2: vert2.x, y2: vert2.y};
4950      point = ZOO.Geometry.segmentsIntersect(seg, target, interOptions);
4951      if(point instanceof ZOO.Geometry.Point) {
4952        if((point.x === seg.x1 && point.y === seg.y1) ||
4953           (point.x === seg.x2 && point.y === seg.y2) ||
4954            point.equals(vert1) || point.equals(vert2))
4955          vertex = true;
4956        else
4957          vertex = false;
4958        if(vertex || edge) {
4959          // push intersections different than the previous
4960          if(!point.equals(intersections[intersections.length-1]))
4961            intersections.push(point.clone());
4962          if(i === 0) {
4963            if(point.equals(vert1))
4964              continue;
4965          }
4966          if(point.equals(vert2))
4967            continue;
4968          split = true;
4969          if(!point.equals(vert1))
4970            points.push(point);
4971          lines.push(new ZOO.Geometry.LineString(points));
4972          points = [point.clone()];
4973        }
4974      }
4975    }
4976    if(split) {
4977      points.push(vert2.clone());
4978      lines.push(new ZOO.Geometry.LineString(points));
4979    }
4980    if(intersections.length > 0) {
4981      // sort intersections along segment
4982      var xDir = seg.x1 < seg.x2 ? 1 : -1;
4983      var yDir = seg.y1 < seg.y2 ? 1 : -1;
4984      result = {
4985        lines: lines,
4986        points: intersections.sort(function(p1, p2) {
4987           return (xDir * p1.x - xDir * p2.x) || (yDir * p1.y - yDir * p2.y);
4988        })
4989      };
4990    }
4991    return result;
4992  },
4993  /**
4994   * Method: split
4995   * Use this geometry (the source) to attempt to split a target geometry.
4996   *
4997   * Parameters:
4998   * target - {<ZOO.Geometry>} The target geometry.
4999   * options - {Object} Properties of this object will be used to determine
5000   *     how the split is conducted.
5001   *
5002   * Valid options:
5003   * mutual - {Boolean} Split the source geometry in addition to the target
5004   *     geometry.  Default is false.
5005   * edge - {Boolean} Allow splitting when only edges intersect.  Default is
5006   *     true.  If false, a vertex on the source must be within the tolerance
5007   *     distance of the intersection to be considered a split.
5008   * tolerance - {Number} If a non-null value is provided, intersections
5009   *     within the tolerance distance of an existing vertex on the source
5010   *     will be assumed to occur at the vertex.
5011   *
5012   * Returns:
5013   * {Array} A list of geometries (of this same type as the target) that
5014   *     result from splitting the target with the source geometry.  The
5015   *     source and target geometry will remain unmodified.  If no split
5016   *     results, null will be returned.  If mutual is true and a split
5017   *     results, return will be an array of two arrays - the first will be
5018   *     all geometries that result from splitting the source geometry and
5019   *     the second will be all geometries that result from splitting the
5020   *     target geometry.
5021   */
5022  split: function(target, options) {
5023    var results = null;
5024    var mutual = options && options.mutual;
5025    var sourceSplit, targetSplit, sourceParts, targetParts;
5026    if(target instanceof ZOO.Geometry.LineString) {
5027      var verts = this.getVertices();
5028      var vert1, vert2, seg, splits, lines, point;
5029      var points = [];
5030      sourceParts = [];
5031      for(var i=0, stop=verts.length-2; i<=stop; ++i) {
5032        vert1 = verts[i];
5033        vert2 = verts[i+1];
5034        seg = {
5035          x1: vert1.x, y1: vert1.y,
5036          x2: vert2.x, y2: vert2.y
5037        };
5038        targetParts = targetParts || [target];
5039        if(mutual)
5040          points.push(vert1.clone());
5041        for(var j=0; j<targetParts.length; ++j) {
5042          splits = targetParts[j].splitWithSegment(seg, options);
5043          if(splits) {
5044            // splice in new features
5045            lines = splits.lines;
5046            if(lines.length > 0) {
5047              lines.unshift(j, 1);
5048              Array.prototype.splice.apply(targetParts, lines);
5049              j += lines.length - 2;
5050            }
5051            if(mutual) {
5052              for(var k=0, len=splits.points.length; k<len; ++k) {
5053                point = splits.points[k];
5054                if(!point.equals(vert1)) {
5055                  points.push(point);
5056                  sourceParts.push(new ZOO.Geometry.LineString(points));
5057                  if(point.equals(vert2))
5058                    points = [];
5059                  else
5060                    points = [point.clone()];
5061                }
5062              }
5063            }
5064          }
5065        }
5066      }
5067      if(mutual && sourceParts.length > 0 && points.length > 0) {
5068        points.push(vert2.clone());
5069        sourceParts.push(new ZOO.Geometry.LineString(points));
5070      }
5071    } else {
5072      results = target.splitWith(this, options);
5073    }
5074    if(targetParts && targetParts.length > 1)
5075      targetSplit = true;
5076    else
5077      targetParts = [];
5078    if(sourceParts && sourceParts.length > 1)
5079      sourceSplit = true;
5080    else
5081      sourceParts = [];
5082    if(targetSplit || sourceSplit) {
5083      if(mutual)
5084        results = [sourceParts, targetParts];
5085      else
5086        results = targetParts;
5087    }
5088    return results;
5089  },
5090  /**
5091   * Method: splitWith
5092   * Split this geometry (the target) with the given geometry (the source).
5093   *
5094   * Parameters:
5095   * geometry - {<ZOO.Geometry>} A geometry used to split this
5096   *     geometry (the source).
5097   * options - {Object} Properties of this object will be used to determine
5098   *     how the split is conducted.
5099   *
5100   * Valid options:
5101   * mutual - {Boolean} Split the source geometry in addition to the target
5102   *     geometry.  Default is false.
5103   * edge - {Boolean} Allow splitting when only edges intersect.  Default is
5104   *     true.  If false, a vertex on the source must be within the tolerance
5105   *     distance of the intersection to be considered a split.
5106   * tolerance - {Number} If a non-null value is provided, intersections
5107   *     within the tolerance distance of an existing vertex on the source
5108   *     will be assumed to occur at the vertex.
5109   *
5110   * Returns:
5111   * {Array} A list of geometries (of this same type as the target) that
5112   *     result from splitting the target with the source geometry.  The
5113   *     source and target geometry will remain unmodified.  If no split
5114   *     results, null will be returned.  If mutual is true and a split
5115   *     results, return will be an array of two arrays - the first will be
5116   *     all geometries that result from splitting the source geometry and
5117   *     the second will be all geometries that result from splitting the
5118   *     target geometry.
5119   */
5120  splitWith: function(geometry, options) {
5121    return geometry.split(this, options);
5122  },
5123  /**
5124   * Method: getVertices
5125   * Return a list of all points in this geometry.
5126   *
5127   * Parameters:
5128   * nodes - {Boolean} For lines, only return vertices that are
5129   *     endpoints.  If false, for lines, only vertices that are not
5130   *     endpoints will be returned.  If not provided, all vertices will
5131   *     be returned.
5132   *
5133   * Returns:
5134   * {Array} A list of all vertices in the geometry.
5135   */
5136  getVertices: function(nodes) {
5137    var vertices;
5138    if(nodes === true)
5139      vertices = [
5140        this.components[0],
5141        this.components[this.components.length-1]
5142      ];
5143    else if (nodes === false)
5144      vertices = this.components.slice(1, this.components.length-1);
5145    else
5146      vertices = this.components.slice();
5147    return vertices;
5148  },
5149  distanceTo: function(geometry, options) {
5150    var edge = !(options && options.edge === false);
5151    var details = edge && options && options.details;
5152    var result, best = {};
5153    var min = Number.POSITIVE_INFINITY;
5154    if(geometry instanceof ZOO.Geometry.Point) {
5155      var segs = this.getSortedSegments();
5156      var x = geometry.x;
5157      var y = geometry.y;
5158      var seg;
5159      for(var i=0, len=segs.length; i<len; ++i) {
5160        seg = segs[i];
5161        result = ZOO.Geometry.distanceToSegment(geometry, seg);
5162        if(result.distance < min) {
5163          min = result.distance;
5164          best = result;
5165          if(min === 0)
5166            break;
5167        } else {
5168          // if distance increases and we cross y0 to the right of x0, no need to keep looking.
5169          if(seg.x2 > x && ((y > seg.y1 && y < seg.y2) || (y < seg.y1 && y > seg.y2)))
5170            break;
5171        }
5172      }
5173      if(details)
5174        best = {
5175          distance: best.distance,
5176          x0: best.x, y0: best.y,
5177          x1: x, y1: y
5178        };
5179      else
5180        best = best.distance;
5181    } else if(geometry instanceof ZOO.Geometry.LineString) { 
5182      var segs0 = this.getSortedSegments();
5183      var segs1 = geometry.getSortedSegments();
5184      var seg0, seg1, intersection, x0, y0;
5185      var len1 = segs1.length;
5186      var interOptions = {point: true};
5187      outer: for(var i=0, len=segs0.length; i<len; ++i) {
5188        seg0 = segs0[i];
5189        x0 = seg0.x1;
5190        y0 = seg0.y1;
5191        for(var j=0; j<len1; ++j) {
5192          seg1 = segs1[j];
5193          intersection = ZOO.Geometry.segmentsIntersect(seg0, seg1, interOptions);
5194          if(intersection) {
5195            min = 0;
5196            best = {
5197              distance: 0,
5198              x0: intersection.x, y0: intersection.y,
5199              x1: intersection.x, y1: intersection.y
5200            };
5201            break outer;
5202          } else {
5203            result = ZOO.Geometry.distanceToSegment({x: x0, y: y0}, seg1);
5204            if(result.distance < min) {
5205              min = result.distance;
5206              best = {
5207                distance: min,
5208                x0: x0, y0: y0,
5209                x1: result.x, y1: result.y
5210              };
5211            }
5212          }
5213        }
5214      }
5215      if(!details)
5216        best = best.distance;
5217      if(min !== 0) {
5218        // check the final vertex in this line's sorted segments
5219        if(seg0) {
5220          result = geometry.distanceTo(
5221              new ZOO.Geometry.Point(seg0.x2, seg0.y2),
5222              options
5223              );
5224          var dist = details ? result.distance : result;
5225          if(dist < min) {
5226            if(details)
5227              best = {
5228                distance: min,
5229                x0: result.x1, y0: result.y1,
5230                x1: result.x0, y1: result.y0
5231              };
5232            else
5233              best = dist;
5234          }
5235        }
5236      }
5237    } else {
5238      best = geometry.distanceTo(this, options);
5239      // swap since target comes from this line
5240      if(details)
5241        best = {
5242          distance: best.distance,
5243          x0: best.x1, y0: best.y1,
5244          x1: best.x0, y1: best.y0
5245        };
5246    }
5247    return best;
5248  },
5249  CLASS_NAME: "ZOO.Geometry.LineString"
5250});
5251/**
5252 * Class: ZOO.Geometry.LinearRing
5253 *
5254 * A Linear Ring is a special LineString which is closed. It closes itself
5255 * automatically on every addPoint/removePoint by adding a copy of the first
5256 * point as the last point.
5257 *
5258 * Also, as it is the first in the line family to close itself, a getArea()
5259 * function is defined to calculate the enclosed area of the linearRing
5260 *
5261 * Inherits:
5262 *  - <OpenLayers.Geometry.LineString>
5263 */
5264ZOO.Geometry.LinearRing = ZOO.Class(
5265  ZOO.Geometry.LineString, {
5266  /**
5267   * Property: componentTypes
5268   * {Array(String)} An array of class names representing the types of
5269   *                 components that the collection can include.  A null
5270   *                 value means the component types are not restricted.
5271   */
5272  componentTypes: ["ZOO.Geometry.Point"],
5273  /**
5274   * Constructor: OpenLayers.Geometry.LinearRing
5275   * Linear rings are constructed with an array of points.  This array
5276   *     can represent a closed or open ring.  If the ring is open (the last
5277   *     point does not equal the first point), the constructor will close
5278   *     the ring.  If the ring is already closed (the last point does equal
5279   *     the first point), it will be left closed.
5280   *
5281   * Parameters:
5282   * points - {Array(<ZOO.Geometry.Point>)} points
5283   */
5284  initialize: function(points) {
5285    ZOO.Geometry.LineString.prototype.initialize.apply(this,arguments);
5286  },
5287  /**
5288   * Method: addComponent
5289   * Adds a point to geometry components.  If the point is to be added to
5290   *     the end of the components array and it is the same as the last point
5291   *     already in that array, the duplicate point is not added.  This has
5292   *     the effect of closing the ring if it is not already closed, and
5293   *     doing the right thing if it is already closed.  This behavior can
5294   *     be overridden by calling the method with a non-null index as the
5295   *     second argument.
5296   *
5297   * Parameter:
5298   * point - {<ZOO.Geometry.Point>}
5299   * index - {Integer} Index into the array to insert the component
5300   *
5301   * Returns:
5302   * {Boolean} Was the Point successfully added?
5303   */
5304  addComponent: function(point, index) {
5305    var added = false;
5306    //remove last point
5307    var lastPoint = this.components.pop();
5308    // given an index, add the point
5309    // without an index only add non-duplicate points
5310    if(index != null || !point.equals(lastPoint))
5311      added = ZOO.Geometry.Collection.prototype.addComponent.apply(this,arguments);
5312    //append copy of first point
5313    var firstPoint = this.components[0];
5314    ZOO.Geometry.Collection.prototype.addComponent.apply(this,[firstPoint]);
5315    return added;
5316  },
5317  /**
5318   * APIMethod: removeComponent
5319   * Removes a point from geometry components.
5320   *
5321   * Parameters:
5322   * point - {<ZOO.Geometry.Point>}
5323   */
5324  removeComponent: function(point) {
5325    if (this.components.length > 4) {
5326      //remove last point
5327      this.components.pop();
5328      //remove our point
5329      ZOO.Geometry.Collection.prototype.removeComponent.apply(this,arguments);
5330      //append copy of first point
5331      var firstPoint = this.components[0];
5332      ZOO.Geometry.Collection.prototype.addComponent.apply(this,[firstPoint]);
5333    }
5334  },
5335  /**
5336   * Method: move
5337   * Moves a geometry by the given displacement along positive x and y axes.
5338   *     This modifies the position of the geometry and clears the cached
5339   *     bounds.
5340   *
5341   * Parameters:
5342   * x - {Float} Distance to move geometry in positive x direction.
5343   * y - {Float} Distance to move geometry in positive y direction.
5344   */
5345  move: function(x, y) {
5346    for(var i = 0, len=this.components.length; i<len - 1; i++) {
5347      this.components[i].move(x, y);
5348    }
5349  },
5350  /**
5351   * Method: rotate
5352   * Rotate a geometry around some origin
5353   *
5354   * Parameters:
5355   * angle - {Float} Rotation angle in degrees (measured counterclockwise
5356   *                 from the positive x-axis)
5357   * origin - {<ZOO.Geometry.Point>} Center point for the rotation
5358   */
5359  rotate: function(angle, origin) {
5360    for(var i=0, len=this.components.length; i<len - 1; ++i) {
5361      this.components[i].rotate(angle, origin);
5362    }
5363  },
5364  /**
5365   * Method: resize
5366   * Resize a geometry relative to some origin.  Use this method to apply
5367   *     a uniform scaling to a geometry.
5368   *
5369   * Parameters:
5370   * scale - {Float} Factor by which to scale the geometry.  A scale of 2
5371   *                 doubles the size of the geometry in each dimension
5372   *                 (lines, for example, will be twice as long, and polygons
5373   *                 will have four times the area).
5374   * origin - {<ZOO.Geometry.Point>} Point of origin for resizing
5375   * ratio - {Float} Optional x:y ratio for resizing.  Default ratio is 1.
5376   *
5377   * Returns:
5378   * {ZOO.Geometry} - The current geometry.
5379   */
5380  resize: function(scale, origin, ratio) {
5381    for(var i=0, len=this.components.length; i<len - 1; ++i) {
5382      this.components[i].resize(scale, origin, ratio);
5383    }
5384    return this;
5385  },
5386  /**
5387   * Method: transform
5388   * Reproject the components geometry from source to dest.
5389   *
5390   * Parameters:
5391   * source - {<ZOO.Projection>}
5392   * dest - {<ZOO.Projection>}
5393   *
5394   * Returns:
5395   * {<ZOO.Geometry>}
5396   */
5397  transform: function(source, dest) {
5398    if (source && dest) {
5399      for (var i=0, len=this.components.length; i<len - 1; i++) {
5400        var component = this.components[i];
5401        component.transform(source, dest);
5402      }
5403      this.bounds = null;
5404    }
5405    return this;
5406  },
5407  /**
5408   * Method: getCentroid
5409   *
5410   * Returns:
5411   * {<ZOO.Geometry.Point>} The centroid of the ring
5412   */
5413  getCentroid: function() {
5414    if ( this.components && (this.components.length > 2)) {
5415      var sumX = 0.0;
5416      var sumY = 0.0;
5417      for (var i = 0; i < this.components.length - 1; i++) {
5418        var b = this.components[i];
5419        var c = this.components[i+1];
5420        sumX += (b.x + c.x) * (b.x * c.y - c.x * b.y);
5421        sumY += (b.y + c.y) * (b.x * c.y - c.x * b.y);
5422      }
5423      var area = -1 * this.getArea();
5424      var x = sumX / (6 * area);
5425      var y = sumY / (6 * area);
5426    }
5427    return new ZOO.Geometry.Point(x, y);
5428  },
5429  /**
5430   * Method: getArea
5431   * Note - The area is positive if the ring is oriented CW, otherwise
5432   *         it will be negative.
5433   *
5434   * Returns:
5435   * {Float} The signed area for a ring.
5436   */
5437  getArea: function() {
5438    var area = 0.0;
5439    if ( this.components && (this.components.length > 2)) {
5440      var sum = 0.0;
5441      for (var i=0, len=this.components.length; i<len - 1; i++) {
5442        var b = this.components[i];
5443        var c = this.components[i+1];
5444        sum += (b.x + c.x) * (c.y - b.y);
5445      }
5446      area = - sum / 2.0;
5447    }
5448    return area;
5449  },
5450  /**
5451   * Method: getGeodesicArea
5452   * Calculate the approximate area of the polygon were it projected onto
5453   *     the earth.  Note that this area will be positive if ring is oriented
5454   *     clockwise, otherwise it will be negative.
5455   *
5456   * Parameters:
5457   * projection - {<ZOO.Projection>} The spatial reference system
5458   *     for the geometry coordinates.  If not provided, Geographic/WGS84 is
5459   *     assumed.
5460   *
5461   * Reference:
5462   * Robert. G. Chamberlain and William H. Duquette, "Some Algorithms for
5463   *     Polygons on a Sphere", JPL Publication 07-03, Jet Propulsion
5464   *     Laboratory, Pasadena, CA, June 2007 http://trs-new.jpl.nasa.gov/dspace/handle/2014/40409
5465   *
5466   * Returns:
5467   * {float} The approximate signed geodesic area of the polygon in square
5468   *     meters.
5469   */
5470  getGeodesicArea: function(projection) {
5471    var ring = this;  // so we can work with a clone if needed
5472    if(projection) {
5473      var gg = new ZOO.Projection("EPSG:4326");
5474      if(!gg.equals(projection)) {
5475        ring = this.clone().transform(projection, gg);
5476      }
5477    }
5478    var area = 0.0;
5479    var len = ring.components && ring.components.length;
5480    if(len > 2) {
5481      var p1, p2;
5482      for(var i=0; i<len-1; i++) {
5483        p1 = ring.components[i];
5484        p2 = ring.components[i+1];
5485        area += ZOO.rad(p2.x - p1.x) *
5486                (2 + Math.sin(ZOO.rad(p1.y)) +
5487                Math.sin(ZOO.rad(p2.y)));
5488      }
5489      area = area * 6378137.0 * 6378137.0 / 2.0;
5490    }
5491    return area;
5492  },
5493  /**
5494   * Method: containsPoint
5495   * Test if a point is inside a linear ring.  For the case where a point
5496   *     is coincident with a linear ring edge, returns 1.  Otherwise,
5497   *     returns boolean.
5498   *
5499   * Parameters:
5500   * point - {<ZOO.Geometry.Point>}
5501   *
5502   * Returns:
5503   * {Boolean | Number} The point is inside the linear ring.  Returns 1 if
5504   *     the point is coincident with an edge.  Returns boolean otherwise.
5505   */
5506  containsPoint: function(point) {
5507    var approx = OpenLayers.Number.limitSigDigs;
5508    var digs = 14;
5509    var px = approx(point.x, digs);
5510    var py = approx(point.y, digs);
5511    function getX(y, x1, y1, x2, y2) {
5512      return (((x1 - x2) * y) + ((x2 * y1) - (x1 * y2))) / (y1 - y2);
5513    }
5514    var numSeg = this.components.length - 1;
5515    var start, end, x1, y1, x2, y2, cx, cy;
5516    var crosses = 0;
5517    for(var i=0; i<numSeg; ++i) {
5518      start = this.components[i];
5519      x1 = approx(start.x, digs);
5520      y1 = approx(start.y, digs);
5521      end = this.components[i + 1];
5522      x2 = approx(end.x, digs);
5523      y2 = approx(end.y, digs);
5524
5525      /**
5526       * The following conditions enforce five edge-crossing rules:
5527       *    1. points coincident with edges are considered contained;
5528       *    2. an upward edge includes its starting endpoint, and
5529       *    excludes its final endpoint;
5530       *    3. a downward edge excludes its starting endpoint, and
5531       *    includes its final endpoint;
5532       *    4. horizontal edges are excluded; and
5533       *    5. the edge-ray intersection point must be strictly right
5534       *    of the point P.
5535       */
5536      if(y1 == y2) {
5537        // horizontal edge
5538        if(py == y1) {
5539          // point on horizontal line
5540          if(x1 <= x2 && (px >= x1 && px <= x2) || // right or vert
5541              x1 >= x2 && (px <= x1 && px >= x2)) { // left or vert
5542            // point on edge
5543            crosses = -1;
5544            break;
5545          }
5546        }
5547        // ignore other horizontal edges
5548        continue;
5549      }
5550      cx = approx(getX(py, x1, y1, x2, y2), digs);
5551      if(cx == px) {
5552        // point on line
5553        if(y1 < y2 && (py >= y1 && py <= y2) || // upward
5554            y1 > y2 && (py <= y1 && py >= y2)) { // downward
5555          // point on edge
5556          crosses = -1;
5557          break;
5558        }
5559      }
5560      if(cx <= px) {
5561        // no crossing to the right
5562        continue;
5563      }
5564      if(x1 != x2 && (cx < Math.min(x1, x2) || cx > Math.max(x1, x2))) {
5565        // no crossing
5566        continue;
5567      }
5568      if(y1 < y2 && (py >= y1 && py < y2) || // upward
5569          y1 > y2 && (py < y1 && py >= y2)) { // downward
5570        ++crosses;
5571      }
5572    }
5573    var contained = (crosses == -1) ?
5574      // on edge
5575      1 :
5576      // even (out) or odd (in)
5577      !!(crosses & 1);
5578
5579    return contained;
5580  },
5581  intersects: function(geometry) {
5582    var intersect = false;
5583    if(geometry.CLASS_NAME == "ZOO.Geometry.Point")
5584      intersect = this.containsPoint(geometry);
5585    else if(geometry.CLASS_NAME == "ZOO.Geometry.LineString")
5586      intersect = geometry.intersects(this);
5587    else if(geometry.CLASS_NAME == "ZOO.Geometry.LinearRing")
5588      intersect = ZOO.Geometry.LineString.prototype.intersects.apply(
5589          this, [geometry]
5590          );
5591    else
5592      for(var i=0, len=geometry.components.length; i<len; ++ i) {
5593        intersect = geometry.components[i].intersects(this);
5594        if(intersect)
5595          break;
5596      }
5597    return intersect;
5598  },
5599  getVertices: function(nodes) {
5600    return (nodes === true) ? [] : this.components.slice(0, this.components.length-1);
5601  },
5602  CLASS_NAME: "ZOO.Geometry.LinearRing"
5603});
5604/**
5605 * Class: ZOO.Geometry.MultiLineString
5606 * A MultiLineString is a geometry with multiple <ZOO.Geometry.LineString>
5607 * components.
5608 *
5609 * Inherits from:
5610 *  - <ZOO.Geometry.Collection>
5611 */
5612ZOO.Geometry.MultiLineString = ZOO.Class(
5613  ZOO.Geometry.Collection, {
5614  componentTypes: ["ZOO.Geometry.LineString"],
5615  /**
5616   * Constructor: ZOO.Geometry.MultiLineString
5617   * Constructor for a MultiLineString Geometry.
5618   *
5619   * Parameters:
5620   * components - {Array(<ZOO.Geometry.LineString>)}
5621   *
5622   */
5623  initialize: function(components) {
5624    ZOO.Geometry.Collection.prototype.initialize.apply(this,arguments);       
5625  },
5626  split: function(geometry, options) {
5627    var results = null;
5628    var mutual = options && options.mutual;
5629    var splits, sourceLine, sourceLines, sourceSplit, targetSplit;
5630    var sourceParts = [];
5631    var targetParts = [geometry];
5632    for(var i=0, len=this.components.length; i<len; ++i) {
5633      sourceLine = this.components[i];
5634      sourceSplit = false;
5635      for(var j=0; j < targetParts.length; ++j) { 
5636        splits = sourceLine.split(targetParts[j], options);
5637        if(splits) {
5638          if(mutual) {
5639            sourceLines = splits[0];
5640            for(var k=0, klen=sourceLines.length; k<klen; ++k) {
5641              if(k===0 && sourceParts.length)
5642                sourceParts[sourceParts.length-1].addComponent(
5643                  sourceLines[k]
5644                );
5645              else
5646                sourceParts.push(
5647                  new ZOO.Geometry.MultiLineString([
5648                    sourceLines[k]
5649                    ])
5650                );
5651            }
5652            sourceSplit = true;
5653            splits = splits[1];
5654          }
5655          if(splits.length) {
5656            // splice in new target parts
5657            splits.unshift(j, 1);
5658            Array.prototype.splice.apply(targetParts, splits);
5659            break;
5660          }
5661        }
5662      }
5663      if(!sourceSplit) {
5664        // source line was not hit
5665        if(sourceParts.length) {
5666          // add line to existing multi
5667          sourceParts[sourceParts.length-1].addComponent(
5668              sourceLine.clone()
5669              );
5670        } else {
5671          // create a fresh multi
5672          sourceParts = [
5673            new ZOO.Geometry.MultiLineString(
5674                sourceLine.clone()
5675                )
5676            ];
5677        }
5678      }
5679    }
5680    if(sourceParts && sourceParts.length > 1)
5681      sourceSplit = true;
5682    else
5683      sourceParts = [];
5684    if(targetParts && targetParts.length > 1)
5685      targetSplit = true;
5686    else
5687      targetParts = [];
5688    if(sourceSplit || targetSplit) {
5689      if(mutual)
5690        results = [sourceParts, targetParts];
5691      else
5692        results = targetParts;
5693    }
5694    return results;
5695  },
5696  splitWith: function(geometry, options) {
5697    var results = null;
5698    var mutual = options && options.mutual;
5699    var splits, targetLine, sourceLines, sourceSplit, targetSplit, sourceParts, targetParts;
5700    if(geometry instanceof ZOO.Geometry.LineString) {
5701      targetParts = [];
5702      sourceParts = [geometry];
5703      for(var i=0, len=this.components.length; i<len; ++i) {
5704        targetSplit = false;
5705        targetLine = this.components[i];
5706        for(var j=0; j<sourceParts.length; ++j) {
5707          splits = sourceParts[j].split(targetLine, options);
5708          if(splits) {
5709            if(mutual) {
5710              sourceLines = splits[0];
5711              if(sourceLines.length) {
5712                // splice in new source parts
5713                sourceLines.unshift(j, 1);
5714                Array.prototype.splice.apply(sourceParts, sourceLines);
5715                j += sourceLines.length - 2;
5716              }
5717              splits = splits[1];
5718              if(splits.length === 0) {
5719                splits = [targetLine.clone()];
5720              }
5721            }
5722            for(var k=0, klen=splits.length; k<klen; ++k) {
5723              if(k===0 && targetParts.length) {
5724                targetParts[targetParts.length-1].addComponent(
5725                    splits[k]
5726                    );
5727              } else {
5728                targetParts.push(
5729                    new ZOO.Geometry.MultiLineString([
5730                      splits[k]
5731                      ])
5732                    );
5733              }
5734            }
5735            targetSplit = true;                   
5736          }
5737        }
5738        if(!targetSplit) {
5739          // target component was not hit
5740          if(targetParts.length) {
5741            // add it to any existing multi-line
5742            targetParts[targetParts.length-1].addComponent(
5743                targetLine.clone()
5744                );
5745          } else {
5746            // or start with a fresh multi-line
5747            targetParts = [
5748              new ZOO.Geometry.MultiLineString([
5749                  targetLine.clone()
5750                  ])
5751              ];
5752          }
5753
5754        }
5755      }
5756    } else {
5757      results = geometry.split(this);
5758    }
5759    if(sourceParts && sourceParts.length > 1)
5760      sourceSplit = true;
5761    else
5762      sourceParts = [];
5763    if(targetParts && targetParts.length > 1)
5764      targetSplit = true;
5765    else
5766      targetParts = [];
5767    if(sourceSplit || targetSplit) {
5768      if(mutual)
5769        results = [sourceParts, targetParts];
5770      else
5771        results = targetParts;
5772    }
5773    return results;
5774  },
5775  CLASS_NAME: "ZOO.Geometry.MultiLineString"
5776});
5777/**
5778 * Class: ZOO.Geometry.Polygon
5779 * Polygon is a collection of <ZOO.Geometry.LinearRing>.
5780 *
5781 * Inherits from:
5782 *  - <ZOO.Geometry.Collection>
5783 */
5784ZOO.Geometry.Polygon = ZOO.Class(
5785  ZOO.Geometry.Collection, {
5786  componentTypes: ["ZOO.Geometry.LinearRing"],
5787  /**
5788   * Constructor: OpenLayers.Geometry.Polygon
5789   * Constructor for a Polygon geometry.
5790   * The first ring (this.component[0])is the outer bounds of the polygon and
5791   * all subsequent rings (this.component[1-n]) are internal holes.
5792   *
5793   *
5794   * Parameters:
5795   * components - {Array(<ZOO.Geometry.LinearRing>)}
5796   */
5797  initialize: function(components) {
5798    ZOO.Geometry.Collection.prototype.initialize.apply(this,arguments);
5799  },
5800  /**
5801   * Method: getArea
5802   * Calculated by subtracting the areas of the internal holes from the
5803   *   area of the outer hole.
5804   *
5805   * Returns:
5806   * {float} The area of the geometry
5807   */
5808  getArea: function() {
5809    var area = 0.0;
5810    if ( this.components && (this.components.length > 0)) {
5811      area += Math.abs(this.components[0].getArea());
5812      for (var i=1, len=this.components.length; i<len; i++) {
5813        area -= Math.abs(this.components[i].getArea());
5814      }
5815    }
5816    return area;
5817  },
5818  /**
5819   * APIMethod: getGeodesicArea
5820   * Calculate the approximate area of the polygon were it projected onto
5821   *     the earth.
5822   *
5823   * Parameters:
5824   * projection - {<ZOO.Projection>} The spatial reference system
5825   *     for the geometry coordinates.  If not provided, Geographic/WGS84 is
5826   *     assumed.
5827   *
5828   * Reference:
5829   * Robert. G. Chamberlain and William H. Duquette, "Some Algorithms for
5830   *     Polygons on a Sphere", JPL Publication 07-03, Jet Propulsion
5831   *     Laboratory, Pasadena, CA, June 2007 http://trs-new.jpl.nasa.gov/dspace/handle/2014/40409
5832   *
5833   * Returns:
5834   * {float} The approximate geodesic area of the polygon in square meters.
5835   */
5836  getGeodesicArea: function(projection) {
5837    var area = 0.0;
5838    if(this.components && (this.components.length > 0)) {
5839      area += Math.abs(this.components[0].getGeodesicArea(projection));
5840      for(var i=1, len=this.components.length; i<len; i++) {
5841          area -= Math.abs(this.components[i].getGeodesicArea(projection));
5842      }
5843    }
5844    return area;
5845  },
5846  /**
5847   * Method: containsPoint
5848   * Test if a point is inside a polygon.  Points on a polygon edge are
5849   *     considered inside.
5850   *
5851   * Parameters:
5852   * point - {<ZOO.Geometry.Point>}
5853   *
5854   * Returns:
5855   * {Boolean | Number} The point is inside the polygon.  Returns 1 if the
5856   *     point is on an edge.  Returns boolean otherwise.
5857   */
5858  containsPoint: function(point) {
5859    var numRings = this.components.length;
5860    var contained = false;
5861    if(numRings > 0) {
5862    // check exterior ring - 1 means on edge, boolean otherwise
5863      contained = this.components[0].containsPoint(point);
5864      if(contained !== 1) {
5865        if(contained && numRings > 1) {
5866          // check interior rings
5867          var hole;
5868          for(var i=1; i<numRings; ++i) {
5869            hole = this.components[i].containsPoint(point);
5870            if(hole) {
5871              if(hole === 1)
5872                contained = 1;
5873              else
5874                contained = false;
5875              break;
5876            }
5877          }
5878        }
5879      }
5880    }
5881    return contained;
5882  },
5883  intersects: function(geometry) {
5884    var intersect = false;
5885    var i, len;
5886    if(geometry.CLASS_NAME == "ZOO.Geometry.Point") {
5887      intersect = this.containsPoint(geometry);
5888    } else if(geometry.CLASS_NAME == "ZOO.Geometry.LineString" ||
5889              geometry.CLASS_NAME == "ZOO.Geometry.LinearRing") {
5890      // check if rings/linestrings intersect
5891      for(i=0, len=this.components.length; i<len; ++i) {
5892        intersect = geometry.intersects(this.components[i]);
5893        if(intersect) {
5894          break;
5895        }
5896      }
5897      if(!intersect) {
5898        // check if this poly contains points of the ring/linestring
5899        for(i=0, len=geometry.components.length; i<len; ++i) {
5900          intersect = this.containsPoint(geometry.components[i]);
5901          if(intersect) {
5902            break;
5903          }
5904        }
5905      }
5906    } else {
5907      for(i=0, len=geometry.components.length; i<len; ++ i) {
5908        intersect = this.intersects(geometry.components[i]);
5909        if(intersect)
5910          break;
5911      }
5912    }
5913    // check case where this poly is wholly contained by another
5914    if(!intersect && geometry.CLASS_NAME == "ZOO.Geometry.Polygon") {
5915      // exterior ring points will be contained in the other geometry
5916      var ring = this.components[0];
5917      for(i=0, len=ring.components.length; i<len; ++i) {
5918        intersect = geometry.containsPoint(ring.components[i]);
5919        if(intersect)
5920          break;
5921      }
5922    }
5923    return intersect;
5924  },
5925  distanceTo: function(geometry, options) {
5926    var edge = !(options && options.edge === false);
5927    var result;
5928    // this is the case where we might not be looking for distance to edge
5929    if(!edge && this.intersects(geometry))
5930      result = 0;
5931    else
5932      result = ZOO.Geometry.Collection.prototype.distanceTo.apply(
5933          this, [geometry, options]
5934          );
5935    return result;
5936  },
5937  CLASS_NAME: "ZOO.Geometry.Polygon"
5938});
5939/**
5940 * Method: createRegularPolygon
5941 * Create a regular polygon around a radius. Useful for creating circles
5942 * and the like.
5943 *
5944 * Parameters:
5945 * origin - {<ZOO.Geometry.Point>} center of polygon.
5946 * radius - {Float} distance to vertex, in map units.
5947 * sides - {Integer} Number of sides. 20 approximates a circle.
5948 * rotation - {Float} original angle of rotation, in degrees.
5949 */
5950ZOO.Geometry.Polygon.createRegularPolygon = function(origin, radius, sides, rotation) { 
5951    var angle = Math.PI * ((1/sides) - (1/2));
5952    if(rotation) {
5953        angle += (rotation / 180) * Math.PI;
5954    }
5955    var rotatedAngle, x, y;
5956    var points = [];
5957    for(var i=0; i<sides; ++i) {
5958        rotatedAngle = angle + (i * 2 * Math.PI / sides);
5959        x = origin.x + (radius * Math.cos(rotatedAngle));
5960        y = origin.y + (radius * Math.sin(rotatedAngle));
5961        points.push(new ZOO.Geometry.Point(x, y));
5962    }
5963    var ring = new ZOO.Geometry.LinearRing(points);
5964    return new ZOO.Geometry.Polygon([ring]);
5965};
5966/**
5967 * Class: ZOO.Geometry.MultiPolygon
5968 * MultiPolygon is a geometry with multiple <ZOO.Geometry.Polygon>
5969 * components.  Create a new instance with the <ZOO.Geometry.MultiPolygon>
5970 * constructor.
5971 *
5972 * Inherits from:
5973 *  - <ZOO.Geometry.Collection>
5974 */
5975ZOO.Geometry.MultiPolygon = ZOO.Class(
5976  ZOO.Geometry.Collection, {
5977  componentTypes: ["ZOO.Geometry.Polygon"],
5978  /**
5979   * Constructor: OpenLayers.Geometry.MultiPolygon
5980   * Create a new MultiPolygon geometry
5981   *
5982   * Parameters:
5983   * components - {Array(<ZOO.Geometry.Polygon>)} An array of polygons
5984   *              used to generate the MultiPolygon
5985   *
5986   */
5987  initialize: function(components) {
5988    ZOO.Geometry.Collection.prototype.initialize.apply(this,arguments);
5989  },
5990  CLASS_NAME: "ZOO.Geometry.MultiPolygon"
5991});
5992
5993ZOO.Process = ZOO.Class({
5994  schemaLocation: "http://www.opengis.net/wps/1.0.0/../wpsExecute_request.xsd",
5995  namespaces: {
5996    ows: "http://www.opengis.net/ows/1.1",
5997    wps: "http://www.opengis.net/wps/1.0.0",
5998    xlink: "http://www.w3.org/1999/xlink",
5999    xsi: "http://www.w3.org/2001/XMLSchema-instance",
6000  },
6001  url: 'http://localhost/zoo',
6002  identifier: null,
6003  initialize: function(url,identifier) {
6004    this.url = url;
6005    this.identifier = identifier;
6006  },
6007  Execute: function(inputs) {
6008    if (this.identifier == null)
6009      return null;
6010    var body = new XML('<wps:Execute service="WPS" version="1.0.0" xmlns:wps="'+this.namespaces['wps']+'" xmlns:ows="'+this.namespaces['ows']+'" xmlns:xlink="'+this.namespaces['xlink']+'" xmlns:xsi="'+this.namespaces['xsi']+'" xsi:schemaLocation="'+this.schemaLocation+'"><ows:Identifier>'+this.identifier+'</ows:Identifier>'+this.buildDataInputsNode(inputs)+'</wps:Execute>');
6011    body = body.toXMLString();
6012    var response = ZOO.Request.Post(this.url,body,['Content-Type: text/xml; charset=UTF-8']);
6013    return response;
6014  },
6015  buildInput: {
6016    'complex': function(identifier,data) {
6017      var input = new XML('<wps:Input xmlns:wps="'+this.namespaces['wps']+'"><ows:Identifier xmlns:ows="'+this.namespaces['ows']+'">'+identifier+'</ows:Identifier><wps:Data><wps:ComplexData>'+data.value+'</wps:ComplexData></wps:Data></wps:Input>');
6018      input.*::Data.*::ComplexData.@mimeType = data.mimetype ? data.mimetype : 'text/plain';
6019      if (data.encoding)
6020        input.*::Data.*::ComplexData.@encoding = data.encoding;
6021      if (data.schema)
6022        input.*::Data.*::ComplexData.@schema = data.schema;
6023      input = input.toXMLString();
6024      return input;
6025    },
6026    'reference': function(identifier,data) {
6027      return '<wps:Input xmlns:wps="'+this.namespaces['wps']+'"><ows:Identifier xmlns:ows="'+this.namespaces['ows']+'">'+identifier+'</ows:Identifier><wps:Reference xmlns:xlink="'+this.namespaces['xlink']+'" xlink:href="'+data.value.replace('&','&amp;','gi')+'"/></wps:Input>';
6028    },
6029    'literal': function(identifier,data) {
6030      var input = new XML('<wps:Input xmlns:wps="'+this.namespaces['wps']+'"><ows:Identifier xmlns:ows="'+this.namespaces['ows']+'">'+identifier+'</ows:Identifier><wps:Data><wps:LiteralData>'+data.value+'</wps:LiteralData></wps:Data></wps:Input>');
6031      if (data.type)
6032        input.*::Data.*::LiteralData.@dataType = data.type;
6033      if (data.uom)
6034        input.*::Data.*::LiteralData.@uom = data.uom;
6035      input = input.toXMLString();
6036      return input;
6037    }
6038  },
6039  buildDataInputsNode:function(inputs){
6040    var data, builder, inputsArray=[];
6041    for (var attr in inputs) {
6042      data = inputs[attr];
6043      if (data.mimetype || data.type == 'complex')
6044        builder = this.buildInput['complex'];
6045      else if (data.type == 'reference' || data.type == 'url')
6046        builder = this.buildInput['reference'];
6047      else
6048        builder = this.buildInput['literal'];
6049      inputsArray.push(builder.apply(this,[attr,data]));
6050    }
6051    return '<wps:DataInputs xmlns:wps="'+this.namespaces['wps']+'">'+inputsArray.join('\n')+'</wps:DataInputs>';
6052  },
6053  CLASS_NAME: "ZOO.Process"
6054});
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png