Search in sources :

Example 1 with Service

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

the class GeoserverUtil method wfsService.

/**
 * Builds WFS Services
 * Must use the external GeoServer url because after these are persisted,
 * they are retrieved by clients external to the network
 * @param layer
 * @return Service with a www-accessible url
 */
private static Service wfsService(String layer) {
    log.info("createWfsService using layer: " + layer);
    Service service = new Service();
    URI uri = UriBuilder.fromUri(geoserverExternalEndpoint).path(PROXY_WORKSPACE).path("wfs").build();
    service.setType(Service.ServiceType.proxy_wfs);
    service.setEndpoint(uri.toString());
    service.setServiceParameter(layer);
    return service;
}
Also used : Service(gov.usgs.cida.coastalhazards.model.Service) DataURI(gov.usgs.cida.coastalhazards.rest.data.DataURI) URI(java.net.URI)

Example 2 with Service

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

the class GeoserverUtil method wmsService.

/**
 * Builds WMS Services
 * Must use the external GeoServer url because after these are persisted,
 * they are retrieved by clients external to the network
 * @param layer
 * @return Service with a www-accessible url
 */
private static Service wmsService(String layer) {
    log.info("createWmsService using layer: " + layer);
    Service service = new Service();
    URI uri = UriBuilder.fromUri(geoserverExternalEndpoint).path(PROXY_WORKSPACE).path("wms").build();
    service.setType(Service.ServiceType.proxy_wms);
    service.setEndpoint(uri.toString());
    service.setServiceParameter(layer);
    return service;
}
Also used : Service(gov.usgs.cida.coastalhazards.model.Service) DataURI(gov.usgs.cida.coastalhazards.rest.data.DataURI) URI(java.net.URI)

Example 3 with Service

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

the class GeoserverUtil method addRasterLayer.

