changeset 84:6bf38b5e30a8

also stores polygon shape image annotations.
author casties
date Fri, 23 Jan 2015 17:24:53 +0100
parents 19ebbcff9638
children ed51eadc82c5
files src/main/java/de/mpiwg/itgroup/annotations/Annotation.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java
diffstat 2 files changed, 57 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/Annotation.java	Mon Jan 19 17:15:51 2015 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/Annotation.java	Fri Jan 23 17:24:53 2015 +0100
@@ -67,7 +67,7 @@
      *
      */
     public static enum FragmentTypes {
-        XPOINTER, AREA
+        XPOINTER, AREA, WKT
     };
     
     /**
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java	Mon Jan 19 17:15:51 2015 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java	Fri Jan 23 17:24:53 2015 +0100
@@ -271,6 +271,8 @@
                     jo.put("ranges", transformToRanges(fragments));
                 } else if (xt == FragmentTypes.AREA) {
                     jo.put("shapes", transformToShapes(fragments));
+                } else if (xt == FragmentTypes.WKT) {
+                    jo.put("shapes", transformToShapes(fragments));
                 }
             }
 
@@ -375,19 +377,21 @@
         return ja;
     }
 
-    private JSONArray transformToShapes(List<String> xpointers) {
+    private JSONArray transformToShapes(List<String> fragments) {
         JSONArray ja = new JSONArray();
-        Pattern rg = Pattern.compile("xywh=(\\w*):([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)");
+        Pattern xywhPattern = Pattern.compile("xywh=(\\w*):([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)");
+        Pattern wktPattern = Pattern.compile("wkt=(\\w+)\\(+([\\d\\.\\,\\ ]+)\\)+");
         try {
-            for (String xpointer : xpointers) {
-                String decoded = xpointer;
-                Matcher m = rg.matcher(decoded);
-                if (m.find()) {
-                    String units = m.group(1);
-                    float x = getFloat(m.group(2));
-                    float y = getFloat(m.group(3));
-                    float width = getFloat(m.group(4));
-                    float height = getFloat(m.group(5));
+            for (String fragment : fragments) {
+                Matcher xywhMatch = xywhPattern.matcher(fragment);
+                Matcher wktMatch = wktPattern.matcher(fragment);
+                if (xywhMatch.find()) {
+                	// xywh rectangle fragment
+                    String units = xywhMatch.group(1);
+                    float x = getFloat(xywhMatch.group(2));
+                    float y = getFloat(xywhMatch.group(3));
+                    float width = getFloat(xywhMatch.group(4));
+                    float height = getFloat(xywhMatch.group(5));
                     JSONObject shape = new JSONObject();
                     JSONObject geom = new JSONObject();
                     geom.put("units", units);
@@ -403,6 +407,24 @@
                         shape.put("geometry", geom);
                     }
                     ja.put(shape);
+                } else if (wktMatch.find()) {
+                	// wkt shape fragment
+                	String type = wktMatch.group(1);
+                	String coordString = wktMatch.group(2);
+                    JSONObject shape = new JSONObject();
+                    JSONObject geom = new JSONObject();
+                    shape.put("type", type.toLowerCase());
+                    // TODO: add units/crs to fragment?
+                    geom.put("units", "fraction");
+                    JSONArray coords = new JSONArray();
+                    String[] coordPairs = coordString.split(", *");
+                    for (String coordPairString : coordPairs) {
+                    	String[] coordPair = coordPairString.split(" +");
+                    	coords.put(new JSONArray(coordPair));
+                    }
+                    geom.put("coordinates", coords);
+                	shape.put("geometry", geom);
+                	ja.put(shape);
                 }
             }
         } catch (JSONException e) {
@@ -416,15 +438,33 @@
         String type = shape.getString("type");
         JSONObject geom = shape.getJSONObject("geometry");
         if (type.equalsIgnoreCase("point")) {
+        	// point shape
             String x = geom.getString("x");
             String y = geom.getString("y");
             fragment = String.format("xywh=fraction:%s,%s,0,0", x, y);
         } else if (type.equalsIgnoreCase("rectangle")) {
+        	// rectangle shape
             String x = geom.getString("x");
             String y = geom.getString("y");
             String width = geom.getString("width");
             String height = geom.getString("height");
             fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height);
+        } else if (type.equalsIgnoreCase("polygon")) {
+        	// polygon shape
+        	JSONArray coordArray = geom.getJSONArray("coordinates");
+            StringBuilder coords = new StringBuilder();
+            int numCoords = coordArray.length();
+        	for (int i = 0; i < numCoords; ++i) {
+        		JSONArray coordPair = coordArray.getJSONArray(i);
+        		coords.append(coordPair.getString(0));
+        		coords.append(" ");
+        		coords.append(coordPair.getString(1));
+        		if (i < numCoords-1) {
+        			coords.append(", ");
+        		}
+        	}
+        	// TODO: add units/crs to wkt
+            fragment = String.format("wkt=POLYGON((%s))", coords);
         } else {
             logger.severe("Unable to parse this shape: " + shape);
         }
@@ -613,9 +653,13 @@
                 JSONArray shapes = jo.getJSONArray("shapes");
                 if (shapes.length() > 0) {
                     JSONObject shape = shapes.getJSONObject(0);
-                    annot.setFragmentType(FragmentTypes.AREA);
                     String fragment = parseShape(shape);
                     annot.setTargetFragment(fragment);
+                    if (fragment.startsWith("wkt=")) {
+                    	annot.setFragmentType(FragmentTypes.WKT);
+                    } else {
+                    	annot.setFragmentType(FragmentTypes.AREA);
+                    }
                 }
             }
         } catch (JSONException e) {