Changeset 84:6bf38b5e30a8 in AnnotationManagerN4J for src/main/java/de/mpiwg/itgroup/annotations
- Timestamp:
- Jan 23, 2015, 4:24:53 PM (10 years ago)
- Branch:
- default
- 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 68 68 */ 69 69 public static enum FragmentTypes { 70 XPOINTER, AREA 70 XPOINTER, AREA, WKT 71 71 }; 72 72 -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java
r76 r84 272 272 } else if (xt == FragmentTypes.AREA) { 273 273 jo.put("shapes", transformToShapes(fragments)); 274 } else if (xt == FragmentTypes.WKT) { 275 jo.put("shapes", transformToShapes(fragments)); 274 276 } 275 277 } … … 376 378 } 377 379 378 private JSONArray transformToShapes(List<String> xpointers) {380 private JSONArray transformToShapes(List<String> fragments) { 379 381 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)); 391 395 JSONObject shape = new JSONObject(); 392 396 JSONObject geom = new JSONObject(); … … 404 408 } 405 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 } … … 417 439 JSONObject geom = shape.getJSONObject("geometry"); 418 440 if (type.equalsIgnoreCase("point")) { 441 // point shape 419 442 String x = geom.getString("x"); 420 443 String y = geom.getString("y"); 421 444 fragment = String.format("xywh=fraction:%s,%s,0,0", x, y); 422 445 } else if (type.equalsIgnoreCase("rectangle")) { 446 // rectangle shape 423 447 String x = geom.getString("x"); 424 448 String y = geom.getString("y"); … … 426 450 String height = geom.getString("height"); 427 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 468 } else { 429 469 logger.severe("Unable to parse this shape: " + shape); … … 614 654 if (shapes.length() > 0) { 615 655 JSONObject shape = shapes.getJSONObject(0); 616 annot.setFragmentType(FragmentTypes.AREA);617 656 String fragment = parseShape(shape); 618 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 }
Note: See TracChangeset
for help on using the changeset viewer.