Search in sources :

Example 6 with AbstractFeature

use of org.apache.sis.feature.AbstractFeature in project sis by apache.

the class Reader method parseWayPoint.

/**
 * Parses a {@code <wpt>}, {@code <rtept>} or {@code <trkpt>} element.
 * The STAX reader {@linkplain XMLStreamReader#getEventType() current event} must be a {@link #START_ELEMENT}.
 * After this method invocation, the reader will be on {@link #END_ELEMENT}.
 *
 * @throws Exception see the list of exceptions documented in {@link #parse(Consumer, boolean)}.
 */
private AbstractFeature parseWayPoint(final int index) throws Exception {
    assert reader.isStartElement();
    /*
         * Way points might be located in different elements: <wpt>, <rtept> and <trkpt>.
         * We have to keep the current tag name in order to know when we reach the end.
         * We are lenient about namespace since we do not allow nested way points.
         */
    final String tagName = reader.getLocalName();
    final String lat = reader.getAttributeValue(null, Attributes.LATITUDE);
    final String lon = reader.getAttributeValue(null, Attributes.LONGITUDE);
    if (lat == null || lon == null) {
        throw new DataStoreContentException(errors().getString(Errors.Keys.MandatoryAttribute_2, (lat == null) ? Attributes.LATITUDE : Attributes.LONGITUDE, tagName));
    }
    final Types types = ((Store) owner).types;
    final AbstractFeature feature = types.wayPoint.newInstance();
    feature.setPropertyValue("sis:identifier", index);
    feature.setPropertyValue("sis:geometry", types.geometries.createPoint(parseDouble(lon), parseDouble(lat)));
    List<Link> links = null;
    while (true) {
        /*
             * We do not need to check 'reader.hasNext()' in above loop
             * since this check is done by the END_DOCUMENT case below.
             */
        switch(next()) {
            case START_ELEMENT:
                {
                    final Object value;
                    final String name = reader.getLocalName();
                    switch(isGPX() ? name : "") {
                        // Fallthrough to getElementText()
                        case Tags.NAME:
                        // ︙
                        case Tags.COMMENT:
                        // ︙
                        case Tags.DESCRIPTION:
                        // ︙
                        case Tags.SOURCE:
                        // ︙
                        case Tags.SYMBOL:
                        case Tags.TYPE:
                            value = getElementText();
                            break;
                        case Tags.TIME:
                            value = getElementAsTemporal();
                            break;
                        // Fallthrough to getElementAsDouble()
                        case Tags.MAGNETIC_VAR:
                        // ︙
                        case Tags.GEOID_HEIGHT:
                        // ︙
                        case Tags.AGE_OF_GPS_DATA:
                        // ︙
                        case Tags.HDOP:
                        // ︙
                        case Tags.PDOP:
                        // ︙
                        case Tags.VDOP:
                        case Tags.ELEVATION:
                            value = getElementAsDouble();
                            break;
                        // Fallthrough to getElementAsInteger()
                        case Tags.SATELITTES:
                        case Tags.DGPS_ID:
                            value = getElementAsInteger();
                            break;
                        case Tags.FIX:
                            value = Fix.fromGPX(getElementText());
                            break;
                        case Tags.LINK:
                            links = Metadata.addIfNonNull(links, unmarshal(Link.class));
                            continue;
                        case Tags.URL:
                            links = Metadata.addIfNonNull(links, Link.valueOf(getElementAsURI()));
                            continue;
                        default:
                            {
                                if (name.equals(tagName)) {
                                    throw new DataStoreContentException(nestedElement(name));
                                }
                                continue;
                            }
                    }
                    feature.setPropertyValue(name, value);
                    break;
                }
            case END_ELEMENT:
                {
                    if (tagName.equals(reader.getLocalName()) && isGPX()) {
                        if (links != null)
                            feature.setPropertyValue(Tags.LINK, links);
                        return feature;
                    }
                    break;
                }
            case END_DOCUMENT:
                {
                    throw new EOFException(endOfFile());
                }
        }
    }
}
Also used : DataStoreContentException(org.apache.sis.storage.DataStoreContentException) EOFException(java.io.EOFException) AbstractFeature(org.apache.sis.feature.AbstractFeature)

