Changeset 84:6bf38b5e30a8 in AnnotationManagerN4J


Ignore:
Timestamp:
Jan 23, 2015, 4:24:53 PM (9 years ago)
Author:
casties
Branch:
default
Message:

also stores polygon shape image annotations.

Location:
src/main/java/de/mpiwg/itgroup/annotations
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/de/mpiwg/itgroup/annotations/Annotation.java

    r76 r84  
    6868     */
    6969    public static enum FragmentTypes {
    70         XPOINTER, AREA
     70        XPOINTER, AREA, WKT
    7171    };
    7272   
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java

    r76 r84  
    272272                } else if (xt == FragmentTypes.AREA) {
    273273                    jo.put("shapes", transformToShapes(fragments));
     274                } else if (xt == FragmentTypes.WKT) {
     275                    jo.put("shapes", transformToShapes(fragments));
    274276                }
    275277            }
     
    376378    }
    377379
    378     private JSONArray transformToShapes(List<String> xpointers) {
     380    private JSONArray transformToShapes(List<String> fragments) {
    379381        JSONArray ja = new JSONArray();
    380         Pattern rg = Pattern.compile("xywh=(\\w*):([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)");
    381         try {
    382             for (String xpointer : xpointers) {
    383                 String decoded = xpointer;
    384                 Matcher m = rg.matcher(decoded);
    385                 if (m.find()) {
    386                     String units = m.group(1);
    387                     float x = getFloat(m.group(2));
    388                     float y = getFloat(m.group(3));
    389                     float width = getFloat(m.group(4));
    390                     float height = getFloat(m.group(5));
     382        Pattern xywhPattern = Pattern.compile("xywh=(\\w*):([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)");
     383        Pattern wktPattern = Pattern.compile("wkt=(\\w+)\\(+([\\d\\.\\,\\ ]+)\\)+");
     384        try {
     385            for (String fragment : fragments) {
     386                Matcher xywhMatch = xywhPattern.matcher(fragment);
     387                Matcher wktMatch = wktPattern.matcher(fragment);
     388                if (xywhMatch.find()) {
     389                        // xywh rectangle fragment
     390                    String units = xywhMatch.group(1);
     391                    float x = getFloat(xywhMatch.group(2));
     392                    float y = getFloat(xywhMatch.group(3));
     393                    float width = getFloat(xywhMatch.group(4));
     394                    float height = getFloat(xywhMatch.group(5));
    391395                    JSONObject shape = new JSONObject();
    392396                    JSONObject geom = new JSONObject();
     
    404408                    }
    405409                    ja.put(shape);
     410                } else if (wktMatch.find()) {
     411                        // wkt shape fragment
     412                        String type = wktMatch.group(1);
     413                        String coordString = wktMatch.group(2);
     414                    JSONObject shape = new JSONObject();
     415                    JSONObject geom = new JSONObject();
     416                    shape.put("type", type.toLowerCase());
     417                    // TODO: add units/crs to fragment?
     418                    geom.put("units", "fraction");
     419                    JSONArray coords = new JSONArray();
     420                    String[] coordPairs = coordString.split(", *");
     421                    for (String coordPairString : coordPairs) {
     422                        String[] coordPair = coordPairString.split(" +");
     423                        coords.put(new JSONArray(coordPair));
     424                    }
     425                    geom.put("coordinates", coords);
     426                        shape.put("geometry", geom);
     427                        ja.put(shape);
    406428                }
    407429            }
     
    417439        JSONObject geom = shape.getJSONObject("geometry");
    418440        if (type.equalsIgnoreCase("point")) {
     441                // point shape
    419442            String x = geom.getString("x");
    420443            String y = geom.getString("y");
    421444            fragment = String.format("xywh=fraction:%s,%s,0,0", x, y);
    422445        } else if (type.equalsIgnoreCase("rectangle")) {
     446                // rectangle shape
    423447            String x = geom.getString("x");
    424448            String y = geom.getString("y");
     
    426450            String height = geom.getString("height");
    427451            fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height);
     452        } else if (type.equalsIgnoreCase("polygon")) {
     453                // polygon shape
     454                JSONArray coordArray = geom.getJSONArray("coordinates");
     455            StringBuilder coords = new StringBuilder();
     456            int numCoords = coordArray.length();
     457                for (int i = 0; i < numCoords; ++i) {
     458                        JSONArray coordPair = coordArray.getJSONArray(i);
     459                        coords.append(coordPair.getString(0));
     460                        coords.append(" ");
     461                        coords.append(coordPair.getString(1));
     462                        if (i < numCoords-1) {
     463                                coords.append(", ");
     464                        }
     465                }
     466                // TODO: add units/crs to wkt
     467            fragment = String.format("wkt=POLYGON((%s))", coords);
    428468        } else {
    429469            logger.severe("Unable to parse this shape: " + shape);
     
    614654                if (shapes.length() > 0) {
    615655                    JSONObject shape = shapes.getJSONObject(0);
    616                     annot.setFragmentType(FragmentTypes.AREA);
    617656                    String fragment = parseShape(shape);
    618657                    annot.setTargetFragment(fragment);
     658                    if (fragment.startsWith("wkt=")) {
     659                        annot.setFragmentType(FragmentTypes.WKT);
     660                    } else {
     661                        annot.setFragmentType(FragmentTypes.AREA);
     662                    }
    619663                }
    620664            }
Note: See TracChangeset for help on using the changeset viewer.