Search in sources :

Example 26 with FactoryException

use of org.opengis.referencing.FactoryException in project hale by halestudio.

the class WKTPreferencesCRSFactory method getDescriptionText.

/**
 * @see AuthorityFactory#getDescriptionText(String)
 */
@Override
public InternationalString getDescriptionText(String code) throws FactoryException {
    if (code == null) {
        return null;
    }
    if (code.startsWith("EPSG:")) {
        // $NON-NLS-1$
        code = code.substring(5);
    }
    code = code.trim();
    String wkt = node.get(code, null);
    if (wkt == null) {
        // $NON-NLS-1$ //$NON-NLS-2$
        throw new FactoryException("Unknown EPSG code: '" + code + "'");
    }
    wkt = wkt.trim();
    int start = wkt.indexOf('"');
    int end = wkt.indexOf('"', start + 1);
    return new org.geotools.util.SimpleInternationalString(wkt.substring(start + 1, end));
}
Also used : FactoryException(org.opengis.referencing.FactoryException) InternationalString(org.opengis.util.InternationalString)

Example 27 with FactoryException

use of org.opengis.referencing.FactoryException in project hale by halestudio.

the class AdvCrsAuthorityFactory method getDescriptionText.

@Override
public InternationalString getDescriptionText(String code) throws FactoryException {
    if (code == null) {
        return null;
    }
    if (code.startsWith(AUTHORITY_PREFIX)) {
        code = code.substring(AUTHORITY_PREFIX.length());
    }
    code = code.trim();
    String epsgCode = mappings.get(code);
    if (epsgCode == null) {
        // $NON-NLS-1$ //$NON-NLS-2$
        throw new FactoryException("Unknown AdV code: '" + code + "'");
    }
    String wkt = CRS.decode(epsgCode).toWKT();
    int start = wkt.indexOf('"');
    int end = wkt.indexOf('"', start + 1);
    return new org.geotools.util.SimpleInternationalString(wkt.substring(start + 1, end));
}
Also used : FactoryException(org.opengis.referencing.FactoryException) SimpleInternationalString(org.geotools.util.SimpleInternationalString) SimpleInternationalString(org.geotools.util.SimpleInternationalString) InternationalString(org.opengis.util.InternationalString)

Example 28 with FactoryException

use of org.opengis.referencing.FactoryException in project hale by halestudio.

the class AdvCrsAuthorityFactory method createCoordinateReferenceSystem.

/**
 * @see CRSAuthorityFactory#createCoordinateReferenceSystem(String)
 */
@Override
public synchronized CoordinateReferenceSystem createCoordinateReferenceSystem(String code) throws FactoryException {
    if (code == null) {
        return null;
    }
    if (!code.startsWith(AUTHORITY_PREFIX)) {
        throw new // $NON-NLS-1$
        NoSuchAuthorityCodeException(// $NON-NLS-1$
        "This factory only understands AdV codes", AUTHORITY, code);
    }
    final String advCode = code.substring(AUTHORITY_PREFIX.length()).trim();
    if (cache.containsKey(advCode)) {
        CoordinateReferenceSystem value = cache.get(advCode);
        if (value != null) {
            // CRS was already created
            return value;
        }
    }
    String epsgCode = getEpsgCode(advCode);
    if (epsgCode == null) {
        // $NON-NLS-1$
        throw new NoSuchAuthorityCodeException("Unknown AdV code", AUTHORITY, code);
    }
    try {
        CoordinateReferenceSystem crs = CRS.decode(epsgCode, false);
        cache.put(advCode, crs);
        return crs;
    } catch (FactoryException fex) {
        throw fex;
    }
}
Also used : NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) FactoryException(org.opengis.referencing.FactoryException) SimpleInternationalString(org.geotools.util.SimpleInternationalString) InternationalString(org.opengis.util.InternationalString) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 29 with FactoryException

use of org.opengis.referencing.FactoryException in project coastal-hazards by USGS-CIDA.

the class CRSUtils method transformFeatureCollection.

/**
 * Returns a SimpleFeatureCollection with transformed default geometry
 *
 * @param featureCollection source feature collection (features may be
 * modified)
 * @param sourceCrs original coordinate reference system
 * @param targetCrs new coordinate reference system
 * @return new SimpleFeatureCollection
 */
public static SimpleFeatureCollection transformFeatureCollection(FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection, CoordinateReferenceSystem sourceCrs, CoordinateReferenceSystem targetCrs) {
    List<SimpleFeature> sfList = new LinkedList<SimpleFeature>();
    MathTransform transform = null;
    try {
        transform = CRS.findMathTransform(sourceCrs, targetCrs, true);
    } catch (FactoryException ex) {
        // do something better than this
        return null;
    }
    FeatureIterator<SimpleFeature> features = null;
    try {
        features = featureCollection.features();
        SimpleFeature feature = null;
        while (features.hasNext()) {
            feature = features.next();
            Geometry geometry = (Geometry) feature.getDefaultGeometry();
            Geometry utmGeometry = null;
            try {
                utmGeometry = JTS.transform(geometry, transform);
            } catch (TransformException ex) {
                // TODO handle exceptions
                LOGGER.warn("Unhandled exception in transformFeatureCollection", ex);
            }
            feature.setDefaultGeometry(utmGeometry);
            sfList.add(feature);
        }
    } finally {
        if (null != features) {
            features.close();
        }
    }
    return DataUtilities.collection(sfList);
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) MathTransform(org.opengis.referencing.operation.MathTransform) FactoryException(org.opengis.referencing.FactoryException) TransformException(org.opengis.referencing.operation.TransformException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) LinkedList(java.util.LinkedList)

