Search in sources :

Example 1 with ShapefileFormatException

use of net.geoprism.registry.etl.ShapefileFormatException in project geoprism-registry by terraframe.

the class ShapefileService method getSheetInformation.

private JSONObject getSheetInformation(File dbf) {
    try {
        ShapefileDataStore store = new ShapefileDataStore(dbf.toURI().toURL());
        try {
            String[] typeNames = store.getTypeNames();
            if (typeNames.length > 0) {
                String typeName = typeNames[0];
                FeatureSource<SimpleFeatureType, SimpleFeature> source = store.getFeatureSource(typeName);
                SimpleFeatureType schema = source.getSchema();
                List<AttributeDescriptor> descriptors = schema.getAttributeDescriptors();
                JSONObject attributes = new JSONObject();
                attributes.put(AttributeBooleanType.TYPE, new JSONArray());
                attributes.put(GeoObjectImportConfiguration.TEXT, new JSONArray());
                attributes.put(GeoObjectImportConfiguration.NUMERIC, new JSONArray());
                attributes.put(AttributeDateType.TYPE, new JSONArray());
                for (AttributeDescriptor descriptor : descriptors) {
                    if (!(descriptor instanceof GeometryDescriptor)) {
                        String name = descriptor.getName().getLocalPart();
                        String baseType = GeoObjectImportConfiguration.getBaseType(descriptor.getType());
                        attributes.getJSONArray(baseType).put(name);
                        if (baseType.equals(GeoObjectImportConfiguration.NUMERIC)) {
                            attributes.getJSONArray(GeoObjectImportConfiguration.TEXT).put(name);
                        }
                    }
                }
                JSONObject sheet = new JSONObject();
                sheet.put("name", typeName);
                sheet.put("attributes", attributes);
                return sheet;
            } else {
                // TODO Change exception type
                throw new ProgrammingErrorException("Shapefile does not contain any types");
            }
        } finally {
            store.dispose();
        }
    } catch (RuntimeException e) {
        Throwable cause = e.getCause();
        if (cause instanceof IOException) {
            throw new ShapefileFormatException(e);
        }
        throw e;
    } catch (Exception e) {
        throw new ProgrammingErrorException(e);
    }
}
Also used : ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) ShapefileFormatException(net.geoprism.registry.etl.ShapefileFormatException) JSONArray(org.json.JSONArray) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException) SmartException(com.runwaysdk.business.SmartException) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException) RunwayException(com.runwaysdk.RunwayException) ShapefileFormatException(net.geoprism.registry.etl.ShapefileFormatException) IOException(java.io.IOException) GeometryDescriptor(org.opengis.feature.type.GeometryDescriptor) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) JSONObject(org.json.JSONObject)

Aggregations

RunwayException (com.runwaysdk.RunwayException)1 SmartException (com.runwaysdk.business.SmartException)1 ProgrammingErrorException (com.runwaysdk.dataaccess.ProgrammingErrorException)1 IOException (java.io.IOException)1 ShapefileFormatException (net.geoprism.registry.etl.ShapefileFormatException)1 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 SimpleFeature (org.opengis.feature.simple.SimpleFeature)1 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)1 AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)1 GeometryDescriptor (org.opengis.feature.type.GeometryDescriptor)1