Search in sources :

Example 11 with Service

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

the class ShorelineTest method testGenerateSLDInfo.

/**
 * Test of generateSLDInfo method, of class Shoreline.
 */
@Test
public void testGenerateSLDInfo() {
    Item item = new Item();
    item.setAttr("Date_");
    item.setId("abcde");
    item.setType(Item.Type.historical);
    List<Service> services = new LinkedList<>();
    Service wmsService = new Service();
    wmsService.setEndpoint("http://test");
    wmsService.setServiceParameter("0");
    wmsService.setType(ServiceType.source_wms);
    services.add(wmsService);
    item.setServices(services);
    Summary summary = new Summary();
    Tiny tiny = new Tiny();
    tiny.setText("Shoreline");
    summary.setTiny(tiny);
    item.setSummary(summary);
    SLDGenerator shoreline = new SLDGenerator(item, item.getId(), null, Shorelines.shorelines);
    Response response = shoreline.generateSLDInfo();
    String json = (String) response.getEntity();
    Map<String, Object> sldInfo = new Gson().fromJson(json, HashMap.class);
    List<Object> bins = (List) sldInfo.get("bins");
    Map<String, Object> bin = (Map) bins.get(0);
    Double year0 = ((List<Double>) bin.get("years")).get(0);
    String color = (String) bin.get("color");
    assertEquals(year0, 0.0f, 0.01f);
    assertEquals(color, "#ff0000");
}
Also used : Tiny(gov.usgs.cida.coastalhazards.model.summary.Tiny) Service(gov.usgs.cida.coastalhazards.model.Service) Gson(com.google.gson.Gson) LinkedList(java.util.LinkedList) Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 12 with Service

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

the class ShorelineTest method testGenerateSLDInfoSelected.

/**
 * Test of generateSLDInfo method, of class Shoreline with an overriding
 * selected item.
 */
@Test
public void testGenerateSLDInfoSelected() {
    Item item = new Item();
    Item selectedItem = new Item();
    item.setAttr("Date_");
    item.setId("abcd");
    item.setType(Item.Type.historical);
    selectedItem.setAttr("Date_");
    selectedItem.setId("efg");
    selectedItem.setType(Item.Type.historical);
    selectedItem.setItemType(Item.ItemType.aggregation);
    List<Service> services = new LinkedList<>();
    Service wmsService = new Service();
    wmsService.setEndpoint("http://test");
    wmsService.setServiceParameter("0");
    wmsService.setType(ServiceType.source_wms);
    services.add(wmsService);
    item.setServices(services);
    selectedItem.setServices(services);
    Summary summary = new Summary();
    Tiny tiny = new Tiny();
    tiny.setText("Shoreline");
    summary.setTiny(tiny);
    item.setSummary(summary);
    selectedItem.setSummary(summary);
    SLDGenerator shoreline = new SLDGenerator(item, selectedItem.getId(), null, Shorelines.shorelines);
    Response response = shoreline.generateSLDInfo();
    String json = (String) response.getEntity();
    Map<String, Object> sldInfo = new Gson().fromJson(json, HashMap.class);
    List<Object> bins = (List) sldInfo.get("bins");
    Map<String, Object> bin = (Map) bins.get(0);
    Double year0 = ((List<Double>) bin.get("years")).get(0);
    String color = (String) bin.get("color");
    assertEquals(year0, 0.0f, 0.01f);
    assertEquals(color, "#ff0000");
}
Also used : Tiny(gov.usgs.cida.coastalhazards.model.summary.Tiny) Service(gov.usgs.cida.coastalhazards.model.Service) Gson(com.google.gson.Gson) LinkedList(java.util.LinkedList) Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 13 with Service

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

the class ShorelineTest method testGenerateSLD.

@Test
public void testGenerateSLD() {
    Item item = new Item();
    item.setAttr("DATE_");
    item.setId("abcde");
    item.setType(Item.Type.historical);
    List<Service> services = new LinkedList<>();
    Service wmsService = new Service();
    wmsService.setEndpoint("http://test");
    wmsService.setServiceParameter("0");
    wmsService.setType(ServiceType.source_wms);
    services.add(wmsService);
    item.setServices(services);
    Summary summary = new Summary();
    Tiny tiny = new Tiny();
    tiny.setText("Shoreline");
    summary.setTiny(tiny);
    item.setSummary(summary);
    SLDGenerator shoreline = new SLDGenerator(item, item.getId(), null, Shorelines.shorelines);
    Response response = shoreline.generateSLD();
    Viewable sld = (Viewable) response.getEntity();
}
Also used : Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) Tiny(gov.usgs.cida.coastalhazards.model.summary.Tiny) Viewable(org.glassfish.jersey.server.mvc.Viewable) Service(gov.usgs.cida.coastalhazards.model.Service) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 14 with Service

use of gov.usgs.cida.coastalhazards.model.Service 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 15 with Service

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

Service (gov.usgs.cida.coastalhazards.model.Service)17 Item (gov.usgs.cida.coastalhazards.model.Item)8 LinkedList (java.util.LinkedList)8 Response (javax.ws.rs.core.Response)8 Summary (gov.usgs.cida.coastalhazards.model.summary.Summary)7 Tiny (gov.usgs.cida.coastalhazards.model.summary.Tiny)6 Test (org.junit.Test)6 WFSService (gov.usgs.cida.coastalhazards.util.ogc.WFSService)5 Gson (com.google.gson.Gson)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 LayerManager (gov.usgs.cida.coastalhazards.jpa.LayerManager)3 Bbox (gov.usgs.cida.coastalhazards.model.Bbox)3 Layer (gov.usgs.cida.coastalhazards.model.Layer)3 List (java.util.List)3 Map (java.util.Map)3 RolesAllowed (javax.annotation.security.RolesAllowed)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 Viewable (org.glassfish.jersey.server.mvc.Viewable)3