Example 7 with AbstractFeature

use of org.apache.sis.feature.AbstractFeature in project sis by apache.

the class Reader method parseTrack.

/**
 * Parses a {@code <trk>} element. The STAX reader {@linkplain XMLStreamReader#getEventType() current event}
 * must be a {@link #START_ELEMENT} and the name of that start element must be {@link Tags#TRACKS}.
 *
 * @throws Exception see the list of exceptions documented in {@link #parse(Consumer, boolean)}.
 */
private AbstractFeature parseTrack(final int index) throws Exception {
    assert reader.isStartElement() && Tags.TRACKS.equals(reader.getLocalName());
    final AbstractFeature feature = ((Store) owner).types.track.newInstance();
    feature.setPropertyValue("sis:identifier", index);
    List<AbstractFeature> segments = null;
    List<Link> links = null;
    while (true) {
        /*
             * We do not need to check 'reader.hasNext()' in above loop
             * since this check is done by the END_DOCUMENT case below.
             */
        switch(next()) {
            case START_ELEMENT:
                {
                    final Object value;
                    final String name = reader.getLocalName();
                    switch(isGPX() ? name : "") {
                        default:
                            continue;
                        // Fallthrough to getElementText()
                        case Tags.NAME:
                        // ︙
                        case Tags.COMMENT:
                        // ︙
                        case Tags.DESCRIPTION:
                        // ︙
                        case Tags.SOURCE:
                        case Tags.TYPE:
                            value = getElementText();
                            break;
                        case Tags.NUMBER:
                            value = getElementAsInteger();
                            break;
                        case Tags.LINK:
                            links = Metadata.addIfNonNull(links, unmarshal(Link.class));
                            continue;
                        case Tags.URL:
                            links = Metadata.addIfNonNull(links, Link.valueOf(getElementAsURI()));
                            continue;
                        case Tags.TRACKS:
                            throw new DataStoreContentException(nestedElement(name));
                        case Tags.TRACK_SEGMENTS:
                            {
                                if (segments == null)
                                    segments = new ArrayList<>(8);
                                segments.add(parseTrackSegment(segments.size() + 1));
                                continue;
                            }
                    }
                    feature.setPropertyValue(name, value);
                    break;
                }
            case END_ELEMENT:
                {
                    if (Tags.TRACKS.equals(reader.getLocalName()) && isGPX()) {
                        if (segments != null)
                            feature.setPropertyValue(Tags.TRACK_SEGMENTS, segments);
                        if (links != null)
                            feature.setPropertyValue(Tags.LINK, links);
                        return feature;
                    }
                    break;
                }
            case END_DOCUMENT:
                {
                    throw new EOFException(endOfFile());
                }
        }
    }
}
Also used : DataStoreContentException(org.apache.sis.storage.DataStoreContentException) ArrayList(java.util.ArrayList) EOFException(java.io.EOFException) AbstractFeature(org.apache.sis.feature.AbstractFeature)

Example 8 with AbstractFeature

use of org.apache.sis.feature.AbstractFeature in project sis by apache.

the class ShapeFileTest method testHandleEofNotification.

/**
 * Checks that the reader is able to detect EoF signs in the DBase file.
 * @throws URISyntaxException if the resource name is incorrect.
 * @throws DataStoreException if a general file reading trouble occurs.
 */