public static Service addRasterLayer(String geoServerEndpoint, InputStream zipFileStream, String layerId, Bbox bbox, String EPSGcode) throws FileNotFoundException, IOException {
    Service rasterService = null;
    String fileId = UUID.randomUUID().toString();
    String realFileName = TempFileResource.getFileNameForId(fileId);
    // temp file must not include fileId, it should include the realFileName. We don't hand out the realFileName.
    File tempFile = new File(TempFileResource.getTempFileSubdirectory(), realFileName);
    FileOutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream(tempFile);
        // this is the renamed zip file (the raster tif)
        IOUtils.copy(zipFileStream, fileOut);
    } catch (IOException ex) {
        throw new RuntimeException("Error writing zip to file '" + tempFile.getAbsolutePath() + "'.", ex);
    } finally {
        IOUtils.closeQuietly(zipFileStream);
        IOUtils.closeQuietly(fileOut);
    }
    // tempFile should now have all the data transferred to it
    // this puts it under <tomcat>/temp/cch-temp<randomkeyA>/<randomkeyB>  without the .zip ext
    log.info("Data should now have been copied to the tempFile located here:" + tempFile.getAbsoluteFile());
    log.info("The file id is: " + fileId);
    String uri = props.getProperty("coastal-hazards.base.url");
    log.info("The uri from the props is: " + uri);
    uri += DataURI.DATA_SERVICE_ENDPOINT + DataURI.TEMP_FILE_PATH + "/" + fileId;
    // String zipUrl <- create a url for the retrieval of the file  ... this is now coastal-hazards-portal/temp-file/<randommonkey>
    String unzippedFilePath = null;
    try {
        // get the security token from DynamicReadOnlyProperties
        String token = props.getProperty("gov.usgs.cida.coastalhazards.wps.fetch.and.unzip.process.token");
        // call the wps process
        unzippedFilePath = importRasterUsingWps(token, uri);
    } finally {
        // regardless of success or failure of wps proc
        tempFile.delete();
        log.info("Deleted contents of temp file");
    }
    if (unzippedFilePath == null || unzippedFilePath.isEmpty()) {
        log.error("File path to unzipped geotiff returned null. Is the Geoserver wps fetchAndUnzip call working? ");
        throw new RuntimeException("Error attempting to call wps process '" + tempFile.getAbsolutePath() + "'.");
    }
    log.info("______File path to unzipped raster on the portal is: " + unzippedFilePath);
    File unzippedFile = new File(unzippedFilePath);
    String fileName = unzippedFile.getName();
    // Publish the raster tiff as a layer on Geoserver
    GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(geoServerEndpoint, geoserverUser, geoserverPass);
    // Then use the GeoServerRESTPublisher to create the stores and layers.
    GSCoverageEncoder coverageEncoder = new GSCoverageEncoder();
    // potential todo: set coverageEncoder description here based on text from the metadata file, or from a service parameter
    // this would provide descriptive info for a user browsing our GeoServer with a generic WMS client
    // (fileName);  //BUG noted below : goeserver v2.4 requires the file name match the coverage name
    coverageEncoder.setName(fileName);
    coverageEncoder.setNativeName(fileName);
    coverageEncoder.setTitle(fileName);
    coverageEncoder.setSRS(EPSGcode);
    coverageEncoder.setProjectionPolicy(GSResourceEncoder.ProjectionPolicy.FORCE_DECLARED);
    GSLayerEncoder layerEncoder = new GSLayerEncoder();
    layerEncoder.setDefaultStyle(DEFAULT_RASTER_STYLE);
    // Geoserver Manager BUG Alert for v2.4 ...Creating a GeoTIFF coverage via REST works if the coverage's name is the same as the store name, but fails otherwise. Setting nativeName or nativeCoverageName does not help. Changing the name after creating the coverage works fine.
    // #TODO# what to do if the workspace:fileName is already in use?
    RESTCoverageStore store = publisher.publishExternalGeoTIFF(PROXY_WORKSPACE, fileName, unzippedFile, coverageEncoder, layerEncoder);
    if (null == store) {
        // if store or layer creation failed
        log.info("Error publishing GeoTiff in GeoServer.");
        rasterService = null;
    } else {
        String newLayerName = store.getWorkspaceName() + ":" + fileName;
        log.info("Published GeoTiff!!!");
        log.info("In GeoserverUtil, about to add wmsService with layer name: " + layerId);
        rasterService = wmsService(newLayerName);
        log.info("Added layer to wms service.");
    }
    return rasterService;
}
Also used : RESTCoverageStore(it.geosolutions.geoserver.rest.decoder.RESTCoverageStore) GSCoverageEncoder(it.geosolutions.geoserver.rest.encoder.coverage.GSCoverageEncoder) FileOutputStream(java.io.FileOutputStream) Service(gov.usgs.cida.coastalhazards.model.Service) GeoServerRESTPublisher(it.geosolutions.geoserver.rest.GeoServerRESTPublisher) IOException(java.io.IOException) File(java.io.File) GSLayerEncoder(it.geosolutions.geoserver.rest.encoder.GSLayerEncoder)

Example 4 with Service

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

the class MetadataUtil method makeCSWServiceForUrl.

public static Service makeCSWServiceForUrl(String url) {
    Service csw = new Service();
    csw.setType(Service.ServiceType.csw);
    csw.setEndpoint(url);
    return csw;
}
Also used : Service(gov.usgs.cida.coastalhazards.model.Service)

Example 5 with Service

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

the class StormUtil method getStormCswDocument.

private static Document getStormCswDocument(Layer layer) {
    Document doc = null;
    Service cswService = getCswServiceFromLayer(layer);
    if (cswService != null) {
        String endpoint = cswService.getEndpoint();
        String cswData = HttpUtil.fetchDataFromUri(endpoint);
        if (cswData != null && cswData.length() > 0) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(false);
            try {
                doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(cswData.getBytes()));
            } catch (Exception e) {
                log.error("Failed to parse storm csw document. Error: " + e.getMessage() + ". Stack Trace: " + e.getStackTrace());
            }
        }
    }
    return doc;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) Service(gov.usgs.cida.coastalhazards.model.Service) Document(org.w3c.dom.Document)

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