001package ca.bc.webarts.tools;
002
003import java.util.regex.Matcher;
004import java.util.regex.Pattern;
005
006import org.junit.Assert;
007import org.junit.Test;
008
009import com.google.gson.Gson;
010import com.google.gson.GsonBuilder;
011import com.google.gson.JsonArray;
012import com.google.gson.JsonObject;
013import com.google.gson.JsonPrimitive;
014
015public class GeoJson2KmlTest {
016
017  private final boolean DEBUG = true;
018
019  @Test
020  public  void testEmpty() {
021    JsonObject empty = new JsonObject();
022    String kml = GeoJson2Kml.convert(empty);
023    print(empty, kml);
024    check(kml, "kml", "Folder");
025  }
026
027  @Test
028  public  void testPoint() {
029    JsonObject obj = new JsonObject();
030    obj.addProperty("type", "Point");
031    JsonArray coordinates = new JsonArray();
032    coordinates.add(new JsonPrimitive(100.1));
033    coordinates.add(new JsonPrimitive(50.1));
034    coordinates.add(new JsonPrimitive(10.1));
035    obj.add("coordinates", coordinates);
036    String kml = GeoJson2Kml.convert(obj);
037    print(obj, kml);
038    check(kml, "Point", "coordinates", "100.1,50.1,10.1");
039  }
040
041  @Test
042  public  void testLineString() {
043    String json = "{ "
044        + "  \"type\": \"LineString\","
045        + "  \"coordinates\": [ [100.0, 0.0], [101.0, 1.0] ]"
046        + "}";
047    JsonObject obj = new Gson().fromJson(json, JsonObject.class);
048    String kml = GeoJson2Kml.convert(obj);
049    print(obj, kml);
050    check(kml, "LineString", "coordinates", "100.0,0.0", "101.0,1.0");
051  }
052
053  @Test
054  public  void testPolygon() {
055    String json = "{ \"type\": \"Polygon\","
056        + "  \"coordinates\": ["
057        + "      [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ],"
058        + "      [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]"
059        + "  ]"
060        + "}";
061    JsonObject obj = new Gson().fromJson(json, JsonObject.class);
062    String kml = GeoJson2Kml.convert(obj);
063    print(obj, kml);
064    check(kml, "Polygon", "outerBoundaryIs", "LinearRing", "coordinates",
065        "100.0,0.0", "innerBoundaryIs", "LinearRing", "coordinates",
066        "100.2,0.2");
067  }
068
069  @Test
070  public  void testGeometryCollection() {
071    String json = "{ \"type\": \"GeometryCollection\","
072        + "  \"geometries\": ["
073        + "    { \"type\": \"Point\","
074        + "      \"coordinates\": [100.0, 0.0]"
075        + "    },"
076        + "    { \"type\": \"LineString\","
077        + "      \"coordinates\": [ [101.0, 0.0], [102.0, 1.0] ]"
078        + "    }"
079        + "  ]"
080        + "}";
081    JsonObject obj = new Gson().fromJson(json, JsonObject.class);
082    String kml = GeoJson2Kml.convert(obj);
083    print(obj, kml);
084    check(kml, "MultiGeometry", "Point", " 100.0,0.0", "LineString",
085        "102.0,1.0");
086  }
087
088  private  void check(String kml, String... parts) {
089    String any = "(.*)";
090    String regex = any;
091    for (String part : parts)
092      regex += part + any;
093    Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
094    Matcher matcher = pattern.matcher(kml);
095    Assert.assertTrue(matcher.matches());
096  }
097
098  private  void print(JsonObject obj, String kml) {
099    if (!DEBUG)
100      return;
101    System.out.println("Converted GeoJSON: ");
102    Gson gson = new GsonBuilder().setPrettyPrinting().create();
103    System.out.println(gson.toJson(obj));
104    System.out.println("to KML: ");
105    System.out.println(kml);
106    System.out.println();
107  }
108
109}