// TODO Adapt with another shapefile.
@Test
// TODO Adapt with another shapefile.
@Ignore
public void testHandleEofNotification() throws URISyntaxException, DataStoreException {
    ShapeFile shp = new ShapeFile(path("DEPARTEMENT.SHP"));
    AbstractFeature first = null, last = null;
    Logger log = org.apache.sis.util.logging.Logging.getLogger(ShapeFileTest.class.getName());
    try (InputFeatureStream is = shp.findAll()) {
        AbstractFeature feature = is.readFeature();
        // Read and retain the first and the last feature.
        while (feature != null) {
            if (first == null) {
                first = feature;
            }
            // Advice : To debug just before the last record, put a conditional breakpoint on department name "MEURTHE-ET-MOSELLE".
            String deptName = (String) ((AbstractAttribute) feature.getProperty("NOM_DEPT")).getValue();
            log.info(deptName);
            last = feature;
            feature = is.readFeature();
        }
    }
    assertNotNull("No record has been found in the DBase file or Shapefile.", first);
    assertNotNull("This test is not working well : last feature should always be set if any feature has been found.", last);
    assertEquals("The first record red must be JURA department.", "JURA", ((AbstractAttribute) first.getProperty("NOM_DEPT")).getValue());
    assertEquals("The last record red must be DEUX-SEVRES department.", "DEUX-SEVRES", ((AbstractAttribute) last.getProperty("NOM_DEPT")).getValue());
}
Also used : AbstractFeature(org.apache.sis.feature.AbstractFeature) Logger(java.util.logging.Logger) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with AbstractFeature

use of org.apache.sis.feature.AbstractFeature in project sis by apache.

the class ShapeFileTest method testDirectAcces.

/**
 * Testing direct access in the shapefile.
 * @throws URISyntaxException if the resource name is incorrect.
 * @throws DataStoreException if a general file reading trouble occurs.
 */
@Test
public void testDirectAcces() throws DataStoreException, URISyntaxException {
    ShapeFile shp = new ShapeFile(path("ABRALicenseePt_4326_clipped.shp"));
    // 1) Find the third record, sequentially.
    AbstractFeature thirdFeature;
    try (InputFeatureStream isSequential = shp.findAll()) {
        isSequential.readFeature();
        isSequential.readFeature();
        thirdFeature = isSequential.readFeature();
    }
    // Take one of its key fields and another field for reference, and its geometry.
    Double sequentialAddressId = Double.valueOf((String) (((AbstractAttribute) thirdFeature.getProperty("ADDRID"))).getValue());
    String sequentialAddress = (String) (((AbstractAttribute) thirdFeature.getProperty("ADDRESS"))).getValue();
    Object sequentialGeometry = thirdFeature.getPropertyValue("geometry");
    // 2) Now attempt a direct access to this feature.
    AbstractFeature directFeature;
    String sql = MessageFormat.format("SELECT * FROM ABRALicenseePt_4326_clipped WHERE ADDRID = {0,number,#0}", sequentialAddressId);
    try (InputFeatureStream isDirect = shp.find(sql)) {
        directFeature = isDirect.readFeature();
        assertNotNull("The direct access feature returned should not be null", directFeature);
    }
    assertNotNull("The field ADDRID in the direct access feature has not been found again.", directFeature.getProperty("ADDRID"));
    Double directAddressId = Double.valueOf((String) (((AbstractAttribute) directFeature.getProperty("ADDRID"))).getValue());
    String directAddress = (String) (((AbstractAttribute) directFeature.getProperty("ADDRESS"))).getValue();
    Object directGeometry = directFeature.getPropertyValue("geometry");
    assertEquals("DBase part : direct access didn't returned the same address id than sequential access.", sequentialAddressId, directAddressId);
    assertEquals("DBase part : direct access didn't returned the same address than sequential access.", sequentialAddress, directAddress);
    assertEquals("Shapefile part : direct access didn't returned the same geometry than sequential access.", sequentialGeometry, directGeometry);
}
Also used : AbstractFeature(org.apache.sis.feature.AbstractFeature) AbstractAttribute(org.apache.sis.feature.AbstractAttribute) Test(org.junit.Test)

