Search in sources :

Example 1 with Feature

use of mil.nga.sf.geojson.Feature in project joa by sebastianfrey.

the class GeoPackageService method getItems.

/**
 * Return all items of a collection from a given service.
 *
 * @param serviceId
 * @param collectionId
 * @return
 */
@Override
public GeoPackageItems getItems(String serviceId, String collectionId, FeatureQuery query) throws Exception {
    GeoPackageItems items = new GeoPackageItems().serviceId(serviceId).collectionId(collectionId).queryString(query.getQueryString()).offset(query.getOffset()).limit(query.getLimit());
    try (GeoPackage gpkg = loadService(serviceId)) {
        FeatureDao featureDao = loadCollection(gpkg, collectionId);
        GeoPackageQueryResult result = new GeoPackageQuery(featureDao, query).execute();
        String geometryType = null;
        switch(featureDao.getGeometryType()) {
            case POINT:
                geometryType = "Point";
                break;
            case LINESTRING:
                geometryType = "LineString";
                break;
            case POLYGON:
                geometryType = "Polygon";
                break;
            case MULTIPOINT:
                geometryType = "MultiPoint";
                break;
            case MULTILINESTRING:
                geometryType = "MultiLineString";
                break;
            case MULTIPOLYGON:
                geometryType = "MultiPolygon";
                break;
            default:
                break;
        }
        items.numberMatched(result.getCount()).geometryType(geometryType).idColumn(featureDao.getIdColumnName());
        GeometryEnvelope bbox = null;
        FeatureResultSet featureResultSet = result.getFeatureResultSet();
        try {
            while (featureResultSet.moveToNext()) {
                FeatureRow featureRow = featureResultSet.getRow();
                Feature feature = createFeature(featureRow);
                if (feature != null) {
                    items.feature(feature);
                }
                GeometryEnvelope envelope = createEnvelope(featureRow);
                if (envelope != null) {
                    if (bbox == null) {
                        bbox = envelope.copy();
                    } else {
                        bbox = bbox.union(envelope);
                    }
                }
            }
        } finally {
            featureResultSet.close();
        }
        if (bbox != null) {
            if (bbox.is3D()) {
                items.bbox(List.of(bbox.getMinX(), bbox.getMinY(), bbox.getMinZ(), bbox.getMaxX(), bbox.getMaxY(), bbox.getMaxZ()));
            } else {
                items.bbox(List.of(bbox.getMinX(), bbox.getMinY(), bbox.getMaxX(), bbox.getMaxY()));
            }
        }
    }
    return items;
}
Also used : FeatureRow(mil.nga.geopackage.features.user.FeatureRow) FeatureResultSet(mil.nga.geopackage.features.user.FeatureResultSet) GeometryEnvelope(mil.nga.sf.GeometryEnvelope) FeatureDao(mil.nga.geopackage.features.user.FeatureDao) GeoPackage(mil.nga.geopackage.GeoPackage) Feature(mil.nga.sf.geojson.Feature)

Example 2 with Feature

use of mil.nga.sf.geojson.Feature in project joa by sebastianfrey.

the class GeoPackageService method getItem.

/**
 * Return a specific item by its id from a given service.
 *
 * @param serviceId
 * @param collectionId
 * @return
 */
@Override
public GeoPackageItem getItem(String serviceId, String collectionId, Long featureId) {
    try (GeoPackage gpkg = loadService(serviceId)) {
        FeatureDao featureDao = loadCollection(gpkg, collectionId);
        FeatureResultSet featureResultSet = featureDao.queryForId(featureId);
        try {
            while (featureResultSet.moveToNext()) {
                FeatureRow featureRow = featureResultSet.getRow();
                Feature feature = createFeature(featureRow);
                return new GeoPackageItem().serviceId(serviceId).collectionId(collectionId).feature(feature);
            }
        } finally {
            featureResultSet.close();
        }
    }
    throw new NotFoundException("Feature with ID '" + featureId + "' does not exist.");
}
Also used : FeatureRow(mil.nga.geopackage.features.user.FeatureRow) FeatureResultSet(mil.nga.geopackage.features.user.FeatureResultSet) NotFoundException(javax.ws.rs.NotFoundException) FeatureDao(mil.nga.geopackage.features.user.FeatureDao) GeoPackage(mil.nga.geopackage.GeoPackage) Feature(mil.nga.sf.geojson.Feature)

