1 /**
  2  * Creates a new Formats object.
  3  * @constructor
  4  */
  5 madrona.Formats = function() {
  6 };
  7 
  8 /**
  9  * Builds the wkt polygon representation from a geojson polygon representation
 10  * @param {Geojson} geojson_obj, geojson representation of a polygon
 11  */
 12 madrona.Formats.prototype.geojsonToWkt = function(geojson_obj) {
 13     var coords = geojson_obj.coordinates;
 14     var wkt = 'POLYGON(';
 15     var numPolys = coords.length;
 16     for ( var poly = 0; poly < numPolys; poly++ ) {
 17         var numPoints = coords[poly].length;
 18         if ( poly == 0 )
 19             wkt += '(';
 20         else
 21             wkt += ', (';
 22         for ( var point = 0; point < numPoints; point++ ) {
 23             if (point > 0)
 24                 wkt += ',';
 25             wkt += coords[poly][point][0] + ' ' + coords[poly][point][1];
 26         }
 27         wkt += ')';
 28     }
 29     wkt += ')';
 30     return wkt;
 31 };
 32 
 33 madrona.Formats.prototype.kmlToWkt = function(shape) {
 34     var linearRing = shape.getGeometry().getOuterBoundary();
 35     var wkt = 'POLYGON((';
 36     for ( var i = 0; i < linearRing.getCoordinates().getLength(); i++ ) {
 37         if ( i > 0 )
 38             wkt = wkt + ',';
 39         wkt = wkt + linearRing.getCoordinates().get(i).getLongitude() + ' ' + linearRing.getCoordinates().get(i).getLatitude();
 40     }
 41     wkt = wkt + '))';
 42     return wkt;
 43 };
 44 
 45 
 46 /**
 47  * Builds the kml placemark representation from a geojson encoded polygon 
 48  * @param {Geojson} geojson_obj, geojson representation of a polygon
 49  */
 50 madrona.Formats.prototype.geojsonToKmlPlacemark = function(geojson_obj) {
 51     var coords = geojson_obj.coordinates;
 52     var inner_kml = this.innerKml(coords);
 53     var kml = '<Placemark> <Style> <LineStyle><color>ffffffff</color><width>2</width></LineStyle> <PolyStyle><color>8000ff00</color></PolyStyle> </Style>'+inner_kml+'</Placemark>';
 54     return kml;
 55 };
 56 
 57 /**
 58  * Called from geojsonToKml
 59  * Builds the coordinate related kml from a geojson list of coordinates
 60  * @param {List} coords, geojson list of coordinates
 61  */
 62 madrona.Formats.prototype.innerKml = function(coords) {
 63     var kml = '<Polygon>';
 64     var numPolys = coords.length;
 65     for ( var poly = 0; poly < numPolys; poly++ ) {
 66         var numPoints = coords[poly].length;
 67         if ( poly == 0 ) 
 68             kml += '<outerBoundaryIs><LinearRing><coordinates>';
 69         else
 70             kml += '<innerBoundaryIs><LinearRing><coordinates>';
 71         for ( var point = 0; point < numPoints; point++ ) {
 72             if ( point > 0 ) 
 73                 kml += ' ';
 74             kml += coords[poly][point][0] + ',' + coords[poly][point][1] + ',0';
 75         }
 76         if ( poly == 0 )
 77             kml += '</coordinates></LinearRing></outerBoundaryIs>';
 78         else
 79             kml += '</coordinates></LinearRing></innerBoundaryIs>';
 80     }
 81     kml += '</Polygon>';
 82     return kml;
 83 };
 84 
 85 madrona.Formats.prototype.wktPolyToKml = function(wkt){
 86     var wkt = wkt.replace('POLYGON', '');
 87     wkt = wkt.replace('SRID=4326;', '');
 88     wkt = jQuery.trim(wkt.replace('((', '').replace('))', ''));
 89     var coords = wkt.split(',');
 90     var new_coords = [];
 91     for(var i=0; i<coords.length;i++){
 92         var values = jQuery.trim(coords[i]).split(' ');
 93         new_coords.push([values[0], values[1]]);
 94     }
 95     var inner_kml = this.innerKml([new_coords]);
 96     var kml = '<Placemark><Style><LineStyle><color>ffffffff</color><width>2</width></LineStyle> <PolyStyle><color>8000ff00</color></PolyStyle></Style>'+inner_kml+'</Placemark>';
 97     return kml;
 98 };