comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java @ 84:6bf38b5e30a8

also stores polygon shape image annotations.
author casties
date Fri, 23 Jan 2015 17:24:53 +0100
parents 4e2dc67997a0
children ed51eadc82c5
comparison
equal deleted inserted replaced
83:19ebbcff9638 84:6bf38b5e30a8
269 FragmentTypes xt = annot.getFragmentType(); 269 FragmentTypes xt = annot.getFragmentType();
270 if (xt == FragmentTypes.XPOINTER) { 270 if (xt == FragmentTypes.XPOINTER) {
271 jo.put("ranges", transformToRanges(fragments)); 271 jo.put("ranges", transformToRanges(fragments));
272 } else if (xt == FragmentTypes.AREA) { 272 } else if (xt == FragmentTypes.AREA) {
273 jo.put("shapes", transformToShapes(fragments)); 273 jo.put("shapes", transformToShapes(fragments));
274 } else if (xt == FragmentTypes.WKT) {
275 jo.put("shapes", transformToShapes(fragments));
274 } 276 }
275 } 277 }
276 278
277 /* 279 /*
278 * permissions 280 * permissions
373 logger.severe("Unable to transform to ranges! "+e); 375 logger.severe("Unable to transform to ranges! "+e);
374 } 376 }
375 return ja; 377 return ja;
376 } 378 }
377 379
378 private JSONArray transformToShapes(List<String> xpointers) { 380 private JSONArray transformToShapes(List<String> fragments) {
379 JSONArray ja = new JSONArray(); 381 JSONArray ja = new JSONArray();
380 Pattern rg = Pattern.compile("xywh=(\\w*):([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)"); 382 Pattern xywhPattern = Pattern.compile("xywh=(\\w*):([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)");
381 try { 383 Pattern wktPattern = Pattern.compile("wkt=(\\w+)\\(+([\\d\\.\\,\\ ]+)\\)+");
382 for (String xpointer : xpointers) { 384 try {
383 String decoded = xpointer; 385 for (String fragment : fragments) {
384 Matcher m = rg.matcher(decoded); 386 Matcher xywhMatch = xywhPattern.matcher(fragment);
385 if (m.find()) { 387 Matcher wktMatch = wktPattern.matcher(fragment);
386 String units = m.group(1); 388 if (xywhMatch.find()) {
387 float x = getFloat(m.group(2)); 389 // xywh rectangle fragment
388 float y = getFloat(m.group(3)); 390 String units = xywhMatch.group(1);
389 float width = getFloat(m.group(4)); 391 float x = getFloat(xywhMatch.group(2));
390 float height = getFloat(m.group(5)); 392 float y = getFloat(xywhMatch.group(3));
393 float width = getFloat(xywhMatch.group(4));
394 float height = getFloat(xywhMatch.group(5));
391 JSONObject shape = new JSONObject(); 395 JSONObject shape = new JSONObject();
392 JSONObject geom = new JSONObject(); 396 JSONObject geom = new JSONObject();
393 geom.put("units", units); 397 geom.put("units", units);
394 geom.put("x", x); 398 geom.put("x", x);
395 geom.put("y", y); 399 geom.put("y", y);
401 geom.put("width", width); 405 geom.put("width", width);
402 geom.put("height", height); 406 geom.put("height", height);
403 shape.put("geometry", geom); 407 shape.put("geometry", geom);
404 } 408 }
405 ja.put(shape); 409 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);
406 } 428 }
407 } 429 }
408 } catch (JSONException e) { 430 } catch (JSONException e) {
409 logger.severe("Unable to transform to shapes! "+e); 431 logger.severe("Unable to transform to shapes! "+e);
410 } 432 }
414 protected String parseShape(JSONObject shape) throws JSONException { 436 protected String parseShape(JSONObject shape) throws JSONException {
415 String fragment = null; 437 String fragment = null;
416 String type = shape.getString("type"); 438 String type = shape.getString("type");
417 JSONObject geom = shape.getJSONObject("geometry"); 439 JSONObject geom = shape.getJSONObject("geometry");
418 if (type.equalsIgnoreCase("point")) { 440 if (type.equalsIgnoreCase("point")) {
441 // point shape
419 String x = geom.getString("x"); 442 String x = geom.getString("x");
420 String y = geom.getString("y"); 443 String y = geom.getString("y");
421 fragment = String.format("xywh=fraction:%s,%s,0,0", x, y); 444 fragment = String.format("xywh=fraction:%s,%s,0,0", x, y);
422 } else if (type.equalsIgnoreCase("rectangle")) { 445 } else if (type.equalsIgnoreCase("rectangle")) {
446 // rectangle shape
423 String x = geom.getString("x"); 447 String x = geom.getString("x");
424 String y = geom.getString("y"); 448 String y = geom.getString("y");
425 String width = geom.getString("width"); 449 String width = geom.getString("width");
426 String height = geom.getString("height"); 450 String height = geom.getString("height");
427 fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height); 451 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);
428 } else { 468 } else {
429 logger.severe("Unable to parse this shape: " + shape); 469 logger.severe("Unable to parse this shape: " + shape);
430 } 470 }
431 return fragment; 471 return fragment;
432 } 472 }
611 try { 651 try {
612 if (jo.has("shapes")) { 652 if (jo.has("shapes")) {
613 JSONArray shapes = jo.getJSONArray("shapes"); 653 JSONArray shapes = jo.getJSONArray("shapes");
614 if (shapes.length() > 0) { 654 if (shapes.length() > 0) {
615 JSONObject shape = shapes.getJSONObject(0); 655 JSONObject shape = shapes.getJSONObject(0);
616 annot.setFragmentType(FragmentTypes.AREA);
617 String fragment = parseShape(shape); 656 String fragment = parseShape(shape);
618 annot.setTargetFragment(fragment); 657 annot.setTargetFragment(fragment);
658 if (fragment.startsWith("wkt=")) {
659 annot.setFragmentType(FragmentTypes.WKT);
660 } else {
661 annot.setFragmentType(FragmentTypes.AREA);
662 }
619 } 663 }
620 } 664 }
621 } catch (JSONException e) { 665 } catch (JSONException e) {
622 // nothing to do 666 // nothing to do
623 } 667 }