Example 3 with Feature

use of mil.nga.sf.geojson.Feature in project geopackage-core-java by ngageoint.

the class OAPIFeatureCoreGenerator method createFeatures.

/**
 * Create features from the feature collection
 *
 * @param featureCollection
 *            feature collection
 * @return features created
 */
protected int createFeatures(FeatureCollection featureCollection) {
    int count = 0;
    geoPackage.beginTransaction();
    try {
        for (Feature feature : featureCollection.getFeatureCollection().getFeatures()) {
            if (!isActive()) {
                break;
            }
            try {
                createFeature(feature);
                count++;
                if (progress != null) {
                    progress.addProgress(1);
                }
            } catch (Exception e) {
                LOGGER.log(Level.WARNING, "Failed to create feature: " + feature.getId(), e);
            }
            if (count > 0 && count % transactionLimit == 0) {
                geoPackage.commit();
            }
        }
    } catch (Exception e) {
        LOGGER.log(Level.WARNING, "Failed to create features", e);
        geoPackage.failTransaction();
    } finally {
        geoPackage.endTransaction();
    }
    Integer numberReturned = featureCollection.getNumberReturned();
    if (numberReturned != null && numberReturned != count) {
        LOGGER.log(Level.WARNING, "Feature Collection number returned does not match number of features created. Number Returned: " + numberReturned + ", Created: " + count);
    }
    featureCollection.setNumberReturned(count);
    return count;
}
Also used : Feature(mil.nga.sf.geojson.Feature) SQLException(java.sql.SQLException) MalformedURLException(java.net.MalformedURLException) GeoPackageException(mil.nga.geopackage.GeoPackageException) IOException(java.io.IOException)

Example 4 with Feature

use of mil.nga.sf.geojson.Feature in project joa by sebastianfrey.

the class GeoPackageService method createFeature.

public Feature createFeature(FeatureRow featureRow) {
    Feature feature = null;
    GeoPackageGeometryData geometryData = featureRow.getGeometry();
    if (geometryData != null && !geometryData.isEmpty()) {
        feature = FeatureConverter.toFeature(geometryData.getGeometry());
        feature.setProperties(new HashMap<>());
        for (Map.Entry<String, ColumnValue> entry : featureRow.getAsMap()) {
            ColumnValue value = entry.getValue();
            String key = entry.getKey();
            if (!key.equals(featureRow.getGeometryColumnName())) {
                feature.getProperties().put(key, value.getValue());
            }
        }
        feature.setId(String.valueOf(featureRow.getId()));
    }
    return feature;
}
Also used : GeoPackageGeometryData(mil.nga.geopackage.geom.GeoPackageGeometryData) ColumnValue(mil.nga.geopackage.user.ColumnValue) Feature(mil.nga.sf.geojson.Feature) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Feature (mil.nga.sf.geojson.Feature)4 GeoPackage (mil.nga.geopackage.GeoPackage)2 FeatureDao (mil.nga.geopackage.features.user.FeatureDao)2 FeatureResultSet (mil.nga.geopackage.features.user.FeatureResultSet)2 FeatureRow (mil.nga.geopackage.features.user.FeatureRow)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NotFoundException (javax.ws.rs.NotFoundException)1 GeoPackageException (mil.nga.geopackage.GeoPackageException)1 GeoPackageGeometryData (mil.nga.geopackage.geom.GeoPackageGeometryData)1 ColumnValue (mil.nga.geopackage.user.ColumnValue)1 GeometryEnvelope (mil.nga.sf.GeometryEnvelope)1