use of eu.transkribus.core.model.beans.pagecontent.RegionType in project TranskribusCore by Transkribus.
the class PageXmlUtils method copyTextContent.
public static void copyTextContent(PcGtsType origPc, PcGtsType newPc) {
if (!hasRegions(origPc) || !hasRegions(newPc)) {
return;
}
List<TrpRegionType> origRegs = origPc.getPage().getTextRegionOrImageRegionOrLineDrawingRegion();
List<TrpRegionType> newRegs = newPc.getPage().getTextRegionOrImageRegionOrLineDrawingRegion();
// map the regions where we want to keep the textContent
Map<String, TextRegionType> textMap = new HashMap<>();
// iterate all old regions. Map the ones containing lines
for (RegionType r : origRegs) {
if (!(r instanceof TextRegionType)) {
continue;
}
TextRegionType tr = (TextRegionType) r;
boolean hasTextLines = tr.getTextLine() != null && !tr.getTextLine().isEmpty();
if (hasTextLines) {
textMap.put(tr.getId(), tr);
}
}
// iterate the new regions and move all the line contents from the old one
for (RegionType r : newRegs) {
if (!(r instanceof TextRegionType) || !textMap.containsKey(r.getId())) {
continue;
}
// this region corresponds with an old one
TextRegionType newTr = (TextRegionType) r;
TextRegionType oldTr = textMap.get(newTr.getId());
copyTextRegionContent(oldTr, newTr);
}
}
use of eu.transkribus.core.model.beans.pagecontent.RegionType in project TranskribusCore by Transkribus.
the class TrpElementCoordinatesComparator method compare.
@Override
public int compare(T o1, T o2) {
// if (!isRegionLineOrWord(o1) || !isRegionLineOrWord(o2))
// return 0;
logger.trace("compare in TrpElementCoordinatesComparator");
// try {
String coords1 = "", coords2 = "";
if (o1 instanceof RegionType) {
RegionType r1 = (RegionType) o1;
RegionType r2 = (RegionType) o2;
if (r1.getCoords() != null && r2.getCoords() != null) {
coords1 = r1.getCoords().getPoints();
coords2 = r2.getCoords().getPoints();
}
} else if (TextLineType.class.isAssignableFrom(o1.getClass())) {
// if existing, take baseline to compare position of lines
if (((TextLineType) o1).getBaseline() != null && ((TextLineType) o2).getBaseline() != null) {
coords1 = ((TextLineType) o1).getBaseline().getPoints();
coords2 = ((TextLineType) o2).getBaseline().getPoints();
} else {
// fall back if there are no baselines
coords1 = ((TextLineType) o1).getCoords().getPoints();
coords2 = ((TextLineType) o2).getCoords().getPoints();
}
} else if (o1 instanceof TrpBaselineType) {
coords1 = ((TrpBaselineType) o1).getPoints();
coords2 = ((TrpBaselineType) o2).getPoints();
} else if (WordType.class.isAssignableFrom(o1.getClass())) {
WordType w1 = (WordType) o1;
WordType w2 = (WordType) o2;
if (w1.getCoords() != null && w2.getCoords() != null) {
coords1 = w1.getCoords().getPoints();
coords2 = w2.getCoords().getPoints();
}
}
// if (coords1.isEmpty() || coords2.isEmpty()) {
// throw new Exception("No coordinates in one of the objects - should not happen!");
// }
// determine orientation of (parent) text regions
Float orientation = null;
if (o1 instanceof ITrpShapeType && o2 instanceof ITrpShapeType && !(o1 instanceof RegionType) && !(o2 instanceof RegionType)) {
TrpTextRegionType tr1 = TrpShapeTypeUtils.getTextRegion((ITrpShapeType) o1);
TrpTextRegionType tr2 = TrpShapeTypeUtils.getTextRegion((ITrpShapeType) o2);
if (tr1 != null && tr2 != null && StringUtils.equals(tr1.getId(), tr2.getId()) && tr1.getOrientation() != null) {
orientation = tr1.getOrientation();
}
}
// --------------------------
java.awt.Polygon p1 = new java.awt.Polygon();
try {
for (java.awt.Point p : PointStrUtils.parsePoints(coords1)) p1.addPoint(p.x, p.y);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
java.awt.Polygon p2 = new java.awt.Polygon();
try {
for (java.awt.Point p : PointStrUtils.parsePoints(coords2)) p2.addPoint(p.x, p.y);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
Rectangle b1 = p1.getBounds();
Rectangle b2 = p2.getBounds();
Point pt1 = new Point(b1.x, b1.y);
Point pt2 = new Point(b2.x, b2.y);
if (orientation != null) {
pt1 = GeomUtils.rotate(pt1, orientation);
pt2 = GeomUtils.rotate(pt2, orientation);
logger.trace("orientation set: " + orientation + " rotated points: " + pt1 + ", " + pt2);
}
if (compareByYX == null) {
// if compareByYX was not set by constructor, determine via shape
compareByYX = !WordType.class.isAssignableFrom(o1.getClass());
}
if (!compareByYX) {
// return compareByXY(b1.x, b2.x, b1.y, b2.y);
return compareByXY(pt1.x, pt2.x, pt1.y, pt2.y);
} else {
return compareByYX(pt1.x, pt2.x, pt1.y, pt2.y);
// return compareByYX(b1.x, b2.x, b1.y, b2.y);
// return compareBy_YOverlap_X(b1, b2);
}
// }
// catch (Exception e) {
// e.printStackTrace();
// return 0;
// }
}
use of eu.transkribus.core.model.beans.pagecontent.RegionType in project TranskribusCore by Transkribus.
the class TrpPageTypeUtils method assignUniqueIDs.
/**
* Assigns unique IDs to the elements in the page using the current order of the elements.
*/
public static void assignUniqueIDs(PageType page) {
int i = 1;
for (RegionType r : page.getTextRegionOrImageRegionOrLineDrawingRegion()) {
if (r instanceof TextRegionType) {
TextRegionType region = (TextRegionType) r;
String rid = "r" + i;
region.setId(rid);
int j = 1;
for (TextLineType l : region.getTextLine()) {
String lid = rid + "l" + j;
l.setId(lid);
int k = 1;
for (WordType word : l.getWord()) {
String wid = lid + "w" + k;
word.setId(wid);
k++;
}
++j;
}
++i;
}
}
}
Aggregations