Example 30 with FactoryException

use of org.opengis.referencing.FactoryException in project coastal-hazards by USGS-CIDA.

the class LayerResource method createRasterLayer.

@POST
@Path("/raster")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
public Response createRasterLayer(@Context HttpServletRequest req, @FormDataParam("metadata") String metadata, @FormDataParam("file") InputStream zipFileStream, @FormDataParam("file") FormDataContentDisposition fileDisposition) {
    List<Service> services = new ArrayList<>();
    try {
        log.info("Raster layer upload - about to parseRequest");
        String newId = IdGenerator.generate();
        String metadataId;
        if (metadata == null || metadata.isEmpty()) {
            throw new ServerErrorException("Metadata file is missing or empty.", Status.INTERNAL_SERVER_ERROR);
        }
        try {
            log.info("Raster layer create - about to doCSWInsertFromString");
            metadataId = MetadataUtil.doCSWInsertFromString(metadata);
        } catch (IOException | ParserConfigurationException | SAXException ex) {
            throw new ServerErrorException("Error inserting metadata to the CSW server.", Status.INTERNAL_SERVER_ERROR, ex);
        }
        log.info("Raster layer create - about to makeCSWServiceForUrl with metadataId: " + metadataId);
        services.add(MetadataUtil.makeCSWServiceForUrl(MetadataUtil.getMetadataByIdUrl(metadataId)));
        Bbox bbox = MetadataUtil.getBoundingBoxFromFgdcMetadata(metadata);
        log.info("Starting CRS Identifier Lookup");
        long startTime = System.nanoTime();
        String EPSGcode = CRS.lookupIdentifier(MetadataUtil.getCrsFromFgdcMetadata(metadata), true);
        long endTime = System.nanoTime();
        // divide by 1000000 to get milliseconds
        long duration = (endTime - startTime) / 1000000;
        log.info("Finished CRS Identifier Lookup. Took " + duration + "ms.");
        if (bbox == null || EPSGcode == null) {
            throw new ServerErrorException("Unable to identify bbox or epsg code from metadata.", Status.INTERNAL_SERVER_ERROR);
        }
        log.info("Raster layer create - about to addRasterLayer to geoserver with an id of: " + newId);
        log.info("Raster layer create - about to addRasterLayer to geoserver with a  bbox: " + bbox.getBbox());
        log.info("Raster layer create - about to addRasterLayer to geoserver with an EPSG of: " + EPSGcode);
        Service rasterService = GeoserverUtil.addRasterLayer(geoserverEndpoint, zipFileStream, newId, bbox, EPSGcode);
        if (null == rasterService) {
            throw new ServerErrorException("Unable to create a store and/or layer in GeoServer.", Status.INTERNAL_SERVER_ERROR);
        } else {
            services.add(rasterService);
        }
        if (!services.isEmpty()) {
            Layer layer = new Layer();
            layer.setId(newId);
            layer.setServices(services);
            layer.setBbox(bbox);
            try (LayerManager manager = new LayerManager()) {
                manager.save(layer);
            }
            return Response.created(layerURI(layer)).build();
        } else {
            throw new ServerErrorException("Unable to create layer", Status.INTERNAL_SERVER_ERROR);
        }
    } catch (JAXBException | FactoryException | IOException ex) {
        throw new ServerErrorException("Error parsing upload request", Status.INTERNAL_SERVER_ERROR, ex);
    }
}
Also used : FactoryException(org.opengis.referencing.FactoryException) JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) WFSService(gov.usgs.cida.coastalhazards.util.ogc.WFSService) Service(gov.usgs.cida.coastalhazards.model.Service) IOException(java.io.IOException) Layer(gov.usgs.cida.coastalhazards.model.Layer) SAXException(org.xml.sax.SAXException) Bbox(gov.usgs.cida.coastalhazards.model.Bbox) ServerErrorException(javax.ws.rs.ServerErrorException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LayerManager(gov.usgs.cida.coastalhazards.jpa.LayerManager) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

FactoryException (org.opengis.referencing.FactoryException)32 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)22 TransformException (org.opengis.referencing.operation.TransformException)11 NoSuchAuthorityCodeException (org.opengis.referencing.NoSuchAuthorityCodeException)8 MathTransform (org.opengis.referencing.operation.MathTransform)8 Collection (java.util.Collection)6 GeometryType (org.opengis.feature.type.GeometryType)5 Geometry (com.vividsolutions.jts.geom.Geometry)4 Hints (org.geotools.factory.Hints)4 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)4 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)4 PropertyType (org.opengis.feature.type.PropertyType)4 ValueComboBoxData (com.sldeditor.ui.widgets.ValueComboBoxData)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)3 Geometry (org.locationtech.jts.geom.Geometry)3 InternationalString (org.opengis.util.InternationalString)3 Optional (com.google.common.base.Optional)2 VendorOptionVersion (com.sldeditor.common.vendoroption.VendorOptionVersion)2