Search in sources :

Example 6 with Bbox

use of gov.usgs.cida.coastalhazards.model.Bbox in project coastal-hazards by USGS-CIDA.

the class WFSIntrospector method getBbox.

public static Bbox getBbox(WFSService service) throws IOException {
    WFSDataStore wfs = createDs(service);
    ReferencedEnvelope env = wfs.getFeatureTypeWGS84Bounds(service.getTypeName());
    Bbox bbox = new Bbox();
    bbox.setBbox(env.getMinX(), env.getMinY(), env.getMaxX(), env.getMaxY());
    return bbox;
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Bbox(gov.usgs.cida.coastalhazards.model.Bbox) WFSDataStore(org.geotools.data.wfs.WFSDataStore)

Example 7 with Bbox

use of gov.usgs.cida.coastalhazards.model.Bbox in project coastal-hazards by USGS-CIDA.

the class BboxAdapterTest method deserializeTest.

@Test
public void deserializeTest() {
    JsonArray json = new JsonArray();
    json.add(new JsonPrimitive(12.3));
    json.add(new JsonPrimitive(-4.56));
    json.add(new JsonPrimitive(78.9));
    json.add(new JsonPrimitive(8.76));
    Bbox deserialized = bboxAdapter.deserialize(json, Bbox.class, null);
    assertThat(deserialized.getBbox(), is(equalTo("BOX(12.300000 -4.560000, 78.900000 8.760000)")));
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) Bbox(gov.usgs.cida.coastalhazards.model.Bbox) Test(org.junit.Test)

Example 8 with Bbox

use of gov.usgs.cida.coastalhazards.model.Bbox in project coastal-hazards by USGS-CIDA.

the class BboxAdapterTest method serializeTest.

@Test
public void serializeTest() {
    Bbox bbox = new Bbox();
    bbox.setId(7);
    bbox.setBbox(12.3, 4.56, 78.9, 8.76);
    JsonElement serialized = bboxAdapter.serialize(bbox, Bbox.class, null);
    assertThat(serialized.toString(), is(equalTo("[12.3,4.56,78.9,8.76]")));
}
Also used : Bbox(gov.usgs.cida.coastalhazards.model.Bbox) JsonElement(com.google.gson.JsonElement) Test(org.junit.Test)

Example 9 with Bbox

use of gov.usgs.cida.coastalhazards.model.Bbox 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)

Example 10 with Bbox

use of gov.usgs.cida.coastalhazards.model.Bbox in project coastal-hazards by USGS-CIDA.

the class LayerResource method createVectorLayer.

@POST
@Path("/")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@Produces(MediaType.TEXT_PLAIN)
@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
public Response createVectorLayer(@Context HttpServletRequest req, InputStream postBody) {
    Response response = null;
    String newId = IdGenerator.generate();
    List<Service> added = null;
    try {
        log.info("Vector layer upload - about to parseRequest");
        byte[] inmemory = IOUtils.toByteArray(postBody);
        try (ByteArrayInputStream bais = new ByteArrayInputStream(inmemory)) {
            log.info("Vector layer create - about to doCSWInsertFromString");
            String metadataId = MetadataUtil.doCSWInsertFromString(MetadataUtil.extractMetadataFromShp(bais));
            bais.reset();
            log.info("Vector layer create - about to do GeoserverUtil.addVectorLayer with Id: " + newId);
            added = GeoserverUtil.addVectorLayer(bais, newId);
            log.info("Vector layer create - about to makeCSWServiceForUrl with metadataId: " + metadataId);
            added.add(MetadataUtil.makeCSWServiceForUrl(MetadataUtil.getMetadataByIdUrl(metadataId)));
        } finally {
            // just in case
            inmemory = null;
        }
    } catch (IOException | ParserConfigurationException | SAXException | IllegalArgumentException e) {
        log.error("Problem creating services from input", e);
    }
    if (added != null && !added.isEmpty()) {
        WFSService wfs = (WFSService) Service.ogcHelper(Service.ServiceType.proxy_wfs, added);
        Bbox bbox = null;
        try {
            bbox = WFSIntrospector.getBbox(wfs);
        } catch (IOException ex) {
            log.debug("Error determining bounding box", ex);
        }
        Layer layer = new Layer();
        layer.setId(newId);
        layer.setServices(added);
        layer.setBbox(bbox);
        try (LayerManager manager = new LayerManager()) {
            manager.save(layer);
        }
        response = Response.created(layerURI(layer)).build();
    } else {
        response = Response.serverError().entity("Unable to create layer").build();
    }
    return response;
}
Also used : 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) Response(javax.ws.rs.core.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) Bbox(gov.usgs.cida.coastalhazards.model.Bbox) WFSService(gov.usgs.cida.coastalhazards.util.ogc.WFSService) 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

Bbox (gov.usgs.cida.coastalhazards.model.Bbox)12 ByteArrayInputStream (java.io.ByteArrayInputStream)4 JAXBException (javax.xml.bind.JAXBException)4 Test (org.junit.Test)4 Service (gov.usgs.cida.coastalhazards.model.Service)3 WFSService (gov.usgs.cida.coastalhazards.util.ogc.WFSService)3 Bounding (gov.usgs.cida.coastalhazards.xml.model.Bounding)3 Idinfo (gov.usgs.cida.coastalhazards.xml.model.Idinfo)3 Metadata (gov.usgs.cida.coastalhazards.xml.model.Metadata)3 Spdom (gov.usgs.cida.coastalhazards.xml.model.Spdom)3 JAXBContext (javax.xml.bind.JAXBContext)3 Unmarshaller (javax.xml.bind.Unmarshaller)3 JsonArray (com.google.gson.JsonArray)2 LayerManager (gov.usgs.cida.coastalhazards.jpa.LayerManager)2 Item (gov.usgs.cida.coastalhazards.model.Item)2 Layer (gov.usgs.cida.coastalhazards.model.Layer)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 RolesAllowed (javax.annotation.security.RolesAllowed)2 Consumes (javax.ws.rs.Consumes)2