package com.github.jsonj.tools;

import static com.github.jsonj.tools.JsonBuilder.array;
import static com.github.jsonj.tools.JsonBuilder.primitive;

import com.github.jsonj.JsonArray;

/**
 * Converting between some common types and json can be a bit tedious. The ones supported in this class
 * are by no means exhaustive. I tend to add to this class on a need to have basis.
 */
public class TypeSupport {

    /**
     * Convert 2d array of doubles to the json equivalent. Useful for polygons.
     * @param twoDArray
     * @return a JsonArray
     */
    public static JsonArray convertFrom2DDoubleArray(double[][] twoDArray) {
        JsonArray result = array();
        for (int i=0;i<twoDArray.length ; i++) {
            double[] values = twoDArray[i];
            JsonArray subArray = array();
            for (int j = 0; j < values.length; j++) {
                subArray.add(primitive(values[j]));
            }
            result.add(subArray);
        }
        return result;
    }
    
    /**
     * Convert json array of arrays of double primitives to a double[][].
     * @param arrayOfArrays
     * @return a double[][]
     */
    public static double[][] convertTo2DDoubleArray(JsonArray arrayOfArrays) {
        double[][] result = new double[arrayOfArrays.size()][arrayOfArrays.get(0).asArray().size()];
        for(int i =0; i<arrayOfArrays.size();i++) {
            JsonArray subArray = arrayOfArrays.get(i).asArray();
            for(int j =0; j<subArray.size();j++) {
                result[i][j] = subArray.get(j).asPrimitive().asDouble();
            }            
        }        
        return result;
    }
}

@@ -453,73 +453,107 @@
  to 
-the json equivalent. Useful for polygons.%0A     * @param twoDArray
+a geojson style multi polygon in a 3D array of arrays of %5Blatitude,longitude%5D%0A     * arrays.%0A     *
 %0A   
@@ -616,32 +616,25 @@
 ray 
-convertFrom2DDoubleArray
+getGeoJsonPolygon
 (dou
@@ -641,25 +641,27 @@
 ble%5B%5D%5B%5D 
-twoDArray
+coordinates
 ) %7B%0A    
@@ -674,22 +674,22 @@
 onArray 
-result
+points
  = array
@@ -715,25 +715,27 @@
 t i=0;i%3C
-twoDArray
+coordinates
 .length 
@@ -777,17 +777,19 @@
 s = 
-twoDArray
+coordinates
 %5Bi%5D;
@@ -967,29 +967,1074 @@
     
-result.add(subArray);
+points.add(subArray);%0A        %7D%0A        if(coordinates%5B0%5D%5B0%5D != coordinates%5Bcoordinates.length-1%5D%5B0%5D %7C%7C coordinates%5B0%5D%5B1%5D != coordinates%5Bcoordinates.length-1%5D%5B1%5D) %7B%0A            // add last coordinate to close the polygon%0A            points.add(array(coordinates%5B0%5D%5B0%5D, coordinates%5B0%5D%5B1%5D));%0A        %7D%0A%0A        JsonArray geoJsonPolygon = array();%0A        geoJsonPolygon.add(points);%0A        return geoJsonPolygon;%0A    %7D%0A%0A    /**%0A     * @param geoJsonMultiPolygon%0A     * @return the 2d array of coordinates for the first polygon in the GeoJson multipolygon.%0A     */%0A    public static double%5B%5D%5B%5D fromGeoJsonPolygon(JsonArray geoJsonMultiPolygon) %7B%0A        JsonArray firstPolygon = geoJsonMultiPolygon.get(0).asArray();%0A        double%5B%5D%5B%5D result = new double%5BfirstPolygon.size()%5D%5BfirstPolygon.get(0).asArray().size()%5D;%0A        for(int i =0; i%3CfirstPolygon.size();i++) %7B%0A            JsonArray subArray = firstPolygon.get(i).asArray();%0A            for(int j =0; j%3CsubArray.size();j++) %7B%0A                result%5Bi%5D%5Bj%5D = subArray.get(j).asPrimitive().asDouble();%0A            %7D
 %0A   
@@ -2069,20 +2069,16 @@
 ;%0A    %7D%0A
-    
 %0A    /**
@@ -2650,28 +2650,16 @@
        %7D
-            
 %0A       
@@ -2660,24 +2660,16 @@
        %7D
-        
 %0A       

add proper geojson style support for polygons