Example 10 with AbstractFeature

use of org.apache.sis.feature.AbstractFeature in project sis by apache.

the class Reader method parseRoute.

/**
 * Parses a {@code <rte>} element. The STAX reader {@linkplain XMLStreamReader#getEventType() current event}
 * must be a {@link #START_ELEMENT} and the name of that start element must be {@link Tags#ROUTES}.
 *
 * @throws Exception see the list of exceptions documented in {@link #parse(Consumer, boolean)}.
 */
private AbstractFeature parseRoute(final int index) throws Exception {
    assert reader.isStartElement() && Tags.ROUTES.equals(reader.getLocalName());
    final AbstractFeature feature = ((Store) owner).types.route.newInstance();
    feature.setPropertyValue("sis:identifier", index);
    List<AbstractFeature> wayPoints = null;
    List<Link> links = null;
    while (true) {
        /*
             * We do not need to check 'reader.hasNext()' in above loop
             * since this check is done by the END_DOCUMENT case below.
             */
        switch(next()) {
            case START_ELEMENT:
                {
                    final Object value;
                    final String name = reader.getLocalName();
                    switch(isGPX() ? name : "") {
                        default:
                            continue;
                        // Fallthrough to getElementText()
                        case Tags.NAME:
                        // ︙
                        case Tags.COMMENT:
                        // ︙
                        case Tags.DESCRIPTION:
                        // ︙
                        case Tags.SOURCE:
                        case Tags.TYPE:
                            value = getElementText();
                            break;
                        case Tags.NUMBER:
                            value = getElementAsInteger();
                            break;
                        case Tags.LINK:
                            links = Metadata.addIfNonNull(links, unmarshal(Link.class));
                            continue;
                        case Tags.URL:
                            links = Metadata.addIfNonNull(links, Link.valueOf(getElementAsURI()));
                            continue;
                        case Tags.ROUTES:
                            throw new DataStoreContentException(nestedElement(name));
                        case Tags.ROUTE_POINTS:
                            {
                                if (wayPoints == null)
                                    wayPoints = new ArrayList<>(8);
                                wayPoints.add(parseWayPoint(wayPoints.size() + 1));
                                continue;
                            }
                    }
                    feature.setPropertyValue(name, value);
                    break;
                }
            case END_ELEMENT:
                {
                    if (Tags.ROUTES.equals(reader.getLocalName()) && isGPX()) {
                        if (wayPoints != null)
                            feature.setPropertyValue(Tags.ROUTE_POINTS, wayPoints);
                        if (links != null)
                            feature.setPropertyValue(Tags.LINK, links);
                        return feature;
                    }
                    break;
                }
            case END_DOCUMENT:
                {
                    throw new EOFException(endOfFile());
                }
        }
    }
}
Also used : DataStoreContentException(org.apache.sis.storage.DataStoreContentException) ArrayList(java.util.ArrayList) EOFException(java.io.EOFException) AbstractFeature(org.apache.sis.feature.AbstractFeature)

Aggregations

AbstractFeature (org.apache.sis.feature.AbstractFeature)12 EOFException (java.io.EOFException)4 DataStoreContentException (org.apache.sis.storage.DataStoreContentException)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 Point (com.esri.core.geometry.Point)2 Polyline (com.esri.core.geometry.Polyline)1 URI (java.net.URI)1 DecimalFormat (java.text.DecimalFormat)1 MessageFormat (java.text.MessageFormat)1 List (java.util.List)1 Logger (java.util.logging.Logger)1 AbstractAttribute (org.apache.sis.feature.AbstractAttribute)1 DBFDatabaseMetaData (org.apache.sis.internal.shapefile.jdbc.metadata.DBFDatabaseMetaData)1 StorageConnector (org.apache.sis.storage.StorageConnector)1 Ignore (org.junit.Ignore)1