Search in sources :

Example 6 with ExternalID

use of de.lmu.ifi.dbs.elki.data.ExternalID in project elki by elki-project.

the class SimplePolygonParser method parseLine.

/**
 * Parse a single line.
 *
 * @return {@code true} if the line was read successful.
 */
private boolean parseLine() {
    cureid = null;
    curpoly = null;
    curlbl = null;
    polys.clear();
    coords.clear();
    labels.clear();
    Matcher m = COORD.matcher(reader.getBuffer());
    for (; /* initialized by nextLineExceptComments */
    tokenizer.valid(); tokenizer.advance()) {
        m.region(tokenizer.getStart(), tokenizer.getEnd());
        if (m.find()) {
            try {
                double c1 = ParseUtil.parseDouble(m.group(1));
                double c2 = ParseUtil.parseDouble(m.group(2));
                if (m.group(3) != null) {
                    double c3 = ParseUtil.parseDouble(m.group(3));
                    coords.add(new double[] { c1, c2, c3 });
                } else {
                    coords.add(new double[] { c1, c2 });
                }
                continue;
            } catch (NumberFormatException e) {
                LOG.warning("Looked like a coordinate pair but didn't parse: " + tokenizer.getSubstring());
            }
        }
        // Match polygon separator:
        // FIXME: Avoid unnecessary subSequence call.
        final int len = tokenizer.getEnd() - tokenizer.getStart();
        if (// 
        POLYGON_SEPARATOR.length() == len && reader.getBuffer().subSequence(tokenizer.getStart(), tokenizer.getEnd()).equals(POLYGON_SEPARATOR)) {
            if (!coords.isEmpty()) {
                polys.add(new Polygon(new ArrayList<>(coords)));
            }
            continue;
        }
        String cur = tokenizer.getSubstring();
        // First label will become the External ID
        if (cureid == null) {
            cureid = new ExternalID(cur);
        } else {
            labels.add(cur);
        }
    }
    // Complete polygon
    if (!coords.isEmpty()) {
        polys.add(new Polygon(coords));
    }
    curpoly = new PolygonsObject(polys);
    curlbl = (haslabels || !labels.isEmpty()) ? LabelList.make(labels) : null;
    return true;
}
Also used : Matcher(java.util.regex.Matcher) ExternalID(de.lmu.ifi.dbs.elki.data.ExternalID) ArrayList(java.util.ArrayList) Polygon(de.lmu.ifi.dbs.elki.data.spatial.Polygon) PolygonsObject(de.lmu.ifi.dbs.elki.data.spatial.PolygonsObject)

Aggregations

ExternalID (de.lmu.ifi.dbs.elki.data.ExternalID)6 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)4 ArrayList (java.util.ArrayList)4 ClassLabel (de.lmu.ifi.dbs.elki.data.ClassLabel)2 SimpleClassLabel (de.lmu.ifi.dbs.elki.data.SimpleClassLabel)2 SparseDoubleVector (de.lmu.ifi.dbs.elki.data.SparseDoubleVector)2 MultipleObjectsBundle (de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle)2 DoubleVector (de.lmu.ifi.dbs.elki.data.DoubleVector)1 LabelList (de.lmu.ifi.dbs.elki.data.LabelList)1 Polygon (de.lmu.ifi.dbs.elki.data.spatial.Polygon)1 PolygonsObject (de.lmu.ifi.dbs.elki.data.spatial.PolygonsObject)1 SimpleTypeInformation (de.lmu.ifi.dbs.elki.data.type.SimpleTypeInformation)1 VectorFieldTypeInformation (de.lmu.ifi.dbs.elki.data.type.VectorFieldTypeInformation)1 Int2DoubleOpenHashMap (it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap)1 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)1 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)1 Object2IntOpenHashMap (it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap)1 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)1 Matcher (java.util.regex.Matcher)1